#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
const int N=1e5+50;
struct Query{
int x,y,z,i;
bool operator <(Query a){return z<a.z;}
}Qs[N+50];
vector<int>T[N+50],vals[N+50];
pair<int,int>a[N+50];
int res[N+50];
void update(int qind,int qind1,int qval){
int lb=lower_bound(vals[qind].begin(),vals[qind].end(),qind1)-vals[qind].begin();
if(lb==0) return;
for(int i=lb;i<T[qind].size();i+=i&-i) T[qind][i]+=qval;
}
void Update(int qind,int qind1,int qval,bool precalc){
if(precalc) for(int i=qind;i<=N;i+=i&-i) vals[i].pb(qind1);
else for(int i=qind;i<=N;i+=i&-i) update(i,qind1,qval);
}
int get(int qind,int qval){
int sum=0;
for(int i=T[qind].size()-1;i>=1;i-=i&-i) sum+=T[qind][i];
int lb=lower_bound(vals[qind].begin(),vals[qind].end(),qval)-vals[qind].begin()-1;
for(int i=lb;i>=1;i-=i&-i) sum-=T[qind][i];
return sum;
}
int Get(int qind,int qval){
int sum=0;
for(int i=qind;i>=1;i-=i&-i) sum+=get(i,qval);
return sum;
}
vector<int>Vals;
int Ind(int x){
return upper_bound(Vals.begin(),Vals.end(),x)-Vals.begin()-1;
}
int main(){
int n,q;scanf("%i%i",&n,&q);
for(int i=1;i<=n;i++) scanf("%i%i",&a[i].fi,&a[i].se);
Vals.pb(-1);
for(int i=1;i<=n;i++) Vals.pb(a[i].fi);
sort(Vals.begin(),Vals.end());
int M=unique(Vals.begin(),Vals.end())-Vals.begin();
Vals.resize(M);
for(int i=1;i<=n;i++) Update(Ind(a[i].fi),a[i].se,1,true);
for(int i=0;i<=N;i++){
vals[i].pb(0);
sort(vals[i].begin(),vals[i].end());
int m=unique(vals[i].begin(),vals[i].end())-vals[i].begin();
vals[i].resize(m);
for(int j=0;j<m;j++) T[i].pb(0);
}
for(int i=1;i<=n;i++) Update(Ind(a[i].fi),a[i].se,1,false);
sort(a+1,a+n+1,[&](pair<int,int> i,pair<int,int> j){return i.fi+i.se<j.fi+j.se;});
for(int i=1;i<=q;i++){
int x,y,z;scanf("%i%i%i",&x,&y,&z);
Qs[i]={x,y,z,i};
}
sort(Qs+1,Qs+q+1);
//for(int i=1;i<=n;i++) printf("%i %i\n",a[i].fi,a[i].se);
//for(int i=1;i<=q;i++) printf("%i %i %i\n",Qs[i].x,Qs[i].y,Qs[i].z);
for(int i=1,j=1;j<=q;j++){
while(i<=n && a[i].fi+a[i].se<Qs[j].z){
Update(Ind(a[i].fi),a[i].se,-1,false);
i++;
}
res[Qs[j].i]=Get(N,Qs[j].y)-Get(Ind(Qs[j].x-1),Qs[j].y);
//printf("*%i %i %i %i %i %i\n",Ind(Qs[j].x-1),Qs[j].x,Qs[j].y,Qs[j].z,Get(N,Qs[j].y),Get(Ind(Qs[j].x-1),Qs[j].y));
}
for(int i=1;i<=q;i++) printf("%i ",res[i]);
return 0;
}
Compilation message
examination.cpp: In function 'void update(int, int, int)':
examination.cpp:19:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i=lb;i<T[qind].size();i+=i&-i) T[qind][i]+=qval;
| ~^~~~~~~~~~~~~~~
examination.cpp: In function 'int main()':
examination.cpp:42:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | int n,q;scanf("%i%i",&n,&q);
| ~~~~~^~~~~~~~~~~~~~
examination.cpp:43:32: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | for(int i=1;i<=n;i++) scanf("%i%i",&a[i].fi,&a[i].se);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:60:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | int x,y,z;scanf("%i%i%i",&x,&y,&z);
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
11356 KB |
Output is correct |
2 |
Correct |
9 ms |
11380 KB |
Output is correct |
3 |
Correct |
10 ms |
11356 KB |
Output is correct |
4 |
Correct |
11 ms |
11356 KB |
Output is correct |
5 |
Correct |
11 ms |
11432 KB |
Output is correct |
6 |
Correct |
10 ms |
11352 KB |
Output is correct |
7 |
Correct |
20 ms |
11864 KB |
Output is correct |
8 |
Correct |
19 ms |
11868 KB |
Output is correct |
9 |
Correct |
20 ms |
12036 KB |
Output is correct |
10 |
Incorrect |
14 ms |
11772 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
363 ms |
24704 KB |
Output is correct |
2 |
Correct |
345 ms |
24540 KB |
Output is correct |
3 |
Correct |
354 ms |
24524 KB |
Output is correct |
4 |
Incorrect |
171 ms |
20936 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
363 ms |
24704 KB |
Output is correct |
2 |
Correct |
345 ms |
24540 KB |
Output is correct |
3 |
Correct |
354 ms |
24524 KB |
Output is correct |
4 |
Incorrect |
171 ms |
20936 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
11356 KB |
Output is correct |
2 |
Correct |
9 ms |
11380 KB |
Output is correct |
3 |
Correct |
10 ms |
11356 KB |
Output is correct |
4 |
Correct |
11 ms |
11356 KB |
Output is correct |
5 |
Correct |
11 ms |
11432 KB |
Output is correct |
6 |
Correct |
10 ms |
11352 KB |
Output is correct |
7 |
Correct |
20 ms |
11864 KB |
Output is correct |
8 |
Correct |
19 ms |
11868 KB |
Output is correct |
9 |
Correct |
20 ms |
12036 KB |
Output is correct |
10 |
Incorrect |
14 ms |
11772 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |