Submission #68413


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;
struct Mo {
private:
int n;
vector<pair<int, int> > lr;
public:
Mo(int n) : n(n) {}
/* [l, r) */
void insert(int l, int r){
lr.emplace_back(l, r);
}
template<typename A, typename E, typename O>
void build(const A &add, const E &erase, const O &out){
int q = (int) lr.size();
int bs = n / min<int>(n, sqrt(q));
vector<int> ord(q);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int a, int b){
int ablock = lr[a].first / bs, bblock = lr[b].first / bs;
if(ablock != bblock) return (ablock < bblock);
if(ablock & 1) return (lr[a].second > lr[b].second);
return (lr[a].second < lr[b].second);
});
int l = 0, r = 0;
for(auto idx : ord){
while(l > lr[idx].first) add(--l);
while(r < lr[idx].second) add(r++);
while(l < lr[idx].first) erase(l++);
while(r > lr[idx].second) erase(--r);
out(idx);
}
}
};
template< typename T >
struct BinaryIndexedTree {
private:
vector< T > data;
public:
BinaryIndexedTree() = default;
explicit BinaryIndexedTree(size_t sz) : data(sz + 1, 0) {}
void add(int k, const T &x) {
for(++k; k < (int) data.size(); k += k & -k) data[k] += x;
}
T fold(int r) const {
T ret = T();
for(; r > 0; r -= r & -r) ret += data[r];
return ret;
}
T fold(int l, int r) const {
return fold(r) - fold(l);
}
};
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n, m, d;
cin >> n >> m >> d;
vector<int> c(n), cc(n), cr(n), cl(n), zip;
for (int i = 0; i < n; ++i) {
cin >> c[i];
zip.emplace_back(c[i]);
zip.emplace_back(c[i] - d);
zip.emplace_back(c[i] + d);
}
sort(zip.begin(), zip.end());
zip.erase(unique(zip.begin(), zip.end()), zip.end());
for (int i = 0; i < n; ++i) {
cc[i] = lower_bound(zip.begin(), zip.end(), c[i]) - zip.begin();
cl[i] = lower_bound(zip.begin(), zip.end(), c[i] - d) - zip.begin();
cr[i] = lower_bound(zip.begin(), zip.end(), c[i] + d) - zip.begin();
}
Mo mo(n);
for (int i = 0; i < m; ++i) {
int l, r;
cin >> l >> r;
--l, --r;
mo.insert(l, r + 1);
}
BinaryIndexedTree<int> bit(zip.size());
vector<long long> res(m);
long long sum = 0;
auto add = [&](int k) {
sum += bit.fold(cl[k], cr[k] + 1);
bit.add(cc[k], 1);
};
auto erase = [&](int k) {
bit.add(cc[k], -1);
sum -= bit.fold(cl[k], cr[k] + 1);
};
auto out = [&](int k) {
res[k] = sum;
};
mo.build(add, erase, out);
for (auto &e : res) {
cout << e << '\n';
}
return (0);
}

ステータス

項目 データ
問題 1524 - Artist
ユーザー名 ei1903
投稿日時 2021-09-12 17:20:51
言語 C++17
状態 Accepted
得点 6
ソースコード長 3100 Byte
最大実行時間 1186 ms
最大メモリ使用量 33712 KB

セット

セット 得点 Cases
1 ALL 6 / 6 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
in01.txt AC 32 ms 604 KB
1
in02.txt AC 24 ms 392 KB
1
in03.txt AC 24 ms 440 KB
1
in04.txt AC 220 ms 3044 KB
1
in05.txt AC 144 ms 4012 KB
1
in06.txt AC 434 ms 4548 KB
1
in07.txt AC 223 ms 3616 KB
1
in08.txt AC 816 ms 6816 KB
1
in09.txt AC 544 ms 6368 KB
1
in10.txt AC 86 ms 3580 KB
1
in11.txt AC 1126 ms 9684 KB
1
in12.txt AC 1128 ms 10540 KB
1
in13.txt AC 1167 ms 11396 KB
1
in14.txt AC 1132 ms 12380 KB
1
in15.txt AC 1142 ms 13104 KB
1
in16.txt AC 1164 ms 14084 KB
1
in17.txt AC 1186 ms 14940 KB
1
in18.txt AC 1120 ms 15796 KB
1
in19.txt AC 1091 ms 16776 KB
1
in20.txt AC 1150 ms 17760 KB
1
in21.txt AC 143 ms 18748 KB
1
in22.txt AC 103 ms 19728 KB
1
in23.txt AC 795 ms 19180 KB
1
in24.txt AC 801 ms 19276 KB
1
in25.txt AC 810 ms 19380 KB
1
in26.txt AC 1101 ms 22600 KB
1
in27.txt AC 1092 ms 22000 KB
1
in28.txt AC 1106 ms 22980 KB
1
in29.txt AC 1117 ms 23968 KB
1
in30.txt AC 1135 ms 24696 KB
1
in31.txt AC 1089 ms 25812 KB
1
in32.txt AC 1112 ms 26668 KB
1
in33.txt AC 1116 ms 27656 KB
1
in34.txt AC 991 ms 27480 KB
1
in35.txt AC 962 ms 29888 KB
1
in36.txt AC 1121 ms 30116 KB
1
in37.txt AC 1139 ms 30972 KB
1
in38.txt AC 133 ms 31188 KB
1
in39.txt AC 127 ms 31400 KB
1
in40.txt AC 809 ms 30972 KB
1
in41.txt AC 849 ms 31072 KB
1
in42.txt AC 752 ms 31428 KB
1
in43.txt AC 750 ms 31912 KB
1
in44.txt AC 39 ms 28684 KB
1
in45.txt AC 39 ms 28836 KB
1
in46.txt AC 25 ms 27836 KB
1
in47.txt AC 30 ms 27764 KB
1
in48.txt AC 22 ms 27028 KB
1
in49.txt AC 28 ms 26988 KB
1
in50.txt AC 30 ms 28088 KB
1
in51.txt AC 45 ms 29160 KB
1
in52.txt AC 27 ms 27724 KB
1
in53.txt AC 72 ms 30840 KB
1
in54.txt AC 32 ms 28944 KB
1
in55.txt AC 295 ms 33712 KB
1
in56.txt AC 103 ms 31516 KB
1
sample.txt AC 22 ms 29336 KB
1