This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define jizz ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ALL(x) (x).begin(),(x).end()
typedef pair<int,int> pii;
#define F first
#define S second
const int MAXN=1E5+10, MAXC=2E5+21;
struct student{
int type, a, b, c, id;
student(int t, int a, int b, int c, int id):type(t),a(a),b(b),c(c),id(id){};
};
bool cmp(student x, student y){
if(x.c==y.c) return x.type<y.type;
return x.c>y.c;
}
int n, q, ans[MAXN];
vector<int> comp;
vector<student> v;
struct BIT{
int data[MAXC]={0};
vector<pii> his;
int lb(int x){
return x&(-x);
}
void modify(int p, int v, bool u=1){
if(u) his.push_back({p,v});
//if(u) cout<<"modified "<<p<<" "<<v<<"\n";
while(p<MAXC){
data[p]+=v;
p+=lb(p);
}
}
int query(int p){
int res=0;
while(p>0){
res+=data[p];
p-=lb(p);
}
return res;
}
int query(int l, int r){
//cout<<"queried "<<l<<" "<<r<<"\n";
int x=query(r)-query(l-1);
//cout<<"result: "<<x<<"\n";
return x;
}
void undo(){
for(auto i:his){
modify(i.F,-i.S,0);
}
his.clear();
}
} bit;
void CDQORZ(int l, int r){
if(l==r) return;
int mid=(l+r)/2;
CDQORZ(l,mid);
CDQORZ(mid+1,r);
vector<student> nv;
int i=l, j=mid+1;
//cout<<"now on "<<l<<" "<<r<<"\n";
while(i<=mid||j<=r){
//cout<<i<<" "<<j<<"\n";
if(j==r+1){
nv.push_back(v[i]);
if(v[i].type==0) bit.modify(v[i].b,1);
i++;
} else if(i==mid+1){
nv.push_back(v[j]);
if(v[j].type==1) ans[v[j].id]+=bit.query(v[j].b,MAXC-1);
j++;
} else if(v[i].a>=v[j].a){
nv.push_back(v[i]);
if(v[i].type==0) bit.modify(v[i].b,1);
i++;
} else{
nv.push_back(v[j]);
if(v[j].type==1) ans[v[j].id]+=bit.query(v[j].b,MAXC-1);
j++;
}
}
bit.undo();
for(int k=0;k<nv.size();k++){
v[l+k]=nv[k];
}
}
int main(){ jizz
cin>>n>>q;
for(int i=0;i<n;i++){
int s, t;
cin>>s>>t;
v.push_back(student(0,s,t,s+t,i));
comp.push_back(t);
}
for(int i=0;i<q;i++){
int x, y, z;
cin>>x>>y>>z;
v.push_back(student(1,x,y,z,i));
comp.push_back(y);
}
sort(ALL(comp));
comp.erase(unique(ALL(comp)),comp.end());
for(int i=0;i<n+q;i++){
v[i].b=lower_bound(ALL(comp),v[i].b)-comp.begin()+1;
}
sort(ALL(v),cmp);
/*for(int i=0;i<n+q;i++){
cout<<v[i].type<<" "<<v[i].a<<" "<<v[i].b<<" "<<v[i].c<<" "<<v[i].id<<"\n";
}*/
CDQORZ(0,n+q-1);
for(int i=0;i<q;i++){
cout<<ans[i]<<"\n";
}
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
*/
/*
10 10
41304 98327
91921 28251
85635 59191
30361 72671
28949 96958
99041 37826
10245 2726
19387 20282
60366 87723
95388 49726
52302 69501 66009
43754 45346 3158
25224 58881 18727
7298 24412 63782
24107 10583 61508
65025 29140 7278
36104 56758 2775
23126 67608 122051
56910 17272 62933
39675 15874 117117
*/
Compilation message (stderr)
examination.cpp: In function 'void CDQORZ(int, int)':
examination.cpp:84:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<student>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int k=0;k<nv.size();k++){
| ~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |