#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;
int m1, m2;
node(){
l = r = 0;
m1 = inff; m2 = 0;
}
};
node *rt;
void init(){
rt = new node();
}
void add(node *v, int tl, int tr, int l, int r, int x, int y){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
if (x != inff) (v -> s1).insert(x);
if (y != 0) (v -> s2).insert(y);
v -> m1 = min(v -> m1, x);
v -> m2 = max(v -> m2, y);
return;
}
int tm = (tl + tr) / 2;
if (!(v -> l)) v -> l = new node();
if (!(v -> r)) v -> r = new node();
add(v -> l, tl, tm, l, r, x, y);
add(v -> r, tm + 1, tr, l, r, x, y);
}
void add(int l, int r){
if (l == 1){
add(rt, 1, inf, 1, r, inf, r + 1);
return;
}
if (r == inf){
add(rt, 1, inf, l, inf, l - 1, 0);
return;
}
int m = (l + r) / 2;
if (l <= m) add(rt, 1, inf, 1, m, l - 1, 0);
if (m < r) add(rt, 1, inf, m + 1, inf, inf, r + 1);
}
void rem(node *v, int tl, int tr, int l, int r, int x, int y){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
if (x != inff) (v -> s1).erase((v -> s1).find(x));
if (y != 0) (v -> s2).erase((v -> s2).find(y));
v -> m1 = (v -> s1).empty() ? inff : *(v -> s1).begin();
v -> m2 = (v -> s2).empty() ? 0 : *prev(v -> s2.end());
return;
}
int tm = (tl + tr) / 2;
rem(v -> l, tl, tm, l, r, x, y);
rem(v -> r, tm + 1, tr, l, r, x, y);
}
void rem(int l, int r){
if (l == 1){
rem(rt, 1, inf, 1, r, inf, r + 1);
return;
}
if (r == inf){
rem(rt, 1, inf, l, inf, l - 1, 0);
return;
}
int m = (l + r) / 2;
if (l > 1 && l <= m) rem(rt, 1, inf, 1, m, l - 1, 0);
if (r < inf && m < r) rem(rt, 1, inf, m + 1, inf, inf, r + 1);
}
pii merge(pii x, pii y){
return {min(x.ff, y.ff), max(x.ss, y.ss)};
}
pii get(node *v, int tl, int tr, int& x){
if (tl == tr) return {v -> m1, v -> m2};
int tm = (tl + tr) / 2;
if (x <= tm){
if (!(v -> l)) return {v -> m1, v -> m2};
return merge({v -> m1, v -> m2}, get(v -> l, tl, tm, x));
}
else {
if (!(v -> r)) return {v -> m1, v -> m2};
return merge({v -> m1, v -> m2}, get(v -> r, tm + 1, tr, x));
}
}
pii get(int x){
return get(rt, 1, inf, x);
}
};
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";
}
}
# |
Verdict |
Execution time |
Memory |
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 |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
12 ms |
4688 KB |
Output is correct |
7 |
Correct |
4 ms |
2384 KB |
Output is correct |
8 |
Correct |
8 ms |
3920 KB |
Output is correct |
9 |
Correct |
4 ms |
2896 KB |
Output is correct |
10 |
Correct |
9 ms |
4944 KB |
Output is correct |
11 |
Correct |
6 ms |
3408 KB |
Output is correct |
12 |
Correct |
13 ms |
4508 KB |
Output is correct |
13 |
Correct |
4 ms |
2640 KB |
Output is correct |
14 |
Correct |
5 ms |
3152 KB |
Output is correct |
15 |
Correct |
6 ms |
3832 KB |
Output is correct |
16 |
Correct |
5 ms |
3408 KB |
Output is correct |
17 |
Correct |
6 ms |
3920 KB |
Output is correct |
18 |
Correct |
6 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3420 KB |
Output is correct |
20 |
Correct |
7 ms |
4088 KB |
Output is correct |
21 |
Correct |
3 ms |
1360 KB |
Output is correct |
22 |
Correct |
7 ms |
3096 KB |
Output is correct |
23 |
Correct |
7 ms |
3664 KB |
Output is correct |
24 |
Correct |
7 ms |
3920 KB |
Output is correct |
25 |
Correct |
7 ms |
4176 KB |
Output is correct |
26 |
Correct |
6 ms |
4176 KB |
Output is correct |
27 |
Correct |
5 ms |
592 KB |
Output is correct |
28 |
Correct |
7 ms |
3408 KB |
Output is correct |
29 |
Correct |
6 ms |
3408 KB |
Output is correct |
30 |
Correct |
4 ms |
2640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
12 ms |
4688 KB |
Output is correct |
7 |
Correct |
4 ms |
2384 KB |
Output is correct |
8 |
Correct |
8 ms |
3920 KB |
Output is correct |
9 |
Correct |
4 ms |
2896 KB |
Output is correct |
10 |
Correct |
9 ms |
4944 KB |
Output is correct |
11 |
Correct |
6 ms |
3408 KB |
Output is correct |
12 |
Correct |
13 ms |
4508 KB |
Output is correct |
13 |
Correct |
4 ms |
2640 KB |
Output is correct |
14 |
Correct |
5 ms |
3152 KB |
Output is correct |
15 |
Correct |
6 ms |
3832 KB |
Output is correct |
16 |
Correct |
5 ms |
3408 KB |
Output is correct |
17 |
Correct |
6 ms |
3920 KB |
Output is correct |
18 |
Correct |
6 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3420 KB |
Output is correct |
20 |
Correct |
7 ms |
4088 KB |
Output is correct |
21 |
Correct |
3 ms |
1360 KB |
Output is correct |
22 |
Correct |
7 ms |
3096 KB |
Output is correct |
23 |
Correct |
7 ms |
3664 KB |
Output is correct |
24 |
Correct |
7 ms |
3920 KB |
Output is correct |
25 |
Correct |
7 ms |
4176 KB |
Output is correct |
26 |
Correct |
6 ms |
4176 KB |
Output is correct |
27 |
Correct |
5 ms |
592 KB |
Output is correct |
28 |
Correct |
7 ms |
3408 KB |
Output is correct |
29 |
Correct |
6 ms |
3408 KB |
Output is correct |
30 |
Correct |
4 ms |
2640 KB |
Output is correct |
31 |
Correct |
4919 ms |
397664 KB |
Output is correct |
32 |
Correct |
241 ms |
5572 KB |
Output is correct |
33 |
Correct |
3537 ms |
371472 KB |
Output is correct |
34 |
Correct |
4323 ms |
369076 KB |
Output is correct |
35 |
Correct |
4484 ms |
401308 KB |
Output is correct |
36 |
Correct |
4211 ms |
398920 KB |
Output is correct |
37 |
Correct |
2754 ms |
364552 KB |
Output is correct |
38 |
Correct |
2292 ms |
366696 KB |
Output is correct |
39 |
Correct |
1729 ms |
356788 KB |
Output is correct |
40 |
Correct |
1964 ms |
358336 KB |
Output is correct |
41 |
Correct |
1344 ms |
315996 KB |
Output is correct |
42 |
Correct |
1299 ms |
317364 KB |
Output is correct |
43 |
Correct |
36 ms |
7656 KB |
Output is correct |
44 |
Correct |
1232 ms |
316596 KB |
Output is correct |
45 |
Correct |
997 ms |
316600 KB |
Output is correct |
46 |
Correct |
808 ms |
316348 KB |
Output is correct |
47 |
Correct |
684 ms |
276404 KB |
Output is correct |
48 |
Correct |
649 ms |
286776 KB |
Output is correct |
49 |
Correct |
831 ms |
302528 KB |
Output is correct |
50 |
Correct |
1278 ms |
298304 KB |
Output is correct |
51 |
Correct |
828 ms |
305592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5092 ms |
833568 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5113 ms |
678820 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
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 |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
12 ms |
4688 KB |
Output is correct |
7 |
Correct |
4 ms |
2384 KB |
Output is correct |
8 |
Correct |
8 ms |
3920 KB |
Output is correct |
9 |
Correct |
4 ms |
2896 KB |
Output is correct |
10 |
Correct |
9 ms |
4944 KB |
Output is correct |
11 |
Correct |
6 ms |
3408 KB |
Output is correct |
12 |
Correct |
13 ms |
4508 KB |
Output is correct |
13 |
Correct |
4 ms |
2640 KB |
Output is correct |
14 |
Correct |
5 ms |
3152 KB |
Output is correct |
15 |
Correct |
6 ms |
3832 KB |
Output is correct |
16 |
Correct |
5 ms |
3408 KB |
Output is correct |
17 |
Correct |
6 ms |
3920 KB |
Output is correct |
18 |
Correct |
6 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3420 KB |
Output is correct |
20 |
Correct |
7 ms |
4088 KB |
Output is correct |
21 |
Correct |
3 ms |
1360 KB |
Output is correct |
22 |
Correct |
7 ms |
3096 KB |
Output is correct |
23 |
Correct |
7 ms |
3664 KB |
Output is correct |
24 |
Correct |
7 ms |
3920 KB |
Output is correct |
25 |
Correct |
7 ms |
4176 KB |
Output is correct |
26 |
Correct |
6 ms |
4176 KB |
Output is correct |
27 |
Correct |
5 ms |
592 KB |
Output is correct |
28 |
Correct |
7 ms |
3408 KB |
Output is correct |
29 |
Correct |
6 ms |
3408 KB |
Output is correct |
30 |
Correct |
4 ms |
2640 KB |
Output is correct |
31 |
Correct |
4919 ms |
397664 KB |
Output is correct |
32 |
Correct |
241 ms |
5572 KB |
Output is correct |
33 |
Correct |
3537 ms |
371472 KB |
Output is correct |
34 |
Correct |
4323 ms |
369076 KB |
Output is correct |
35 |
Correct |
4484 ms |
401308 KB |
Output is correct |
36 |
Correct |
4211 ms |
398920 KB |
Output is correct |
37 |
Correct |
2754 ms |
364552 KB |
Output is correct |
38 |
Correct |
2292 ms |
366696 KB |
Output is correct |
39 |
Correct |
1729 ms |
356788 KB |
Output is correct |
40 |
Correct |
1964 ms |
358336 KB |
Output is correct |
41 |
Correct |
1344 ms |
315996 KB |
Output is correct |
42 |
Correct |
1299 ms |
317364 KB |
Output is correct |
43 |
Correct |
36 ms |
7656 KB |
Output is correct |
44 |
Correct |
1232 ms |
316596 KB |
Output is correct |
45 |
Correct |
997 ms |
316600 KB |
Output is correct |
46 |
Correct |
808 ms |
316348 KB |
Output is correct |
47 |
Correct |
684 ms |
276404 KB |
Output is correct |
48 |
Correct |
649 ms |
286776 KB |
Output is correct |
49 |
Correct |
831 ms |
302528 KB |
Output is correct |
50 |
Correct |
1278 ms |
298304 KB |
Output is correct |
51 |
Correct |
828 ms |
305592 KB |
Output is correct |
52 |
Correct |
1379 ms |
285876 KB |
Output is correct |
53 |
Correct |
1150 ms |
194232 KB |
Output is correct |
54 |
Correct |
3185 ms |
353528 KB |
Output is correct |
55 |
Correct |
1763 ms |
306320 KB |
Output is correct |
56 |
Correct |
1644 ms |
299188 KB |
Output is correct |
57 |
Correct |
1667 ms |
313316 KB |
Output is correct |
58 |
Correct |
1685 ms |
283828 KB |
Output is correct |
59 |
Correct |
1676 ms |
273588 KB |
Output is correct |
60 |
Correct |
1734 ms |
306740 KB |
Output is correct |
61 |
Correct |
562 ms |
156348 KB |
Output is correct |
62 |
Correct |
1387 ms |
286276 KB |
Output is correct |
63 |
Correct |
2571 ms |
322996 KB |
Output is correct |
64 |
Correct |
3108 ms |
327440 KB |
Output is correct |
65 |
Correct |
3034 ms |
322064 KB |
Output is correct |
66 |
Correct |
1581 ms |
317364 KB |
Output is correct |
67 |
Correct |
3480 ms |
34588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
12 ms |
4688 KB |
Output is correct |
7 |
Correct |
4 ms |
2384 KB |
Output is correct |
8 |
Correct |
8 ms |
3920 KB |
Output is correct |
9 |
Correct |
4 ms |
2896 KB |
Output is correct |
10 |
Correct |
9 ms |
4944 KB |
Output is correct |
11 |
Correct |
6 ms |
3408 KB |
Output is correct |
12 |
Correct |
13 ms |
4508 KB |
Output is correct |
13 |
Correct |
4 ms |
2640 KB |
Output is correct |
14 |
Correct |
5 ms |
3152 KB |
Output is correct |
15 |
Correct |
6 ms |
3832 KB |
Output is correct |
16 |
Correct |
5 ms |
3408 KB |
Output is correct |
17 |
Correct |
6 ms |
3920 KB |
Output is correct |
18 |
Correct |
6 ms |
3660 KB |
Output is correct |
19 |
Correct |
6 ms |
3420 KB |
Output is correct |
20 |
Correct |
7 ms |
4088 KB |
Output is correct |
21 |
Correct |
3 ms |
1360 KB |
Output is correct |
22 |
Correct |
7 ms |
3096 KB |
Output is correct |
23 |
Correct |
7 ms |
3664 KB |
Output is correct |
24 |
Correct |
7 ms |
3920 KB |
Output is correct |
25 |
Correct |
7 ms |
4176 KB |
Output is correct |
26 |
Correct |
6 ms |
4176 KB |
Output is correct |
27 |
Correct |
5 ms |
592 KB |
Output is correct |
28 |
Correct |
7 ms |
3408 KB |
Output is correct |
29 |
Correct |
6 ms |
3408 KB |
Output is correct |
30 |
Correct |
4 ms |
2640 KB |
Output is correct |
31 |
Correct |
4919 ms |
397664 KB |
Output is correct |
32 |
Correct |
241 ms |
5572 KB |
Output is correct |
33 |
Correct |
3537 ms |
371472 KB |
Output is correct |
34 |
Correct |
4323 ms |
369076 KB |
Output is correct |
35 |
Correct |
4484 ms |
401308 KB |
Output is correct |
36 |
Correct |
4211 ms |
398920 KB |
Output is correct |
37 |
Correct |
2754 ms |
364552 KB |
Output is correct |
38 |
Correct |
2292 ms |
366696 KB |
Output is correct |
39 |
Correct |
1729 ms |
356788 KB |
Output is correct |
40 |
Correct |
1964 ms |
358336 KB |
Output is correct |
41 |
Correct |
1344 ms |
315996 KB |
Output is correct |
42 |
Correct |
1299 ms |
317364 KB |
Output is correct |
43 |
Correct |
36 ms |
7656 KB |
Output is correct |
44 |
Correct |
1232 ms |
316596 KB |
Output is correct |
45 |
Correct |
997 ms |
316600 KB |
Output is correct |
46 |
Correct |
808 ms |
316348 KB |
Output is correct |
47 |
Correct |
684 ms |
276404 KB |
Output is correct |
48 |
Correct |
649 ms |
286776 KB |
Output is correct |
49 |
Correct |
831 ms |
302528 KB |
Output is correct |
50 |
Correct |
1278 ms |
298304 KB |
Output is correct |
51 |
Correct |
828 ms |
305592 KB |
Output is correct |
52 |
Execution timed out |
5092 ms |
833568 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |