#include <bits/stdc++.h>
using namespace std;
int n,q,t[200005<<2],ct,ans[200005];
struct edge{
int a,b,c,op,id;
}e[200005];
vector<int> v;
vector<edge> in,qu;
map<int,int> mp;
int cmp1(edge l,edge r)
{
if (l.a!=r.a) return l.a>r.a;
else return l.op<r.op;
}
int cmp2(edge l,edge r)
{
if (l.b!=r.b) return l.b>r.b;
else if (l.c!=r.c) return l.c<r.c;
else return l.id<r.id;
}
void clean(int id,int x,int y)
{
if (t[id]==0) return;
t[id]=0;
if (x!=y){
int mid=(x+y)/2;
clean(id*2,x,mid);
clean(id*2+1,mid+1,y);
}
}
void update(int id,int x,int y,int pos)
{
if (x==y) t[id]++;
else {
int mid=(x+y)/2;
if (pos<=mid) update(id*2,x,mid,pos);
else update(id*2+1,mid+1,y,pos);
t[id]=t[id*2+1]+t[id*2];
}
}
int query(int id,int x,int y,int l,int r)
{
if (l<=x&&y<=r) return t[id];
if (x>r||y<l) return 0;
int mid=(x+y)/2;
return query(id*2,x,mid,l,r)+query(id*2+1,mid+1,y,l,r);
}
void check(int id,int x,int y)
{
if (!t[id]) return;
if (x!=y){
int mid=(x+y)/2;
check(id*2,x,mid);
check(id*2+1,mid+1,y);
}
}
void cdq(int l,int r)
{
if (l>=r) return;
int mid=(l+r)/2;
cdq(l,mid);cdq(mid+1,r);
in.clear();
qu.clear();
for (int i=l;i<=mid;i++) if (!e[i].op) in.push_back(e[i]);
for (int i=mid+1;i<=r;i++) if (e[i].op) qu.push_back(e[i]);
sort(in.begin(),in.end(),cmp2);
sort(qu.begin(),qu.end(),cmp2);
clean(1,1,ct);
int pt=0;
for (edge i:qu){
while (pt<in.size()&&in[pt].b>=i.b){
update(1,1,ct,in[pt].c);
pt++;
}
ans[i.id]+=query(1,1,ct,i.c,ct);
}
//separate query and insert
//sweep insert and query like
//doing task2126 hedgehog... algorithm
//to avoid query comes before insert
//0 100 3 4
//35 100 6 0
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>q;
for (int i=1;i<=n;i++){
cin>>e[i].a>>e[i].b;
e[i].c=e[i].a+e[i].b;
e[i].op=0;
v.push_back(e[i].c);
}
for (int i=n+1;i<=n+q;i++){
cin>>e[i].a>>e[i].b>>e[i].c;
e[i].op=1,e[i].id=i-n;
v.push_back(e[i].c);
}
sort(v.begin(),v.end());
for (int i:v) if(!mp[i]) mp[i]=++ct;
for (int i=1;i<=n+q;i++) e[i].c=mp[e[i].c];
sort(e+1,e+n+q+1,cmp1);
cdq(1,n+q);
for (int i=1;i<=q;i++) cout<<ans[i]<<"\n";
}
Compilation message
examination.cpp: In function 'void cdq(int, int)':
examination.cpp:71:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | while (pt<in.size()&&in[pt].b>=i.b){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
14 ms |
1100 KB |
Output is correct |
8 |
Correct |
15 ms |
1044 KB |
Output is correct |
9 |
Correct |
14 ms |
1100 KB |
Output is correct |
10 |
Correct |
14 ms |
1056 KB |
Output is correct |
11 |
Correct |
15 ms |
972 KB |
Output is correct |
12 |
Correct |
7 ms |
588 KB |
Output is correct |
13 |
Correct |
10 ms |
844 KB |
Output is correct |
14 |
Correct |
10 ms |
824 KB |
Output is correct |
15 |
Correct |
11 ms |
828 KB |
Output is correct |
16 |
Correct |
8 ms |
816 KB |
Output is correct |
17 |
Correct |
13 ms |
972 KB |
Output is correct |
18 |
Correct |
5 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
554 ms |
15924 KB |
Output is correct |
2 |
Correct |
562 ms |
15992 KB |
Output is correct |
3 |
Correct |
554 ms |
15896 KB |
Output is correct |
4 |
Correct |
532 ms |
14084 KB |
Output is correct |
5 |
Correct |
376 ms |
14388 KB |
Output is correct |
6 |
Correct |
266 ms |
10072 KB |
Output is correct |
7 |
Correct |
436 ms |
16772 KB |
Output is correct |
8 |
Correct |
582 ms |
15820 KB |
Output is correct |
9 |
Correct |
416 ms |
16680 KB |
Output is correct |
10 |
Correct |
331 ms |
13716 KB |
Output is correct |
11 |
Correct |
428 ms |
14036 KB |
Output is correct |
12 |
Correct |
203 ms |
8820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
554 ms |
15924 KB |
Output is correct |
2 |
Correct |
562 ms |
15992 KB |
Output is correct |
3 |
Correct |
554 ms |
15896 KB |
Output is correct |
4 |
Correct |
532 ms |
14084 KB |
Output is correct |
5 |
Correct |
376 ms |
14388 KB |
Output is correct |
6 |
Correct |
266 ms |
10072 KB |
Output is correct |
7 |
Correct |
436 ms |
16772 KB |
Output is correct |
8 |
Correct |
582 ms |
15820 KB |
Output is correct |
9 |
Correct |
416 ms |
16680 KB |
Output is correct |
10 |
Correct |
331 ms |
13716 KB |
Output is correct |
11 |
Correct |
428 ms |
14036 KB |
Output is correct |
12 |
Correct |
203 ms |
8820 KB |
Output is correct |
13 |
Correct |
790 ms |
18644 KB |
Output is correct |
14 |
Correct |
753 ms |
18044 KB |
Output is correct |
15 |
Correct |
558 ms |
15872 KB |
Output is correct |
16 |
Correct |
684 ms |
16032 KB |
Output is correct |
17 |
Correct |
575 ms |
16320 KB |
Output is correct |
18 |
Correct |
305 ms |
10172 KB |
Output is correct |
19 |
Correct |
779 ms |
18664 KB |
Output is correct |
20 |
Correct |
792 ms |
18180 KB |
Output is correct |
21 |
Correct |
707 ms |
19608 KB |
Output is correct |
22 |
Correct |
322 ms |
13624 KB |
Output is correct |
23 |
Correct |
422 ms |
13940 KB |
Output is correct |
24 |
Correct |
225 ms |
8812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
14 ms |
1100 KB |
Output is correct |
8 |
Correct |
15 ms |
1044 KB |
Output is correct |
9 |
Correct |
14 ms |
1100 KB |
Output is correct |
10 |
Correct |
14 ms |
1056 KB |
Output is correct |
11 |
Correct |
15 ms |
972 KB |
Output is correct |
12 |
Correct |
7 ms |
588 KB |
Output is correct |
13 |
Correct |
10 ms |
844 KB |
Output is correct |
14 |
Correct |
10 ms |
824 KB |
Output is correct |
15 |
Correct |
11 ms |
828 KB |
Output is correct |
16 |
Correct |
8 ms |
816 KB |
Output is correct |
17 |
Correct |
13 ms |
972 KB |
Output is correct |
18 |
Correct |
5 ms |
588 KB |
Output is correct |
19 |
Correct |
554 ms |
15924 KB |
Output is correct |
20 |
Correct |
562 ms |
15992 KB |
Output is correct |
21 |
Correct |
554 ms |
15896 KB |
Output is correct |
22 |
Correct |
532 ms |
14084 KB |
Output is correct |
23 |
Correct |
376 ms |
14388 KB |
Output is correct |
24 |
Correct |
266 ms |
10072 KB |
Output is correct |
25 |
Correct |
436 ms |
16772 KB |
Output is correct |
26 |
Correct |
582 ms |
15820 KB |
Output is correct |
27 |
Correct |
416 ms |
16680 KB |
Output is correct |
28 |
Correct |
331 ms |
13716 KB |
Output is correct |
29 |
Correct |
428 ms |
14036 KB |
Output is correct |
30 |
Correct |
203 ms |
8820 KB |
Output is correct |
31 |
Correct |
790 ms |
18644 KB |
Output is correct |
32 |
Correct |
753 ms |
18044 KB |
Output is correct |
33 |
Correct |
558 ms |
15872 KB |
Output is correct |
34 |
Correct |
684 ms |
16032 KB |
Output is correct |
35 |
Correct |
575 ms |
16320 KB |
Output is correct |
36 |
Correct |
305 ms |
10172 KB |
Output is correct |
37 |
Correct |
779 ms |
18664 KB |
Output is correct |
38 |
Correct |
792 ms |
18180 KB |
Output is correct |
39 |
Correct |
707 ms |
19608 KB |
Output is correct |
40 |
Correct |
322 ms |
13624 KB |
Output is correct |
41 |
Correct |
422 ms |
13940 KB |
Output is correct |
42 |
Correct |
225 ms |
8812 KB |
Output is correct |
43 |
Correct |
861 ms |
25180 KB |
Output is correct |
44 |
Correct |
894 ms |
25272 KB |
Output is correct |
45 |
Correct |
862 ms |
25256 KB |
Output is correct |
46 |
Correct |
793 ms |
23656 KB |
Output is correct |
47 |
Correct |
691 ms |
23864 KB |
Output is correct |
48 |
Correct |
325 ms |
10024 KB |
Output is correct |
49 |
Correct |
719 ms |
26172 KB |
Output is correct |
50 |
Correct |
899 ms |
25120 KB |
Output is correct |
51 |
Correct |
732 ms |
25864 KB |
Output is correct |
52 |
Correct |
647 ms |
23120 KB |
Output is correct |
53 |
Correct |
465 ms |
16864 KB |
Output is correct |