#include<bits/stdc++.h>
#pragma GCC optimize ("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define F first
#define S second
typedef long long ll;
const int N=1e5+5;
int n, q, s[N], t[N], a[N], aa[N], b[N], bb[N], c[N], cc[N], ans[N];
vector<pair<pii, pii>>v;
vector<pair<pii, int>>w;
bool cmp(const int &x, const int &y){
return s[x]+t[x]<s[y]+t[y];
}
bool cpp(const int &x, const int &y){
return s[x]<s[y];
}
bool ppp(const int &x, const int &y){
return t[x]<t[y];
}
struct fenwick{
vector<int>bit=vector<int>(n, 0);
void add(int i){
for(int j=i;j<n;j|=(j+1))
++bit[j];
}
int sum(int i){
int as=0;
for(int j=i;j>=0;j=(j&(j+1))-1)
as+=bit[j];
return as;
}
};
int bin1(int x){
if(s[a[n-1]]<x)
return n;
int l=0, r=n-1;
while(r>l){
int m=(r+l)/2;
if(s[a[m]]>=x)
r=m;
else
l=m+1;
}
return l;
}
int bin2(int x){
if(t[b[n-1]]<x)
return n;
int l=0, r=n-1;
while(r>l){
int m=(r+l)/2;
if(t[b[m]]>=x)
r=m;
else
l=m+1;
}
return l;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>q;
for(int i=0;i<n;++i)
cin>>s[i]>>t[i];
iota(a, a+n, 0);
iota(b, b+n, 0);
iota(c, c+n, 0);
sort(a, a+n, cpp);
for(int i=0;i<n;++i)
aa[a[i]]=i;
sort(b, b+n, ppp);
for(int i=0;i<n;++i)
bb[b[i]]=i;
sort(c, c+n, cmp);
for(int i=0;i<n;++i)
cc[c[i]]=i;
int x, y, z;
for(int i=0;i<q;++i){
cin>>x>>y>>z;
if(z<=x+y)
w.push_back({{x, y}, i});
else
v.push_back({{z, i}, {x, y}});
}
sort(all(v));
sort(all(w));
reverse(all(v));
reverse(all(w));
fenwick A;
int j=n-1;
for(auto u:w){
while(j>=0 && s[a[j]]>=u.F.F)
A.add(bb[a[j--]]);
int x=bin2(u.F.S);
if(x>=0)
x=A.sum(x-1);
ans[u.S]=n-j-1-x;
}
j=n-1;
fill(all(A.bit), 0);
for(auto u:v){
while(j>=0 && s[c[j]]+t[c[j]]>=u.F.F)
A.add(aa[c[j--]]);
int x=bin1(u.S.F);
if(x>=0)
x=A.sum(x-1);
ans[u.F.S]=n-j-1-x;
}
j=n-1;
fill(all(A.bit), 0);
for(auto u: v){
while(j>=0 && s[c[j]]+t[c[j]]>=u.F.F)
A.add(bb[c[j--]]);
int x=bin2(u.S.S);
if(x>=0)
x=A.sum(x-1);
ans[u.F.S]-=x;
}
for(int i=0;i<q;++i)
cout<<ans[i]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
460 KB |
Output is correct |
8 |
Correct |
4 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
5892 KB |
Output is correct |
2 |
Correct |
117 ms |
5840 KB |
Output is correct |
3 |
Correct |
110 ms |
5908 KB |
Output is correct |
4 |
Correct |
89 ms |
5956 KB |
Output is correct |
5 |
Correct |
88 ms |
5864 KB |
Output is correct |
6 |
Correct |
75 ms |
5852 KB |
Output is correct |
7 |
Correct |
124 ms |
5960 KB |
Output is correct |
8 |
Correct |
104 ms |
5972 KB |
Output is correct |
9 |
Correct |
100 ms |
5900 KB |
Output is correct |
10 |
Correct |
81 ms |
5712 KB |
Output is correct |
11 |
Correct |
81 ms |
5724 KB |
Output is correct |
12 |
Correct |
52 ms |
5516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
5892 KB |
Output is correct |
2 |
Correct |
117 ms |
5840 KB |
Output is correct |
3 |
Correct |
110 ms |
5908 KB |
Output is correct |
4 |
Correct |
89 ms |
5956 KB |
Output is correct |
5 |
Correct |
88 ms |
5864 KB |
Output is correct |
6 |
Correct |
75 ms |
5852 KB |
Output is correct |
7 |
Correct |
124 ms |
5960 KB |
Output is correct |
8 |
Correct |
104 ms |
5972 KB |
Output is correct |
9 |
Correct |
100 ms |
5900 KB |
Output is correct |
10 |
Correct |
81 ms |
5712 KB |
Output is correct |
11 |
Correct |
81 ms |
5724 KB |
Output is correct |
12 |
Correct |
52 ms |
5516 KB |
Output is correct |
13 |
Correct |
135 ms |
6100 KB |
Output is correct |
14 |
Correct |
121 ms |
6104 KB |
Output is correct |
15 |
Correct |
122 ms |
5944 KB |
Output is correct |
16 |
Correct |
105 ms |
6100 KB |
Output is correct |
17 |
Correct |
106 ms |
6052 KB |
Output is correct |
18 |
Correct |
71 ms |
6164 KB |
Output is correct |
19 |
Correct |
129 ms |
6072 KB |
Output is correct |
20 |
Correct |
127 ms |
6108 KB |
Output is correct |
21 |
Correct |
130 ms |
6624 KB |
Output is correct |
22 |
Correct |
82 ms |
5712 KB |
Output is correct |
23 |
Correct |
76 ms |
5688 KB |
Output is correct |
24 |
Correct |
55 ms |
5664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
460 KB |
Output is correct |
8 |
Correct |
4 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
460 KB |
Output is correct |
10 |
Correct |
3 ms |
460 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
3 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
3 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
3 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
460 KB |
Output is correct |
19 |
Correct |
110 ms |
5892 KB |
Output is correct |
20 |
Correct |
117 ms |
5840 KB |
Output is correct |
21 |
Correct |
110 ms |
5908 KB |
Output is correct |
22 |
Correct |
89 ms |
5956 KB |
Output is correct |
23 |
Correct |
88 ms |
5864 KB |
Output is correct |
24 |
Correct |
75 ms |
5852 KB |
Output is correct |
25 |
Correct |
124 ms |
5960 KB |
Output is correct |
26 |
Correct |
104 ms |
5972 KB |
Output is correct |
27 |
Correct |
100 ms |
5900 KB |
Output is correct |
28 |
Correct |
81 ms |
5712 KB |
Output is correct |
29 |
Correct |
81 ms |
5724 KB |
Output is correct |
30 |
Correct |
52 ms |
5516 KB |
Output is correct |
31 |
Correct |
135 ms |
6100 KB |
Output is correct |
32 |
Correct |
121 ms |
6104 KB |
Output is correct |
33 |
Correct |
122 ms |
5944 KB |
Output is correct |
34 |
Correct |
105 ms |
6100 KB |
Output is correct |
35 |
Correct |
106 ms |
6052 KB |
Output is correct |
36 |
Correct |
71 ms |
6164 KB |
Output is correct |
37 |
Correct |
129 ms |
6072 KB |
Output is correct |
38 |
Correct |
127 ms |
6108 KB |
Output is correct |
39 |
Correct |
130 ms |
6624 KB |
Output is correct |
40 |
Correct |
82 ms |
5712 KB |
Output is correct |
41 |
Correct |
76 ms |
5688 KB |
Output is correct |
42 |
Correct |
55 ms |
5664 KB |
Output is correct |
43 |
Correct |
140 ms |
6136 KB |
Output is correct |
44 |
Correct |
141 ms |
6100 KB |
Output is correct |
45 |
Correct |
131 ms |
6032 KB |
Output is correct |
46 |
Correct |
112 ms |
6128 KB |
Output is correct |
47 |
Correct |
112 ms |
6140 KB |
Output is correct |
48 |
Correct |
74 ms |
6328 KB |
Output is correct |
49 |
Correct |
134 ms |
6116 KB |
Output is correct |
50 |
Correct |
143 ms |
6580 KB |
Output is correct |
51 |
Correct |
140 ms |
6500 KB |
Output is correct |
52 |
Correct |
104 ms |
5932 KB |
Output is correct |
53 |
Correct |
80 ms |
5720 KB |
Output is correct |