#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 6e5;
const int INF = 2e8;
int N, Q, K;
struct Data
{
int x, t, a, b;
};
struct Query
{
int l, y, p;
};
struct Data2
{
int t, p, q;
};
Data A[MAXN+10];
Query B[MAXN+10];
set<int> S[MAXN+10];
multiset<int> S2[MAXN+10];
vector<int> comp;
int ans[MAXN+10];
struct SEG
{
multiset<int, greater<int>> val[MAXN+10];
int tree[MAXN*4+10];
void init(int node, int tl, int tr)
{
tree[node]=-INF;
if(tl==tr) return;
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
}
void update(int node, int tl, int tr, int p)
{
if(tl==tr)
{
if(!val[tl].empty()) tree[node]=*val[tl].begin();
else tree[node]=-INF;
return;
}
int mid=tl+tr>>1;
if(p<=mid) update(node*2, tl, mid, p);
else update(node*2+1, mid+1, tr, p);
tree[node]=max(tree[node*2], tree[node*2+1]);
}
int query(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return -INF;
int mid=tl+tr>>1;
return max(query(node*2, tl, mid, l, r), query(node*2+1, mid+1, tr, l, r));
}
void push(int x, int y)
{
val[x].insert(y);
update(1, 1, comp.size(), x);
}
void pop(int x, int y)
{
assert(val[x].find(y)!=val[x].end());
val[x].erase(val[x].find(y));
update(1, 1, comp.size(), x);
}
int query(int l, int r)
{
return query(1, 1, comp.size(), l, r);
}
}seg1, seg2;
void push(int x, int y)
{
int x2=lower_bound(comp.begin(), comp.end(), x)-comp.begin()+1;
assert(comp[x2-1]==x);
seg1.push(x2, y-x);
seg2.push(x2, x+y);
}
void pop(int x, int y)
{
int x2=lower_bound(comp.begin(), comp.end(), x)-comp.begin()+1;
assert(comp[x2-1]==x);
seg1.pop(x2, y-x);
seg2.pop(x2, x+y);
}
int main()
{
scanf("%d%d%d", &N, &K, &Q);
for(int i=1; i<=N; i++)
{
scanf("%d%d%d%d", &A[i].x, &A[i].t, &A[i].a, &A[i].b);
A[i].x*=2;
}
for(int i=1; i<=Q; i++)
{
scanf("%d%d", &B[i].l, &B[i].y);
B[i].l*=2;
B[i].p=i;
}
vector<Data2> V;
for(int i=1; i<=N; i++)
{
V.push_back({A[i].a, -1, i});
V.push_back({A[i].b, 1, i});
}
for(int i=1; i<=Q; i++)
{
V.push_back({B[i].y, 0, i});
}
sort(V.begin(), V.end(), [&](const Data2 &p, const Data2 &q)
{
if(p.t!=q.t) return p.t<q.t;
return p.p<q.p;
});
for(int i=1; i<=K; i++)
{
S[i].insert(-INF);
S[i].insert(INF*2);
S2[i].insert(-INF);
S2[i].insert(INF*2);
}
comp.push_back(INF/2);
for(auto it : V)
{
if(it.p<0)
{
auto now = A[it.q];
S2[now.t].insert(now.x);
if(S[now.t].find(now.x)!=S[now.t].end()) continue;
auto pt=S[now.t].lower_bound(now.x);
int l=*prev(pt), r=*pt;
comp.push_back((l+now.x)>>1);
comp.push_back((now.x+r)>>1);
S[now.t].insert(now.x);
}
else if(it.p>0)
{
auto now = A[it.q];
S2[now.t].erase(S2[now.t].find(now.x));
if(S2[now.t].find(now.x)!=S2[now.t].end()) continue;
auto pt=S[now.t].lower_bound(now.x);
int l=*prev(pt), r=*next(pt);
comp.push_back((l+r)>>1);
S[now.t].erase(now.x);
}
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for(int i=1; i<=K; i++)
{
S[i].clear(); S2[i].clear();
S[i].insert(-INF);
S[i].insert(INF*2);
S2[i].insert(-INF);
S2[i].insert(INF*2);
push(INF/2, INF+INF/2);
}
seg1.init(1, 1, comp.size());
seg2.init(1, 1, comp.size());
int cnt=0;
for(auto it : V)
{
if(it.p<0)
{
auto now = A[it.q];
if(S2[now.t].size()==2) cnt++;
S2[now.t].insert(now.x);
if(S[now.t].find(now.x)!=S[now.t].end()) continue;
auto pt=S[now.t].lower_bound(now.x);
int l=*prev(pt), r=*pt;
pop((l+r)/2, (r-l)/2);
push((l+now.x)/2, (now.x-l)/2);
push((now.x+r)/2, (r-now.x)/2);
S[now.t].insert(now.x);
}
else if(it.p>0)
{
auto now = A[it.q];
S2[now.t].erase(S2[now.t].find(now.x));
if(S2[now.t].size()==2) cnt--;
if(S2[now.t].find(now.x)!=S2[now.t].end()) continue;
auto pt=S[now.t].lower_bound(now.x);
int l=*prev(pt), r=*next(pt);
push((l+r)/2, (r-l)/2);
pop((l+now.x)/2, (now.x-l)/2);
pop((now.x+r)/2, (r-now.x)/2);
S[now.t].erase(now.x);
}
else
{
auto now = B[it.q];
if(cnt!=K)
{
ans[it.q]=-1;
continue;
}
int val=-INF;
int x=lower_bound(comp.begin(), comp.end(), now.l)-comp.begin()+1;
val=max(val, seg1.query(x, comp.size())+now.l);
x=upper_bound(comp.begin(), comp.end(), now.l)-comp.begin();
val=max(val, seg2.query(1, x)-now.l);
ans[it.q]=val/2;
}
}
for(int i=1; i<=Q; i++) printf("%d\n", ans[i]);
}
Compilation message
new_home.cpp: In member function 'void SEG::init(int, int, int)':
new_home.cpp:45:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid=tl+tr>>1;
| ~~^~~
new_home.cpp: In member function 'void SEG::update(int, int, int, int)':
new_home.cpp:57:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | int mid=tl+tr>>1;
| ~~^~~
new_home.cpp: In member function 'int SEG::query(int, int, int, int, int)':
new_home.cpp:66:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int mid=tl+tr>>1;
| ~~^~~
new_home.cpp: In function 'int main()':
new_home.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
104 | scanf("%d%d%d", &N, &K, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:107:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
107 | scanf("%d%d%d%d", &A[i].x, &A[i].t, &A[i].a, &A[i].b);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
112 | scanf("%d%d", &B[i].l, &B[i].y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113132 KB |
Output is correct |
2 |
Correct |
75 ms |
113152 KB |
Output is correct |
3 |
Correct |
84 ms |
113132 KB |
Output is correct |
4 |
Correct |
77 ms |
113024 KB |
Output is correct |
5 |
Correct |
78 ms |
113132 KB |
Output is correct |
6 |
Correct |
78 ms |
113260 KB |
Output is correct |
7 |
Correct |
87 ms |
113260 KB |
Output is correct |
8 |
Correct |
80 ms |
113260 KB |
Output is correct |
9 |
Correct |
78 ms |
113296 KB |
Output is correct |
10 |
Correct |
80 ms |
113388 KB |
Output is correct |
11 |
Correct |
80 ms |
113132 KB |
Output is correct |
12 |
Correct |
76 ms |
113260 KB |
Output is correct |
13 |
Correct |
76 ms |
113260 KB |
Output is correct |
14 |
Correct |
76 ms |
113132 KB |
Output is correct |
15 |
Correct |
78 ms |
113260 KB |
Output is correct |
16 |
Correct |
88 ms |
113260 KB |
Output is correct |
17 |
Correct |
77 ms |
113260 KB |
Output is correct |
18 |
Correct |
81 ms |
113260 KB |
Output is correct |
19 |
Correct |
80 ms |
113260 KB |
Output is correct |
20 |
Correct |
79 ms |
113336 KB |
Output is correct |
21 |
Correct |
76 ms |
113280 KB |
Output is correct |
22 |
Correct |
80 ms |
113388 KB |
Output is correct |
23 |
Correct |
78 ms |
113280 KB |
Output is correct |
24 |
Correct |
78 ms |
113260 KB |
Output is correct |
25 |
Correct |
77 ms |
113280 KB |
Output is correct |
26 |
Correct |
76 ms |
113132 KB |
Output is correct |
27 |
Correct |
75 ms |
113132 KB |
Output is correct |
28 |
Correct |
77 ms |
113232 KB |
Output is correct |
29 |
Correct |
76 ms |
113260 KB |
Output is correct |
30 |
Correct |
75 ms |
113132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113132 KB |
Output is correct |
2 |
Correct |
75 ms |
113152 KB |
Output is correct |
3 |
Correct |
84 ms |
113132 KB |
Output is correct |
4 |
Correct |
77 ms |
113024 KB |
Output is correct |
5 |
Correct |
78 ms |
113132 KB |
Output is correct |
6 |
Correct |
78 ms |
113260 KB |
Output is correct |
7 |
Correct |
87 ms |
113260 KB |
Output is correct |
8 |
Correct |
80 ms |
113260 KB |
Output is correct |
9 |
Correct |
78 ms |
113296 KB |
Output is correct |
10 |
Correct |
80 ms |
113388 KB |
Output is correct |
11 |
Correct |
80 ms |
113132 KB |
Output is correct |
12 |
Correct |
76 ms |
113260 KB |
Output is correct |
13 |
Correct |
76 ms |
113260 KB |
Output is correct |
14 |
Correct |
76 ms |
113132 KB |
Output is correct |
15 |
Correct |
78 ms |
113260 KB |
Output is correct |
16 |
Correct |
88 ms |
113260 KB |
Output is correct |
17 |
Correct |
77 ms |
113260 KB |
Output is correct |
18 |
Correct |
81 ms |
113260 KB |
Output is correct |
19 |
Correct |
80 ms |
113260 KB |
Output is correct |
20 |
Correct |
79 ms |
113336 KB |
Output is correct |
21 |
Correct |
76 ms |
113280 KB |
Output is correct |
22 |
Correct |
80 ms |
113388 KB |
Output is correct |
23 |
Correct |
78 ms |
113280 KB |
Output is correct |
24 |
Correct |
78 ms |
113260 KB |
Output is correct |
25 |
Correct |
77 ms |
113280 KB |
Output is correct |
26 |
Correct |
76 ms |
113132 KB |
Output is correct |
27 |
Correct |
75 ms |
113132 KB |
Output is correct |
28 |
Correct |
77 ms |
113232 KB |
Output is correct |
29 |
Correct |
76 ms |
113260 KB |
Output is correct |
30 |
Correct |
75 ms |
113132 KB |
Output is correct |
31 |
Correct |
966 ms |
136796 KB |
Output is correct |
32 |
Correct |
205 ms |
119292 KB |
Output is correct |
33 |
Correct |
889 ms |
129344 KB |
Output is correct |
34 |
Correct |
882 ms |
129628 KB |
Output is correct |
35 |
Correct |
984 ms |
136604 KB |
Output is correct |
36 |
Correct |
965 ms |
136512 KB |
Output is correct |
37 |
Correct |
639 ms |
126544 KB |
Output is correct |
38 |
Correct |
652 ms |
126428 KB |
Output is correct |
39 |
Correct |
484 ms |
126136 KB |
Output is correct |
40 |
Correct |
498 ms |
126128 KB |
Output is correct |
41 |
Correct |
620 ms |
126508 KB |
Output is correct |
42 |
Correct |
616 ms |
126428 KB |
Output is correct |
43 |
Correct |
166 ms |
122972 KB |
Output is correct |
44 |
Correct |
623 ms |
126428 KB |
Output is correct |
45 |
Correct |
591 ms |
126480 KB |
Output is correct |
46 |
Correct |
574 ms |
126392 KB |
Output is correct |
47 |
Correct |
382 ms |
125916 KB |
Output is correct |
48 |
Correct |
366 ms |
125788 KB |
Output is correct |
49 |
Correct |
391 ms |
126044 KB |
Output is correct |
50 |
Correct |
416 ms |
126300 KB |
Output is correct |
51 |
Correct |
418 ms |
126172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1219 ms |
400960 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1333 ms |
381752 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113132 KB |
Output is correct |
2 |
Correct |
75 ms |
113152 KB |
Output is correct |
3 |
Correct |
84 ms |
113132 KB |
Output is correct |
4 |
Correct |
77 ms |
113024 KB |
Output is correct |
5 |
Correct |
78 ms |
113132 KB |
Output is correct |
6 |
Correct |
78 ms |
113260 KB |
Output is correct |
7 |
Correct |
87 ms |
113260 KB |
Output is correct |
8 |
Correct |
80 ms |
113260 KB |
Output is correct |
9 |
Correct |
78 ms |
113296 KB |
Output is correct |
10 |
Correct |
80 ms |
113388 KB |
Output is correct |
11 |
Correct |
80 ms |
113132 KB |
Output is correct |
12 |
Correct |
76 ms |
113260 KB |
Output is correct |
13 |
Correct |
76 ms |
113260 KB |
Output is correct |
14 |
Correct |
76 ms |
113132 KB |
Output is correct |
15 |
Correct |
78 ms |
113260 KB |
Output is correct |
16 |
Correct |
88 ms |
113260 KB |
Output is correct |
17 |
Correct |
77 ms |
113260 KB |
Output is correct |
18 |
Correct |
81 ms |
113260 KB |
Output is correct |
19 |
Correct |
80 ms |
113260 KB |
Output is correct |
20 |
Correct |
79 ms |
113336 KB |
Output is correct |
21 |
Correct |
76 ms |
113280 KB |
Output is correct |
22 |
Correct |
80 ms |
113388 KB |
Output is correct |
23 |
Correct |
78 ms |
113280 KB |
Output is correct |
24 |
Correct |
78 ms |
113260 KB |
Output is correct |
25 |
Correct |
77 ms |
113280 KB |
Output is correct |
26 |
Correct |
76 ms |
113132 KB |
Output is correct |
27 |
Correct |
75 ms |
113132 KB |
Output is correct |
28 |
Correct |
77 ms |
113232 KB |
Output is correct |
29 |
Correct |
76 ms |
113260 KB |
Output is correct |
30 |
Correct |
75 ms |
113132 KB |
Output is correct |
31 |
Correct |
966 ms |
136796 KB |
Output is correct |
32 |
Correct |
205 ms |
119292 KB |
Output is correct |
33 |
Correct |
889 ms |
129344 KB |
Output is correct |
34 |
Correct |
882 ms |
129628 KB |
Output is correct |
35 |
Correct |
984 ms |
136604 KB |
Output is correct |
36 |
Correct |
965 ms |
136512 KB |
Output is correct |
37 |
Correct |
639 ms |
126544 KB |
Output is correct |
38 |
Correct |
652 ms |
126428 KB |
Output is correct |
39 |
Correct |
484 ms |
126136 KB |
Output is correct |
40 |
Correct |
498 ms |
126128 KB |
Output is correct |
41 |
Correct |
620 ms |
126508 KB |
Output is correct |
42 |
Correct |
616 ms |
126428 KB |
Output is correct |
43 |
Correct |
166 ms |
122972 KB |
Output is correct |
44 |
Correct |
623 ms |
126428 KB |
Output is correct |
45 |
Correct |
591 ms |
126480 KB |
Output is correct |
46 |
Correct |
574 ms |
126392 KB |
Output is correct |
47 |
Correct |
382 ms |
125916 KB |
Output is correct |
48 |
Correct |
366 ms |
125788 KB |
Output is correct |
49 |
Correct |
391 ms |
126044 KB |
Output is correct |
50 |
Correct |
416 ms |
126300 KB |
Output is correct |
51 |
Correct |
418 ms |
126172 KB |
Output is correct |
52 |
Correct |
770 ms |
151340 KB |
Output is correct |
53 |
Correct |
770 ms |
144220 KB |
Output is correct |
54 |
Correct |
791 ms |
142204 KB |
Output is correct |
55 |
Correct |
690 ms |
135096 KB |
Output is correct |
56 |
Correct |
698 ms |
139612 KB |
Output is correct |
57 |
Correct |
647 ms |
128604 KB |
Output is correct |
58 |
Correct |
692 ms |
135132 KB |
Output is correct |
59 |
Correct |
703 ms |
139612 KB |
Output is correct |
60 |
Correct |
637 ms |
128348 KB |
Output is correct |
61 |
Correct |
400 ms |
148956 KB |
Output is correct |
62 |
Correct |
774 ms |
151644 KB |
Output is correct |
63 |
Correct |
802 ms |
143068 KB |
Output is correct |
64 |
Correct |
826 ms |
138584 KB |
Output is correct |
65 |
Correct |
783 ms |
129908 KB |
Output is correct |
66 |
Correct |
681 ms |
126812 KB |
Output is correct |
67 |
Correct |
524 ms |
123484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
113132 KB |
Output is correct |
2 |
Correct |
75 ms |
113152 KB |
Output is correct |
3 |
Correct |
84 ms |
113132 KB |
Output is correct |
4 |
Correct |
77 ms |
113024 KB |
Output is correct |
5 |
Correct |
78 ms |
113132 KB |
Output is correct |
6 |
Correct |
78 ms |
113260 KB |
Output is correct |
7 |
Correct |
87 ms |
113260 KB |
Output is correct |
8 |
Correct |
80 ms |
113260 KB |
Output is correct |
9 |
Correct |
78 ms |
113296 KB |
Output is correct |
10 |
Correct |
80 ms |
113388 KB |
Output is correct |
11 |
Correct |
80 ms |
113132 KB |
Output is correct |
12 |
Correct |
76 ms |
113260 KB |
Output is correct |
13 |
Correct |
76 ms |
113260 KB |
Output is correct |
14 |
Correct |
76 ms |
113132 KB |
Output is correct |
15 |
Correct |
78 ms |
113260 KB |
Output is correct |
16 |
Correct |
88 ms |
113260 KB |
Output is correct |
17 |
Correct |
77 ms |
113260 KB |
Output is correct |
18 |
Correct |
81 ms |
113260 KB |
Output is correct |
19 |
Correct |
80 ms |
113260 KB |
Output is correct |
20 |
Correct |
79 ms |
113336 KB |
Output is correct |
21 |
Correct |
76 ms |
113280 KB |
Output is correct |
22 |
Correct |
80 ms |
113388 KB |
Output is correct |
23 |
Correct |
78 ms |
113280 KB |
Output is correct |
24 |
Correct |
78 ms |
113260 KB |
Output is correct |
25 |
Correct |
77 ms |
113280 KB |
Output is correct |
26 |
Correct |
76 ms |
113132 KB |
Output is correct |
27 |
Correct |
75 ms |
113132 KB |
Output is correct |
28 |
Correct |
77 ms |
113232 KB |
Output is correct |
29 |
Correct |
76 ms |
113260 KB |
Output is correct |
30 |
Correct |
75 ms |
113132 KB |
Output is correct |
31 |
Correct |
966 ms |
136796 KB |
Output is correct |
32 |
Correct |
205 ms |
119292 KB |
Output is correct |
33 |
Correct |
889 ms |
129344 KB |
Output is correct |
34 |
Correct |
882 ms |
129628 KB |
Output is correct |
35 |
Correct |
984 ms |
136604 KB |
Output is correct |
36 |
Correct |
965 ms |
136512 KB |
Output is correct |
37 |
Correct |
639 ms |
126544 KB |
Output is correct |
38 |
Correct |
652 ms |
126428 KB |
Output is correct |
39 |
Correct |
484 ms |
126136 KB |
Output is correct |
40 |
Correct |
498 ms |
126128 KB |
Output is correct |
41 |
Correct |
620 ms |
126508 KB |
Output is correct |
42 |
Correct |
616 ms |
126428 KB |
Output is correct |
43 |
Correct |
166 ms |
122972 KB |
Output is correct |
44 |
Correct |
623 ms |
126428 KB |
Output is correct |
45 |
Correct |
591 ms |
126480 KB |
Output is correct |
46 |
Correct |
574 ms |
126392 KB |
Output is correct |
47 |
Correct |
382 ms |
125916 KB |
Output is correct |
48 |
Correct |
366 ms |
125788 KB |
Output is correct |
49 |
Correct |
391 ms |
126044 KB |
Output is correct |
50 |
Correct |
416 ms |
126300 KB |
Output is correct |
51 |
Correct |
418 ms |
126172 KB |
Output is correct |
52 |
Runtime error |
1219 ms |
400960 KB |
Execution killed with signal 11 |
53 |
Halted |
0 ms |
0 KB |
- |