#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
struct Fenwick{
vector<int> a,fw;
void upd(int pos,int x){
int n = fw.size() - 1;
while(pos <= n){
fw[pos] += x;
pos += (pos & -pos);
}
}
int gt(int pos){
int res = 0;
while(pos > 0){
res += fw[pos];
pos -= (pos & -pos);
}
return res;
}
int get(int pos){
return gt(fw.size() - 1) - gt(pos - 1);
}
};
struct SegTree{
int n; vector<Fenwick> seg; vector<int> arr;
SegTree(int _n){
seg.resize(4 * (n = _n) + 4);
arr.resize(_n + 1);
};
void build(int v,int tl,int tr){
if(tl == tr){
seg[v].fw.assign(2,0);
seg[v].a.assign(1,arr[tl]);
return;
}
int tm = (tl + tr) / 2;
build(v*2,tl,tm), build(v*2+1,tm+1,tr);
seg[v].fw.resize(seg[v*2].a.size() + seg[v*2+1].a.size() + 1);
seg[v].a.resize(seg[v*2].a.size() + seg[v*2+1].a.size());
merge(all(seg[v*2].a),all(seg[v*2+1].a),seg[v].a.begin());
}
void upd(int v,int tl,int tr,int pos,int x){
if(tl > pos || tr < pos) return;
if(tl == pos && tr == pos){
seg[v].upd(1,1);
return;
}
int tm = (tl + tr) / 2;
upd(v*2,tl,tm,pos,x),upd(v*2+1,tm+1,tr,pos,x);
int co = lower_bound(all(seg[v].a),x) - seg[v].a.begin() + 1;
seg[v].upd(co,1);
}
void upd(int pos,int x){
upd(1,1,n,pos,x);
}
int get(int v,int tl,int tr,int l,int r,int x){
if(tl > r || tr < l) return 0;
if(tl >= l && tr <= r){
int pos = lower_bound(all(seg[v].a),x) - seg[v].a.begin() + 1;
return seg[v].get(pos);
}
int tm = (tl + tr) / 2;
return get(v*2,tl,tm,l,r,x) + get(v*2+1,tm+1,tr,l,r,x);
}
int get(int pos,int x){
return get(1,1,n,pos,n,x);
}
};
void solve(){
int n,t;
cin >> n >> t;
vector<int> nums,ans(t + 1); vector<array<int,3>> v(n);
for(auto &[x,y,z] : v){
cin >> x >> y; nums.pb(y);
}
sort(all(v),[&](array<int,3> a,array<int,3> b){
return a[1] < b[1];
});
int pos = 0;
for(auto &[x,y,z] : v){
z = ++pos;
}
vector<array<int,4>> q(t); pos = 0;
for(auto &[x,y,z,t] : q){
cin >> x >> y >> z; t = ++pos;
}
pos = -1; SegTree seg(n);
for(int i = 1; i <= n; i++){
seg.arr[i] = v[i-1][0] + v[i-1][1];
}
seg.build(1,1,n);
sort(all(nums)), sort(rall(v)), sort(rall(q));
auto f = [&](int x){
return lower_bound(all(nums),x) - nums.begin() + 1;
};
for(auto[x,y,z,t] : q){
while(pos + 1 < n && v[pos+1][0] >= x){
++pos; seg.upd(v[pos][2],v[pos][0] + v[pos][1]);
}
ans[t] = seg.get(f(y),z);
}
for(int i = 1; i <= t; i++ ){
cout << ans[i] << endl;
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
9 ms |
1984 KB |
Output is correct |
8 |
Correct |
9 ms |
2064 KB |
Output is correct |
9 |
Correct |
8 ms |
2128 KB |
Output is correct |
10 |
Correct |
7 ms |
2004 KB |
Output is correct |
11 |
Correct |
7 ms |
2004 KB |
Output is correct |
12 |
Correct |
6 ms |
2004 KB |
Output is correct |
13 |
Correct |
9 ms |
2092 KB |
Output is correct |
14 |
Correct |
10 ms |
2004 KB |
Output is correct |
15 |
Correct |
7 ms |
2004 KB |
Output is correct |
16 |
Correct |
6 ms |
2036 KB |
Output is correct |
17 |
Correct |
8 ms |
2000 KB |
Output is correct |
18 |
Correct |
4 ms |
1876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
560 ms |
63492 KB |
Output is correct |
2 |
Correct |
480 ms |
65480 KB |
Output is correct |
3 |
Correct |
488 ms |
65556 KB |
Output is correct |
4 |
Correct |
348 ms |
64848 KB |
Output is correct |
5 |
Correct |
347 ms |
64752 KB |
Output is correct |
6 |
Correct |
246 ms |
64064 KB |
Output is correct |
7 |
Correct |
469 ms |
65352 KB |
Output is correct |
8 |
Correct |
449 ms |
65448 KB |
Output is correct |
9 |
Correct |
425 ms |
65288 KB |
Output is correct |
10 |
Correct |
250 ms |
64608 KB |
Output is correct |
11 |
Correct |
307 ms |
64544 KB |
Output is correct |
12 |
Correct |
180 ms |
63632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
560 ms |
63492 KB |
Output is correct |
2 |
Correct |
480 ms |
65480 KB |
Output is correct |
3 |
Correct |
488 ms |
65556 KB |
Output is correct |
4 |
Correct |
348 ms |
64848 KB |
Output is correct |
5 |
Correct |
347 ms |
64752 KB |
Output is correct |
6 |
Correct |
246 ms |
64064 KB |
Output is correct |
7 |
Correct |
469 ms |
65352 KB |
Output is correct |
8 |
Correct |
449 ms |
65448 KB |
Output is correct |
9 |
Correct |
425 ms |
65288 KB |
Output is correct |
10 |
Correct |
250 ms |
64608 KB |
Output is correct |
11 |
Correct |
307 ms |
64544 KB |
Output is correct |
12 |
Correct |
180 ms |
63632 KB |
Output is correct |
13 |
Correct |
564 ms |
65956 KB |
Output is correct |
14 |
Correct |
547 ms |
65892 KB |
Output is correct |
15 |
Correct |
484 ms |
65484 KB |
Output is correct |
16 |
Correct |
437 ms |
65164 KB |
Output is correct |
17 |
Correct |
383 ms |
65128 KB |
Output is correct |
18 |
Correct |
233 ms |
64008 KB |
Output is correct |
19 |
Correct |
617 ms |
66044 KB |
Output is correct |
20 |
Correct |
583 ms |
65904 KB |
Output is correct |
21 |
Correct |
547 ms |
65900 KB |
Output is correct |
22 |
Correct |
255 ms |
64584 KB |
Output is correct |
23 |
Correct |
315 ms |
64596 KB |
Output is correct |
24 |
Correct |
183 ms |
63704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
9 ms |
1984 KB |
Output is correct |
8 |
Correct |
9 ms |
2064 KB |
Output is correct |
9 |
Correct |
8 ms |
2128 KB |
Output is correct |
10 |
Correct |
7 ms |
2004 KB |
Output is correct |
11 |
Correct |
7 ms |
2004 KB |
Output is correct |
12 |
Correct |
6 ms |
2004 KB |
Output is correct |
13 |
Correct |
9 ms |
2092 KB |
Output is correct |
14 |
Correct |
10 ms |
2004 KB |
Output is correct |
15 |
Correct |
7 ms |
2004 KB |
Output is correct |
16 |
Correct |
6 ms |
2036 KB |
Output is correct |
17 |
Correct |
8 ms |
2000 KB |
Output is correct |
18 |
Correct |
4 ms |
1876 KB |
Output is correct |
19 |
Correct |
560 ms |
63492 KB |
Output is correct |
20 |
Correct |
480 ms |
65480 KB |
Output is correct |
21 |
Correct |
488 ms |
65556 KB |
Output is correct |
22 |
Correct |
348 ms |
64848 KB |
Output is correct |
23 |
Correct |
347 ms |
64752 KB |
Output is correct |
24 |
Correct |
246 ms |
64064 KB |
Output is correct |
25 |
Correct |
469 ms |
65352 KB |
Output is correct |
26 |
Correct |
449 ms |
65448 KB |
Output is correct |
27 |
Correct |
425 ms |
65288 KB |
Output is correct |
28 |
Correct |
250 ms |
64608 KB |
Output is correct |
29 |
Correct |
307 ms |
64544 KB |
Output is correct |
30 |
Correct |
180 ms |
63632 KB |
Output is correct |
31 |
Correct |
564 ms |
65956 KB |
Output is correct |
32 |
Correct |
547 ms |
65892 KB |
Output is correct |
33 |
Correct |
484 ms |
65484 KB |
Output is correct |
34 |
Correct |
437 ms |
65164 KB |
Output is correct |
35 |
Correct |
383 ms |
65128 KB |
Output is correct |
36 |
Correct |
233 ms |
64008 KB |
Output is correct |
37 |
Correct |
617 ms |
66044 KB |
Output is correct |
38 |
Correct |
583 ms |
65904 KB |
Output is correct |
39 |
Correct |
547 ms |
65900 KB |
Output is correct |
40 |
Correct |
255 ms |
64584 KB |
Output is correct |
41 |
Correct |
315 ms |
64596 KB |
Output is correct |
42 |
Correct |
183 ms |
63704 KB |
Output is correct |
43 |
Correct |
564 ms |
67872 KB |
Output is correct |
44 |
Correct |
576 ms |
67880 KB |
Output is correct |
45 |
Correct |
611 ms |
67820 KB |
Output is correct |
46 |
Correct |
484 ms |
66248 KB |
Output is correct |
47 |
Correct |
376 ms |
66248 KB |
Output is correct |
48 |
Correct |
244 ms |
63880 KB |
Output is correct |
49 |
Correct |
538 ms |
67804 KB |
Output is correct |
50 |
Correct |
544 ms |
67776 KB |
Output is correct |
51 |
Correct |
530 ms |
67728 KB |
Output is correct |
52 |
Correct |
289 ms |
66244 KB |
Output is correct |
53 |
Correct |
323 ms |
65468 KB |
Output is correct |