#include<bits/stdc++.h>
using namespace std;
struct entry{
int op;
int a,b,c;
int id;
};
bool cmpa(entry x, entry y) {
if(x.a == y.a) return x.op < y.op;
return x.a > y.a;
}
bool cmpb(entry x, entry y) {
if(x.b == y.b) return x.op < y.op;
return x.b > y.b;
}
struct node{
int v;
node *l, *r;
node(): v(0), l(NULL), r(NULL) {}
};
entry a[200005];
entry b[200005];
int ans[200005];
pair<int,int> c[200005];
int n,q;
int w,x,y;
void upd(node *x, long long l, long long r, int pos) {
if(l == r) {
x->v++;
} else {
if(x->l == NULL) {
x -> l = new node();
x -> r = new node();
}
if(pos<=(l+r)/2) upd(x->l, l, (l+r)/2, pos);
else upd(x->r, (l+r)/2+1, r, pos);
x->v = x->l->v + x->r->v;
}
}
int qry(node *x, long long l, long long r, int ll, int rr) {
if(ll > r || rr < l) return 0;
if(ll <= l && r <= rr) return x -> v;
if(x -> l == NULL) return x -> v;
return qry(x->l, l, (l+r)/2, ll, rr) + qry(x->r, (l+r)/2+1, r, ll, rr);
}
void cdq(int l, int r) {
if(l >= r) return;
int mid = (l+r)/2;
int ptr = 0;
for(int i=l; i<=r; i++) {
if(i <= mid && a[i].op == 1) {
b[ptr] = a[i];
c[ptr] = make_pair(a[i].c,ptr);
ptr++;
} else if(i > mid && a[i].op == 2) {
b[ptr] = a[i];
c[ptr] = make_pair(a[i].c,ptr);
ptr++;
}
}
sort(c,c+ptr);
int temp = 0;
for(int i=0; i<ptr; i++) {
if(i>0 && c[i].first != c[i-1].first) temp++;
b[c[i].second].c = temp;
}
sort(b,b+ptr,cmpb);
node *rt = new node();
for(int i=0; i<ptr; i++) {
if(b[i].op == 1) {
upd(rt, 0, r-l+1, b[i].c);
} else {
ans[b[i].id] += qry(rt, 0, r-l+1, b[i].c, r-l+1);
}
}
cdq(l, mid);
cdq(mid+1, r);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for(int i=0; i<n; i++) {
cin >> x >> y;
a[i] = (entry){1,x,y,x+y,0};
}
for(int i=n; i<n+q; i++) {
cin >> w >> x >> y;
a[i] = (entry){2,w,x,y,i-n};
}
sort(a,a+n+q,cmpa);
cdq(0,n+q-1);
for(int i=0; i<q; i++) cout << ans[i] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
13 ms |
2768 KB |
Output is correct |
8 |
Correct |
14 ms |
2896 KB |
Output is correct |
9 |
Correct |
14 ms |
2832 KB |
Output is correct |
10 |
Correct |
13 ms |
2516 KB |
Output is correct |
11 |
Correct |
13 ms |
2644 KB |
Output is correct |
12 |
Correct |
12 ms |
1476 KB |
Output is correct |
13 |
Correct |
15 ms |
2516 KB |
Output is correct |
14 |
Correct |
12 ms |
2516 KB |
Output is correct |
15 |
Correct |
12 ms |
2480 KB |
Output is correct |
16 |
Correct |
11 ms |
2132 KB |
Output is correct |
17 |
Correct |
12 ms |
2516 KB |
Output is correct |
18 |
Correct |
9 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
545 ms |
83816 KB |
Output is correct |
2 |
Correct |
545 ms |
83944 KB |
Output is correct |
3 |
Correct |
547 ms |
83788 KB |
Output is correct |
4 |
Correct |
502 ms |
70016 KB |
Output is correct |
5 |
Correct |
502 ms |
73980 KB |
Output is correct |
6 |
Correct |
401 ms |
36500 KB |
Output is correct |
7 |
Correct |
545 ms |
81440 KB |
Output is correct |
8 |
Correct |
536 ms |
83932 KB |
Output is correct |
9 |
Correct |
538 ms |
81344 KB |
Output is correct |
10 |
Correct |
508 ms |
72600 KB |
Output is correct |
11 |
Correct |
427 ms |
67620 KB |
Output is correct |
12 |
Correct |
329 ms |
30088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
545 ms |
83816 KB |
Output is correct |
2 |
Correct |
545 ms |
83944 KB |
Output is correct |
3 |
Correct |
547 ms |
83788 KB |
Output is correct |
4 |
Correct |
502 ms |
70016 KB |
Output is correct |
5 |
Correct |
502 ms |
73980 KB |
Output is correct |
6 |
Correct |
401 ms |
36500 KB |
Output is correct |
7 |
Correct |
545 ms |
81440 KB |
Output is correct |
8 |
Correct |
536 ms |
83932 KB |
Output is correct |
9 |
Correct |
538 ms |
81344 KB |
Output is correct |
10 |
Correct |
508 ms |
72600 KB |
Output is correct |
11 |
Correct |
427 ms |
67620 KB |
Output is correct |
12 |
Correct |
329 ms |
30088 KB |
Output is correct |
13 |
Correct |
646 ms |
101092 KB |
Output is correct |
14 |
Correct |
589 ms |
98828 KB |
Output is correct |
15 |
Correct |
553 ms |
83736 KB |
Output is correct |
16 |
Correct |
543 ms |
70544 KB |
Output is correct |
17 |
Correct |
536 ms |
84924 KB |
Output is correct |
18 |
Correct |
469 ms |
36568 KB |
Output is correct |
19 |
Correct |
613 ms |
100868 KB |
Output is correct |
20 |
Correct |
610 ms |
102776 KB |
Output is correct |
21 |
Correct |
628 ms |
98788 KB |
Output is correct |
22 |
Correct |
467 ms |
72636 KB |
Output is correct |
23 |
Correct |
431 ms |
67488 KB |
Output is correct |
24 |
Correct |
320 ms |
29968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
13 ms |
2768 KB |
Output is correct |
8 |
Correct |
14 ms |
2896 KB |
Output is correct |
9 |
Correct |
14 ms |
2832 KB |
Output is correct |
10 |
Correct |
13 ms |
2516 KB |
Output is correct |
11 |
Correct |
13 ms |
2644 KB |
Output is correct |
12 |
Correct |
12 ms |
1476 KB |
Output is correct |
13 |
Correct |
15 ms |
2516 KB |
Output is correct |
14 |
Correct |
12 ms |
2516 KB |
Output is correct |
15 |
Correct |
12 ms |
2480 KB |
Output is correct |
16 |
Correct |
11 ms |
2132 KB |
Output is correct |
17 |
Correct |
12 ms |
2516 KB |
Output is correct |
18 |
Correct |
9 ms |
1236 KB |
Output is correct |
19 |
Correct |
545 ms |
83816 KB |
Output is correct |
20 |
Correct |
545 ms |
83944 KB |
Output is correct |
21 |
Correct |
547 ms |
83788 KB |
Output is correct |
22 |
Correct |
502 ms |
70016 KB |
Output is correct |
23 |
Correct |
502 ms |
73980 KB |
Output is correct |
24 |
Correct |
401 ms |
36500 KB |
Output is correct |
25 |
Correct |
545 ms |
81440 KB |
Output is correct |
26 |
Correct |
536 ms |
83932 KB |
Output is correct |
27 |
Correct |
538 ms |
81344 KB |
Output is correct |
28 |
Correct |
508 ms |
72600 KB |
Output is correct |
29 |
Correct |
427 ms |
67620 KB |
Output is correct |
30 |
Correct |
329 ms |
30088 KB |
Output is correct |
31 |
Correct |
646 ms |
101092 KB |
Output is correct |
32 |
Correct |
589 ms |
98828 KB |
Output is correct |
33 |
Correct |
553 ms |
83736 KB |
Output is correct |
34 |
Correct |
543 ms |
70544 KB |
Output is correct |
35 |
Correct |
536 ms |
84924 KB |
Output is correct |
36 |
Correct |
469 ms |
36568 KB |
Output is correct |
37 |
Correct |
613 ms |
100868 KB |
Output is correct |
38 |
Correct |
610 ms |
102776 KB |
Output is correct |
39 |
Correct |
628 ms |
98788 KB |
Output is correct |
40 |
Correct |
467 ms |
72636 KB |
Output is correct |
41 |
Correct |
431 ms |
67488 KB |
Output is correct |
42 |
Correct |
320 ms |
29968 KB |
Output is correct |
43 |
Correct |
703 ms |
103444 KB |
Output is correct |
44 |
Correct |
703 ms |
103772 KB |
Output is correct |
45 |
Correct |
682 ms |
101000 KB |
Output is correct |
46 |
Correct |
599 ms |
87716 KB |
Output is correct |
47 |
Correct |
659 ms |
88928 KB |
Output is correct |
48 |
Correct |
526 ms |
36396 KB |
Output is correct |
49 |
Correct |
730 ms |
97540 KB |
Output is correct |
50 |
Correct |
734 ms |
103680 KB |
Output is correct |
51 |
Correct |
730 ms |
97572 KB |
Output is correct |
52 |
Correct |
599 ms |
79536 KB |
Output is correct |
53 |
Correct |
460 ms |
85260 KB |
Output is correct |