#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;
const int N = 1e6;
struct IT{
struct node{
int m1, m2, i, l, r;
node(){
l = r = 0;
m1 = inff; m2 = 0;
}
};
multiset<int> s[N];
vector<node> all;
int cc, cc1;
void init(){
all.pb(node());
all.pb(node());
cc = 1; cc1 = 0;
}
int nw(int tl, int tr){
all.pb(node()); cc++;
if (tl == tr){
cc1++;
all[cc].i = cc1;
}
return cc;
}
void add2(int p, int x){
int v = 1, tl = 1, tr = inf;
while (true){
all[v].m2 = max(all[v].m2, x);
if (tl == tr){
s[all[v].i].insert(x);
break;
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!all[v].l) all[v].l = nw(tl, tm);
v = all[v].l;
tr = tm;
}
else {
if (!all[v].r) all[v].r = nw(tm + 1, tr);
v = all[v].r;
tl = tm + 1;
}
}
}
void add1(int p, int x){
int v = 1, tl = 1, tr = inf;
while (true){
all[v].m1 = min(all[v].m1, x);
if (tl == tr){
s[all[v].i].insert(x);
break;
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!all[v].l) all[v].l = nw(tl, tm);
v = all[v].l;
tr = tm;
}
else {
if (!all[v].r) all[v].r = nw(tm + 1, tr);
v = all[v].r;
tl = tm + 1;
}
}
}
void add(int l, int r){
if (l == 1){
add2(l, r + 1);
return;
}
if (r == inf){
add1(inf, l - 1);
return;
}
int m = (l + r) / 2;
if (l <= m) add1(m, l - 1);
if (m < r) add2(m + 1, r + 1);
}
vector<int> up;
void rem2(int p, int x){
up.clear();
int v = 1, tl = 1, tr = inf;
while (true){
if (tl == tr){
s[all[v].i].erase(s[all[v].i].find(x));
all[v].m2 = s[all[v].i].empty() ? 0 : *prev(s[all[v].i].end());
break;
}
up.pb(v);
int tm = (tl + tr) / 2;
if (p <= tm){
v = all[v].l;
tr = tm;
}
else {
v = all[v].r;
tl = tm + 1;
}
}
reverse(up.begin(), up.end());
for (int i: up){
all[i].m2 = 0;
if (all[i].l) all[i].m2 = max(all[i].m2, all[all[i].l].m2);
if (all[i].r) all[i].m2 = max(all[i].m2, all[all[i].r].m2);
}
}
void rem1(int p, int x){
up.clear();
int v = 1, tl = 1, tr = inf;
while (true){
if (tl == tr){
s[all[v].i].erase(s[all[v].i].find(x));
all[v].m1 = s[all[v].i].empty() ? inff : *s[all[v].i].begin();
break;
}
up.pb(v);
int tm = (tl + tr) / 2;
if (p <= tm){
v = all[v].l;
tr = tm;
}
else {
v = all[v].r;
tl = tm + 1;
}
}
reverse(up.begin(), up.end());
for (int i: up){
all[i].m1 = inff;
if (all[i].l) all[i].m1 = min(all[i].m1, all[all[i].l].m1);
if (all[i].r) all[i].m1 = min(all[i].m1, all[all[i].r].m1);
}
}
void rem(int l, int r){
if (l == 1){
rem2(l, r + 1);
return;
}
if (r == inf){
rem1(inf, l - 1);
return;
}
int m = (l + r) / 2;
if (l <= m) rem1(m, l - 1);
if (m < r) rem2(m + 1, r + 1);
}
int get2(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].m2;
int tm = (tl + tr) / 2, out = 0;
if (all[v].l){
out = max(out, get2(all[v].l, tl, tm, l, r));
}
if (all[v].r){
out = max(out, get2(all[v].r, tm + 1, tr, l, r));
}
return out;
}
int get1(int v, int tl, int tr, int l, int r){
if (l > tr || r < tl) return inff;
if (l <= tl && tr <= r) return all[v].m1;
int tm = (tl + tr) / 2, out = inff;
if (all[v].l){
out = min(out, get1(all[v].l, tl, tm, l, r));
}
if (all[v].r){
out = min(out, get1(all[v].r, tm + 1, tr, l, r));
}
return out;
}
pii get(int x){
return {get1(1, 1, inf, x, inf), get2(1, 1, inf, 1, 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 |
40 ms |
47176 KB |
Output is correct |
2 |
Correct |
41 ms |
47184 KB |
Output is correct |
3 |
Correct |
55 ms |
47172 KB |
Output is correct |
4 |
Correct |
50 ms |
47292 KB |
Output is correct |
5 |
Correct |
47 ms |
47480 KB |
Output is correct |
6 |
Correct |
50 ms |
48032 KB |
Output is correct |
7 |
Correct |
46 ms |
47288 KB |
Output is correct |
8 |
Correct |
50 ms |
47944 KB |
Output is correct |
9 |
Correct |
46 ms |
47440 KB |
Output is correct |
10 |
Correct |
44 ms |
48028 KB |
Output is correct |
11 |
Correct |
45 ms |
47544 KB |
Output is correct |
12 |
Correct |
56 ms |
47904 KB |
Output is correct |
13 |
Correct |
42 ms |
47432 KB |
Output is correct |
14 |
Correct |
51 ms |
47504 KB |
Output is correct |
15 |
Correct |
40 ms |
47600 KB |
Output is correct |
16 |
Correct |
49 ms |
47688 KB |
Output is correct |
17 |
Correct |
51 ms |
47924 KB |
Output is correct |
18 |
Correct |
49 ms |
47688 KB |
Output is correct |
19 |
Correct |
49 ms |
47704 KB |
Output is correct |
20 |
Correct |
43 ms |
47760 KB |
Output is correct |
21 |
Correct |
48 ms |
47432 KB |
Output is correct |
22 |
Correct |
50 ms |
47432 KB |
Output is correct |
23 |
Correct |
54 ms |
47680 KB |
Output is correct |
24 |
Correct |
52 ms |
47720 KB |
Output is correct |
25 |
Correct |
44 ms |
47928 KB |
Output is correct |
26 |
Correct |
46 ms |
47768 KB |
Output is correct |
27 |
Correct |
45 ms |
47460 KB |
Output is correct |
28 |
Correct |
42 ms |
47688 KB |
Output is correct |
29 |
Correct |
49 ms |
47624 KB |
Output is correct |
30 |
Correct |
45 ms |
47432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
47176 KB |
Output is correct |
2 |
Correct |
41 ms |
47184 KB |
Output is correct |
3 |
Correct |
55 ms |
47172 KB |
Output is correct |
4 |
Correct |
50 ms |
47292 KB |
Output is correct |
5 |
Correct |
47 ms |
47480 KB |
Output is correct |
6 |
Correct |
50 ms |
48032 KB |
Output is correct |
7 |
Correct |
46 ms |
47288 KB |
Output is correct |
8 |
Correct |
50 ms |
47944 KB |
Output is correct |
9 |
Correct |
46 ms |
47440 KB |
Output is correct |
10 |
Correct |
44 ms |
48028 KB |
Output is correct |
11 |
Correct |
45 ms |
47544 KB |
Output is correct |
12 |
Correct |
56 ms |
47904 KB |
Output is correct |
13 |
Correct |
42 ms |
47432 KB |
Output is correct |
14 |
Correct |
51 ms |
47504 KB |
Output is correct |
15 |
Correct |
40 ms |
47600 KB |
Output is correct |
16 |
Correct |
49 ms |
47688 KB |
Output is correct |
17 |
Correct |
51 ms |
47924 KB |
Output is correct |
18 |
Correct |
49 ms |
47688 KB |
Output is correct |
19 |
Correct |
49 ms |
47704 KB |
Output is correct |
20 |
Correct |
43 ms |
47760 KB |
Output is correct |
21 |
Correct |
48 ms |
47432 KB |
Output is correct |
22 |
Correct |
50 ms |
47432 KB |
Output is correct |
23 |
Correct |
54 ms |
47680 KB |
Output is correct |
24 |
Correct |
52 ms |
47720 KB |
Output is correct |
25 |
Correct |
44 ms |
47928 KB |
Output is correct |
26 |
Correct |
46 ms |
47768 KB |
Output is correct |
27 |
Correct |
45 ms |
47460 KB |
Output is correct |
28 |
Correct |
42 ms |
47688 KB |
Output is correct |
29 |
Correct |
49 ms |
47624 KB |
Output is correct |
30 |
Correct |
45 ms |
47432 KB |
Output is correct |
31 |
Correct |
548 ms |
100984 KB |
Output is correct |
32 |
Correct |
295 ms |
52544 KB |
Output is correct |
33 |
Correct |
428 ms |
97180 KB |
Output is correct |
34 |
Correct |
378 ms |
97188 KB |
Output is correct |
35 |
Correct |
450 ms |
101128 KB |
Output is correct |
36 |
Correct |
438 ms |
143688 KB |
Output is correct |
37 |
Correct |
364 ms |
96664 KB |
Output is correct |
38 |
Correct |
365 ms |
96784 KB |
Output is correct |
39 |
Correct |
324 ms |
96924 KB |
Output is correct |
40 |
Correct |
315 ms |
96668 KB |
Output is correct |
41 |
Correct |
271 ms |
96672 KB |
Output is correct |
42 |
Correct |
265 ms |
96672 KB |
Output is correct |
43 |
Correct |
92 ms |
53788 KB |
Output is correct |
44 |
Correct |
270 ms |
96672 KB |
Output is correct |
45 |
Correct |
262 ms |
96672 KB |
Output is correct |
46 |
Correct |
250 ms |
96824 KB |
Output is correct |
47 |
Correct |
212 ms |
76192 KB |
Output is correct |
48 |
Correct |
239 ms |
75992 KB |
Output is correct |
49 |
Correct |
223 ms |
76208 KB |
Output is correct |
50 |
Correct |
245 ms |
76240 KB |
Output is correct |
51 |
Correct |
229 ms |
76200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1274 ms |
187720 KB |
Output is correct |
2 |
Runtime error |
1230 ms |
417116 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1281 ms |
433800 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
47176 KB |
Output is correct |
2 |
Correct |
41 ms |
47184 KB |
Output is correct |
3 |
Correct |
55 ms |
47172 KB |
Output is correct |
4 |
Correct |
50 ms |
47292 KB |
Output is correct |
5 |
Correct |
47 ms |
47480 KB |
Output is correct |
6 |
Correct |
50 ms |
48032 KB |
Output is correct |
7 |
Correct |
46 ms |
47288 KB |
Output is correct |
8 |
Correct |
50 ms |
47944 KB |
Output is correct |
9 |
Correct |
46 ms |
47440 KB |
Output is correct |
10 |
Correct |
44 ms |
48028 KB |
Output is correct |
11 |
Correct |
45 ms |
47544 KB |
Output is correct |
12 |
Correct |
56 ms |
47904 KB |
Output is correct |
13 |
Correct |
42 ms |
47432 KB |
Output is correct |
14 |
Correct |
51 ms |
47504 KB |
Output is correct |
15 |
Correct |
40 ms |
47600 KB |
Output is correct |
16 |
Correct |
49 ms |
47688 KB |
Output is correct |
17 |
Correct |
51 ms |
47924 KB |
Output is correct |
18 |
Correct |
49 ms |
47688 KB |
Output is correct |
19 |
Correct |
49 ms |
47704 KB |
Output is correct |
20 |
Correct |
43 ms |
47760 KB |
Output is correct |
21 |
Correct |
48 ms |
47432 KB |
Output is correct |
22 |
Correct |
50 ms |
47432 KB |
Output is correct |
23 |
Correct |
54 ms |
47680 KB |
Output is correct |
24 |
Correct |
52 ms |
47720 KB |
Output is correct |
25 |
Correct |
44 ms |
47928 KB |
Output is correct |
26 |
Correct |
46 ms |
47768 KB |
Output is correct |
27 |
Correct |
45 ms |
47460 KB |
Output is correct |
28 |
Correct |
42 ms |
47688 KB |
Output is correct |
29 |
Correct |
49 ms |
47624 KB |
Output is correct |
30 |
Correct |
45 ms |
47432 KB |
Output is correct |
31 |
Correct |
548 ms |
100984 KB |
Output is correct |
32 |
Correct |
295 ms |
52544 KB |
Output is correct |
33 |
Correct |
428 ms |
97180 KB |
Output is correct |
34 |
Correct |
378 ms |
97188 KB |
Output is correct |
35 |
Correct |
450 ms |
101128 KB |
Output is correct |
36 |
Correct |
438 ms |
143688 KB |
Output is correct |
37 |
Correct |
364 ms |
96664 KB |
Output is correct |
38 |
Correct |
365 ms |
96784 KB |
Output is correct |
39 |
Correct |
324 ms |
96924 KB |
Output is correct |
40 |
Correct |
315 ms |
96668 KB |
Output is correct |
41 |
Correct |
271 ms |
96672 KB |
Output is correct |
42 |
Correct |
265 ms |
96672 KB |
Output is correct |
43 |
Correct |
92 ms |
53788 KB |
Output is correct |
44 |
Correct |
270 ms |
96672 KB |
Output is correct |
45 |
Correct |
262 ms |
96672 KB |
Output is correct |
46 |
Correct |
250 ms |
96824 KB |
Output is correct |
47 |
Correct |
212 ms |
76192 KB |
Output is correct |
48 |
Correct |
239 ms |
75992 KB |
Output is correct |
49 |
Correct |
223 ms |
76208 KB |
Output is correct |
50 |
Correct |
245 ms |
76240 KB |
Output is correct |
51 |
Correct |
229 ms |
76200 KB |
Output is correct |
52 |
Correct |
210 ms |
62784 KB |
Output is correct |
53 |
Correct |
168 ms |
57464 KB |
Output is correct |
54 |
Correct |
309 ms |
81520 KB |
Output is correct |
55 |
Correct |
259 ms |
80164 KB |
Output is correct |
56 |
Correct |
239 ms |
82096 KB |
Output is correct |
57 |
Correct |
278 ms |
77472 KB |
Output is correct |
58 |
Correct |
268 ms |
77488 KB |
Output is correct |
59 |
Correct |
249 ms |
79524 KB |
Output is correct |
60 |
Correct |
300 ms |
76456 KB |
Output is correct |
61 |
Correct |
93 ms |
59600 KB |
Output is correct |
62 |
Correct |
174 ms |
62900 KB |
Output is correct |
63 |
Correct |
263 ms |
70432 KB |
Output is correct |
64 |
Correct |
297 ms |
80292 KB |
Output is correct |
65 |
Correct |
280 ms |
77308 KB |
Output is correct |
66 |
Correct |
265 ms |
76196 KB |
Output is correct |
67 |
Correct |
280 ms |
53688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
47176 KB |
Output is correct |
2 |
Correct |
41 ms |
47184 KB |
Output is correct |
3 |
Correct |
55 ms |
47172 KB |
Output is correct |
4 |
Correct |
50 ms |
47292 KB |
Output is correct |
5 |
Correct |
47 ms |
47480 KB |
Output is correct |
6 |
Correct |
50 ms |
48032 KB |
Output is correct |
7 |
Correct |
46 ms |
47288 KB |
Output is correct |
8 |
Correct |
50 ms |
47944 KB |
Output is correct |
9 |
Correct |
46 ms |
47440 KB |
Output is correct |
10 |
Correct |
44 ms |
48028 KB |
Output is correct |
11 |
Correct |
45 ms |
47544 KB |
Output is correct |
12 |
Correct |
56 ms |
47904 KB |
Output is correct |
13 |
Correct |
42 ms |
47432 KB |
Output is correct |
14 |
Correct |
51 ms |
47504 KB |
Output is correct |
15 |
Correct |
40 ms |
47600 KB |
Output is correct |
16 |
Correct |
49 ms |
47688 KB |
Output is correct |
17 |
Correct |
51 ms |
47924 KB |
Output is correct |
18 |
Correct |
49 ms |
47688 KB |
Output is correct |
19 |
Correct |
49 ms |
47704 KB |
Output is correct |
20 |
Correct |
43 ms |
47760 KB |
Output is correct |
21 |
Correct |
48 ms |
47432 KB |
Output is correct |
22 |
Correct |
50 ms |
47432 KB |
Output is correct |
23 |
Correct |
54 ms |
47680 KB |
Output is correct |
24 |
Correct |
52 ms |
47720 KB |
Output is correct |
25 |
Correct |
44 ms |
47928 KB |
Output is correct |
26 |
Correct |
46 ms |
47768 KB |
Output is correct |
27 |
Correct |
45 ms |
47460 KB |
Output is correct |
28 |
Correct |
42 ms |
47688 KB |
Output is correct |
29 |
Correct |
49 ms |
47624 KB |
Output is correct |
30 |
Correct |
45 ms |
47432 KB |
Output is correct |
31 |
Correct |
548 ms |
100984 KB |
Output is correct |
32 |
Correct |
295 ms |
52544 KB |
Output is correct |
33 |
Correct |
428 ms |
97180 KB |
Output is correct |
34 |
Correct |
378 ms |
97188 KB |
Output is correct |
35 |
Correct |
450 ms |
101128 KB |
Output is correct |
36 |
Correct |
438 ms |
143688 KB |
Output is correct |
37 |
Correct |
364 ms |
96664 KB |
Output is correct |
38 |
Correct |
365 ms |
96784 KB |
Output is correct |
39 |
Correct |
324 ms |
96924 KB |
Output is correct |
40 |
Correct |
315 ms |
96668 KB |
Output is correct |
41 |
Correct |
271 ms |
96672 KB |
Output is correct |
42 |
Correct |
265 ms |
96672 KB |
Output is correct |
43 |
Correct |
92 ms |
53788 KB |
Output is correct |
44 |
Correct |
270 ms |
96672 KB |
Output is correct |
45 |
Correct |
262 ms |
96672 KB |
Output is correct |
46 |
Correct |
250 ms |
96824 KB |
Output is correct |
47 |
Correct |
212 ms |
76192 KB |
Output is correct |
48 |
Correct |
239 ms |
75992 KB |
Output is correct |
49 |
Correct |
223 ms |
76208 KB |
Output is correct |
50 |
Correct |
245 ms |
76240 KB |
Output is correct |
51 |
Correct |
229 ms |
76200 KB |
Output is correct |
52 |
Correct |
1274 ms |
187720 KB |
Output is correct |
53 |
Runtime error |
1230 ms |
417116 KB |
Execution killed with signal 11 |
54 |
Halted |
0 ms |
0 KB |
- |