#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
struct query{
int x, y, z, i;
query() {}
query(int x, int y, int z, int i) :
x(x), y(y), z(z), i(i) {}
};
vector <query> Q;
vector <int> Y;
pii P[101010], _P[101010];
int T[101010], A[101010];
int n, q;
void addval(int p, int v) { for(; p<=n; p+=p&-p) T[p] += v; }
int getval(int p) { return p? T[p] + getval(p - (p & -p)) : 0; }
void dnc(int s, int e, vector <query> &Q)
{
vector <query> QL, QR, QH;
int i, j, k, l, m;
m = s + e >> 1;
for(query &q: Q){
if(q.z <= s) QH.push_back(q);
else if(m < q.z) QR.push_back(q);
else QL.push_back(q), QR.push_back(q);
}
if(s < e){
dnc(s, m, QL);
dnc(m + 1, e, QR);
}
for(i=s, j=m+1, k=0, l=0; i<=m || j<=e; l++){
if(j > e || (i <= m && P[i] > P[j])) _P[l] = P[i++];
else _P[l] = P[j++];
for(; k<QH.size() && QH[k].x > _P[l].first; k++){
A[QH[k].i] += getval(QH[k].y);
}
addval(_P[l].second, 1);
}
for(; k<QH.size(); k++){
A[QH[k].i] += getval(QH[k].y);
}
for(i=s; i<=e; i++){
P[i] = _P[i - s];
addval(P[i].second, -1);
}
}
int main()
{
int i, x, y, z;
scanf("%d%d", &n, &q);
for(i=0; i<n; i++){
scanf("%d%d", &P[i].first, &P[i].second);
Y.push_back(-P[i].second);
}
sort(P, P + n, [&](pii &pa, pii &pb){
return pa.first + pa.second < pb.first + pb.second;
});
sort(Y.begin(), Y.end());
for(i=0; i<q; i++){
scanf("%d%d%d", &x, &y, &z);
y = upper_bound(Y.begin(), Y.end(), -y) - Y.begin();
z = lower_bound(P, P + n, z, [&](pii &pa, int z){
return pa.first + pa.second < z;
}) - P;
Q.emplace_back(x, y, z, i);
}
for(i=0; i<n; i++){
P[i].second = upper_bound(Y.begin(), Y.end(), -P[i].second) - Y.begin();
}
sort(Q.begin(), Q.end(), [&](query &qa, query &qb){
return qa.x > qb.x;
});
dnc(0, n - 1, Q);
for(i=0; i<q; i++){
printf("%d\n", A[i]);
}
return 0;
}
Compilation message
examination.cpp: In function 'void dnc(int, int, std::vector<query>&)':
examination.cpp:28:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
m = s + e >> 1;
~~^~~
examination.cpp:45:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; k<QH.size() && QH[k].x > _P[l].first; k++){
~^~~~~~~~~~
examination.cpp:51:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; k<QH.size(); k++){
~^~~~~~~~~~
examination.cpp: In function 'int main()':
examination.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &q);
~~~~~^~~~~~~~~~~~~~~~
examination.cpp:68:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &P[i].first, &P[i].second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &x, &y, &z);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
11 ms |
760 KB |
Output is correct |
8 |
Correct |
11 ms |
760 KB |
Output is correct |
9 |
Correct |
11 ms |
888 KB |
Output is correct |
10 |
Correct |
9 ms |
760 KB |
Output is correct |
11 |
Correct |
10 ms |
760 KB |
Output is correct |
12 |
Correct |
7 ms |
760 KB |
Output is correct |
13 |
Correct |
8 ms |
632 KB |
Output is correct |
14 |
Correct |
8 ms |
632 KB |
Output is correct |
15 |
Correct |
7 ms |
632 KB |
Output is correct |
16 |
Correct |
6 ms |
632 KB |
Output is correct |
17 |
Correct |
8 ms |
632 KB |
Output is correct |
18 |
Correct |
5 ms |
1016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
6392 KB |
Output is correct |
2 |
Correct |
236 ms |
6380 KB |
Output is correct |
3 |
Correct |
234 ms |
6308 KB |
Output is correct |
4 |
Correct |
183 ms |
6252 KB |
Output is correct |
5 |
Correct |
201 ms |
6328 KB |
Output is correct |
6 |
Correct |
145 ms |
6116 KB |
Output is correct |
7 |
Correct |
232 ms |
6396 KB |
Output is correct |
8 |
Correct |
228 ms |
6372 KB |
Output is correct |
9 |
Correct |
227 ms |
6344 KB |
Output is correct |
10 |
Correct |
190 ms |
6336 KB |
Output is correct |
11 |
Correct |
111 ms |
6212 KB |
Output is correct |
12 |
Correct |
83 ms |
6144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
6392 KB |
Output is correct |
2 |
Correct |
236 ms |
6380 KB |
Output is correct |
3 |
Correct |
234 ms |
6308 KB |
Output is correct |
4 |
Correct |
183 ms |
6252 KB |
Output is correct |
5 |
Correct |
201 ms |
6328 KB |
Output is correct |
6 |
Correct |
145 ms |
6116 KB |
Output is correct |
7 |
Correct |
232 ms |
6396 KB |
Output is correct |
8 |
Correct |
228 ms |
6372 KB |
Output is correct |
9 |
Correct |
227 ms |
6344 KB |
Output is correct |
10 |
Correct |
190 ms |
6336 KB |
Output is correct |
11 |
Correct |
111 ms |
6212 KB |
Output is correct |
12 |
Correct |
83 ms |
6144 KB |
Output is correct |
13 |
Correct |
385 ms |
12464 KB |
Output is correct |
14 |
Correct |
370 ms |
17876 KB |
Output is correct |
15 |
Correct |
238 ms |
6288 KB |
Output is correct |
16 |
Correct |
316 ms |
11368 KB |
Output is correct |
17 |
Correct |
337 ms |
11668 KB |
Output is correct |
18 |
Correct |
213 ms |
17052 KB |
Output is correct |
19 |
Correct |
369 ms |
12448 KB |
Output is correct |
20 |
Correct |
369 ms |
13160 KB |
Output is correct |
21 |
Correct |
365 ms |
12596 KB |
Output is correct |
22 |
Correct |
187 ms |
6372 KB |
Output is correct |
23 |
Correct |
111 ms |
6176 KB |
Output is correct |
24 |
Correct |
82 ms |
6120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
11 ms |
760 KB |
Output is correct |
8 |
Correct |
11 ms |
760 KB |
Output is correct |
9 |
Correct |
11 ms |
888 KB |
Output is correct |
10 |
Correct |
9 ms |
760 KB |
Output is correct |
11 |
Correct |
10 ms |
760 KB |
Output is correct |
12 |
Correct |
7 ms |
760 KB |
Output is correct |
13 |
Correct |
8 ms |
632 KB |
Output is correct |
14 |
Correct |
8 ms |
632 KB |
Output is correct |
15 |
Correct |
7 ms |
632 KB |
Output is correct |
16 |
Correct |
6 ms |
632 KB |
Output is correct |
17 |
Correct |
8 ms |
632 KB |
Output is correct |
18 |
Correct |
5 ms |
1016 KB |
Output is correct |
19 |
Correct |
235 ms |
6392 KB |
Output is correct |
20 |
Correct |
236 ms |
6380 KB |
Output is correct |
21 |
Correct |
234 ms |
6308 KB |
Output is correct |
22 |
Correct |
183 ms |
6252 KB |
Output is correct |
23 |
Correct |
201 ms |
6328 KB |
Output is correct |
24 |
Correct |
145 ms |
6116 KB |
Output is correct |
25 |
Correct |
232 ms |
6396 KB |
Output is correct |
26 |
Correct |
228 ms |
6372 KB |
Output is correct |
27 |
Correct |
227 ms |
6344 KB |
Output is correct |
28 |
Correct |
190 ms |
6336 KB |
Output is correct |
29 |
Correct |
111 ms |
6212 KB |
Output is correct |
30 |
Correct |
83 ms |
6144 KB |
Output is correct |
31 |
Correct |
385 ms |
12464 KB |
Output is correct |
32 |
Correct |
370 ms |
17876 KB |
Output is correct |
33 |
Correct |
238 ms |
6288 KB |
Output is correct |
34 |
Correct |
316 ms |
11368 KB |
Output is correct |
35 |
Correct |
337 ms |
11668 KB |
Output is correct |
36 |
Correct |
213 ms |
17052 KB |
Output is correct |
37 |
Correct |
369 ms |
12448 KB |
Output is correct |
38 |
Correct |
369 ms |
13160 KB |
Output is correct |
39 |
Correct |
365 ms |
12596 KB |
Output is correct |
40 |
Correct |
187 ms |
6372 KB |
Output is correct |
41 |
Correct |
111 ms |
6176 KB |
Output is correct |
42 |
Correct |
82 ms |
6120 KB |
Output is correct |
43 |
Correct |
394 ms |
12360 KB |
Output is correct |
44 |
Correct |
400 ms |
12480 KB |
Output is correct |
45 |
Correct |
382 ms |
18272 KB |
Output is correct |
46 |
Correct |
321 ms |
11104 KB |
Output is correct |
47 |
Correct |
349 ms |
11256 KB |
Output is correct |
48 |
Correct |
179 ms |
24708 KB |
Output is correct |
49 |
Correct |
380 ms |
18072 KB |
Output is correct |
50 |
Correct |
383 ms |
12488 KB |
Output is correct |
51 |
Correct |
369 ms |
16608 KB |
Output is correct |
52 |
Correct |
325 ms |
11364 KB |
Output is correct |
53 |
Correct |
117 ms |
6112 KB |
Output is correct |