#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 inff = inf + 1;
struct IT{
struct node{
node *l, *r;
multiset<int> s1, s2, s3, s4;
int m1, m2, m3, m4;
node(){
l = r = 0;
m1 = m3 = inff; m2 = m4 = 0;
}
};
node *rt;
void init(){
rt = new node();
}
void recalc(node *v){
v -> m1 = v -> m3 = inff; v -> m2 = v -> m4 = 0;
if (v -> l){
v -> m1 = min(v -> m1, v -> l -> m1);
v -> m2 = max(v -> m2, v -> l -> m2);
v -> m3 = min(v -> m3, v -> l -> m3);
v -> m4 = max(v -> m4, v -> l -> m4);
}
if (v -> r){
v -> m1 = min(v -> m1, v -> r -> m1);
v -> m2 = max(v -> m2, v -> r -> m2);
v -> m3 = min(v -> m3, v -> r -> m3);
v -> m4 = max(v -> m4, v -> r -> m4);
}
}
void add1(node *v, int tl, int tr, int p, int x, bool t){
if (tl == tr){
if (t){
(v -> s4).insert(x);
v -> m4 = max(v -> m4, x);
}
else{
(v -> s3).insert(x);
v -> m3 = min(v -> m3, x);
}
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!(v -> l)) v -> l = new node();
add1(v -> l, tl, tm, p, x, t);
}
else {
if (!(v -> r)) v -> r = new node();
add1(v -> r, tm + 1, tr, p, x, t);
}
recalc(v);
}
void add2(node *v, int tl, int tr, int p, int x, bool t){
if (tl == tr){
if (t){
(v -> s2).insert(x);
v -> m2 = max(v -> m2, x);
}
else{
(v -> s1).insert(x);
v -> m1 = min(v -> m1, x);
}
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!(v -> l)) v -> l = new node();
add2(v -> l, tl, tm, p, x, t);
}
else {
if (!(v -> r)) v -> r = new node();
add2(v -> r, tm + 1, tr, p, x, t);
}
recalc(v);
}
void add(int l, int r){
if (l == 1){
add1(rt, 1, inf, r, r + 1, 1);
return;
}
if (r == inf){
add1(rt, 1, inf, l, l - 1, 0);
return;
}
int m = (l + r) / 2;
if (l <= m) add2(rt, 1, inf, m, l - 1, 0);
if (m < r) add2(rt, 1, inf, m + 1, r + 1, 1);
}
void rem1(node *v, int tl, int tr, int p, int x, bool t){
if (tl == tr){
if (t){
(v -> s4).erase((v -> s4).find(x));
v -> m4 = (v -> s4).empty() ? 0 : *prev((v -> s4).end());
}
else{
(v -> s3).erase((v -> s3).find(x));
v -> m3 = (v -> s3).empty() ? inff : *(v -> s3).begin();
}
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
rem1(v -> l, tl, tm, p, x, t);
}
else {
rem1(v -> r, tm + 1, tr, p, x, t);
}
recalc(v);
}
void rem2(node *v, int tl, int tr, int p, int x, bool t){
if (tl == tr){
if (t){
(v -> s2).erase((v -> s2).find(x));
v -> m2 = (v -> s2).empty() ? 0 : *prev((v -> s2).end());
}
else{
(v -> s1).erase((v -> s1).find(x));
v -> m1 = (v -> s1).empty() ? inff : *(v -> s1).begin();
}
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
rem2(v -> l, tl, tm, p, x, t);
}
else {
rem2(v -> r, tm + 1, tr, p, x, t);
}
recalc(v);
}
void rem(int l, int r){
if (l == 1){
rem1(rt, 1, inf, r, r + 1, 1);
return;
}
if (r == inf){
rem1(rt, 1, inf, l, l - 1, 0);
return;
}
int m = (l + r) / 2;
if (l <= m) rem2(rt, 1, inf, m, l - 1, 0);
if (m < r) rem2(rt, 1, inf, m + 1, r + 1, 1);
}
int get1(node *v, int tl, int tr, int l, int r, bool t){
if (l > tr || r < tl) return inff * !t;
if (l <= tl && tr <= r) return (t) ? (v -> m2) : (v -> m1);
int tm = (tl + tr) / 2, out = inff * !t;
if (v -> l){
if (t){
out = max(out, get1(v -> l, tl, tm, l, r, t));
}
else {
out = min(out, get1(v -> l, tl, tm, l, r, t));
}
}
if (v -> r){
if (t){
out = max(out, get1(v -> r, tm + 1, tr, l, r, t));
}
else {
out = min(out, get1(v -> r, tm + 1, tr, l, r, t));
}
}
return out;
}
int get2(node *v, int tl, int tr, int l, int r, bool t){
if (l > tr || r < tl) return inff * !t;
if (l <= tl && tr <= r) return (t) ? (v -> m4) : (v -> m3);
int tm = (tl + tr) / 2, out = inff * !t;
if (v -> l){
if (t){
out = max(out, get2(v -> l, tl, tm, l, r, t));
}
else {
out = min(out, get2(v -> l, tl, tm, l, r, t));
}
}
if (v -> r){
if (t){
out = max(out, get2(v -> r, tm + 1, tr, l, r, t));
}
else {
out = min(out, get2(v -> r, tm + 1, tr, l, r, t));
}
}
return out;
}
pii get(int x){
return {min(get1(rt, 1, inf, x, inf, 0), get2(rt, 1, inf, 1, x, 0)), max(get1(rt, 1, inf, 1, x, 1), get2(rt, 1, inf, x, inf, 1))};
}
};
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 [l, r] = T.get(i);
if (l != inff) out[ii] = max(out[ii], i - l);
if (r != 0) out[ii] = max(out[ii], r - i);
}
}
for (int i = 1; i <= q; i++){
cout<<out[i]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
764 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
6 ms |
5200 KB |
Output is correct |
7 |
Correct |
3 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
3920 KB |
Output is correct |
9 |
Correct |
3 ms |
2640 KB |
Output is correct |
10 |
Correct |
7 ms |
5456 KB |
Output is correct |
11 |
Correct |
4 ms |
3664 KB |
Output is correct |
12 |
Correct |
6 ms |
5200 KB |
Output is correct |
13 |
Correct |
3 ms |
2896 KB |
Output is correct |
14 |
Correct |
4 ms |
3664 KB |
Output is correct |
15 |
Correct |
5 ms |
3664 KB |
Output is correct |
16 |
Correct |
5 ms |
3408 KB |
Output is correct |
17 |
Correct |
6 ms |
4432 KB |
Output is correct |
18 |
Correct |
5 ms |
3920 KB |
Output is correct |
19 |
Correct |
5 ms |
3408 KB |
Output is correct |
20 |
Correct |
5 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
3 ms |
2640 KB |
Output is correct |
23 |
Correct |
4 ms |
3408 KB |
Output is correct |
24 |
Correct |
5 ms |
3920 KB |
Output is correct |
25 |
Correct |
5 ms |
4432 KB |
Output is correct |
26 |
Correct |
6 ms |
4688 KB |
Output is correct |
27 |
Correct |
2 ms |
368 KB |
Output is correct |
28 |
Correct |
5 ms |
3920 KB |
Output is correct |
29 |
Correct |
5 ms |
3920 KB |
Output is correct |
30 |
Correct |
3 ms |
2896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
764 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
6 ms |
5200 KB |
Output is correct |
7 |
Correct |
3 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
3920 KB |
Output is correct |
9 |
Correct |
3 ms |
2640 KB |
Output is correct |
10 |
Correct |
7 ms |
5456 KB |
Output is correct |
11 |
Correct |
4 ms |
3664 KB |
Output is correct |
12 |
Correct |
6 ms |
5200 KB |
Output is correct |
13 |
Correct |
3 ms |
2896 KB |
Output is correct |
14 |
Correct |
4 ms |
3664 KB |
Output is correct |
15 |
Correct |
5 ms |
3664 KB |
Output is correct |
16 |
Correct |
5 ms |
3408 KB |
Output is correct |
17 |
Correct |
6 ms |
4432 KB |
Output is correct |
18 |
Correct |
5 ms |
3920 KB |
Output is correct |
19 |
Correct |
5 ms |
3408 KB |
Output is correct |
20 |
Correct |
5 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
3 ms |
2640 KB |
Output is correct |
23 |
Correct |
4 ms |
3408 KB |
Output is correct |
24 |
Correct |
5 ms |
3920 KB |
Output is correct |
25 |
Correct |
5 ms |
4432 KB |
Output is correct |
26 |
Correct |
6 ms |
4688 KB |
Output is correct |
27 |
Correct |
2 ms |
368 KB |
Output is correct |
28 |
Correct |
5 ms |
3920 KB |
Output is correct |
29 |
Correct |
5 ms |
3920 KB |
Output is correct |
30 |
Correct |
3 ms |
2896 KB |
Output is correct |
31 |
Correct |
1246 ms |
501308 KB |
Output is correct |
32 |
Correct |
269 ms |
5572 KB |
Output is correct |
33 |
Correct |
1164 ms |
494004 KB |
Output is correct |
34 |
Correct |
1226 ms |
485392 KB |
Output is correct |
35 |
Correct |
1255 ms |
505524 KB |
Output is correct |
36 |
Correct |
1132 ms |
507620 KB |
Output is correct |
37 |
Correct |
882 ms |
489140 KB |
Output is correct |
38 |
Correct |
990 ms |
490876 KB |
Output is correct |
39 |
Correct |
744 ms |
485308 KB |
Output is correct |
40 |
Correct |
772 ms |
488632 KB |
Output is correct |
41 |
Correct |
789 ms |
416180 KB |
Output is correct |
42 |
Correct |
789 ms |
417204 KB |
Output is correct |
43 |
Correct |
42 ms |
6844 KB |
Output is correct |
44 |
Correct |
864 ms |
417520 KB |
Output is correct |
45 |
Correct |
764 ms |
417972 KB |
Output is correct |
46 |
Correct |
667 ms |
418172 KB |
Output is correct |
47 |
Correct |
444 ms |
362940 KB |
Output is correct |
48 |
Correct |
459 ms |
377444 KB |
Output is correct |
49 |
Correct |
521 ms |
398636 KB |
Output is correct |
50 |
Correct |
592 ms |
392068 KB |
Output is correct |
51 |
Correct |
579 ms |
402768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2095 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
2187 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
764 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
6 ms |
5200 KB |
Output is correct |
7 |
Correct |
3 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
3920 KB |
Output is correct |
9 |
Correct |
3 ms |
2640 KB |
Output is correct |
10 |
Correct |
7 ms |
5456 KB |
Output is correct |
11 |
Correct |
4 ms |
3664 KB |
Output is correct |
12 |
Correct |
6 ms |
5200 KB |
Output is correct |
13 |
Correct |
3 ms |
2896 KB |
Output is correct |
14 |
Correct |
4 ms |
3664 KB |
Output is correct |
15 |
Correct |
5 ms |
3664 KB |
Output is correct |
16 |
Correct |
5 ms |
3408 KB |
Output is correct |
17 |
Correct |
6 ms |
4432 KB |
Output is correct |
18 |
Correct |
5 ms |
3920 KB |
Output is correct |
19 |
Correct |
5 ms |
3408 KB |
Output is correct |
20 |
Correct |
5 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
3 ms |
2640 KB |
Output is correct |
23 |
Correct |
4 ms |
3408 KB |
Output is correct |
24 |
Correct |
5 ms |
3920 KB |
Output is correct |
25 |
Correct |
5 ms |
4432 KB |
Output is correct |
26 |
Correct |
6 ms |
4688 KB |
Output is correct |
27 |
Correct |
2 ms |
368 KB |
Output is correct |
28 |
Correct |
5 ms |
3920 KB |
Output is correct |
29 |
Correct |
5 ms |
3920 KB |
Output is correct |
30 |
Correct |
3 ms |
2896 KB |
Output is correct |
31 |
Correct |
1246 ms |
501308 KB |
Output is correct |
32 |
Correct |
269 ms |
5572 KB |
Output is correct |
33 |
Correct |
1164 ms |
494004 KB |
Output is correct |
34 |
Correct |
1226 ms |
485392 KB |
Output is correct |
35 |
Correct |
1255 ms |
505524 KB |
Output is correct |
36 |
Correct |
1132 ms |
507620 KB |
Output is correct |
37 |
Correct |
882 ms |
489140 KB |
Output is correct |
38 |
Correct |
990 ms |
490876 KB |
Output is correct |
39 |
Correct |
744 ms |
485308 KB |
Output is correct |
40 |
Correct |
772 ms |
488632 KB |
Output is correct |
41 |
Correct |
789 ms |
416180 KB |
Output is correct |
42 |
Correct |
789 ms |
417204 KB |
Output is correct |
43 |
Correct |
42 ms |
6844 KB |
Output is correct |
44 |
Correct |
864 ms |
417520 KB |
Output is correct |
45 |
Correct |
764 ms |
417972 KB |
Output is correct |
46 |
Correct |
667 ms |
418172 KB |
Output is correct |
47 |
Correct |
444 ms |
362940 KB |
Output is correct |
48 |
Correct |
459 ms |
377444 KB |
Output is correct |
49 |
Correct |
521 ms |
398636 KB |
Output is correct |
50 |
Correct |
592 ms |
392068 KB |
Output is correct |
51 |
Correct |
579 ms |
402768 KB |
Output is correct |
52 |
Correct |
412 ms |
225276 KB |
Output is correct |
53 |
Correct |
405 ms |
220340 KB |
Output is correct |
54 |
Correct |
848 ms |
354868 KB |
Output is correct |
55 |
Correct |
727 ms |
352796 KB |
Output is correct |
56 |
Correct |
638 ms |
318644 KB |
Output is correct |
57 |
Correct |
778 ms |
397492 KB |
Output is correct |
58 |
Correct |
707 ms |
352560 KB |
Output is correct |
59 |
Correct |
663 ms |
318776 KB |
Output is correct |
60 |
Correct |
784 ms |
398116 KB |
Output is correct |
61 |
Correct |
56 ms |
12520 KB |
Output is correct |
62 |
Correct |
464 ms |
225556 KB |
Output is correct |
63 |
Correct |
678 ms |
314044 KB |
Output is correct |
64 |
Correct |
791 ms |
346292 KB |
Output is correct |
65 |
Correct |
875 ms |
394492 KB |
Output is correct |
66 |
Correct |
822 ms |
416252 KB |
Output is correct |
67 |
Correct |
216 ms |
6580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
764 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
6 ms |
5200 KB |
Output is correct |
7 |
Correct |
3 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
3920 KB |
Output is correct |
9 |
Correct |
3 ms |
2640 KB |
Output is correct |
10 |
Correct |
7 ms |
5456 KB |
Output is correct |
11 |
Correct |
4 ms |
3664 KB |
Output is correct |
12 |
Correct |
6 ms |
5200 KB |
Output is correct |
13 |
Correct |
3 ms |
2896 KB |
Output is correct |
14 |
Correct |
4 ms |
3664 KB |
Output is correct |
15 |
Correct |
5 ms |
3664 KB |
Output is correct |
16 |
Correct |
5 ms |
3408 KB |
Output is correct |
17 |
Correct |
6 ms |
4432 KB |
Output is correct |
18 |
Correct |
5 ms |
3920 KB |
Output is correct |
19 |
Correct |
5 ms |
3408 KB |
Output is correct |
20 |
Correct |
5 ms |
4432 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Correct |
3 ms |
2640 KB |
Output is correct |
23 |
Correct |
4 ms |
3408 KB |
Output is correct |
24 |
Correct |
5 ms |
3920 KB |
Output is correct |
25 |
Correct |
5 ms |
4432 KB |
Output is correct |
26 |
Correct |
6 ms |
4688 KB |
Output is correct |
27 |
Correct |
2 ms |
368 KB |
Output is correct |
28 |
Correct |
5 ms |
3920 KB |
Output is correct |
29 |
Correct |
5 ms |
3920 KB |
Output is correct |
30 |
Correct |
3 ms |
2896 KB |
Output is correct |
31 |
Correct |
1246 ms |
501308 KB |
Output is correct |
32 |
Correct |
269 ms |
5572 KB |
Output is correct |
33 |
Correct |
1164 ms |
494004 KB |
Output is correct |
34 |
Correct |
1226 ms |
485392 KB |
Output is correct |
35 |
Correct |
1255 ms |
505524 KB |
Output is correct |
36 |
Correct |
1132 ms |
507620 KB |
Output is correct |
37 |
Correct |
882 ms |
489140 KB |
Output is correct |
38 |
Correct |
990 ms |
490876 KB |
Output is correct |
39 |
Correct |
744 ms |
485308 KB |
Output is correct |
40 |
Correct |
772 ms |
488632 KB |
Output is correct |
41 |
Correct |
789 ms |
416180 KB |
Output is correct |
42 |
Correct |
789 ms |
417204 KB |
Output is correct |
43 |
Correct |
42 ms |
6844 KB |
Output is correct |
44 |
Correct |
864 ms |
417520 KB |
Output is correct |
45 |
Correct |
764 ms |
417972 KB |
Output is correct |
46 |
Correct |
667 ms |
418172 KB |
Output is correct |
47 |
Correct |
444 ms |
362940 KB |
Output is correct |
48 |
Correct |
459 ms |
377444 KB |
Output is correct |
49 |
Correct |
521 ms |
398636 KB |
Output is correct |
50 |
Correct |
592 ms |
392068 KB |
Output is correct |
51 |
Correct |
579 ms |
402768 KB |
Output is correct |
52 |
Runtime error |
2095 ms |
1048576 KB |
Execution killed with signal 9 |
53 |
Halted |
0 ms |
0 KB |
- |