#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
#include<random>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
struct Obj{
ll a,b,c;
int res;
bool is_q;
};
bool cmp1(Obj*a,Obj*b){
return a->c<b->c;
}
bool cmp2(Obj*a,Obj*b){
return a->b<b->b;
}
const int TREE_SIZE=1<<30;
struct SegTree{
SegTree*ch1,*ch2;
int val;
SegTree():ch1(nullptr),ch2(nullptr),val(0){}
};
void add(SegTree*node,int rl,int rr,int i){
node->val++;
if(rl==rr-1)
return;
int mid=(rl+rr)/2;
if(i<mid){
if(node->ch1==nullptr)
node->ch1=new SegTree;
add(node->ch1,rl,mid,i);
}else{
if(node->ch2==nullptr)
node->ch2=new SegTree;
add(node->ch2,mid,rr,i);
}
}
int get(SegTree*node,int rl,int rr,int l,int r){
if(r<=rl||rr<=l)
return 0;
if(l<=rl&&rr<=r)
return node->val;
int res=0;
int mid=(rl+rr)/2;
if(node->ch1)
res+=get(node->ch1,rl,mid,l,r);
if(node->ch2)
res+=get(node->ch2,mid,rr,l,r);
return res;
}
void proc(vector<Obj*>vals,vector<Obj*>qrs){
sort(vals.begin(),vals.end(),cmp2);
reverse(vals.begin(),vals.end());
sort(qrs.begin(),qrs.end(),cmp2);
reverse(qrs.begin(),qrs.end());
int i=0;
SegTree tree;
for(auto qr:qrs){
while(i<(int)vals.size()&&vals[i]->b>=qr->b){
add(&tree,0,TREE_SIZE,(int)vals[i]->a);
i++;
}
qr->res+=get(&tree,0,TREE_SIZE,(int)qr->a,TREE_SIZE);
}
}
void dc(vector<Obj*>arr){
int n=(int)arr.size();
if(arr[0]->c==arr.back()->c){
vector<Obj*>qrs,vals;
for(int i=0;i<n;i++){
if(arr[i]->is_q)
qrs.push_back(arr[i]);
else
vals.push_back(arr[i]);
}
proc(vals,qrs);
return;
}
int mid=0;
for(int i=0;i<n/2||mid==0;i++)
if(arr[i]->c!=arr[i+1]->c)
mid=i+1;
vector<Obj*>qrs,vals;
vector<Obj*>arr1,arr2;
for(int i=mid;i<n;i++){
arr2.push_back(arr[i]);
if(!arr[i]->is_q)
vals.push_back(arr[i]);
}
for(int i=0;i<mid;i++){
arr1.push_back(arr[i]);
if(arr[i]->is_q)
qrs.push_back(arr[i]);
}
proc(vals,qrs);
dc(arr1);
dc(arr2);
}
void solve(){
int n,q;
cin>>n>>q;
vector<Obj>arr(n);
for(Obj&i:arr){
cin>>i.a>>i.b;
i.c=i.a+i.b;
i.is_q=false;
i.res=0;
}
vector<Obj>qrs(q);
for(Obj&i:qrs){
cin>>i.a>>i.b>>i.c;
i.is_q=true;
i.res=0;
}
vector<Obj*>all;
for(Obj&i:arr)
all.push_back(&i);
for(Obj&i:qrs)
all.push_back(&i);
sort(all.begin(),all.end(),cmp1);
dc(all);
for(auto i:qrs)
cout<<i.res<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("/Users/jakob/cp_testing/test.txt","r",stdin);//freopen("output.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
5 4
35 100
70 70
45 15
80 40
20 95
20 50 120
10 10 100
60 60 80
0 100 100
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
20 ms |
12620 KB |
Output is correct |
8 |
Correct |
20 ms |
12696 KB |
Output is correct |
9 |
Correct |
21 ms |
12636 KB |
Output is correct |
10 |
Correct |
19 ms |
11352 KB |
Output is correct |
11 |
Correct |
10 ms |
2908 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
7 ms |
2908 KB |
Output is correct |
14 |
Correct |
8 ms |
2808 KB |
Output is correct |
15 |
Correct |
7 ms |
2652 KB |
Output is correct |
16 |
Correct |
4 ms |
860 KB |
Output is correct |
17 |
Correct |
18 ms |
11396 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
200 ms |
22156 KB |
Output is correct |
2 |
Correct |
203 ms |
24648 KB |
Output is correct |
3 |
Correct |
216 ms |
24600 KB |
Output is correct |
4 |
Correct |
176 ms |
23856 KB |
Output is correct |
5 |
Correct |
158 ms |
19300 KB |
Output is correct |
6 |
Correct |
119 ms |
19140 KB |
Output is correct |
7 |
Correct |
225 ms |
24620 KB |
Output is correct |
8 |
Correct |
212 ms |
24876 KB |
Output is correct |
9 |
Correct |
195 ms |
24364 KB |
Output is correct |
10 |
Correct |
132 ms |
18948 KB |
Output is correct |
11 |
Correct |
128 ms |
23888 KB |
Output is correct |
12 |
Correct |
49 ms |
14196 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
200 ms |
22156 KB |
Output is correct |
2 |
Correct |
203 ms |
24648 KB |
Output is correct |
3 |
Correct |
216 ms |
24600 KB |
Output is correct |
4 |
Correct |
176 ms |
23856 KB |
Output is correct |
5 |
Correct |
158 ms |
19300 KB |
Output is correct |
6 |
Correct |
119 ms |
19140 KB |
Output is correct |
7 |
Correct |
225 ms |
24620 KB |
Output is correct |
8 |
Correct |
212 ms |
24876 KB |
Output is correct |
9 |
Correct |
195 ms |
24364 KB |
Output is correct |
10 |
Correct |
132 ms |
18948 KB |
Output is correct |
11 |
Correct |
128 ms |
23888 KB |
Output is correct |
12 |
Correct |
49 ms |
14196 KB |
Output is correct |
13 |
Correct |
719 ms |
264072 KB |
Output is correct |
14 |
Correct |
548 ms |
153804 KB |
Output is correct |
15 |
Correct |
204 ms |
24960 KB |
Output is correct |
16 |
Correct |
489 ms |
134956 KB |
Output is correct |
17 |
Correct |
413 ms |
91788 KB |
Output is correct |
18 |
Correct |
127 ms |
19932 KB |
Output is correct |
19 |
Correct |
661 ms |
264436 KB |
Output is correct |
20 |
Correct |
638 ms |
245836 KB |
Output is correct |
21 |
Correct |
689 ms |
290764 KB |
Output is correct |
22 |
Correct |
140 ms |
18736 KB |
Output is correct |
23 |
Correct |
126 ms |
23852 KB |
Output is correct |
24 |
Correct |
48 ms |
14024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
20 ms |
12620 KB |
Output is correct |
8 |
Correct |
20 ms |
12696 KB |
Output is correct |
9 |
Correct |
21 ms |
12636 KB |
Output is correct |
10 |
Correct |
19 ms |
11352 KB |
Output is correct |
11 |
Correct |
10 ms |
2908 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
7 ms |
2908 KB |
Output is correct |
14 |
Correct |
8 ms |
2808 KB |
Output is correct |
15 |
Correct |
7 ms |
2652 KB |
Output is correct |
16 |
Correct |
4 ms |
860 KB |
Output is correct |
17 |
Correct |
18 ms |
11396 KB |
Output is correct |
18 |
Correct |
2 ms |
860 KB |
Output is correct |
19 |
Correct |
200 ms |
22156 KB |
Output is correct |
20 |
Correct |
203 ms |
24648 KB |
Output is correct |
21 |
Correct |
216 ms |
24600 KB |
Output is correct |
22 |
Correct |
176 ms |
23856 KB |
Output is correct |
23 |
Correct |
158 ms |
19300 KB |
Output is correct |
24 |
Correct |
119 ms |
19140 KB |
Output is correct |
25 |
Correct |
225 ms |
24620 KB |
Output is correct |
26 |
Correct |
212 ms |
24876 KB |
Output is correct |
27 |
Correct |
195 ms |
24364 KB |
Output is correct |
28 |
Correct |
132 ms |
18948 KB |
Output is correct |
29 |
Correct |
128 ms |
23888 KB |
Output is correct |
30 |
Correct |
49 ms |
14196 KB |
Output is correct |
31 |
Correct |
719 ms |
264072 KB |
Output is correct |
32 |
Correct |
548 ms |
153804 KB |
Output is correct |
33 |
Correct |
204 ms |
24960 KB |
Output is correct |
34 |
Correct |
489 ms |
134956 KB |
Output is correct |
35 |
Correct |
413 ms |
91788 KB |
Output is correct |
36 |
Correct |
127 ms |
19932 KB |
Output is correct |
37 |
Correct |
661 ms |
264436 KB |
Output is correct |
38 |
Correct |
638 ms |
245836 KB |
Output is correct |
39 |
Correct |
689 ms |
290764 KB |
Output is correct |
40 |
Correct |
140 ms |
18736 KB |
Output is correct |
41 |
Correct |
126 ms |
23852 KB |
Output is correct |
42 |
Correct |
48 ms |
14024 KB |
Output is correct |
43 |
Correct |
1008 ms |
547824 KB |
Output is correct |
44 |
Correct |
946 ms |
547392 KB |
Output is correct |
45 |
Correct |
712 ms |
330492 KB |
Output is correct |
46 |
Correct |
750 ms |
412356 KB |
Output is correct |
47 |
Correct |
410 ms |
88016 KB |
Output is correct |
48 |
Correct |
126 ms |
18380 KB |
Output is correct |
49 |
Correct |
695 ms |
329516 KB |
Output is correct |
50 |
Correct |
1014 ms |
606512 KB |
Output is correct |
51 |
Correct |
760 ms |
372940 KB |
Output is correct |
52 |
Correct |
386 ms |
79308 KB |
Output is correct |
53 |
Correct |
188 ms |
64908 KB |
Output is correct |