#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
struct node {
ll s,e,m,v;
node *l, *r;
node(ll _s,ll _e){
s = _s;
e = _e;
m = (s + e) / 2;
v = 0;
if(s != e){
l = new node(s,m);
r = new node(m + 1,e);
}
}
void update(ll x,ll nval){
if(s == e){
v += nval;
return;
}else{
if(x > m) r->update(x,nval);
else l->update(x,nval);
v = l->v + r->v;
}
}
ll query(ll x,ll y){
if(s == x && e == y){
return v;
}else{
if(x > m) return r->query(x,y);
else if(y <= m) return l->query(x,y);
else return l->query(x,m) + r->query(m+1,y);
}
}
} *Sroot, *Troot;
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll N,Q;
cin>>N>>Q;
Sroot = new node(0,MAXN - 1);
Troot = new node(0,MAXN - 1);
pair<pair<ll,ll>,pair<ll,ll> > queries[Q];
pair<pair<ll,ll>,pair<ll,ll> > students[N];
vector<ll> d;
for(ll i = 0;i < N;i++){
cin>>students[i].second.first>>students[i].second.second;
students[i].first.first = students[i].second.first + students[i].second.second;
students[i].first.second = i;
d.push_back(students[i].first.first);
d.push_back(students[i].second.first);
d.push_back(students[i].second.second);
}
for(ll i = 0;i < Q;i++){
cin>>queries[i].second.first>>queries[i].second.second>>queries[i].first.first;
queries[i].first.first = max(queries[i].first.first,queries[i].second.first + queries[i].second.second);
queries[i].first.second = i;
d.push_back(queries[i].first.first);
d.push_back(queries[i].second.first);
d.push_back(queries[i].second.second);
}
sort(d.begin(),d.end());
d.resize(unique(d.begin(),d.end()) - d.begin());
for(ll i = 0;i < N;i++){
students[i].first.first = lower_bound(d.begin(),d.end(),students[i].first.first) - d.begin();
students[i].second.first = lower_bound(d.begin(),d.end(),students[i].second.first) - d.begin();
students[i].second.second = lower_bound(d.begin(),d.end(),students[i].second.second) - d.begin();
}
for(ll i = 0;i < Q;i++){
queries[i].first.first = lower_bound(d.begin(),d.end(),queries[i].first.first) - d.begin();
queries[i].second.first = lower_bound(d.begin(),d.end(),queries[i].second.first) - d.begin();
queries[i].second.second = lower_bound(d.begin(),d.end(),queries[i].second.second) - d.begin();
}
sort(students,students + N,greater<pair<pair<ll,ll>,pair<ll,ll> > >());
sort(queries,queries + Q,greater<pair<pair<ll,ll>,pair<ll,ll> > >());
ll ptr = -1;
ll Qu[Q];
for(ll q = 0;q < Q;q++){
ll Z = queries[q].first.first;
ll X = queries[q].second.first; //S <= X
ll Y = queries[q].second.second; //T <= Y
while(ptr + 1 < N && students[ptr + 1].first.first >= Z){
ptr++;
Sroot -> update(students[ptr].second.first,1);
Troot -> update(students[ptr].second.second,1);
}
ll ans = ptr + 1;
if(X >= 1) ans -= Sroot -> query(0,X - 1);
if(Y >= 1) ans -= Troot -> query(0,Y - 1);
Qu[queries[q].first.second] = ans;
}
for(ll q = 0;q < Q;q++){
cout<<Qu[q]<<'\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
250816 KB |
Output is correct |
2 |
Correct |
150 ms |
250740 KB |
Output is correct |
3 |
Correct |
149 ms |
250708 KB |
Output is correct |
4 |
Correct |
154 ms |
250940 KB |
Output is correct |
5 |
Correct |
147 ms |
250684 KB |
Output is correct |
6 |
Correct |
148 ms |
250856 KB |
Output is correct |
7 |
Correct |
155 ms |
251472 KB |
Output is correct |
8 |
Correct |
158 ms |
251724 KB |
Output is correct |
9 |
Correct |
155 ms |
251576 KB |
Output is correct |
10 |
Correct |
159 ms |
251580 KB |
Output is correct |
11 |
Correct |
152 ms |
251420 KB |
Output is correct |
12 |
Correct |
151 ms |
251408 KB |
Output is correct |
13 |
Correct |
159 ms |
251508 KB |
Output is correct |
14 |
Correct |
158 ms |
251476 KB |
Output is correct |
15 |
Correct |
158 ms |
251564 KB |
Output is correct |
16 |
Correct |
152 ms |
251488 KB |
Output is correct |
17 |
Correct |
160 ms |
251564 KB |
Output is correct |
18 |
Correct |
151 ms |
251392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
441 ms |
268200 KB |
Output is correct |
2 |
Correct |
433 ms |
268740 KB |
Output is correct |
3 |
Correct |
431 ms |
269512 KB |
Output is correct |
4 |
Correct |
311 ms |
267352 KB |
Output is correct |
5 |
Correct |
314 ms |
268600 KB |
Output is correct |
6 |
Correct |
252 ms |
267208 KB |
Output is correct |
7 |
Correct |
411 ms |
268016 KB |
Output is correct |
8 |
Correct |
434 ms |
269716 KB |
Output is correct |
9 |
Correct |
392 ms |
268484 KB |
Output is correct |
10 |
Correct |
304 ms |
268488 KB |
Output is correct |
11 |
Correct |
303 ms |
268740 KB |
Output is correct |
12 |
Correct |
222 ms |
267836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
441 ms |
268200 KB |
Output is correct |
2 |
Correct |
433 ms |
268740 KB |
Output is correct |
3 |
Correct |
431 ms |
269512 KB |
Output is correct |
4 |
Correct |
311 ms |
267352 KB |
Output is correct |
5 |
Correct |
314 ms |
268600 KB |
Output is correct |
6 |
Correct |
252 ms |
267208 KB |
Output is correct |
7 |
Correct |
411 ms |
268016 KB |
Output is correct |
8 |
Correct |
434 ms |
269716 KB |
Output is correct |
9 |
Correct |
392 ms |
268484 KB |
Output is correct |
10 |
Correct |
304 ms |
268488 KB |
Output is correct |
11 |
Correct |
303 ms |
268740 KB |
Output is correct |
12 |
Correct |
222 ms |
267836 KB |
Output is correct |
13 |
Correct |
444 ms |
269764 KB |
Output is correct |
14 |
Correct |
458 ms |
269356 KB |
Output is correct |
15 |
Correct |
441 ms |
269504 KB |
Output is correct |
16 |
Correct |
315 ms |
267972 KB |
Output is correct |
17 |
Correct |
324 ms |
268484 KB |
Output is correct |
18 |
Correct |
246 ms |
266696 KB |
Output is correct |
19 |
Correct |
459 ms |
268224 KB |
Output is correct |
20 |
Correct |
480 ms |
268224 KB |
Output is correct |
21 |
Correct |
443 ms |
270352 KB |
Output is correct |
22 |
Correct |
319 ms |
268480 KB |
Output is correct |
23 |
Correct |
308 ms |
268236 KB |
Output is correct |
24 |
Correct |
257 ms |
268084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
250816 KB |
Output is correct |
2 |
Correct |
150 ms |
250740 KB |
Output is correct |
3 |
Correct |
149 ms |
250708 KB |
Output is correct |
4 |
Correct |
154 ms |
250940 KB |
Output is correct |
5 |
Correct |
147 ms |
250684 KB |
Output is correct |
6 |
Correct |
148 ms |
250856 KB |
Output is correct |
7 |
Correct |
155 ms |
251472 KB |
Output is correct |
8 |
Correct |
158 ms |
251724 KB |
Output is correct |
9 |
Correct |
155 ms |
251576 KB |
Output is correct |
10 |
Correct |
159 ms |
251580 KB |
Output is correct |
11 |
Correct |
152 ms |
251420 KB |
Output is correct |
12 |
Correct |
151 ms |
251408 KB |
Output is correct |
13 |
Correct |
159 ms |
251508 KB |
Output is correct |
14 |
Correct |
158 ms |
251476 KB |
Output is correct |
15 |
Correct |
158 ms |
251564 KB |
Output is correct |
16 |
Correct |
152 ms |
251488 KB |
Output is correct |
17 |
Correct |
160 ms |
251564 KB |
Output is correct |
18 |
Correct |
151 ms |
251392 KB |
Output is correct |
19 |
Correct |
441 ms |
268200 KB |
Output is correct |
20 |
Correct |
433 ms |
268740 KB |
Output is correct |
21 |
Correct |
431 ms |
269512 KB |
Output is correct |
22 |
Correct |
311 ms |
267352 KB |
Output is correct |
23 |
Correct |
314 ms |
268600 KB |
Output is correct |
24 |
Correct |
252 ms |
267208 KB |
Output is correct |
25 |
Correct |
411 ms |
268016 KB |
Output is correct |
26 |
Correct |
434 ms |
269716 KB |
Output is correct |
27 |
Correct |
392 ms |
268484 KB |
Output is correct |
28 |
Correct |
304 ms |
268488 KB |
Output is correct |
29 |
Correct |
303 ms |
268740 KB |
Output is correct |
30 |
Correct |
222 ms |
267836 KB |
Output is correct |
31 |
Correct |
444 ms |
269764 KB |
Output is correct |
32 |
Correct |
458 ms |
269356 KB |
Output is correct |
33 |
Correct |
441 ms |
269504 KB |
Output is correct |
34 |
Correct |
315 ms |
267972 KB |
Output is correct |
35 |
Correct |
324 ms |
268484 KB |
Output is correct |
36 |
Correct |
246 ms |
266696 KB |
Output is correct |
37 |
Correct |
459 ms |
268224 KB |
Output is correct |
38 |
Correct |
480 ms |
268224 KB |
Output is correct |
39 |
Correct |
443 ms |
270352 KB |
Output is correct |
40 |
Correct |
319 ms |
268480 KB |
Output is correct |
41 |
Correct |
308 ms |
268236 KB |
Output is correct |
42 |
Correct |
257 ms |
268084 KB |
Output is correct |
43 |
Correct |
622 ms |
271724 KB |
Output is correct |
44 |
Correct |
608 ms |
270532 KB |
Output is correct |
45 |
Correct |
586 ms |
271432 KB |
Output is correct |
46 |
Correct |
359 ms |
268900 KB |
Output is correct |
47 |
Correct |
361 ms |
269596 KB |
Output is correct |
48 |
Correct |
248 ms |
268232 KB |
Output is correct |
49 |
Correct |
591 ms |
271292 KB |
Output is correct |
50 |
Correct |
600 ms |
270984 KB |
Output is correct |
51 |
Correct |
587 ms |
271552 KB |
Output is correct |
52 |
Correct |
348 ms |
268876 KB |
Output is correct |
53 |
Correct |
313 ms |
268228 KB |
Output is correct |