#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef priority_queue<pii> max_heap;
typedef priority_queue<pii, vector<pii>, greater<pii>> min_heap;
const int MX=300010, inf=1e9;
struct SHOP {
int x, type, s, e, idx;
} S[MX];
struct QUERY {
int x, t, idx, ans;
} Q[MX];
int n, q, k;
void input(){
cin>>n>>k>>q;
for(int i=1; i<=n; i++){
int t, x, s, e;
cin>>x>>t>>s>>e;
S[i]={x,t,s,e,i};
}
for(int i=1; i<=q; i++){
int x, t;
cin>>x>>t;
Q[i]={x,t,i};
}
}
vector<int> X;
int xmax;
int lo(int x){
return upper_bound(X.begin(), X.end(), x)-X.begin()-1;
}
int hi(int x){
return lower_bound(X.begin(), X.end(), x)-X.begin();
}
void prec(){
for(int i=1; i<=n; i++){
S[i].x*=2;
}
for(int i=1; i<=q; i++){
Q[i].x*=2;
X.push_back(Q[i].x);
}
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end())-X.begin());
xmax=X.size()-1U;
}
//////
multiset<int> pos[MX];
int nonzero_cnt;
// seg
struct SegTree {
int tree[4*MX]={};
multiset<int> leaf[MX];
// [0, xmax]
void init(int v=1, int s=0, int e=xmax){
tree[v]=-inf;
if(s==e) return;
init(v*2,s,(s+e)/2);
init(v*2+1,(s+e)/2+1,e);
}
int mx(int v, int s, int e, int l, int r){
if(r<s || e<l) return -inf;
if(l<=s && e<=r) return tree[v];
return max(mx(v*2, s, (s+e)/2, l, r), mx(v*2+1, (s+e)/2+1, e, l, r));
}
int mx(int l, int r){
return mx(1,0,xmax,l,r);
}
void upt(int v, int s, int e, int x){
if(x<s || e<x) return;
if(s==e){
if(leaf[x].empty()) tree[v]=-inf;
else tree[v]=*leaf[x].rbegin();
return;
}
upt(v*2, s, (s+e)/2, x);
upt(v*2+1, (s+e)/2+1, e, x);
tree[v]=max(tree[v*2], tree[v*2+1]);
}
void put(int x, int val){
// cout<<"PUT: "<<x<<' '<<val<<'\n';
if(x<0 || xmax<x) return;
leaf[x].insert(val);
upt(1,0,xmax,x);
}
void pop(int x, int val){
// cout<<"POP: "<<x<<' '<<val<<'\n';
if(x<0 || xmax<x) return;
leaf[x].erase(leaf[x].find(val));
upt(1,0,xmax,x);
}
} Seg1, Seg2;
// 1이 /, 2가 \
1: max(y-x), 2: max(y+x)
void add(int x1, int x2){
int mx=(0LL+x1+x2)/2, my=(0LL+x2-x1)/2;
Seg1.put(lo(mx), my-mx);
Seg2.put(hi(mx), my+mx);
}
void del(int x1, int x2){
int mx=(0LL+x1+x2)/2, my=(0LL+x2-x1)/2;
Seg1.pop(lo(mx), my-mx);
Seg2.pop(hi(mx), my+mx);
}
void debug(){
cout<<"\nDEBUG...\n";
cout<<"SHOPS: \n";
for(int i=1; i<=k; i++){
for(auto p:pos[i])
cout<<p<<' ';
cout<<'\n';
}
cout<<'\n';
cout<<"lines: \n";
cout<<"RAW TREE:\n";
for(int i=1; i<=4*xmax+4; i++){
cout<<Seg1.tree[i]<<' ';
}
cout<<"\nMAXES:\n";
for(int i=0; i<=xmax; i++){
cout<<i<<"- / : "<<Seg1.mx(i, i)<<", \\ : "<<Seg2.mx(i,i)<<'\n';
int a=-inf, b=-inf;
if(!Seg1.leaf[i].empty()) a=*Seg1.leaf[i].rbegin();
if(!Seg2.leaf[i].empty()) b=*Seg2.leaf[i].rbegin();
cout<<i<<"~ / : "<<a<<", \\ : "<<b<<'\n';
}
cout<<"\n";
}
int find(int qx){
if(nonzero_cnt<k) return -2;
// 1: /, 2: \
1: qx이상, max(y-x), 2: qx이하, max(y+x)
int x=lo(qx);
// cout<<Seg1.mx(x,xmax)<<" "<<Seg2.mx(0,x)<<'\n';
int ans=max(Seg1.mx(x, xmax)+qx, Seg2.mx(0, x)-qx);
// cout<<qx<<": "<<ans<<'\n';
return ans;
/*
int ans=-inf;
for(pii p:A){
int x, y; tie(x,y)=p;
if(x<qx) continue;
ans=max(ans, y-x+qx);
}
for(pii p:B){
int x, y; tie(x,y)=p;
if(x>qx) continue;
ans=max(ans, y+x-qx);
}
return ans;
*/
}
////
void add_store(int idx){
// cout<<"\nADD: "<<idx<<'\n';
multiset<int> &stores = pos[S[idx].type];
if(stores.size()==2U) nonzero_cnt++;
int x=S[idx].x;
if(stores.find(x)!=stores.end()){
stores.insert(x);
return;
}
auto it1=stores.lower_bound(x);
auto it2=stores.lower_bound(x); it2--;
int lx=*it2, rx=*it1;
del(lx, rx);
add(lx, x); add(x, rx);
stores.insert(x);
}
void del_store(int idx){
// cout<<"\nDEL: "<<idx<<'\n';
multiset<int> &stores = pos[S[idx].type];
if(stores.size()==3U) nonzero_cnt--;
int x=S[idx].x;
stores.erase(stores.find(x));
if(stores.find(x)!=stores.end()){
return;
}
auto it1=stores.lower_bound(x);
auto it2=stores.lower_bound(x); it2--;
int lx=*it2, rx=*it1;
del(lx, x); del(x, rx);
add(lx, rx);
}
void init(){
Seg1.init();
Seg2.init();
for(int i=1; i<=k; i++){
pos[i].insert(inf);
pos[i].insert(-inf);
add(-inf, inf);
}
}
////
void solve(){
min_heap in, out;
for(int i=1; i<=n; i++){
in.push({S[i].s, i});
out.push({S[i].e, i});
}
sort(Q+1, Q+q+1, [](QUERY &a, QUERY &b){ return a.t<b.t; });
init();
for(int i=1; i<=q; i++){
int qt=Q[i].t, qx=Q[i].x;
while(!in.empty()){
int t,idx; tie(t,idx)=in.top();
if(qt<t) break;
in.pop();
add_store(idx);
}
while(!out.empty()){
int t,idx; tie(t,idx)=out.top();
if(qt<=t) break;
out.pop();
del_store(idx);
}
Q[i].ans=find(qx);
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
input();
prec();
solve();
sort(Q+1, Q+q+1, [](QUERY &a, QUERY &b){ return a.idx<b.idx; });
for(int i=1; i<=q; i++){
cout<<Q[i].ans/2<<'\n';
}
return 0;
}
Compilation message
new_home.cpp:108:1: warning: multi-line comment [-Wcomment]
// 1이 /, 2가 \
^
new_home.cpp:150:2: warning: multi-line comment [-Wcomment]
// 1: /, 2: \
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
51960 KB |
Output is correct |
2 |
Correct |
47 ms |
52108 KB |
Output is correct |
3 |
Correct |
46 ms |
52108 KB |
Output is correct |
4 |
Correct |
46 ms |
52108 KB |
Output is correct |
5 |
Correct |
48 ms |
52140 KB |
Output is correct |
6 |
Correct |
48 ms |
52348 KB |
Output is correct |
7 |
Correct |
46 ms |
52348 KB |
Output is correct |
8 |
Correct |
46 ms |
52348 KB |
Output is correct |
9 |
Correct |
47 ms |
52348 KB |
Output is correct |
10 |
Correct |
49 ms |
52348 KB |
Output is correct |
11 |
Correct |
49 ms |
52348 KB |
Output is correct |
12 |
Correct |
47 ms |
52348 KB |
Output is correct |
13 |
Correct |
47 ms |
52348 KB |
Output is correct |
14 |
Correct |
46 ms |
52348 KB |
Output is correct |
15 |
Correct |
48 ms |
52408 KB |
Output is correct |
16 |
Correct |
48 ms |
52408 KB |
Output is correct |
17 |
Correct |
48 ms |
52408 KB |
Output is correct |
18 |
Correct |
46 ms |
52408 KB |
Output is correct |
19 |
Correct |
47 ms |
52408 KB |
Output is correct |
20 |
Correct |
47 ms |
52408 KB |
Output is correct |
21 |
Correct |
48 ms |
52408 KB |
Output is correct |
22 |
Correct |
48 ms |
52408 KB |
Output is correct |
23 |
Correct |
47 ms |
52408 KB |
Output is correct |
24 |
Correct |
45 ms |
52460 KB |
Output is correct |
25 |
Correct |
49 ms |
52460 KB |
Output is correct |
26 |
Correct |
47 ms |
52460 KB |
Output is correct |
27 |
Correct |
46 ms |
52460 KB |
Output is correct |
28 |
Correct |
49 ms |
52460 KB |
Output is correct |
29 |
Correct |
52 ms |
52460 KB |
Output is correct |
30 |
Correct |
46 ms |
52460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
51960 KB |
Output is correct |
2 |
Correct |
47 ms |
52108 KB |
Output is correct |
3 |
Correct |
46 ms |
52108 KB |
Output is correct |
4 |
Correct |
46 ms |
52108 KB |
Output is correct |
5 |
Correct |
48 ms |
52140 KB |
Output is correct |
6 |
Correct |
48 ms |
52348 KB |
Output is correct |
7 |
Correct |
46 ms |
52348 KB |
Output is correct |
8 |
Correct |
46 ms |
52348 KB |
Output is correct |
9 |
Correct |
47 ms |
52348 KB |
Output is correct |
10 |
Correct |
49 ms |
52348 KB |
Output is correct |
11 |
Correct |
49 ms |
52348 KB |
Output is correct |
12 |
Correct |
47 ms |
52348 KB |
Output is correct |
13 |
Correct |
47 ms |
52348 KB |
Output is correct |
14 |
Correct |
46 ms |
52348 KB |
Output is correct |
15 |
Correct |
48 ms |
52408 KB |
Output is correct |
16 |
Correct |
48 ms |
52408 KB |
Output is correct |
17 |
Correct |
48 ms |
52408 KB |
Output is correct |
18 |
Correct |
46 ms |
52408 KB |
Output is correct |
19 |
Correct |
47 ms |
52408 KB |
Output is correct |
20 |
Correct |
47 ms |
52408 KB |
Output is correct |
21 |
Correct |
48 ms |
52408 KB |
Output is correct |
22 |
Correct |
48 ms |
52408 KB |
Output is correct |
23 |
Correct |
47 ms |
52408 KB |
Output is correct |
24 |
Correct |
45 ms |
52460 KB |
Output is correct |
25 |
Correct |
49 ms |
52460 KB |
Output is correct |
26 |
Correct |
47 ms |
52460 KB |
Output is correct |
27 |
Correct |
46 ms |
52460 KB |
Output is correct |
28 |
Correct |
49 ms |
52460 KB |
Output is correct |
29 |
Correct |
52 ms |
52460 KB |
Output is correct |
30 |
Correct |
46 ms |
52460 KB |
Output is correct |
31 |
Correct |
844 ms |
64328 KB |
Output is correct |
32 |
Correct |
164 ms |
64328 KB |
Output is correct |
33 |
Correct |
748 ms |
64328 KB |
Output is correct |
34 |
Correct |
761 ms |
65684 KB |
Output is correct |
35 |
Correct |
873 ms |
73876 KB |
Output is correct |
36 |
Correct |
730 ms |
76660 KB |
Output is correct |
37 |
Correct |
546 ms |
76660 KB |
Output is correct |
38 |
Correct |
557 ms |
76660 KB |
Output is correct |
39 |
Correct |
481 ms |
76928 KB |
Output is correct |
40 |
Correct |
466 ms |
79784 KB |
Output is correct |
41 |
Correct |
551 ms |
82768 KB |
Output is correct |
42 |
Correct |
399 ms |
85664 KB |
Output is correct |
43 |
Correct |
119 ms |
90828 KB |
Output is correct |
44 |
Correct |
476 ms |
90996 KB |
Output is correct |
45 |
Correct |
515 ms |
93932 KB |
Output is correct |
46 |
Correct |
414 ms |
96708 KB |
Output is correct |
47 |
Correct |
271 ms |
99432 KB |
Output is correct |
48 |
Correct |
272 ms |
102156 KB |
Output is correct |
49 |
Correct |
300 ms |
104972 KB |
Output is correct |
50 |
Correct |
332 ms |
107964 KB |
Output is correct |
51 |
Correct |
337 ms |
107964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2715 ms |
168864 KB |
Output is correct |
2 |
Correct |
2652 ms |
176020 KB |
Output is correct |
3 |
Correct |
2245 ms |
217908 KB |
Output is correct |
4 |
Correct |
2651 ms |
217908 KB |
Output is correct |
5 |
Correct |
2559 ms |
217908 KB |
Output is correct |
6 |
Correct |
2721 ms |
228476 KB |
Output is correct |
7 |
Correct |
2217 ms |
270480 KB |
Output is correct |
8 |
Correct |
2186 ms |
270480 KB |
Output is correct |
9 |
Correct |
2147 ms |
272428 KB |
Output is correct |
10 |
Correct |
2452 ms |
282736 KB |
Output is correct |
11 |
Correct |
1552 ms |
293892 KB |
Output is correct |
12 |
Correct |
1732 ms |
294884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5020 ms |
295752 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
51960 KB |
Output is correct |
2 |
Correct |
47 ms |
52108 KB |
Output is correct |
3 |
Correct |
46 ms |
52108 KB |
Output is correct |
4 |
Correct |
46 ms |
52108 KB |
Output is correct |
5 |
Correct |
48 ms |
52140 KB |
Output is correct |
6 |
Correct |
48 ms |
52348 KB |
Output is correct |
7 |
Correct |
46 ms |
52348 KB |
Output is correct |
8 |
Correct |
46 ms |
52348 KB |
Output is correct |
9 |
Correct |
47 ms |
52348 KB |
Output is correct |
10 |
Correct |
49 ms |
52348 KB |
Output is correct |
11 |
Correct |
49 ms |
52348 KB |
Output is correct |
12 |
Correct |
47 ms |
52348 KB |
Output is correct |
13 |
Correct |
47 ms |
52348 KB |
Output is correct |
14 |
Correct |
46 ms |
52348 KB |
Output is correct |
15 |
Correct |
48 ms |
52408 KB |
Output is correct |
16 |
Correct |
48 ms |
52408 KB |
Output is correct |
17 |
Correct |
48 ms |
52408 KB |
Output is correct |
18 |
Correct |
46 ms |
52408 KB |
Output is correct |
19 |
Correct |
47 ms |
52408 KB |
Output is correct |
20 |
Correct |
47 ms |
52408 KB |
Output is correct |
21 |
Correct |
48 ms |
52408 KB |
Output is correct |
22 |
Correct |
48 ms |
52408 KB |
Output is correct |
23 |
Correct |
47 ms |
52408 KB |
Output is correct |
24 |
Correct |
45 ms |
52460 KB |
Output is correct |
25 |
Correct |
49 ms |
52460 KB |
Output is correct |
26 |
Correct |
47 ms |
52460 KB |
Output is correct |
27 |
Correct |
46 ms |
52460 KB |
Output is correct |
28 |
Correct |
49 ms |
52460 KB |
Output is correct |
29 |
Correct |
52 ms |
52460 KB |
Output is correct |
30 |
Correct |
46 ms |
52460 KB |
Output is correct |
31 |
Correct |
844 ms |
64328 KB |
Output is correct |
32 |
Correct |
164 ms |
64328 KB |
Output is correct |
33 |
Correct |
748 ms |
64328 KB |
Output is correct |
34 |
Correct |
761 ms |
65684 KB |
Output is correct |
35 |
Correct |
873 ms |
73876 KB |
Output is correct |
36 |
Correct |
730 ms |
76660 KB |
Output is correct |
37 |
Correct |
546 ms |
76660 KB |
Output is correct |
38 |
Correct |
557 ms |
76660 KB |
Output is correct |
39 |
Correct |
481 ms |
76928 KB |
Output is correct |
40 |
Correct |
466 ms |
79784 KB |
Output is correct |
41 |
Correct |
551 ms |
82768 KB |
Output is correct |
42 |
Correct |
399 ms |
85664 KB |
Output is correct |
43 |
Correct |
119 ms |
90828 KB |
Output is correct |
44 |
Correct |
476 ms |
90996 KB |
Output is correct |
45 |
Correct |
515 ms |
93932 KB |
Output is correct |
46 |
Correct |
414 ms |
96708 KB |
Output is correct |
47 |
Correct |
271 ms |
99432 KB |
Output is correct |
48 |
Correct |
272 ms |
102156 KB |
Output is correct |
49 |
Correct |
300 ms |
104972 KB |
Output is correct |
50 |
Correct |
332 ms |
107964 KB |
Output is correct |
51 |
Correct |
337 ms |
107964 KB |
Output is correct |
52 |
Correct |
573 ms |
295752 KB |
Output is correct |
53 |
Correct |
567 ms |
295752 KB |
Output is correct |
54 |
Correct |
680 ms |
295752 KB |
Output is correct |
55 |
Correct |
531 ms |
295752 KB |
Output is correct |
56 |
Correct |
606 ms |
295752 KB |
Output is correct |
57 |
Correct |
500 ms |
295752 KB |
Output is correct |
58 |
Correct |
513 ms |
295752 KB |
Output is correct |
59 |
Correct |
523 ms |
295752 KB |
Output is correct |
60 |
Correct |
499 ms |
295752 KB |
Output is correct |
61 |
Correct |
210 ms |
295752 KB |
Output is correct |
62 |
Correct |
608 ms |
295752 KB |
Output is correct |
63 |
Correct |
655 ms |
295752 KB |
Output is correct |
64 |
Correct |
642 ms |
295752 KB |
Output is correct |
65 |
Correct |
617 ms |
295752 KB |
Output is correct |
66 |
Correct |
492 ms |
295752 KB |
Output is correct |
67 |
Correct |
463 ms |
295752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
51960 KB |
Output is correct |
2 |
Correct |
47 ms |
52108 KB |
Output is correct |
3 |
Correct |
46 ms |
52108 KB |
Output is correct |
4 |
Correct |
46 ms |
52108 KB |
Output is correct |
5 |
Correct |
48 ms |
52140 KB |
Output is correct |
6 |
Correct |
48 ms |
52348 KB |
Output is correct |
7 |
Correct |
46 ms |
52348 KB |
Output is correct |
8 |
Correct |
46 ms |
52348 KB |
Output is correct |
9 |
Correct |
47 ms |
52348 KB |
Output is correct |
10 |
Correct |
49 ms |
52348 KB |
Output is correct |
11 |
Correct |
49 ms |
52348 KB |
Output is correct |
12 |
Correct |
47 ms |
52348 KB |
Output is correct |
13 |
Correct |
47 ms |
52348 KB |
Output is correct |
14 |
Correct |
46 ms |
52348 KB |
Output is correct |
15 |
Correct |
48 ms |
52408 KB |
Output is correct |
16 |
Correct |
48 ms |
52408 KB |
Output is correct |
17 |
Correct |
48 ms |
52408 KB |
Output is correct |
18 |
Correct |
46 ms |
52408 KB |
Output is correct |
19 |
Correct |
47 ms |
52408 KB |
Output is correct |
20 |
Correct |
47 ms |
52408 KB |
Output is correct |
21 |
Correct |
48 ms |
52408 KB |
Output is correct |
22 |
Correct |
48 ms |
52408 KB |
Output is correct |
23 |
Correct |
47 ms |
52408 KB |
Output is correct |
24 |
Correct |
45 ms |
52460 KB |
Output is correct |
25 |
Correct |
49 ms |
52460 KB |
Output is correct |
26 |
Correct |
47 ms |
52460 KB |
Output is correct |
27 |
Correct |
46 ms |
52460 KB |
Output is correct |
28 |
Correct |
49 ms |
52460 KB |
Output is correct |
29 |
Correct |
52 ms |
52460 KB |
Output is correct |
30 |
Correct |
46 ms |
52460 KB |
Output is correct |
31 |
Correct |
844 ms |
64328 KB |
Output is correct |
32 |
Correct |
164 ms |
64328 KB |
Output is correct |
33 |
Correct |
748 ms |
64328 KB |
Output is correct |
34 |
Correct |
761 ms |
65684 KB |
Output is correct |
35 |
Correct |
873 ms |
73876 KB |
Output is correct |
36 |
Correct |
730 ms |
76660 KB |
Output is correct |
37 |
Correct |
546 ms |
76660 KB |
Output is correct |
38 |
Correct |
557 ms |
76660 KB |
Output is correct |
39 |
Correct |
481 ms |
76928 KB |
Output is correct |
40 |
Correct |
466 ms |
79784 KB |
Output is correct |
41 |
Correct |
551 ms |
82768 KB |
Output is correct |
42 |
Correct |
399 ms |
85664 KB |
Output is correct |
43 |
Correct |
119 ms |
90828 KB |
Output is correct |
44 |
Correct |
476 ms |
90996 KB |
Output is correct |
45 |
Correct |
515 ms |
93932 KB |
Output is correct |
46 |
Correct |
414 ms |
96708 KB |
Output is correct |
47 |
Correct |
271 ms |
99432 KB |
Output is correct |
48 |
Correct |
272 ms |
102156 KB |
Output is correct |
49 |
Correct |
300 ms |
104972 KB |
Output is correct |
50 |
Correct |
332 ms |
107964 KB |
Output is correct |
51 |
Correct |
337 ms |
107964 KB |
Output is correct |
52 |
Correct |
2715 ms |
168864 KB |
Output is correct |
53 |
Correct |
2652 ms |
176020 KB |
Output is correct |
54 |
Correct |
2245 ms |
217908 KB |
Output is correct |
55 |
Correct |
2651 ms |
217908 KB |
Output is correct |
56 |
Correct |
2559 ms |
217908 KB |
Output is correct |
57 |
Correct |
2721 ms |
228476 KB |
Output is correct |
58 |
Correct |
2217 ms |
270480 KB |
Output is correct |
59 |
Correct |
2186 ms |
270480 KB |
Output is correct |
60 |
Correct |
2147 ms |
272428 KB |
Output is correct |
61 |
Correct |
2452 ms |
282736 KB |
Output is correct |
62 |
Correct |
1552 ms |
293892 KB |
Output is correct |
63 |
Correct |
1732 ms |
294884 KB |
Output is correct |
64 |
Execution timed out |
5020 ms |
295752 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |