#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
template <typename T>
struct fenwick_tree {
int size;
vector<T> tree;
T neutral;
fenwick_tree(int n, T _neutral) {
size = n;
neutral = _neutral;
tree = vector<T>(n + 1);
}
void update(int index, T value) {
for(; index <= size; index += index & -index) {
tree[index] += value;
}
}
T query_prefix(int index) {
T ans = neutral;
for(; index; index -= index & -index) {
ans += tree[index];
}
return ans;
}
T query(int l, int r) {
return query_prefix(r) - query_prefix(l - 1);
}
};
map<int, int> compress(vector<int> &coords) {
sort(coords.begin(), coords.end());
map<int, int> compressed;
for(int i = 0, current = 0; i < (int)coords.size(); ++i) {
if(i == 0 || coords[i] != coords[i - 1]) ++current;
compressed[coords[i]] = current;
}
return compressed;
}
int x[N], y[N], a[N], b[N], c[N];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<int>> v;
vector<int> sorted_sums;
for(int i = 1; i <= n; ++i) {
cin >> x[i] >> y[i];
sorted_sums.push_back(x[i] + y[i]);
v.push_back({x[i], y[i]});
}
sort(sorted_sums.begin(), sorted_sums.end());
sort(v.begin(), v.end());
for(int i = 1; i <= n; ++i) {
x[i] = v[i - 1][0];
y[i] = v[i - 1][1];
}
v.clear();
for(int i = 1; i <= m; ++i) {
cin >> a[i] >> b[i] >> c[i];
}
vector<int> type1, type2;
for(int i = 1; i <= m; ++i) {
if(a[i] + b[i] >= c[i]) {
type1.push_back(i);
} else {
type2.push_back(i);
}
}
map<int, int> compressed_sum, compressed_x, compressed_y;
//sum
vector<int> coords(n + m);
for(int i = 1; i <= m; ++i) {
coords[i - 1] = c[i];
}
for(int i = 1; i <= n; ++i) {
coords[i + m - 1] = x[i] + y[i];
}
compressed_sum = compress(coords);
//x
for(int i = 1; i <= m; ++i) {
coords[i - 1] = a[i];
}
for(int i = 1; i <= n; ++i) {
coords[i + m - 1] = x[i];
}
compressed_x = compress(coords);
//y
for(int i = 1; i <= m; ++i) {
coords[i - 1] = b[i];
}
for(int i = 1; i <= n; ++i) {
coords[i + m - 1] = y[i];
}
compressed_y = compress(coords);
sort(type1.begin(), type1.end(), [&](int x, int y) {
if(a[x] == a[y]) {
if(b[x] == b[y]) {
return c[x] < c[y];
}
return b[x] < b[y];
}
return a[x] < a[y];
});
auto sum = [](int x, int y) -> int {
return x + y;
};
fenwick_tree<int> t(N, 0);
fenwick_tree<int> tx(N, 0);
fenwick_tree<int> ty(N, 0);
vector<int> ans(m+1);
for(int i = (int)type1.size() - 1, j = n; i >= 0; --i) {
int q_id = type1[i];
while(j > 0 && x[j] >= a[q_id]) {
int val = compressed_y[y[j]];
t.update(val, 1);
--j;
}
int val = compressed_y[b[q_id]];
ans[q_id] = t.query(val, t.size);
}
for(int i : type2) {
int l = 0, r = n - 1, ind = n;
while(r >= l) {
int middle = l + r >> 1;
if(sorted_sums[middle] >= c[i]) {
r = middle - 1;
ind = middle;
} else {
l = middle + 1;
}
}
ans[i] = n - ind;
}
vector<int> sums[N], queries[N];
for(int i = 1; i <= n; ++i) {
int val = compressed_sum[x[i] + y[i]];
sums[val].push_back(i);
}
for(int i : type2) {
if(i == -1) continue;
int val = compressed_sum[c[i]];
queries[val].push_back(i);
}
int max_sum_val = (int)compressed_sum.size();
for(int i = max_sum_val; i >= 1; --i) {
for(int id : sums[i]) {
int val_x = compressed_x[x[id]];
int val_y = compressed_y[y[id]];
tx.update(val_x, 1);
ty.update(val_y, 1);
}
for(int id : queries[i]) {
int val_x = compressed_x[a[id]];
int val_y = compressed_y[b[id]];
ans[id] -= tx.query(1, val_x - 1);
ans[id] -= ty.query(1, val_y - 1);
}
}
for(int i = 1; i <= m; ++i) {
cout << ans[i] << '\n';
}
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:125:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
125 | int middle = l + r >> 1;
| ~~^~~
examination.cpp:105:7: warning: variable 'sum' set but not used [-Wunused-but-set-variable]
105 | auto sum = [](int x, int y) -> int {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
18028 KB |
Output is correct |
2 |
Correct |
11 ms |
18028 KB |
Output is correct |
3 |
Correct |
12 ms |
18028 KB |
Output is correct |
4 |
Correct |
12 ms |
18028 KB |
Output is correct |
5 |
Correct |
12 ms |
18028 KB |
Output is correct |
6 |
Correct |
12 ms |
18028 KB |
Output is correct |
7 |
Correct |
23 ms |
19180 KB |
Output is correct |
8 |
Correct |
23 ms |
19180 KB |
Output is correct |
9 |
Correct |
23 ms |
19180 KB |
Output is correct |
10 |
Correct |
20 ms |
18924 KB |
Output is correct |
11 |
Correct |
22 ms |
18924 KB |
Output is correct |
12 |
Correct |
16 ms |
18284 KB |
Output is correct |
13 |
Correct |
21 ms |
19052 KB |
Output is correct |
14 |
Correct |
21 ms |
18924 KB |
Output is correct |
15 |
Correct |
21 ms |
18924 KB |
Output is correct |
16 |
Correct |
18 ms |
18668 KB |
Output is correct |
17 |
Correct |
19 ms |
18924 KB |
Output is correct |
18 |
Correct |
15 ms |
18284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
454 ms |
38940 KB |
Output is correct |
2 |
Correct |
443 ms |
38812 KB |
Output is correct |
3 |
Correct |
471 ms |
39088 KB |
Output is correct |
4 |
Correct |
230 ms |
33980 KB |
Output is correct |
5 |
Correct |
267 ms |
34076 KB |
Output is correct |
6 |
Correct |
139 ms |
26908 KB |
Output is correct |
7 |
Correct |
419 ms |
37916 KB |
Output is correct |
8 |
Correct |
431 ms |
37904 KB |
Output is correct |
9 |
Correct |
437 ms |
37028 KB |
Output is correct |
10 |
Correct |
237 ms |
33820 KB |
Output is correct |
11 |
Correct |
221 ms |
33820 KB |
Output is correct |
12 |
Correct |
111 ms |
26012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
454 ms |
38940 KB |
Output is correct |
2 |
Correct |
443 ms |
38812 KB |
Output is correct |
3 |
Correct |
471 ms |
39088 KB |
Output is correct |
4 |
Correct |
230 ms |
33980 KB |
Output is correct |
5 |
Correct |
267 ms |
34076 KB |
Output is correct |
6 |
Correct |
139 ms |
26908 KB |
Output is correct |
7 |
Correct |
419 ms |
37916 KB |
Output is correct |
8 |
Correct |
431 ms |
37904 KB |
Output is correct |
9 |
Correct |
437 ms |
37028 KB |
Output is correct |
10 |
Correct |
237 ms |
33820 KB |
Output is correct |
11 |
Correct |
221 ms |
33820 KB |
Output is correct |
12 |
Correct |
111 ms |
26012 KB |
Output is correct |
13 |
Correct |
563 ms |
42524 KB |
Output is correct |
14 |
Correct |
501 ms |
41500 KB |
Output is correct |
15 |
Correct |
447 ms |
38940 KB |
Output is correct |
16 |
Correct |
302 ms |
36252 KB |
Output is correct |
17 |
Correct |
341 ms |
36252 KB |
Output is correct |
18 |
Correct |
137 ms |
26140 KB |
Output is correct |
19 |
Correct |
573 ms |
42396 KB |
Output is correct |
20 |
Correct |
542 ms |
41756 KB |
Output is correct |
21 |
Correct |
608 ms |
42484 KB |
Output is correct |
22 |
Correct |
238 ms |
33820 KB |
Output is correct |
23 |
Correct |
215 ms |
33820 KB |
Output is correct |
24 |
Correct |
113 ms |
26012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
18028 KB |
Output is correct |
2 |
Correct |
11 ms |
18028 KB |
Output is correct |
3 |
Correct |
12 ms |
18028 KB |
Output is correct |
4 |
Correct |
12 ms |
18028 KB |
Output is correct |
5 |
Correct |
12 ms |
18028 KB |
Output is correct |
6 |
Correct |
12 ms |
18028 KB |
Output is correct |
7 |
Correct |
23 ms |
19180 KB |
Output is correct |
8 |
Correct |
23 ms |
19180 KB |
Output is correct |
9 |
Correct |
23 ms |
19180 KB |
Output is correct |
10 |
Correct |
20 ms |
18924 KB |
Output is correct |
11 |
Correct |
22 ms |
18924 KB |
Output is correct |
12 |
Correct |
16 ms |
18284 KB |
Output is correct |
13 |
Correct |
21 ms |
19052 KB |
Output is correct |
14 |
Correct |
21 ms |
18924 KB |
Output is correct |
15 |
Correct |
21 ms |
18924 KB |
Output is correct |
16 |
Correct |
18 ms |
18668 KB |
Output is correct |
17 |
Correct |
19 ms |
18924 KB |
Output is correct |
18 |
Correct |
15 ms |
18284 KB |
Output is correct |
19 |
Correct |
454 ms |
38940 KB |
Output is correct |
20 |
Correct |
443 ms |
38812 KB |
Output is correct |
21 |
Correct |
471 ms |
39088 KB |
Output is correct |
22 |
Correct |
230 ms |
33980 KB |
Output is correct |
23 |
Correct |
267 ms |
34076 KB |
Output is correct |
24 |
Correct |
139 ms |
26908 KB |
Output is correct |
25 |
Correct |
419 ms |
37916 KB |
Output is correct |
26 |
Correct |
431 ms |
37904 KB |
Output is correct |
27 |
Correct |
437 ms |
37028 KB |
Output is correct |
28 |
Correct |
237 ms |
33820 KB |
Output is correct |
29 |
Correct |
221 ms |
33820 KB |
Output is correct |
30 |
Correct |
111 ms |
26012 KB |
Output is correct |
31 |
Correct |
563 ms |
42524 KB |
Output is correct |
32 |
Correct |
501 ms |
41500 KB |
Output is correct |
33 |
Correct |
447 ms |
38940 KB |
Output is correct |
34 |
Correct |
302 ms |
36252 KB |
Output is correct |
35 |
Correct |
341 ms |
36252 KB |
Output is correct |
36 |
Correct |
137 ms |
26140 KB |
Output is correct |
37 |
Correct |
573 ms |
42396 KB |
Output is correct |
38 |
Correct |
542 ms |
41756 KB |
Output is correct |
39 |
Correct |
608 ms |
42484 KB |
Output is correct |
40 |
Correct |
238 ms |
33820 KB |
Output is correct |
41 |
Correct |
215 ms |
33820 KB |
Output is correct |
42 |
Correct |
113 ms |
26012 KB |
Output is correct |
43 |
Correct |
761 ms |
57884 KB |
Output is correct |
44 |
Correct |
774 ms |
58012 KB |
Output is correct |
45 |
Correct |
672 ms |
56952 KB |
Output is correct |
46 |
Correct |
366 ms |
48412 KB |
Output is correct |
47 |
Correct |
464 ms |
48412 KB |
Output is correct |
48 |
Correct |
139 ms |
26780 KB |
Output is correct |
49 |
Correct |
707 ms |
57756 KB |
Output is correct |
50 |
Correct |
807 ms |
58524 KB |
Output is correct |
51 |
Correct |
828 ms |
58984 KB |
Output is correct |
52 |
Correct |
389 ms |
48284 KB |
Output is correct |
53 |
Correct |
247 ms |
42012 KB |
Output is correct |