#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+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;
}
struct point {
int x, y, sum;
bool operator < (const point &other) {
if(x == other.x) {
return y < other.y;
}
return x < other.x;
}
};
point a[N], b[N];
void compress(vector<int*> &coords) {
sort(coords.begin(), coords.end(), [&](const int* x, const int* y) {
return *x < *y;
});
int prev = -1;
for(int i = 0, current = 0; i < (int)coords.size(); ++i) {
if(prev != *coords[i]) {
++current;
}
prev = *coords[i];
*coords[i] = current;
}
}
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 >> a[i].x >> a[i].y;
a[i].sum = a[i].x + a[i].y;
sorted_sums.push_back(a[i].sum);
}
sort(sorted_sums.begin(), sorted_sums.end());
sort(a + 1, a + n + 1);
for(int i = 1; i <= m; ++i) {
cin >> b[i].x >> b[i].y >> b[i].sum;
b[i].sum = max(b[i].sum, b[i].x + b[i].y);
}
vector<int> ans(m+1);
for(int i = 1; i <= m; ++i) {
int l = 0, r = n - 1, ind = n;
while(r >= l) {
int middle = l + r >> 1;
if(sorted_sums[middle] >= b[i].sum) {
r = middle - 1;
ind = middle;
} else {
l = middle + 1;
}
}
ans[i] = n - ind;
}
vector<int*> coords(n+m);
for(int i = 0; i < n + m; ++i) {
coords[i] = i < n ? &a[i + 1].x : &b[i + 1 - n].x;
}
compress(coords);
for(int i = 0; i < n + m; ++i) {
coords[i] = i < n ? &a[i + 1].y : &b[i + 1 - n].y;
}
compress(coords);
for(int i = 0; i < n + m; ++i) {
coords[i] = i < n ? &a[i + 1].sum : &b[i + 1 - n].sum;
}
compress(coords);
fenwick_tree<int> tx(2 * N, 0);
fenwick_tree<int> ty(2 * N, 0);
vector<int> sums[2 * N], queries[2 * N];
for(int i = 1; i <= n; ++i) {
int val = a[i].sum;
sums[val].push_back(i);
}
for(int i = 1; i <= m; ++i) {
int val = b[i].sum;
queries[val].push_back(i);
}
for(int i = 2 * N - 1; i >= 1; --i) {
for(int id : sums[i]) {
int val_x = a[id].x;
int val_y = a[id].y;
tx.update(val_x, 1);
ty.update(val_y, 1);
}
for(int id : queries[i]) {
int val_x = b[id].x;
int val_y = b[id].y;
ans[id] -= tx.query_prefix(val_x - 1);
ans[id] -= ty.query_prefix(val_y - 1);
}
}
for(int i = 1; i <= m; ++i) {
cout << ans[i] << '\n';
}
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:84:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
84 | int middle = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
8 ms |
11244 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
8 ms |
11244 KB |
Output is correct |
7 |
Correct |
14 ms |
11628 KB |
Output is correct |
8 |
Correct |
14 ms |
11628 KB |
Output is correct |
9 |
Correct |
14 ms |
11628 KB |
Output is correct |
10 |
Correct |
12 ms |
11756 KB |
Output is correct |
11 |
Correct |
13 ms |
11628 KB |
Output is correct |
12 |
Correct |
14 ms |
11500 KB |
Output is correct |
13 |
Correct |
13 ms |
11628 KB |
Output is correct |
14 |
Correct |
13 ms |
11628 KB |
Output is correct |
15 |
Correct |
14 ms |
11628 KB |
Output is correct |
16 |
Correct |
15 ms |
11628 KB |
Output is correct |
17 |
Correct |
14 ms |
11628 KB |
Output is correct |
18 |
Correct |
11 ms |
11500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
21248 KB |
Output is correct |
2 |
Correct |
225 ms |
21228 KB |
Output is correct |
3 |
Correct |
227 ms |
21228 KB |
Output is correct |
4 |
Correct |
170 ms |
20588 KB |
Output is correct |
5 |
Correct |
179 ms |
20588 KB |
Output is correct |
6 |
Correct |
105 ms |
17644 KB |
Output is correct |
7 |
Correct |
221 ms |
20896 KB |
Output is correct |
8 |
Correct |
204 ms |
20972 KB |
Output is correct |
9 |
Correct |
180 ms |
19820 KB |
Output is correct |
10 |
Correct |
162 ms |
20460 KB |
Output is correct |
11 |
Correct |
161 ms |
20332 KB |
Output is correct |
12 |
Correct |
85 ms |
17264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
214 ms |
21248 KB |
Output is correct |
2 |
Correct |
225 ms |
21228 KB |
Output is correct |
3 |
Correct |
227 ms |
21228 KB |
Output is correct |
4 |
Correct |
170 ms |
20588 KB |
Output is correct |
5 |
Correct |
179 ms |
20588 KB |
Output is correct |
6 |
Correct |
105 ms |
17644 KB |
Output is correct |
7 |
Correct |
221 ms |
20896 KB |
Output is correct |
8 |
Correct |
204 ms |
20972 KB |
Output is correct |
9 |
Correct |
180 ms |
19820 KB |
Output is correct |
10 |
Correct |
162 ms |
20460 KB |
Output is correct |
11 |
Correct |
161 ms |
20332 KB |
Output is correct |
12 |
Correct |
85 ms |
17264 KB |
Output is correct |
13 |
Correct |
213 ms |
21100 KB |
Output is correct |
14 |
Correct |
224 ms |
21084 KB |
Output is correct |
15 |
Correct |
216 ms |
21228 KB |
Output is correct |
16 |
Correct |
173 ms |
20332 KB |
Output is correct |
17 |
Correct |
183 ms |
20460 KB |
Output is correct |
18 |
Correct |
106 ms |
17644 KB |
Output is correct |
19 |
Correct |
230 ms |
21212 KB |
Output is correct |
20 |
Correct |
220 ms |
21016 KB |
Output is correct |
21 |
Correct |
222 ms |
21228 KB |
Output is correct |
22 |
Correct |
166 ms |
20460 KB |
Output is correct |
23 |
Correct |
163 ms |
20400 KB |
Output is correct |
24 |
Correct |
87 ms |
17208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11244 KB |
Output is correct |
2 |
Correct |
8 ms |
11244 KB |
Output is correct |
3 |
Correct |
8 ms |
11244 KB |
Output is correct |
4 |
Correct |
8 ms |
11244 KB |
Output is correct |
5 |
Correct |
8 ms |
11264 KB |
Output is correct |
6 |
Correct |
8 ms |
11244 KB |
Output is correct |
7 |
Correct |
14 ms |
11628 KB |
Output is correct |
8 |
Correct |
14 ms |
11628 KB |
Output is correct |
9 |
Correct |
14 ms |
11628 KB |
Output is correct |
10 |
Correct |
12 ms |
11756 KB |
Output is correct |
11 |
Correct |
13 ms |
11628 KB |
Output is correct |
12 |
Correct |
14 ms |
11500 KB |
Output is correct |
13 |
Correct |
13 ms |
11628 KB |
Output is correct |
14 |
Correct |
13 ms |
11628 KB |
Output is correct |
15 |
Correct |
14 ms |
11628 KB |
Output is correct |
16 |
Correct |
15 ms |
11628 KB |
Output is correct |
17 |
Correct |
14 ms |
11628 KB |
Output is correct |
18 |
Correct |
11 ms |
11500 KB |
Output is correct |
19 |
Correct |
214 ms |
21248 KB |
Output is correct |
20 |
Correct |
225 ms |
21228 KB |
Output is correct |
21 |
Correct |
227 ms |
21228 KB |
Output is correct |
22 |
Correct |
170 ms |
20588 KB |
Output is correct |
23 |
Correct |
179 ms |
20588 KB |
Output is correct |
24 |
Correct |
105 ms |
17644 KB |
Output is correct |
25 |
Correct |
221 ms |
20896 KB |
Output is correct |
26 |
Correct |
204 ms |
20972 KB |
Output is correct |
27 |
Correct |
180 ms |
19820 KB |
Output is correct |
28 |
Correct |
162 ms |
20460 KB |
Output is correct |
29 |
Correct |
161 ms |
20332 KB |
Output is correct |
30 |
Correct |
85 ms |
17264 KB |
Output is correct |
31 |
Correct |
213 ms |
21100 KB |
Output is correct |
32 |
Correct |
224 ms |
21084 KB |
Output is correct |
33 |
Correct |
216 ms |
21228 KB |
Output is correct |
34 |
Correct |
173 ms |
20332 KB |
Output is correct |
35 |
Correct |
183 ms |
20460 KB |
Output is correct |
36 |
Correct |
106 ms |
17644 KB |
Output is correct |
37 |
Correct |
230 ms |
21212 KB |
Output is correct |
38 |
Correct |
220 ms |
21016 KB |
Output is correct |
39 |
Correct |
222 ms |
21228 KB |
Output is correct |
40 |
Correct |
166 ms |
20460 KB |
Output is correct |
41 |
Correct |
163 ms |
20400 KB |
Output is correct |
42 |
Correct |
87 ms |
17208 KB |
Output is correct |
43 |
Correct |
244 ms |
22892 KB |
Output is correct |
44 |
Correct |
248 ms |
22764 KB |
Output is correct |
45 |
Correct |
249 ms |
22892 KB |
Output is correct |
46 |
Correct |
190 ms |
22892 KB |
Output is correct |
47 |
Correct |
210 ms |
22892 KB |
Output is correct |
48 |
Correct |
104 ms |
17516 KB |
Output is correct |
49 |
Correct |
239 ms |
22892 KB |
Output is correct |
50 |
Correct |
249 ms |
22892 KB |
Output is correct |
51 |
Correct |
241 ms |
23020 KB |
Output is correct |
52 |
Correct |
185 ms |
22636 KB |
Output is correct |
53 |
Correct |
183 ms |
22764 KB |
Output is correct |