#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(3)
int ans[300100];
multiset<int>stor[300100];
map<int,int>importpos;
struct segtree{
stack<pair<int,int>>stk;
int vl[1<<20];
segtree(){for(auto&i:vl)i=-1e9;}
void update(int i,int l,int r,int ll,int rr,int v){
if(vl[i]>=v)return;
if(l>rr||ll>r)return;
if(ll<=l&&r<=rr) return stk.push({i,vl[i]}),void(vl[i]=max(vl[i],v));
update(i*2,l,l+r>>1,ll,rr,v);
update(i*2+1,l+r+2>>1,r,ll,rr,v);
}
int query(int i,int l,int r,int p){
if(l==r) return vl[i];
if(l+r>>1<p)
return max(vl[i],query(i*2+1,l+r+2>>1,r,p));
return max(vl[i],query(i*2,l,l+r>>1,p));
}
void upd(int l,int r,int v){
update(1,1,importpos.size(),importpos.lower_bound(l)->second,
(--importpos.upper_bound(r))->second,v);
}
int qry(int p) {
return query(1,1,importpos.size(),importpos[p]);
}
void reroll(int k){
while(stk.size()>k){
auto[a,b]=stk.top();
stk.pop();
vl[a]=b;
}
}
} seg1,seg2;
vector<pair<int,int>>todie[1<<20],qrys[300100];
void a_range(int l,int r){
seg1.upd(l,l+r>>1,-l);
seg2.upd(l+r+2>>1,r,r);
}
map<int,vector<pair<int,int>>>qr;
map<int,int>compr;
void splatonto(int i,int l,int r,int ll,int rr,pair<int,int>vl){
if(ll<=l&&r<=rr)
return todie[i].push_back(vl);
if(ll>r||l>rr) return;
splatonto(i*2,l,l+r>>1,ll,rr,vl);
splatonto(i*2+1,l+r+2>>1,r,ll,rr,vl);
}
int depth=0;
void solve(int i,int ll,int rr){
if(ll>rr)return;
int sz1=seg1.stk.size(),sz2=seg2.stk.size();
for(auto[p,t]:todie[i]) {
stor[t].erase(stor[t].find(p));
if(stor[t].find(p)==stor[t].end())
a_range(*--stor[t].lower_bound(p),*stor[t].lower_bound(p));
}
if(ll==rr){
for(auto[p,i]:qrys[ll])
ans[i]=max(seg1.qry(p)+p,seg2.qry(p)-p);
} else {
solve(i*2,ll,ll+rr>>1);
solve(i*2+1,ll+rr+2>>1,rr);
}
for(auto[p,t]:todie[i])
stor[t].insert(p);
seg1.reroll(sz1),seg2.reroll(sz2);
}
vector<array<int,4>> stores;
int main(){
cin.tie(0)->sync_with_stdio(0);
importpos[-1e9]=0;
int n,k,q;
cin>>n>>k>>q;
for(int i=0;i<n;i++){
int l,r,a,b;
cin>>a>>b>>l>>r;
stor[b].insert(a);
stores.push_back({a,b,l,r});
}
for(int i=1;i<=k;i++)
stor[i].insert(-1e9),stor[i].insert(1e9);
for(int i=0;i<q;i++){
int x,y; cin>>x>>y;
qr[y].push_back({x,i});
compr[y];
importpos[x];
}
int CDCC=0,CDC=0;
for(auto&[i,j]:importpos)
j=++CDC;
importpos[1e9]=CDC+1;
for(auto&[i,j]:compr)
j=++CDCC;
for(auto[k,x]:qr){
int ind=compr[k];
for(auto[p,i]:x)
qrys[ind].push_back({p,i});
}
compr[-1e9]=0;
compr[1e9]=CDCC+1;
for(auto[pos,t,l,r]:stores){
int k=(--compr.lower_bound(l))->second;
splatonto(1,1,CDCC,1,k,{pos,t});
k=compr.upper_bound(r)->second;
splatonto(1,1,CDCC,k,CDCC,{pos,t});
}
for(int i=1;i<=k;i++){
auto it=++stor[i].begin();
while(it!=stor[i].end()){
a_range(*prev(it),*it);
it++;
}
}
solve(1,1,CDCC);
for(int i=0;i<q;i++)
cout<<(ans[i]>1e8?-1:ans[i])<<'\n';
}
Compilation message
new_home.cpp: In member function 'void segtree::update(int, int, int, int, int, int)':
new_home.cpp:15:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
15 | update(i*2,l,l+r>>1,ll,rr,v);
| ~^~
new_home.cpp:16:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
16 | update(i*2+1,l+r+2>>1,r,ll,rr,v);
| ~~~^~
new_home.cpp: In member function 'int segtree::query(int, int, int, int)':
new_home.cpp:20:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
20 | if(l+r>>1<p)
| ~^~
new_home.cpp:21:45: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | return max(vl[i],query(i*2+1,l+r+2>>1,r,p));
| ~~~^~
new_home.cpp:22:39: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | return max(vl[i],query(i*2,l,l+r>>1,p));
| ~^~
new_home.cpp: In member function 'void segtree::reroll(int)':
new_home.cpp:32:25: warning: comparison of integer expressions of different signedness: 'std::stack<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
32 | while(stk.size()>k){
| ~~~~~~~~~~^~
new_home.cpp: In function 'void a_range(int, int)':
new_home.cpp:41:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
41 | seg1.upd(l,l+r>>1,-l);
| ~^~
new_home.cpp:42:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | seg2.upd(l+r+2>>1,r,r);
| ~~~^~
new_home.cpp: In function 'void splatonto(int, int, int, int, int, std::pair<int, int>)':
new_home.cpp:50:22: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | splatonto(i*2,l,l+r>>1,ll,rr,vl);
| ~^~
new_home.cpp:51:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | splatonto(i*2+1,l+r+2>>1,r,ll,rr,vl);
| ~~~^~
new_home.cpp: In function 'void solve(int, int, int)':
new_home.cpp:66:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | solve(i*2,ll,ll+rr>>1);
| ~~^~~
new_home.cpp:67:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | solve(i*2+1,ll+rr+2>>1,rr);
| ~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
54360 KB |
Output is correct |
2 |
Correct |
22 ms |
54364 KB |
Output is correct |
3 |
Correct |
22 ms |
54364 KB |
Output is correct |
4 |
Correct |
27 ms |
54364 KB |
Output is correct |
5 |
Correct |
29 ms |
54364 KB |
Output is correct |
6 |
Correct |
25 ms |
54864 KB |
Output is correct |
7 |
Correct |
24 ms |
54620 KB |
Output is correct |
8 |
Correct |
24 ms |
54616 KB |
Output is correct |
9 |
Correct |
23 ms |
54620 KB |
Output is correct |
10 |
Correct |
24 ms |
54612 KB |
Output is correct |
11 |
Correct |
25 ms |
54616 KB |
Output is correct |
12 |
Correct |
25 ms |
54620 KB |
Output is correct |
13 |
Correct |
24 ms |
54448 KB |
Output is correct |
14 |
Correct |
25 ms |
54620 KB |
Output is correct |
15 |
Correct |
25 ms |
54620 KB |
Output is correct |
16 |
Correct |
27 ms |
54620 KB |
Output is correct |
17 |
Correct |
31 ms |
54616 KB |
Output is correct |
18 |
Correct |
24 ms |
54872 KB |
Output is correct |
19 |
Correct |
24 ms |
54612 KB |
Output is correct |
20 |
Correct |
26 ms |
54620 KB |
Output is correct |
21 |
Correct |
24 ms |
54360 KB |
Output is correct |
22 |
Correct |
24 ms |
54616 KB |
Output is correct |
23 |
Correct |
24 ms |
54616 KB |
Output is correct |
24 |
Correct |
24 ms |
54436 KB |
Output is correct |
25 |
Correct |
27 ms |
54616 KB |
Output is correct |
26 |
Correct |
25 ms |
54620 KB |
Output is correct |
27 |
Correct |
24 ms |
54364 KB |
Output is correct |
28 |
Correct |
24 ms |
54616 KB |
Output is correct |
29 |
Correct |
26 ms |
54568 KB |
Output is correct |
30 |
Correct |
24 ms |
54620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
54360 KB |
Output is correct |
2 |
Correct |
22 ms |
54364 KB |
Output is correct |
3 |
Correct |
22 ms |
54364 KB |
Output is correct |
4 |
Correct |
27 ms |
54364 KB |
Output is correct |
5 |
Correct |
29 ms |
54364 KB |
Output is correct |
6 |
Correct |
25 ms |
54864 KB |
Output is correct |
7 |
Correct |
24 ms |
54620 KB |
Output is correct |
8 |
Correct |
24 ms |
54616 KB |
Output is correct |
9 |
Correct |
23 ms |
54620 KB |
Output is correct |
10 |
Correct |
24 ms |
54612 KB |
Output is correct |
11 |
Correct |
25 ms |
54616 KB |
Output is correct |
12 |
Correct |
25 ms |
54620 KB |
Output is correct |
13 |
Correct |
24 ms |
54448 KB |
Output is correct |
14 |
Correct |
25 ms |
54620 KB |
Output is correct |
15 |
Correct |
25 ms |
54620 KB |
Output is correct |
16 |
Correct |
27 ms |
54620 KB |
Output is correct |
17 |
Correct |
31 ms |
54616 KB |
Output is correct |
18 |
Correct |
24 ms |
54872 KB |
Output is correct |
19 |
Correct |
24 ms |
54612 KB |
Output is correct |
20 |
Correct |
26 ms |
54620 KB |
Output is correct |
21 |
Correct |
24 ms |
54360 KB |
Output is correct |
22 |
Correct |
24 ms |
54616 KB |
Output is correct |
23 |
Correct |
24 ms |
54616 KB |
Output is correct |
24 |
Correct |
24 ms |
54436 KB |
Output is correct |
25 |
Correct |
27 ms |
54616 KB |
Output is correct |
26 |
Correct |
25 ms |
54620 KB |
Output is correct |
27 |
Correct |
24 ms |
54364 KB |
Output is correct |
28 |
Correct |
24 ms |
54616 KB |
Output is correct |
29 |
Correct |
26 ms |
54568 KB |
Output is correct |
30 |
Correct |
24 ms |
54620 KB |
Output is correct |
31 |
Correct |
1164 ms |
85692 KB |
Output is correct |
32 |
Correct |
113 ms |
62408 KB |
Output is correct |
33 |
Correct |
1438 ms |
87236 KB |
Output is correct |
34 |
Correct |
1078 ms |
86288 KB |
Output is correct |
35 |
Correct |
1167 ms |
85960 KB |
Output is correct |
36 |
Correct |
1384 ms |
86788 KB |
Output is correct |
37 |
Correct |
967 ms |
87436 KB |
Output is correct |
38 |
Correct |
1100 ms |
87764 KB |
Output is correct |
39 |
Correct |
780 ms |
87824 KB |
Output is correct |
40 |
Correct |
901 ms |
87488 KB |
Output is correct |
41 |
Correct |
816 ms |
86400 KB |
Output is correct |
42 |
Correct |
851 ms |
86280 KB |
Output is correct |
43 |
Correct |
55 ms |
63168 KB |
Output is correct |
44 |
Correct |
957 ms |
87016 KB |
Output is correct |
45 |
Correct |
890 ms |
86720 KB |
Output is correct |
46 |
Correct |
1245 ms |
87700 KB |
Output is correct |
47 |
Correct |
583 ms |
86724 KB |
Output is correct |
48 |
Correct |
623 ms |
86472 KB |
Output is correct |
49 |
Correct |
581 ms |
86472 KB |
Output is correct |
50 |
Correct |
593 ms |
86980 KB |
Output is correct |
51 |
Correct |
707 ms |
86468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1743 ms |
166536 KB |
Output is correct |
2 |
Correct |
1546 ms |
162720 KB |
Output is correct |
3 |
Correct |
1516 ms |
189564 KB |
Output is correct |
4 |
Correct |
1579 ms |
170400 KB |
Output is correct |
5 |
Correct |
1373 ms |
160160 KB |
Output is correct |
6 |
Correct |
1558 ms |
162988 KB |
Output is correct |
7 |
Correct |
1359 ms |
189464 KB |
Output is correct |
8 |
Correct |
1405 ms |
170300 KB |
Output is correct |
9 |
Correct |
1517 ms |
164008 KB |
Output is correct |
10 |
Correct |
1482 ms |
160448 KB |
Output is correct |
11 |
Correct |
1051 ms |
161964 KB |
Output is correct |
12 |
Correct |
1114 ms |
160684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5042 ms |
192724 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
54360 KB |
Output is correct |
2 |
Correct |
22 ms |
54364 KB |
Output is correct |
3 |
Correct |
22 ms |
54364 KB |
Output is correct |
4 |
Correct |
27 ms |
54364 KB |
Output is correct |
5 |
Correct |
29 ms |
54364 KB |
Output is correct |
6 |
Correct |
25 ms |
54864 KB |
Output is correct |
7 |
Correct |
24 ms |
54620 KB |
Output is correct |
8 |
Correct |
24 ms |
54616 KB |
Output is correct |
9 |
Correct |
23 ms |
54620 KB |
Output is correct |
10 |
Correct |
24 ms |
54612 KB |
Output is correct |
11 |
Correct |
25 ms |
54616 KB |
Output is correct |
12 |
Correct |
25 ms |
54620 KB |
Output is correct |
13 |
Correct |
24 ms |
54448 KB |
Output is correct |
14 |
Correct |
25 ms |
54620 KB |
Output is correct |
15 |
Correct |
25 ms |
54620 KB |
Output is correct |
16 |
Correct |
27 ms |
54620 KB |
Output is correct |
17 |
Correct |
31 ms |
54616 KB |
Output is correct |
18 |
Correct |
24 ms |
54872 KB |
Output is correct |
19 |
Correct |
24 ms |
54612 KB |
Output is correct |
20 |
Correct |
26 ms |
54620 KB |
Output is correct |
21 |
Correct |
24 ms |
54360 KB |
Output is correct |
22 |
Correct |
24 ms |
54616 KB |
Output is correct |
23 |
Correct |
24 ms |
54616 KB |
Output is correct |
24 |
Correct |
24 ms |
54436 KB |
Output is correct |
25 |
Correct |
27 ms |
54616 KB |
Output is correct |
26 |
Correct |
25 ms |
54620 KB |
Output is correct |
27 |
Correct |
24 ms |
54364 KB |
Output is correct |
28 |
Correct |
24 ms |
54616 KB |
Output is correct |
29 |
Correct |
26 ms |
54568 KB |
Output is correct |
30 |
Correct |
24 ms |
54620 KB |
Output is correct |
31 |
Correct |
1164 ms |
85692 KB |
Output is correct |
32 |
Correct |
113 ms |
62408 KB |
Output is correct |
33 |
Correct |
1438 ms |
87236 KB |
Output is correct |
34 |
Correct |
1078 ms |
86288 KB |
Output is correct |
35 |
Correct |
1167 ms |
85960 KB |
Output is correct |
36 |
Correct |
1384 ms |
86788 KB |
Output is correct |
37 |
Correct |
967 ms |
87436 KB |
Output is correct |
38 |
Correct |
1100 ms |
87764 KB |
Output is correct |
39 |
Correct |
780 ms |
87824 KB |
Output is correct |
40 |
Correct |
901 ms |
87488 KB |
Output is correct |
41 |
Correct |
816 ms |
86400 KB |
Output is correct |
42 |
Correct |
851 ms |
86280 KB |
Output is correct |
43 |
Correct |
55 ms |
63168 KB |
Output is correct |
44 |
Correct |
957 ms |
87016 KB |
Output is correct |
45 |
Correct |
890 ms |
86720 KB |
Output is correct |
46 |
Correct |
1245 ms |
87700 KB |
Output is correct |
47 |
Correct |
583 ms |
86724 KB |
Output is correct |
48 |
Correct |
623 ms |
86472 KB |
Output is correct |
49 |
Correct |
581 ms |
86472 KB |
Output is correct |
50 |
Correct |
593 ms |
86980 KB |
Output is correct |
51 |
Correct |
707 ms |
86468 KB |
Output is correct |
52 |
Correct |
609 ms |
90824 KB |
Output is correct |
53 |
Correct |
551 ms |
91336 KB |
Output is correct |
54 |
Correct |
753 ms |
87004 KB |
Output is correct |
55 |
Correct |
682 ms |
87804 KB |
Output is correct |
56 |
Correct |
679 ms |
86864 KB |
Output is correct |
57 |
Correct |
802 ms |
86608 KB |
Output is correct |
58 |
Correct |
678 ms |
88772 KB |
Output is correct |
59 |
Correct |
659 ms |
88516 KB |
Output is correct |
60 |
Correct |
822 ms |
87064 KB |
Output is correct |
61 |
Correct |
56 ms |
68816 KB |
Output is correct |
62 |
Correct |
500 ms |
90824 KB |
Output is correct |
63 |
Correct |
721 ms |
88452 KB |
Output is correct |
64 |
Correct |
710 ms |
87984 KB |
Output is correct |
65 |
Correct |
800 ms |
87904 KB |
Output is correct |
66 |
Correct |
905 ms |
86472 KB |
Output is correct |
67 |
Correct |
132 ms |
65024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
54360 KB |
Output is correct |
2 |
Correct |
22 ms |
54364 KB |
Output is correct |
3 |
Correct |
22 ms |
54364 KB |
Output is correct |
4 |
Correct |
27 ms |
54364 KB |
Output is correct |
5 |
Correct |
29 ms |
54364 KB |
Output is correct |
6 |
Correct |
25 ms |
54864 KB |
Output is correct |
7 |
Correct |
24 ms |
54620 KB |
Output is correct |
8 |
Correct |
24 ms |
54616 KB |
Output is correct |
9 |
Correct |
23 ms |
54620 KB |
Output is correct |
10 |
Correct |
24 ms |
54612 KB |
Output is correct |
11 |
Correct |
25 ms |
54616 KB |
Output is correct |
12 |
Correct |
25 ms |
54620 KB |
Output is correct |
13 |
Correct |
24 ms |
54448 KB |
Output is correct |
14 |
Correct |
25 ms |
54620 KB |
Output is correct |
15 |
Correct |
25 ms |
54620 KB |
Output is correct |
16 |
Correct |
27 ms |
54620 KB |
Output is correct |
17 |
Correct |
31 ms |
54616 KB |
Output is correct |
18 |
Correct |
24 ms |
54872 KB |
Output is correct |
19 |
Correct |
24 ms |
54612 KB |
Output is correct |
20 |
Correct |
26 ms |
54620 KB |
Output is correct |
21 |
Correct |
24 ms |
54360 KB |
Output is correct |
22 |
Correct |
24 ms |
54616 KB |
Output is correct |
23 |
Correct |
24 ms |
54616 KB |
Output is correct |
24 |
Correct |
24 ms |
54436 KB |
Output is correct |
25 |
Correct |
27 ms |
54616 KB |
Output is correct |
26 |
Correct |
25 ms |
54620 KB |
Output is correct |
27 |
Correct |
24 ms |
54364 KB |
Output is correct |
28 |
Correct |
24 ms |
54616 KB |
Output is correct |
29 |
Correct |
26 ms |
54568 KB |
Output is correct |
30 |
Correct |
24 ms |
54620 KB |
Output is correct |
31 |
Correct |
1164 ms |
85692 KB |
Output is correct |
32 |
Correct |
113 ms |
62408 KB |
Output is correct |
33 |
Correct |
1438 ms |
87236 KB |
Output is correct |
34 |
Correct |
1078 ms |
86288 KB |
Output is correct |
35 |
Correct |
1167 ms |
85960 KB |
Output is correct |
36 |
Correct |
1384 ms |
86788 KB |
Output is correct |
37 |
Correct |
967 ms |
87436 KB |
Output is correct |
38 |
Correct |
1100 ms |
87764 KB |
Output is correct |
39 |
Correct |
780 ms |
87824 KB |
Output is correct |
40 |
Correct |
901 ms |
87488 KB |
Output is correct |
41 |
Correct |
816 ms |
86400 KB |
Output is correct |
42 |
Correct |
851 ms |
86280 KB |
Output is correct |
43 |
Correct |
55 ms |
63168 KB |
Output is correct |
44 |
Correct |
957 ms |
87016 KB |
Output is correct |
45 |
Correct |
890 ms |
86720 KB |
Output is correct |
46 |
Correct |
1245 ms |
87700 KB |
Output is correct |
47 |
Correct |
583 ms |
86724 KB |
Output is correct |
48 |
Correct |
623 ms |
86472 KB |
Output is correct |
49 |
Correct |
581 ms |
86472 KB |
Output is correct |
50 |
Correct |
593 ms |
86980 KB |
Output is correct |
51 |
Correct |
707 ms |
86468 KB |
Output is correct |
52 |
Correct |
1743 ms |
166536 KB |
Output is correct |
53 |
Correct |
1546 ms |
162720 KB |
Output is correct |
54 |
Correct |
1516 ms |
189564 KB |
Output is correct |
55 |
Correct |
1579 ms |
170400 KB |
Output is correct |
56 |
Correct |
1373 ms |
160160 KB |
Output is correct |
57 |
Correct |
1558 ms |
162988 KB |
Output is correct |
58 |
Correct |
1359 ms |
189464 KB |
Output is correct |
59 |
Correct |
1405 ms |
170300 KB |
Output is correct |
60 |
Correct |
1517 ms |
164008 KB |
Output is correct |
61 |
Correct |
1482 ms |
160448 KB |
Output is correct |
62 |
Correct |
1051 ms |
161964 KB |
Output is correct |
63 |
Correct |
1114 ms |
160684 KB |
Output is correct |
64 |
Execution timed out |
5042 ms |
192724 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |