#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int M=4e6;
int b[N],sum[N],cnt=0,froot[N],sroot[N];
pair<int,int> arr[N];
struct{
int l,r;
long long val;
}s[M];
int clone(int x){s[++cnt]=s[x]; return cnt;}
struct persist{
int build(int l,int r,int idx){
if(l==r){
s[idx]={0,0,0};
return idx;
}
int m=(l+r)/2;
s[idx].l=build(l,m,++cnt);
s[idx].r=build(m+1,r,++cnt);
s[idx].val=s[s[idx].l].val+s[s[idx].r].val;
return idx;
}
int update(int l,int r,int idx,int a,int b){
int now=clone(idx);
if(l==r){
s[now].val+=b;
return now;
}
int m=(l+r)/2;
if(a<=m) s[now].l=update(l,m,s[idx].l,a,b);
else s[now].r=update(m+1,r,s[idx].r,a,b);
s[now].val=s[s[now].l].val+s[s[now].r].val;
return now;
}
int query(int l,int r,int idx,int a,int b){
if(a>r || b<l) return 0;
if(a<=l && b>=r) return s[idx].val;
int m=(l+r)/2;
return query(l,m,s[idx].l,a,b)+query(m+1,r,s[idx].r,a,b);
}
}ftree,stree;
int main(){
int n,q;
cin>>n >>q;
for(int i=1;i<=n;i++){
cin>>arr[i].first >>arr[i].second;
b[i]=arr[i].second;
sum[i]=arr[i].first+arr[i].second;
}
sort(arr+1,arr+n+1);
sort(b+1,b+n+1);
sort(sum+1,sum+n+1);
froot[0]=ftree.build(1,n,++cnt);
sroot[0]=stree.build(1,n,++cnt);
for(int i=1;i<=n;i++){
int tb=lower_bound(b+1,b+n+1,arr[i].second)-b;
int ts=lower_bound(sum+1,sum+n+1,arr[i].first+arr[i].second)-sum;
froot[i]=ftree.update(1,n,froot[i-1],tb,1);
sroot[i]=stree.update(1,n,sroot[i-1],ts,1);
}
while(q--){
int x,y,z,ans=0;
cin>>x >>y >>z;
int ta=lower_bound(arr+1,arr+n+1,make_pair(x,0))-arr;
int md=lower_bound(arr+ta,arr+n+1,make_pair(z-y,0))-arr;
int fb=lower_bound(b+1,b+n+1,y)-b;
int fs=lower_bound(sum+1,sum+n+1,z)-sum;
ans+=ftree.query(1,n,froot[n],fb,n)-ftree.query(1,n,froot[md-1],fb,n);
ans+=stree.query(1,n,sroot[md-1],fs,n)-stree.query(1,n,sroot[ta-1],fs,n);
cout<<ans <<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
13 ms |
4432 KB |
Output is correct |
8 |
Correct |
12 ms |
4432 KB |
Output is correct |
9 |
Correct |
12 ms |
4432 KB |
Output is correct |
10 |
Correct |
11 ms |
4432 KB |
Output is correct |
11 |
Correct |
11 ms |
4432 KB |
Output is correct |
12 |
Correct |
8 ms |
4432 KB |
Output is correct |
13 |
Correct |
17 ms |
4600 KB |
Output is correct |
14 |
Correct |
11 ms |
4432 KB |
Output is correct |
15 |
Correct |
18 ms |
4432 KB |
Output is correct |
16 |
Correct |
10 ms |
4604 KB |
Output is correct |
17 |
Correct |
10 ms |
4432 KB |
Output is correct |
18 |
Correct |
8 ms |
4584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
522 ms |
64840 KB |
Output is correct |
2 |
Correct |
515 ms |
67292 KB |
Output is correct |
3 |
Correct |
509 ms |
67400 KB |
Output is correct |
4 |
Correct |
338 ms |
66888 KB |
Output is correct |
5 |
Correct |
411 ms |
66508 KB |
Output is correct |
6 |
Correct |
285 ms |
66120 KB |
Output is correct |
7 |
Correct |
468 ms |
67276 KB |
Output is correct |
8 |
Correct |
511 ms |
67388 KB |
Output is correct |
9 |
Correct |
449 ms |
67128 KB |
Output is correct |
10 |
Correct |
359 ms |
66372 KB |
Output is correct |
11 |
Correct |
348 ms |
67048 KB |
Output is correct |
12 |
Correct |
255 ms |
66376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
522 ms |
64840 KB |
Output is correct |
2 |
Correct |
515 ms |
67292 KB |
Output is correct |
3 |
Correct |
509 ms |
67400 KB |
Output is correct |
4 |
Correct |
338 ms |
66888 KB |
Output is correct |
5 |
Correct |
411 ms |
66508 KB |
Output is correct |
6 |
Correct |
285 ms |
66120 KB |
Output is correct |
7 |
Correct |
468 ms |
67276 KB |
Output is correct |
8 |
Correct |
511 ms |
67388 KB |
Output is correct |
9 |
Correct |
449 ms |
67128 KB |
Output is correct |
10 |
Correct |
359 ms |
66372 KB |
Output is correct |
11 |
Correct |
348 ms |
67048 KB |
Output is correct |
12 |
Correct |
255 ms |
66376 KB |
Output is correct |
13 |
Correct |
613 ms |
67656 KB |
Output is correct |
14 |
Correct |
580 ms |
67788 KB |
Output is correct |
15 |
Correct |
480 ms |
67400 KB |
Output is correct |
16 |
Correct |
481 ms |
67008 KB |
Output is correct |
17 |
Correct |
534 ms |
67080 KB |
Output is correct |
18 |
Correct |
302 ms |
66120 KB |
Output is correct |
19 |
Correct |
667 ms |
67844 KB |
Output is correct |
20 |
Correct |
647 ms |
67656 KB |
Output is correct |
21 |
Correct |
637 ms |
67856 KB |
Output is correct |
22 |
Correct |
328 ms |
66380 KB |
Output is correct |
23 |
Correct |
303 ms |
66888 KB |
Output is correct |
24 |
Correct |
249 ms |
66376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4432 KB |
Output is correct |
2 |
Correct |
1 ms |
4432 KB |
Output is correct |
3 |
Correct |
1 ms |
4432 KB |
Output is correct |
4 |
Correct |
1 ms |
4432 KB |
Output is correct |
5 |
Correct |
1 ms |
4432 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
13 ms |
4432 KB |
Output is correct |
8 |
Correct |
12 ms |
4432 KB |
Output is correct |
9 |
Correct |
12 ms |
4432 KB |
Output is correct |
10 |
Correct |
11 ms |
4432 KB |
Output is correct |
11 |
Correct |
11 ms |
4432 KB |
Output is correct |
12 |
Correct |
8 ms |
4432 KB |
Output is correct |
13 |
Correct |
17 ms |
4600 KB |
Output is correct |
14 |
Correct |
11 ms |
4432 KB |
Output is correct |
15 |
Correct |
18 ms |
4432 KB |
Output is correct |
16 |
Correct |
10 ms |
4604 KB |
Output is correct |
17 |
Correct |
10 ms |
4432 KB |
Output is correct |
18 |
Correct |
8 ms |
4584 KB |
Output is correct |
19 |
Correct |
522 ms |
64840 KB |
Output is correct |
20 |
Correct |
515 ms |
67292 KB |
Output is correct |
21 |
Correct |
509 ms |
67400 KB |
Output is correct |
22 |
Correct |
338 ms |
66888 KB |
Output is correct |
23 |
Correct |
411 ms |
66508 KB |
Output is correct |
24 |
Correct |
285 ms |
66120 KB |
Output is correct |
25 |
Correct |
468 ms |
67276 KB |
Output is correct |
26 |
Correct |
511 ms |
67388 KB |
Output is correct |
27 |
Correct |
449 ms |
67128 KB |
Output is correct |
28 |
Correct |
359 ms |
66372 KB |
Output is correct |
29 |
Correct |
348 ms |
67048 KB |
Output is correct |
30 |
Correct |
255 ms |
66376 KB |
Output is correct |
31 |
Correct |
613 ms |
67656 KB |
Output is correct |
32 |
Correct |
580 ms |
67788 KB |
Output is correct |
33 |
Correct |
480 ms |
67400 KB |
Output is correct |
34 |
Correct |
481 ms |
67008 KB |
Output is correct |
35 |
Correct |
534 ms |
67080 KB |
Output is correct |
36 |
Correct |
302 ms |
66120 KB |
Output is correct |
37 |
Correct |
667 ms |
67844 KB |
Output is correct |
38 |
Correct |
647 ms |
67656 KB |
Output is correct |
39 |
Correct |
637 ms |
67856 KB |
Output is correct |
40 |
Correct |
328 ms |
66380 KB |
Output is correct |
41 |
Correct |
303 ms |
66888 KB |
Output is correct |
42 |
Correct |
249 ms |
66376 KB |
Output is correct |
43 |
Correct |
730 ms |
69688 KB |
Output is correct |
44 |
Correct |
742 ms |
69828 KB |
Output is correct |
45 |
Correct |
699 ms |
69716 KB |
Output is correct |
46 |
Correct |
466 ms |
68240 KB |
Output is correct |
47 |
Correct |
528 ms |
68168 KB |
Output is correct |
48 |
Correct |
286 ms |
65608 KB |
Output is correct |
49 |
Correct |
643 ms |
69704 KB |
Output is correct |
50 |
Correct |
632 ms |
69620 KB |
Output is correct |
51 |
Correct |
669 ms |
69452 KB |
Output is correct |
52 |
Correct |
480 ms |
67928 KB |
Output is correct |
53 |
Correct |
332 ms |
67656 KB |
Output is correct |