#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define arr3 array<int, 3>
const int inf = 1e8;
const int N = 3e5 + 1;
const int NN = 1e7;
struct IT{
struct node{
int m, l, r, s;
node(){
l = r = m = 0;
}
};
node all[NN];
multiset<int> st[N];
int rt, cc, cc1;
void init(){
all[1] = node();
rt = cc = 1;
cc1 = -1;
}
int nw(int tl, int tr){
assert((cc + 1) < NN);
all[++cc] = node();
if (tl == tr){
assert((cc1 + 1) < N);
st[++cc1] = {};
all[cc].s = cc1;
}
return cc;
}
void add(int v, int tl, int tr, int& p, int& x){
if (tl == tr){
st[all[v].s].insert(x);
all[v].m = max(all[v].m, x);
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!all[v].l) all[v].l = nw(tl, tm);
add(all[v].l, tl, tm, p, x);
}
else {
if (!all[v].r) all[v].r = nw(tm + 1, tr);
add(all[v].r, tm + 1, tr, p, x);
}
all[v].m = 0;
if (all[v].l) all[v].m = max(all[v].m, all[all[v].l].m);
if (all[v].r) all[v].m = max(all[v].m, all[all[v].r].m);
}
void add(int l, int r){
add(rt, 1, inf, l, r);
}
void rem(int v, int tl, int tr, int& p, int& x){
if (tl == tr){
st[all[v].s].erase(st[all[v].s].find(x));
all[v].m = (st[all[v].s].empty()) ? 0 : *prev(st[all[v].s].end());
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
rem(all[v].l, tl, tm, p, x);
}
else {
rem(all[v].r, tm + 1, tr, p, x);
}
all[v].m = 0;
if (all[v].l) all[v].m = max(all[v].m, all[all[v].l].m);
if (all[v].r) all[v].m = max(all[v].m, all[all[v].r].m);
}
void rem(int l, int r){
rem(rt, 1, inf, l, r);
}
int get(int v, int tl, int tr, int l, int r){
if (l > tr || r < tl) return 0;
if (l <= tl && tr <= r) return all[v].m;
int tm = (tl + tr) / 2, out = 0;
if (all[v].l) out = max(out, get(all[v].l, tl, tm, l, r));
if (all[v].r) out = max(out, get(all[v].r, tm + 1, tr, l, r));
return out;
}
bool get(int l, int r){
return get(rt, 1, inf, 1, l) >= r;
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k, q; cin>>n>>k>>q;
vector<int> x(n + 1), t(n + 1), a(n + 1), b(n + 1);
for (int i = 1; i <= n; i++){
cin>>x[i]>>t[i]>>a[i]>>b[i];
}
vector<int> xq(q + 1), yq(q + 1);
for (int i = 1; i <= q; i++){
cin>>xq[i]>>yq[i];
}
vector<arr3> mp;
for (int i = 1; i <= n; i++){
mp.pb({a[i], 1, i});
if (b[i] < inf) mp.pb({b[i] + 1, 2, i});
}
for (int i = 1; i <= q; i++){
mp.pb({yq[i], i + 2, xq[i]});
}
auto cmp = [&](arr3 x, arr3 y){
if (x[0] == y[0]){
return x[1] < y[1];
}
return x[0] < y[0];
};
sort(mp.begin(), mp.end(), cmp);
vector<int> out(q + 1), cnt(k + 1);
multiset<int> st[k + 1];
int df = 0, l, r;
IT T; T.init();
for (auto [X, tt, i]: mp){
if (tt == 1){
df += !cnt[t[i]];
cnt[t[i]]++;
if (cnt[t[i]] == 1){
if (1 < x[i]) T.add(1, x[i] - 1);
if (x[i] < inf) T.add(x[i] + 1, inf);
st[t[i]].insert(x[i]);
continue;
}
if (st[t[i]].find(x[i]) != st[t[i]].end()){
st[t[i]].insert(x[i]);
continue;
}
auto it = st[t[i]].upper_bound(x[i]);
if (it == st[t[i]].end()) r = inf;
else r = (*it) - 1;
if (it == st[t[i]].begin()) l = 1;
else l = *prev(it) + 1;
if (l <= r) T.rem(l, r);
if (l < x[i]) T.add(l, x[i] - 1);
if (x[i] < r) T.add(x[i] + 1, r);
st[t[i]].insert(x[i]);
}
else if (tt == 2){
cnt[t[i]]--;
df -= !cnt[t[i]];
if (st[t[i]].count(x[i]) > 1){
st[t[i]].erase(st[t[i]].find(x[i]));
continue;
}
auto it = st[t[i]].find(x[i]);
if (it == st[t[i]].begin()) l = 1;
else l = *prev(it) + 1;
if (it == prev(st[t[i]].end())) r = inf;
else r = *next(it) - 1;
if (l < x[i]) T.rem(l, x[i] - 1);
if (x[i] < r) T.rem(x[i] + 1, r);
if (cnt[t[i]]) T.add(l, r);
st[t[i]].erase(it);
}
else {
int ii = tt - 2;
if (df < k){
out[ii] = -1;
continue;
}
auto check = [&](int d){
return T.get(max(1, i - d), min(inf, i + d));
};
int l = 0, r = inf;
while (l + 1 < r){
int m = (l + r) / 2;
if (check(m)){
l = m + 1;
}
else {
r = m;
}
}
if (check(l)) l = r;
out[ii] = l;
}
}
for (int i = 1; i <= q; i++){
cout<<out[i]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
171080 KB |
Output is correct |
2 |
Correct |
154 ms |
171084 KB |
Output is correct |
3 |
Correct |
155 ms |
171000 KB |
Output is correct |
4 |
Correct |
154 ms |
171084 KB |
Output is correct |
5 |
Correct |
154 ms |
171100 KB |
Output is correct |
6 |
Correct |
157 ms |
170916 KB |
Output is correct |
7 |
Correct |
152 ms |
171084 KB |
Output is correct |
8 |
Correct |
155 ms |
171080 KB |
Output is correct |
9 |
Correct |
148 ms |
171084 KB |
Output is correct |
10 |
Correct |
148 ms |
170936 KB |
Output is correct |
11 |
Correct |
159 ms |
171136 KB |
Output is correct |
12 |
Correct |
154 ms |
171080 KB |
Output is correct |
13 |
Correct |
148 ms |
171340 KB |
Output is correct |
14 |
Correct |
148 ms |
171092 KB |
Output is correct |
15 |
Correct |
143 ms |
171080 KB |
Output is correct |
16 |
Correct |
137 ms |
171092 KB |
Output is correct |
17 |
Correct |
138 ms |
171096 KB |
Output is correct |
18 |
Correct |
140 ms |
171072 KB |
Output is correct |
19 |
Correct |
140 ms |
171152 KB |
Output is correct |
20 |
Correct |
138 ms |
171012 KB |
Output is correct |
21 |
Correct |
138 ms |
171080 KB |
Output is correct |
22 |
Correct |
143 ms |
171080 KB |
Output is correct |
23 |
Correct |
137 ms |
171080 KB |
Output is correct |
24 |
Correct |
144 ms |
171080 KB |
Output is correct |
25 |
Correct |
138 ms |
171092 KB |
Output is correct |
26 |
Correct |
143 ms |
171080 KB |
Output is correct |
27 |
Correct |
153 ms |
170968 KB |
Output is correct |
28 |
Correct |
159 ms |
171124 KB |
Output is correct |
29 |
Correct |
149 ms |
171080 KB |
Output is correct |
30 |
Correct |
155 ms |
171080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
171080 KB |
Output is correct |
2 |
Correct |
154 ms |
171084 KB |
Output is correct |
3 |
Correct |
155 ms |
171000 KB |
Output is correct |
4 |
Correct |
154 ms |
171084 KB |
Output is correct |
5 |
Correct |
154 ms |
171100 KB |
Output is correct |
6 |
Correct |
157 ms |
170916 KB |
Output is correct |
7 |
Correct |
152 ms |
171084 KB |
Output is correct |
8 |
Correct |
155 ms |
171080 KB |
Output is correct |
9 |
Correct |
148 ms |
171084 KB |
Output is correct |
10 |
Correct |
148 ms |
170936 KB |
Output is correct |
11 |
Correct |
159 ms |
171136 KB |
Output is correct |
12 |
Correct |
154 ms |
171080 KB |
Output is correct |
13 |
Correct |
148 ms |
171340 KB |
Output is correct |
14 |
Correct |
148 ms |
171092 KB |
Output is correct |
15 |
Correct |
143 ms |
171080 KB |
Output is correct |
16 |
Correct |
137 ms |
171092 KB |
Output is correct |
17 |
Correct |
138 ms |
171096 KB |
Output is correct |
18 |
Correct |
140 ms |
171072 KB |
Output is correct |
19 |
Correct |
140 ms |
171152 KB |
Output is correct |
20 |
Correct |
138 ms |
171012 KB |
Output is correct |
21 |
Correct |
138 ms |
171080 KB |
Output is correct |
22 |
Correct |
143 ms |
171080 KB |
Output is correct |
23 |
Correct |
137 ms |
171080 KB |
Output is correct |
24 |
Correct |
144 ms |
171080 KB |
Output is correct |
25 |
Correct |
138 ms |
171092 KB |
Output is correct |
26 |
Correct |
143 ms |
171080 KB |
Output is correct |
27 |
Correct |
153 ms |
170968 KB |
Output is correct |
28 |
Correct |
159 ms |
171124 KB |
Output is correct |
29 |
Correct |
149 ms |
171080 KB |
Output is correct |
30 |
Correct |
155 ms |
171080 KB |
Output is correct |
31 |
Correct |
655 ms |
180916 KB |
Output is correct |
32 |
Correct |
576 ms |
176196 KB |
Output is correct |
33 |
Correct |
603 ms |
177316 KB |
Output is correct |
34 |
Correct |
584 ms |
177252 KB |
Output is correct |
35 |
Correct |
632 ms |
180804 KB |
Output is correct |
36 |
Correct |
667 ms |
180664 KB |
Output is correct |
37 |
Correct |
541 ms |
176076 KB |
Output is correct |
38 |
Correct |
549 ms |
176140 KB |
Output is correct |
39 |
Correct |
489 ms |
176316 KB |
Output is correct |
40 |
Correct |
509 ms |
176316 KB |
Output is correct |
41 |
Correct |
509 ms |
176060 KB |
Output is correct |
42 |
Correct |
426 ms |
176060 KB |
Output is correct |
43 |
Correct |
363 ms |
177596 KB |
Output is correct |
44 |
Correct |
469 ms |
176104 KB |
Output is correct |
45 |
Correct |
474 ms |
176072 KB |
Output is correct |
46 |
Correct |
467 ms |
176060 KB |
Output is correct |
47 |
Correct |
356 ms |
176060 KB |
Output is correct |
48 |
Correct |
398 ms |
176152 KB |
Output is correct |
49 |
Correct |
424 ms |
176060 KB |
Output is correct |
50 |
Correct |
391 ms |
176084 KB |
Output is correct |
51 |
Correct |
455 ms |
175952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2098 ms |
224128 KB |
Output is correct |
2 |
Correct |
2932 ms |
216984 KB |
Output is correct |
3 |
Correct |
1996 ms |
247620 KB |
Output is correct |
4 |
Correct |
1903 ms |
228176 KB |
Output is correct |
5 |
Correct |
2876 ms |
216484 KB |
Output is correct |
6 |
Correct |
3003 ms |
216996 KB |
Output is correct |
7 |
Correct |
2055 ms |
247616 KB |
Output is correct |
8 |
Correct |
1831 ms |
228188 KB |
Output is correct |
9 |
Correct |
1800 ms |
221144 KB |
Output is correct |
10 |
Correct |
2610 ms |
217784 KB |
Output is correct |
11 |
Correct |
1963 ms |
216596 KB |
Output is correct |
12 |
Correct |
2021 ms |
217504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3260 ms |
222704 KB |
Output is correct |
2 |
Execution timed out |
5061 ms |
205864 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
171080 KB |
Output is correct |
2 |
Correct |
154 ms |
171084 KB |
Output is correct |
3 |
Correct |
155 ms |
171000 KB |
Output is correct |
4 |
Correct |
154 ms |
171084 KB |
Output is correct |
5 |
Correct |
154 ms |
171100 KB |
Output is correct |
6 |
Correct |
157 ms |
170916 KB |
Output is correct |
7 |
Correct |
152 ms |
171084 KB |
Output is correct |
8 |
Correct |
155 ms |
171080 KB |
Output is correct |
9 |
Correct |
148 ms |
171084 KB |
Output is correct |
10 |
Correct |
148 ms |
170936 KB |
Output is correct |
11 |
Correct |
159 ms |
171136 KB |
Output is correct |
12 |
Correct |
154 ms |
171080 KB |
Output is correct |
13 |
Correct |
148 ms |
171340 KB |
Output is correct |
14 |
Correct |
148 ms |
171092 KB |
Output is correct |
15 |
Correct |
143 ms |
171080 KB |
Output is correct |
16 |
Correct |
137 ms |
171092 KB |
Output is correct |
17 |
Correct |
138 ms |
171096 KB |
Output is correct |
18 |
Correct |
140 ms |
171072 KB |
Output is correct |
19 |
Correct |
140 ms |
171152 KB |
Output is correct |
20 |
Correct |
138 ms |
171012 KB |
Output is correct |
21 |
Correct |
138 ms |
171080 KB |
Output is correct |
22 |
Correct |
143 ms |
171080 KB |
Output is correct |
23 |
Correct |
137 ms |
171080 KB |
Output is correct |
24 |
Correct |
144 ms |
171080 KB |
Output is correct |
25 |
Correct |
138 ms |
171092 KB |
Output is correct |
26 |
Correct |
143 ms |
171080 KB |
Output is correct |
27 |
Correct |
153 ms |
170968 KB |
Output is correct |
28 |
Correct |
159 ms |
171124 KB |
Output is correct |
29 |
Correct |
149 ms |
171080 KB |
Output is correct |
30 |
Correct |
155 ms |
171080 KB |
Output is correct |
31 |
Correct |
655 ms |
180916 KB |
Output is correct |
32 |
Correct |
576 ms |
176196 KB |
Output is correct |
33 |
Correct |
603 ms |
177316 KB |
Output is correct |
34 |
Correct |
584 ms |
177252 KB |
Output is correct |
35 |
Correct |
632 ms |
180804 KB |
Output is correct |
36 |
Correct |
667 ms |
180664 KB |
Output is correct |
37 |
Correct |
541 ms |
176076 KB |
Output is correct |
38 |
Correct |
549 ms |
176140 KB |
Output is correct |
39 |
Correct |
489 ms |
176316 KB |
Output is correct |
40 |
Correct |
509 ms |
176316 KB |
Output is correct |
41 |
Correct |
509 ms |
176060 KB |
Output is correct |
42 |
Correct |
426 ms |
176060 KB |
Output is correct |
43 |
Correct |
363 ms |
177596 KB |
Output is correct |
44 |
Correct |
469 ms |
176104 KB |
Output is correct |
45 |
Correct |
474 ms |
176072 KB |
Output is correct |
46 |
Correct |
467 ms |
176060 KB |
Output is correct |
47 |
Correct |
356 ms |
176060 KB |
Output is correct |
48 |
Correct |
398 ms |
176152 KB |
Output is correct |
49 |
Correct |
424 ms |
176060 KB |
Output is correct |
50 |
Correct |
391 ms |
176084 KB |
Output is correct |
51 |
Correct |
455 ms |
175952 KB |
Output is correct |
52 |
Correct |
355 ms |
186296 KB |
Output is correct |
53 |
Correct |
343 ms |
181176 KB |
Output is correct |
54 |
Correct |
366 ms |
182516 KB |
Output is correct |
55 |
Correct |
484 ms |
179208 KB |
Output is correct |
56 |
Correct |
507 ms |
181172 KB |
Output is correct |
57 |
Correct |
510 ms |
176600 KB |
Output is correct |
58 |
Correct |
456 ms |
179016 KB |
Output is correct |
59 |
Correct |
423 ms |
180796 KB |
Output is correct |
60 |
Correct |
424 ms |
176308 KB |
Output is correct |
61 |
Correct |
352 ms |
183360 KB |
Output is correct |
62 |
Correct |
416 ms |
186556 KB |
Output is correct |
63 |
Correct |
399 ms |
182772 KB |
Output is correct |
64 |
Correct |
420 ms |
180924 KB |
Output is correct |
65 |
Correct |
456 ms |
177076 KB |
Output is correct |
66 |
Correct |
486 ms |
176060 KB |
Output is correct |
67 |
Correct |
380 ms |
176848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
171080 KB |
Output is correct |
2 |
Correct |
154 ms |
171084 KB |
Output is correct |
3 |
Correct |
155 ms |
171000 KB |
Output is correct |
4 |
Correct |
154 ms |
171084 KB |
Output is correct |
5 |
Correct |
154 ms |
171100 KB |
Output is correct |
6 |
Correct |
157 ms |
170916 KB |
Output is correct |
7 |
Correct |
152 ms |
171084 KB |
Output is correct |
8 |
Correct |
155 ms |
171080 KB |
Output is correct |
9 |
Correct |
148 ms |
171084 KB |
Output is correct |
10 |
Correct |
148 ms |
170936 KB |
Output is correct |
11 |
Correct |
159 ms |
171136 KB |
Output is correct |
12 |
Correct |
154 ms |
171080 KB |
Output is correct |
13 |
Correct |
148 ms |
171340 KB |
Output is correct |
14 |
Correct |
148 ms |
171092 KB |
Output is correct |
15 |
Correct |
143 ms |
171080 KB |
Output is correct |
16 |
Correct |
137 ms |
171092 KB |
Output is correct |
17 |
Correct |
138 ms |
171096 KB |
Output is correct |
18 |
Correct |
140 ms |
171072 KB |
Output is correct |
19 |
Correct |
140 ms |
171152 KB |
Output is correct |
20 |
Correct |
138 ms |
171012 KB |
Output is correct |
21 |
Correct |
138 ms |
171080 KB |
Output is correct |
22 |
Correct |
143 ms |
171080 KB |
Output is correct |
23 |
Correct |
137 ms |
171080 KB |
Output is correct |
24 |
Correct |
144 ms |
171080 KB |
Output is correct |
25 |
Correct |
138 ms |
171092 KB |
Output is correct |
26 |
Correct |
143 ms |
171080 KB |
Output is correct |
27 |
Correct |
153 ms |
170968 KB |
Output is correct |
28 |
Correct |
159 ms |
171124 KB |
Output is correct |
29 |
Correct |
149 ms |
171080 KB |
Output is correct |
30 |
Correct |
155 ms |
171080 KB |
Output is correct |
31 |
Correct |
655 ms |
180916 KB |
Output is correct |
32 |
Correct |
576 ms |
176196 KB |
Output is correct |
33 |
Correct |
603 ms |
177316 KB |
Output is correct |
34 |
Correct |
584 ms |
177252 KB |
Output is correct |
35 |
Correct |
632 ms |
180804 KB |
Output is correct |
36 |
Correct |
667 ms |
180664 KB |
Output is correct |
37 |
Correct |
541 ms |
176076 KB |
Output is correct |
38 |
Correct |
549 ms |
176140 KB |
Output is correct |
39 |
Correct |
489 ms |
176316 KB |
Output is correct |
40 |
Correct |
509 ms |
176316 KB |
Output is correct |
41 |
Correct |
509 ms |
176060 KB |
Output is correct |
42 |
Correct |
426 ms |
176060 KB |
Output is correct |
43 |
Correct |
363 ms |
177596 KB |
Output is correct |
44 |
Correct |
469 ms |
176104 KB |
Output is correct |
45 |
Correct |
474 ms |
176072 KB |
Output is correct |
46 |
Correct |
467 ms |
176060 KB |
Output is correct |
47 |
Correct |
356 ms |
176060 KB |
Output is correct |
48 |
Correct |
398 ms |
176152 KB |
Output is correct |
49 |
Correct |
424 ms |
176060 KB |
Output is correct |
50 |
Correct |
391 ms |
176084 KB |
Output is correct |
51 |
Correct |
455 ms |
175952 KB |
Output is correct |
52 |
Correct |
2098 ms |
224128 KB |
Output is correct |
53 |
Correct |
2932 ms |
216984 KB |
Output is correct |
54 |
Correct |
1996 ms |
247620 KB |
Output is correct |
55 |
Correct |
1903 ms |
228176 KB |
Output is correct |
56 |
Correct |
2876 ms |
216484 KB |
Output is correct |
57 |
Correct |
3003 ms |
216996 KB |
Output is correct |
58 |
Correct |
2055 ms |
247616 KB |
Output is correct |
59 |
Correct |
1831 ms |
228188 KB |
Output is correct |
60 |
Correct |
1800 ms |
221144 KB |
Output is correct |
61 |
Correct |
2610 ms |
217784 KB |
Output is correct |
62 |
Correct |
1963 ms |
216596 KB |
Output is correct |
63 |
Correct |
2021 ms |
217504 KB |
Output is correct |
64 |
Correct |
3260 ms |
222704 KB |
Output is correct |
65 |
Execution timed out |
5061 ms |
205864 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |