#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, q, A[N + 5], B[N + 5], X[N + 5], Y[N + 5], Z[N + 5], arrIndex[N + 5], queryIndex[N + 5], ans[N + 5];
vector<int> node[2*N + 5], tree[2*N + 5];
void fakeUpdate(int x, int y) {
for (; x <= 2*N; x += x&-x) node[x].push_back(y);
}
void fakeQuery(int x, int y){
for (; x; x -= x&-x) node[x].push_back(y);
}
void compress() {
for (int i = 1; i <= 2*N; i++) {
sort(node[i].begin(), node[i].end());
node[i].erase(unique(node[i].begin(), node[i].end()), node[i].end());
tree[i].resize(node[i].size() + 1);
}
}
void update(int x, int y, int v) {
for (; x <= 2*N; x += x&-x) {
for (int yy = lower_bound(node[x].begin(), node[x].end(), y) - node[x].begin() + 1; yy <= node[x].size(); yy += yy&-yy) {
tree[x][yy] += v;
}
}
}
int query(int x, int y) {
int res = 0;
for (; x; x -= x&-x) {
for (int yy = lower_bound(node[x].begin(), node[x].end(), y) - node[x].begin() + 1; yy; yy -= yy&-yy) {
res += tree[x][yy];
}
}
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> A[i] >> B[i];
for (int i = 1; i <= q; i++) cin >> X[i] >> Y[i] >> Z[i];
vector<int> mp1, mp2;
for (int i = 1; i <= n; i++) mp1.push_back(A[i]), mp2.push_back(B[i]);
for (int i = 1; i <= q; i++) mp1.push_back(X[i]), mp2.push_back(Y[i]);
sort(mp1.begin(), mp1.end());
sort(mp2.begin(), mp2.end());
auto getIndex = [&](int a, vector<int>&p) { return lower_bound(p.begin(), p.end(), a) - p.begin() + 1; };
iota(arrIndex + 1, arrIndex + n + 1, 1ll);
sort(arrIndex + 1, arrIndex + n + 1, [&](int a, int b) {
tuple<int, int, int> f = {A[a] + B[a], A[a], B[a]}, se = {A[b] + B[b], A[b], B[b]};
return f > se;
});
iota(queryIndex + 1, queryIndex + q + 1, 1ll);
sort(queryIndex + 1, queryIndex + q + 1, [&](int a, int b) {
tuple<int, int, int> f = {Z[a], X[a], Y[a]}, se = {Z[b], X[b], Y[b]};
return f > se;
});
for (int i = 1; i <= q; i++) {
int mpX = getIndex(X[i], mp1), mpY = getIndex(Y[i], mp2);
fakeQuery(2*N, 2*N);
fakeQuery(2*N, mpY - 1);
fakeQuery(mpX - 1, 2*N);
fakeQuery(mpX - 1, mpY - 1);
}
for (int i = 1; i <= n; i++) {
int mpA = getIndex(A[i], mp1), mpB = getIndex(B[i], mp2);
fakeUpdate(mpA, mpB);
}
compress();
for (int i = 1, index = 1; i <= q; i++) {
int pos = queryIndex[i];
while (index <= n && A[arrIndex[index]] + B[arrIndex[index]] >= Z[pos]) {
int mpA = getIndex(A[arrIndex[index]], mp1), mpB = getIndex(B[arrIndex[index]], mp2);
update(mpA, mpB, 1);
index++;
}
int mpX = getIndex(X[pos], mp1), mpY = getIndex(Y[pos], mp2);
ans[pos] = query(2*N, 2*N) - query(2*N, mpY - 1) - query(mpX - 1, 2*N) + query(mpX - 1, mpY - 1);
}
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
return 0;
}
Compilation message
examination.cpp: In function 'void update(int, int, int)':
examination.cpp:27:90: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int yy = lower_bound(node[x].begin(), node[x].end(), y) - node[x].begin() + 1; yy <= node[x].size(); yy += yy&-yy) {
| ~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
15956 KB |
Output is correct |
2 |
Correct |
14 ms |
15992 KB |
Output is correct |
3 |
Correct |
13 ms |
16008 KB |
Output is correct |
4 |
Correct |
14 ms |
15992 KB |
Output is correct |
5 |
Correct |
14 ms |
15972 KB |
Output is correct |
6 |
Correct |
13 ms |
15956 KB |
Output is correct |
7 |
Correct |
30 ms |
17100 KB |
Output is correct |
8 |
Correct |
30 ms |
17096 KB |
Output is correct |
9 |
Correct |
30 ms |
17192 KB |
Output is correct |
10 |
Correct |
24 ms |
16904 KB |
Output is correct |
11 |
Correct |
28 ms |
17108 KB |
Output is correct |
12 |
Correct |
22 ms |
16724 KB |
Output is correct |
13 |
Correct |
29 ms |
17108 KB |
Output is correct |
14 |
Correct |
30 ms |
17092 KB |
Output is correct |
15 |
Correct |
30 ms |
17156 KB |
Output is correct |
16 |
Correct |
31 ms |
17108 KB |
Output is correct |
17 |
Correct |
22 ms |
16908 KB |
Output is correct |
18 |
Correct |
20 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
737 ms |
51312 KB |
Output is correct |
2 |
Correct |
738 ms |
51312 KB |
Output is correct |
3 |
Correct |
753 ms |
51356 KB |
Output is correct |
4 |
Correct |
444 ms |
44688 KB |
Output is correct |
5 |
Correct |
504 ms |
50376 KB |
Output is correct |
6 |
Correct |
221 ms |
41284 KB |
Output is correct |
7 |
Correct |
654 ms |
49536 KB |
Output is correct |
8 |
Correct |
703 ms |
49844 KB |
Output is correct |
9 |
Correct |
618 ms |
47900 KB |
Output is correct |
10 |
Correct |
489 ms |
46708 KB |
Output is correct |
11 |
Correct |
300 ms |
43564 KB |
Output is correct |
12 |
Correct |
207 ms |
41120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
737 ms |
51312 KB |
Output is correct |
2 |
Correct |
738 ms |
51312 KB |
Output is correct |
3 |
Correct |
753 ms |
51356 KB |
Output is correct |
4 |
Correct |
444 ms |
44688 KB |
Output is correct |
5 |
Correct |
504 ms |
50376 KB |
Output is correct |
6 |
Correct |
221 ms |
41284 KB |
Output is correct |
7 |
Correct |
654 ms |
49536 KB |
Output is correct |
8 |
Correct |
703 ms |
49844 KB |
Output is correct |
9 |
Correct |
618 ms |
47900 KB |
Output is correct |
10 |
Correct |
489 ms |
46708 KB |
Output is correct |
11 |
Correct |
300 ms |
43564 KB |
Output is correct |
12 |
Correct |
207 ms |
41120 KB |
Output is correct |
13 |
Correct |
986 ms |
51752 KB |
Output is correct |
14 |
Correct |
910 ms |
51664 KB |
Output is correct |
15 |
Correct |
823 ms |
51384 KB |
Output is correct |
16 |
Correct |
481 ms |
44996 KB |
Output is correct |
17 |
Correct |
739 ms |
50872 KB |
Output is correct |
18 |
Correct |
247 ms |
41492 KB |
Output is correct |
19 |
Correct |
910 ms |
51748 KB |
Output is correct |
20 |
Correct |
945 ms |
51868 KB |
Output is correct |
21 |
Correct |
846 ms |
50172 KB |
Output is correct |
22 |
Correct |
490 ms |
45944 KB |
Output is correct |
23 |
Correct |
311 ms |
43540 KB |
Output is correct |
24 |
Correct |
172 ms |
39780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
15956 KB |
Output is correct |
2 |
Correct |
14 ms |
15992 KB |
Output is correct |
3 |
Correct |
13 ms |
16008 KB |
Output is correct |
4 |
Correct |
14 ms |
15992 KB |
Output is correct |
5 |
Correct |
14 ms |
15972 KB |
Output is correct |
6 |
Correct |
13 ms |
15956 KB |
Output is correct |
7 |
Correct |
30 ms |
17100 KB |
Output is correct |
8 |
Correct |
30 ms |
17096 KB |
Output is correct |
9 |
Correct |
30 ms |
17192 KB |
Output is correct |
10 |
Correct |
24 ms |
16904 KB |
Output is correct |
11 |
Correct |
28 ms |
17108 KB |
Output is correct |
12 |
Correct |
22 ms |
16724 KB |
Output is correct |
13 |
Correct |
29 ms |
17108 KB |
Output is correct |
14 |
Correct |
30 ms |
17092 KB |
Output is correct |
15 |
Correct |
30 ms |
17156 KB |
Output is correct |
16 |
Correct |
31 ms |
17108 KB |
Output is correct |
17 |
Correct |
22 ms |
16908 KB |
Output is correct |
18 |
Correct |
20 ms |
16724 KB |
Output is correct |
19 |
Correct |
737 ms |
51312 KB |
Output is correct |
20 |
Correct |
738 ms |
51312 KB |
Output is correct |
21 |
Correct |
753 ms |
51356 KB |
Output is correct |
22 |
Correct |
444 ms |
44688 KB |
Output is correct |
23 |
Correct |
504 ms |
50376 KB |
Output is correct |
24 |
Correct |
221 ms |
41284 KB |
Output is correct |
25 |
Correct |
654 ms |
49536 KB |
Output is correct |
26 |
Correct |
703 ms |
49844 KB |
Output is correct |
27 |
Correct |
618 ms |
47900 KB |
Output is correct |
28 |
Correct |
489 ms |
46708 KB |
Output is correct |
29 |
Correct |
300 ms |
43564 KB |
Output is correct |
30 |
Correct |
207 ms |
41120 KB |
Output is correct |
31 |
Correct |
986 ms |
51752 KB |
Output is correct |
32 |
Correct |
910 ms |
51664 KB |
Output is correct |
33 |
Correct |
823 ms |
51384 KB |
Output is correct |
34 |
Correct |
481 ms |
44996 KB |
Output is correct |
35 |
Correct |
739 ms |
50872 KB |
Output is correct |
36 |
Correct |
247 ms |
41492 KB |
Output is correct |
37 |
Correct |
910 ms |
51748 KB |
Output is correct |
38 |
Correct |
945 ms |
51868 KB |
Output is correct |
39 |
Correct |
846 ms |
50172 KB |
Output is correct |
40 |
Correct |
490 ms |
45944 KB |
Output is correct |
41 |
Correct |
311 ms |
43540 KB |
Output is correct |
42 |
Correct |
172 ms |
39780 KB |
Output is correct |
43 |
Correct |
943 ms |
55076 KB |
Output is correct |
44 |
Correct |
920 ms |
55008 KB |
Output is correct |
45 |
Correct |
978 ms |
55012 KB |
Output is correct |
46 |
Correct |
437 ms |
46716 KB |
Output is correct |
47 |
Correct |
747 ms |
52624 KB |
Output is correct |
48 |
Correct |
230 ms |
41136 KB |
Output is correct |
49 |
Correct |
819 ms |
53272 KB |
Output is correct |
50 |
Correct |
926 ms |
55084 KB |
Output is correct |
51 |
Correct |
811 ms |
53256 KB |
Output is correct |
52 |
Correct |
626 ms |
50984 KB |
Output is correct |
53 |
Correct |
330 ms |
44752 KB |
Output is correct |