#include <bits/stdc++.h>
using namespace std;
const int kN = 1e5;
int N, sol[kN], aib[1 + 2 * kN];
struct student {
int x, y, z, index;
} a[2 * kN];
bool cmp1(const student &lhs, const student &rhs) {
if (lhs.z != rhs.z) {
return lhs.z > rhs.z;
}
return lhs.index < rhs.index;
}
bool cmp2(const student &lhs, const student &rhs) {
if (lhs.x != rhs.x) {
return lhs.x > rhs.x;
}
return lhs.index < rhs.index;
}
void update(int x, int v) {
for (int i = x; i <= N; i += i & -i) {
aib[i] += v;
}
}
int query(int x) {
int ans = 0;
for (int i = x; i > 0; i = i & (i - 1)) {
ans += aib[i];
}
return ans;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
void solve(int l, int r) {
if (r <= l) {
return;
}
int mid = (l + r) / 2;
vector<student> ev;
for (int i = l; i <= mid; ++i) {
if (a[i].index == -1) {
ev.emplace_back(a[i]);
}
}
for (int i = mid + 1; i <= r; ++i) {
if (a[i].index != -1) {
ev.emplace_back(a[i]);
}
}
sort(ev.begin(), ev.end(), cmp2);
vector<int> updates;
for (auto it : ev) {
int y = it.y, index = it.index;
if (index == -1) {
update(y, 1);
updates.emplace_back(y);
} else {
sol[index] += query(y, N);
}
}
ev.clear();
for (int y : updates) {
update(y, -1);
}
updates.clear();
solve(l, mid);
solve(mid + 1, r);
}
void testCase() {
int n, q;
cin >> n >> q;
vector<int> ys(n + q);
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
a[i] = {x, y, x + y, -1};
ys[i] = y;
}
for (int i = 0; i < q; ++i) {
int x, y, z;
cin >> x >> y >> z;
a[n + i] = {x, y, z, i};
ys[n + i] = y;
}
int m = n + q;
sort(a, a + m, cmp1);
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
N = ys.size();
for (int i = 0; i < m; ++i) {
a[i].y = distance(ys.begin(), upper_bound(ys.begin(), ys.end(), a[i].y));
}
ys.clear();
solve(0, m - 1);
for (int i = 0; i < q; ++i) {
cout << sol[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
7 ms |
588 KB |
Output is correct |
8 |
Correct |
7 ms |
588 KB |
Output is correct |
9 |
Correct |
7 ms |
588 KB |
Output is correct |
10 |
Correct |
5 ms |
588 KB |
Output is correct |
11 |
Correct |
7 ms |
620 KB |
Output is correct |
12 |
Correct |
4 ms |
588 KB |
Output is correct |
13 |
Correct |
7 ms |
800 KB |
Output is correct |
14 |
Correct |
7 ms |
708 KB |
Output is correct |
15 |
Correct |
7 ms |
840 KB |
Output is correct |
16 |
Correct |
5 ms |
708 KB |
Output is correct |
17 |
Correct |
5 ms |
708 KB |
Output is correct |
18 |
Correct |
4 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
272 ms |
10864 KB |
Output is correct |
2 |
Correct |
273 ms |
10876 KB |
Output is correct |
3 |
Correct |
271 ms |
10828 KB |
Output is correct |
4 |
Correct |
196 ms |
10536 KB |
Output is correct |
5 |
Correct |
260 ms |
10864 KB |
Output is correct |
6 |
Correct |
190 ms |
10560 KB |
Output is correct |
7 |
Correct |
276 ms |
13272 KB |
Output is correct |
8 |
Correct |
271 ms |
13184 KB |
Output is correct |
9 |
Correct |
267 ms |
13108 KB |
Output is correct |
10 |
Correct |
220 ms |
12724 KB |
Output is correct |
11 |
Correct |
173 ms |
12308 KB |
Output is correct |
12 |
Correct |
164 ms |
11572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
272 ms |
10864 KB |
Output is correct |
2 |
Correct |
273 ms |
10876 KB |
Output is correct |
3 |
Correct |
271 ms |
10828 KB |
Output is correct |
4 |
Correct |
196 ms |
10536 KB |
Output is correct |
5 |
Correct |
260 ms |
10864 KB |
Output is correct |
6 |
Correct |
190 ms |
10560 KB |
Output is correct |
7 |
Correct |
276 ms |
13272 KB |
Output is correct |
8 |
Correct |
271 ms |
13184 KB |
Output is correct |
9 |
Correct |
267 ms |
13108 KB |
Output is correct |
10 |
Correct |
220 ms |
12724 KB |
Output is correct |
11 |
Correct |
173 ms |
12308 KB |
Output is correct |
12 |
Correct |
164 ms |
11572 KB |
Output is correct |
13 |
Correct |
296 ms |
12216 KB |
Output is correct |
14 |
Correct |
302 ms |
13680 KB |
Output is correct |
15 |
Correct |
279 ms |
13360 KB |
Output is correct |
16 |
Correct |
214 ms |
10968 KB |
Output is correct |
17 |
Correct |
281 ms |
11288 KB |
Output is correct |
18 |
Correct |
215 ms |
11388 KB |
Output is correct |
19 |
Correct |
296 ms |
12196 KB |
Output is correct |
20 |
Correct |
299 ms |
12620 KB |
Output is correct |
21 |
Correct |
289 ms |
12168 KB |
Output is correct |
22 |
Correct |
222 ms |
12596 KB |
Output is correct |
23 |
Correct |
179 ms |
12224 KB |
Output is correct |
24 |
Correct |
155 ms |
11572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
7 ms |
588 KB |
Output is correct |
8 |
Correct |
7 ms |
588 KB |
Output is correct |
9 |
Correct |
7 ms |
588 KB |
Output is correct |
10 |
Correct |
5 ms |
588 KB |
Output is correct |
11 |
Correct |
7 ms |
620 KB |
Output is correct |
12 |
Correct |
4 ms |
588 KB |
Output is correct |
13 |
Correct |
7 ms |
800 KB |
Output is correct |
14 |
Correct |
7 ms |
708 KB |
Output is correct |
15 |
Correct |
7 ms |
840 KB |
Output is correct |
16 |
Correct |
5 ms |
708 KB |
Output is correct |
17 |
Correct |
5 ms |
708 KB |
Output is correct |
18 |
Correct |
4 ms |
588 KB |
Output is correct |
19 |
Correct |
272 ms |
10864 KB |
Output is correct |
20 |
Correct |
273 ms |
10876 KB |
Output is correct |
21 |
Correct |
271 ms |
10828 KB |
Output is correct |
22 |
Correct |
196 ms |
10536 KB |
Output is correct |
23 |
Correct |
260 ms |
10864 KB |
Output is correct |
24 |
Correct |
190 ms |
10560 KB |
Output is correct |
25 |
Correct |
276 ms |
13272 KB |
Output is correct |
26 |
Correct |
271 ms |
13184 KB |
Output is correct |
27 |
Correct |
267 ms |
13108 KB |
Output is correct |
28 |
Correct |
220 ms |
12724 KB |
Output is correct |
29 |
Correct |
173 ms |
12308 KB |
Output is correct |
30 |
Correct |
164 ms |
11572 KB |
Output is correct |
31 |
Correct |
296 ms |
12216 KB |
Output is correct |
32 |
Correct |
302 ms |
13680 KB |
Output is correct |
33 |
Correct |
279 ms |
13360 KB |
Output is correct |
34 |
Correct |
214 ms |
10968 KB |
Output is correct |
35 |
Correct |
281 ms |
11288 KB |
Output is correct |
36 |
Correct |
215 ms |
11388 KB |
Output is correct |
37 |
Correct |
296 ms |
12196 KB |
Output is correct |
38 |
Correct |
299 ms |
12620 KB |
Output is correct |
39 |
Correct |
289 ms |
12168 KB |
Output is correct |
40 |
Correct |
222 ms |
12596 KB |
Output is correct |
41 |
Correct |
179 ms |
12224 KB |
Output is correct |
42 |
Correct |
155 ms |
11572 KB |
Output is correct |
43 |
Correct |
303 ms |
14516 KB |
Output is correct |
44 |
Correct |
302 ms |
14544 KB |
Output is correct |
45 |
Correct |
312 ms |
16008 KB |
Output is correct |
46 |
Correct |
221 ms |
12092 KB |
Output is correct |
47 |
Correct |
291 ms |
12912 KB |
Output is correct |
48 |
Correct |
180 ms |
9212 KB |
Output is correct |
49 |
Correct |
303 ms |
15928 KB |
Output is correct |
50 |
Correct |
300 ms |
14448 KB |
Output is correct |
51 |
Correct |
306 ms |
15796 KB |
Output is correct |
52 |
Correct |
262 ms |
12856 KB |
Output is correct |
53 |
Correct |
180 ms |
13068 KB |
Output is correct |