#include <bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define ll long long
#define endl '\n'
const int N = 1e6 + 6, LOG = 27, MOD = 1e9 + 7;
const ll INF = 1e18;
void solver();
signed main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1; // cin>>t;
while (t--)
solver();
}
int n,q;
ll res[N],max_val = 1;
struct op{
ll x = 0,y = 0,z = 0; int idx = 0,val = 0;
op(ll x,ll y,ll z,int i,int v):x(x),y(y),z(z),idx(i),val(v){}
bool operator < (const op &b) const{
op a = *this;
if(a.x == b.x) return a.val > b.val;
return (a.x == b.x ? (a.y == b.y ? a.z < b.z : a.y < b.y) : a.x > b.x);
}
};
vector<op>v;
void compress(){
map<ll,int>mp;
for(int i = 0;i < n + q;++i){
mp[v[i].x] = true;
mp[v[i].y] = true;
mp[v[i].z] = true;
}
for(auto i : mp) mp[i.first] = max_val++;
for(int i = 0; i < n + q; i++){
v[i].x = mp[v[i].x];
v[i].y = mp[v[i].y];
v[i].z = mp[v[i].z];
}
}
struct fenwick{
vector<ll>tr; int n;
fenwick(){}
void build(int sz){
n = sz;
tr.resize(n + 2);
}
int lowbit(int i){return i&(-i);}
void update(int i,ll val){
for(;i <= max_val;i += lowbit(i)) tr[i] += val;
}
ll sum(int i){
ll res = 0;
for(;i > 0;i -= lowbit(i)) res += tr[i];
return res;
}
ll get(int l,int r){
return sum(r) - sum(l - 1);
}
}bit;
void calc_cont(int l,int m,int r){
int a = l,b = m + 1;
vector<op>tmp; vector<pair<int,int>>record;
while(a <= m && b <= r){
if(v[a].y >= v[b].y){
bit.update(v[a].z,v[a].val);
record.emplace_back(v[a].z,v[a].val);
tmp.push_back(v[a]);
++a;
}else{
res[v[b].idx] += bit.get(v[b].z,max_val);
tmp.push_back(v[b]);
++b;
}
}
while(a <= m){
tmp.push_back(v[a]);
++a;
}
while(b <= r){
res[v[b].idx] += bit.get(v[b].z,max_val);
tmp.push_back(v[b]);
++b;
}
for(int i = l;i <= r;++i) v[i] = tmp[i - l];
for(auto &[pos,val] : record) bit.update(pos,-val);
}
void cdq(int l,int r){
if(l == r) return;
int m = (r+l) >> 1;
cdq(l,m); cdq(m+1,r);
calc_cont(l,m,r);
}
void solver() {
cin>>n>>q;
for(int i = 0;i < n;++i){
ll x,y; cin>>x>>y;
v.push_back({x,y,x + y,i,1});
}
for(int i = n;i < n + q;++i){
ll x,y,z; cin>>x>>y>>z;
v.push_back({x,y,z,i,0});
}
compress();
sort(v.begin(),v.end());
bit.build(max_val);
cdq(0,n + q - 1);
for(int i = n;i < n + q;++i) cout<<res[i]<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
12 ms |
1796 KB |
Output is correct |
8 |
Correct |
12 ms |
1788 KB |
Output is correct |
9 |
Correct |
12 ms |
1688 KB |
Output is correct |
10 |
Correct |
9 ms |
1504 KB |
Output is correct |
11 |
Correct |
9 ms |
1292 KB |
Output is correct |
12 |
Correct |
4 ms |
1056 KB |
Output is correct |
13 |
Correct |
11 ms |
1504 KB |
Output is correct |
14 |
Correct |
11 ms |
1500 KB |
Output is correct |
15 |
Correct |
12 ms |
1504 KB |
Output is correct |
16 |
Correct |
7 ms |
1152 KB |
Output is correct |
17 |
Correct |
9 ms |
1252 KB |
Output is correct |
18 |
Correct |
3 ms |
1052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
456 ms |
26984 KB |
Output is correct |
2 |
Correct |
444 ms |
29572 KB |
Output is correct |
3 |
Correct |
453 ms |
28340 KB |
Output is correct |
4 |
Correct |
339 ms |
24776 KB |
Output is correct |
5 |
Correct |
310 ms |
28348 KB |
Output is correct |
6 |
Correct |
139 ms |
26604 KB |
Output is correct |
7 |
Correct |
417 ms |
28340 KB |
Output is correct |
8 |
Correct |
433 ms |
27856 KB |
Output is correct |
9 |
Correct |
392 ms |
29364 KB |
Output is correct |
10 |
Correct |
281 ms |
28856 KB |
Output is correct |
11 |
Correct |
279 ms |
26040 KB |
Output is correct |
12 |
Correct |
96 ms |
26180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
456 ms |
26984 KB |
Output is correct |
2 |
Correct |
444 ms |
29572 KB |
Output is correct |
3 |
Correct |
453 ms |
28340 KB |
Output is correct |
4 |
Correct |
339 ms |
24776 KB |
Output is correct |
5 |
Correct |
310 ms |
28348 KB |
Output is correct |
6 |
Correct |
139 ms |
26604 KB |
Output is correct |
7 |
Correct |
417 ms |
28340 KB |
Output is correct |
8 |
Correct |
433 ms |
27856 KB |
Output is correct |
9 |
Correct |
392 ms |
29364 KB |
Output is correct |
10 |
Correct |
281 ms |
28856 KB |
Output is correct |
11 |
Correct |
279 ms |
26040 KB |
Output is correct |
12 |
Correct |
96 ms |
26180 KB |
Output is correct |
13 |
Correct |
505 ms |
29368 KB |
Output is correct |
14 |
Correct |
483 ms |
28692 KB |
Output is correct |
15 |
Correct |
443 ms |
27276 KB |
Output is correct |
16 |
Correct |
353 ms |
27568 KB |
Output is correct |
17 |
Correct |
365 ms |
26544 KB |
Output is correct |
18 |
Correct |
131 ms |
23388 KB |
Output is correct |
19 |
Correct |
505 ms |
29980 KB |
Output is correct |
20 |
Correct |
482 ms |
26808 KB |
Output is correct |
21 |
Correct |
497 ms |
30348 KB |
Output is correct |
22 |
Correct |
279 ms |
27064 KB |
Output is correct |
23 |
Correct |
298 ms |
28824 KB |
Output is correct |
24 |
Correct |
89 ms |
26900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
12 ms |
1796 KB |
Output is correct |
8 |
Correct |
12 ms |
1788 KB |
Output is correct |
9 |
Correct |
12 ms |
1688 KB |
Output is correct |
10 |
Correct |
9 ms |
1504 KB |
Output is correct |
11 |
Correct |
9 ms |
1292 KB |
Output is correct |
12 |
Correct |
4 ms |
1056 KB |
Output is correct |
13 |
Correct |
11 ms |
1504 KB |
Output is correct |
14 |
Correct |
11 ms |
1500 KB |
Output is correct |
15 |
Correct |
12 ms |
1504 KB |
Output is correct |
16 |
Correct |
7 ms |
1152 KB |
Output is correct |
17 |
Correct |
9 ms |
1252 KB |
Output is correct |
18 |
Correct |
3 ms |
1052 KB |
Output is correct |
19 |
Correct |
456 ms |
26984 KB |
Output is correct |
20 |
Correct |
444 ms |
29572 KB |
Output is correct |
21 |
Correct |
453 ms |
28340 KB |
Output is correct |
22 |
Correct |
339 ms |
24776 KB |
Output is correct |
23 |
Correct |
310 ms |
28348 KB |
Output is correct |
24 |
Correct |
139 ms |
26604 KB |
Output is correct |
25 |
Correct |
417 ms |
28340 KB |
Output is correct |
26 |
Correct |
433 ms |
27856 KB |
Output is correct |
27 |
Correct |
392 ms |
29364 KB |
Output is correct |
28 |
Correct |
281 ms |
28856 KB |
Output is correct |
29 |
Correct |
279 ms |
26040 KB |
Output is correct |
30 |
Correct |
96 ms |
26180 KB |
Output is correct |
31 |
Correct |
505 ms |
29368 KB |
Output is correct |
32 |
Correct |
483 ms |
28692 KB |
Output is correct |
33 |
Correct |
443 ms |
27276 KB |
Output is correct |
34 |
Correct |
353 ms |
27568 KB |
Output is correct |
35 |
Correct |
365 ms |
26544 KB |
Output is correct |
36 |
Correct |
131 ms |
23388 KB |
Output is correct |
37 |
Correct |
505 ms |
29980 KB |
Output is correct |
38 |
Correct |
482 ms |
26808 KB |
Output is correct |
39 |
Correct |
497 ms |
30348 KB |
Output is correct |
40 |
Correct |
279 ms |
27064 KB |
Output is correct |
41 |
Correct |
298 ms |
28824 KB |
Output is correct |
42 |
Correct |
89 ms |
26900 KB |
Output is correct |
43 |
Correct |
890 ms |
48120 KB |
Output is correct |
44 |
Correct |
928 ms |
48784 KB |
Output is correct |
45 |
Correct |
959 ms |
46868 KB |
Output is correct |
46 |
Correct |
537 ms |
33796 KB |
Output is correct |
47 |
Correct |
558 ms |
41652 KB |
Output is correct |
48 |
Correct |
149 ms |
24668 KB |
Output is correct |
49 |
Correct |
902 ms |
47256 KB |
Output is correct |
50 |
Correct |
903 ms |
48440 KB |
Output is correct |
51 |
Correct |
864 ms |
46260 KB |
Output is correct |
52 |
Correct |
483 ms |
36060 KB |
Output is correct |
53 |
Correct |
380 ms |
29980 KB |
Output is correct |