#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5+5;
const int MAXV = 1e8;
int n, k, q;
class event {
public:
int time;
bool type; // 0 update, 1 query
bool start_or_end; // 0 start, 1 end. Only for updates
int store_type; // update only
int pos; // both
int id; // query
event(int tim, bool se, int tp, int p) {
time = tim;
type = 0;
start_or_end = se;
store_type = tp;
pos = p;
}
event(int tim, int p, int i) {
type = 1;
time = tim;
pos = p;
id = i;
}
};
bool operator<(event &a, event &b) {
return (a.time == b.time) ? (a.type < b.type) : (a.time < b.time);
}
vector<event> events;
int answs[MAXN];
int cnt[MAXN];
int num_active = 0;
multiset<int> locs[MAXN];
class stree {
public:
int lp, rp;
stree *l = nullptr;
stree *r = nullptr;
int mx;
multiset<int> all_vals; // only for lowest level
stree(int lv, int rv) {
mx = 0;
lp = lv;
rp = rv;
}
int mxq(int lv, int rv) {
if (lp > rv || rp < lv) return 0;
if (lp >= lv && rp <= rv) return mx;
int best = 0;
if (l) best = max(best, l->mxq(lv, rv));
if (r) best = max(best, r->mxq(lv, rv));
return best;
}
void update(int p, int v) {
if (lp == rp) {
all_vals.insert(v);
mx = *(all_vals.rbegin());
return;
}
int m = (lp+rp)/2;
if (p <= m) {
if (!l) l = new stree(lp, m);
l->update(p, v);
}
else {
if (!r) r = new stree(m+1, rp);
r->update(p, v);
}
mx = 0;
if (l) mx = max(mx, l->mx);
if (r) mx = max(mx, r->mx);
}
void deupdate(int p, int v) {
if (lp == rp) {
all_vals.erase(all_vals.find(v));
if (all_vals.empty()) mx = 0;
else mx = *(all_vals.rbegin());
return;
}
int m = (lp+rp)/2;
if (p <= m) {
if (!l) l = new stree(lp, m);
l->deupdate(p, v);
}
else {
if (!r) r = new stree(m+1, rp);
r->deupdate(p, v);
}
mx = 0;
if (l) mx = max(mx, l->mx);
if (r) mx = max(mx, r->mx);
}
};
stree *ltree, *rtree;
void make(int l, int r) {
if (l == -1 && r == -1) return;
if (l == -1) {
ltree->update(0, r);
return;
}
if (r == -1) {
rtree->update(MAXV, MAXV-l);
return;
}
int p1 = (l+r)/2;
int p2 = (l+r+1)/2;
int v1 = MAXV-l;
int v2 = r;
ltree->update(p2, v2);
rtree->update(p1, v1);
}
void unmake(int l, int r) {
if (l == -1 && r == -1) return;
if (l == -1) {
ltree->deupdate(0, r);
return;
}
if (r == -1) {
rtree->deupdate(MAXV, MAXV-l);
return;
}
int p1 = (l+r)/2;
int p2 = (l+r+1)/2;
int v1 = MAXV-l;
int v2 = r;
ltree->deupdate(p2, v2);
rtree->deupdate(p1, v1);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> k >> q;
ltree = new stree(0, MAXV);
rtree = new stree(0, MAXV);
for (int i = 0; i < n; i++) {
int x, t, a, b;
cin >> x >> t >> a >> b; t--;
events.push_back(event(a, 0, t, x));
events.push_back(event(b+1, 1, t, x));
}
for (int i = 0; i < q; i++) {
int x, t; cin >> x >> t;
events.push_back(event(t, x, i));
}
sort(events.begin(), events.end());
for (event ev: events) {
if (ev.type == 0) {
int type = ev.store_type;
if (ev.start_or_end == 0) {
num_active += (cnt[type] == 0);
cnt[type]++;
if (locs[type].find(ev.pos) != locs[type].end()) {
locs[type].insert(ev.pos);
continue;
}
auto iter = locs[type].lower_bound(ev.pos);
int r = -1;
int l = -1;
if (iter != locs[type].end()) r = *iter;
if (iter != locs[type].begin()) {
iter--;
l = *iter;
}
unmake(l, r);
locs[type].insert(ev.pos);
make(l, ev.pos);
make(ev.pos, r);
}
else {
cnt[type]--;
num_active -= (cnt[type] == 0);
locs[type].erase(locs[type].find(ev.pos));
if (locs[type].find(ev.pos) != locs[type].end()) continue;
auto iter = locs[type].lower_bound(ev.pos);
int r = -1;
int l = -1;
if (iter != locs[type].end()) r = *iter;
if (iter != locs[type].begin()) {
iter--;
l = *iter;
}
make(l, r);
unmake(l, ev.pos);
unmake(ev.pos, r);
}
}
if (ev.type == 1) {
if (num_active < k) {
answs[ev.id] = -1;
continue;
}
answs[ev.id] = max(ltree->mxq(0, ev.pos)-ev.pos, rtree->mxq(ev.pos, MAXV)-MAXV+ev.pos);
}
}
for (int i = 0; i < q; i++) cout << answs[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
12 ms |
17364 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
12 ms |
15928 KB |
Output is correct |
9 |
Correct |
10 ms |
14420 KB |
Output is correct |
10 |
Correct |
14 ms |
17720 KB |
Output is correct |
11 |
Correct |
9 ms |
15572 KB |
Output is correct |
12 |
Correct |
12 ms |
17108 KB |
Output is correct |
13 |
Correct |
9 ms |
14804 KB |
Output is correct |
14 |
Correct |
10 ms |
15584 KB |
Output is correct |
15 |
Correct |
9 ms |
15572 KB |
Output is correct |
16 |
Correct |
9 ms |
15316 KB |
Output is correct |
17 |
Correct |
10 ms |
15956 KB |
Output is correct |
18 |
Correct |
9 ms |
15552 KB |
Output is correct |
19 |
Correct |
9 ms |
15316 KB |
Output is correct |
20 |
Correct |
10 ms |
15956 KB |
Output is correct |
21 |
Correct |
8 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
9 ms |
15184 KB |
Output is correct |
24 |
Correct |
9 ms |
15572 KB |
Output is correct |
25 |
Correct |
10 ms |
16044 KB |
Output is correct |
26 |
Correct |
10 ms |
16172 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
12 ms |
15672 KB |
Output is correct |
29 |
Correct |
9 ms |
15688 KB |
Output is correct |
30 |
Correct |
9 ms |
15060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
12 ms |
17364 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
12 ms |
15928 KB |
Output is correct |
9 |
Correct |
10 ms |
14420 KB |
Output is correct |
10 |
Correct |
14 ms |
17720 KB |
Output is correct |
11 |
Correct |
9 ms |
15572 KB |
Output is correct |
12 |
Correct |
12 ms |
17108 KB |
Output is correct |
13 |
Correct |
9 ms |
14804 KB |
Output is correct |
14 |
Correct |
10 ms |
15584 KB |
Output is correct |
15 |
Correct |
9 ms |
15572 KB |
Output is correct |
16 |
Correct |
9 ms |
15316 KB |
Output is correct |
17 |
Correct |
10 ms |
15956 KB |
Output is correct |
18 |
Correct |
9 ms |
15552 KB |
Output is correct |
19 |
Correct |
9 ms |
15316 KB |
Output is correct |
20 |
Correct |
10 ms |
15956 KB |
Output is correct |
21 |
Correct |
8 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
9 ms |
15184 KB |
Output is correct |
24 |
Correct |
9 ms |
15572 KB |
Output is correct |
25 |
Correct |
10 ms |
16044 KB |
Output is correct |
26 |
Correct |
10 ms |
16172 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
12 ms |
15672 KB |
Output is correct |
29 |
Correct |
9 ms |
15688 KB |
Output is correct |
30 |
Correct |
9 ms |
15060 KB |
Output is correct |
31 |
Correct |
1105 ms |
344792 KB |
Output is correct |
32 |
Correct |
77 ms |
19652 KB |
Output is correct |
33 |
Correct |
1134 ms |
347916 KB |
Output is correct |
34 |
Correct |
1143 ms |
329128 KB |
Output is correct |
35 |
Correct |
1131 ms |
354044 KB |
Output is correct |
36 |
Correct |
1161 ms |
358836 KB |
Output is correct |
37 |
Correct |
888 ms |
340272 KB |
Output is correct |
38 |
Correct |
868 ms |
344632 KB |
Output is correct |
39 |
Correct |
585 ms |
332744 KB |
Output is correct |
40 |
Correct |
671 ms |
340112 KB |
Output is correct |
41 |
Correct |
555 ms |
186296 KB |
Output is correct |
42 |
Correct |
514 ms |
186808 KB |
Output is correct |
43 |
Correct |
68 ms |
21484 KB |
Output is correct |
44 |
Correct |
541 ms |
186904 KB |
Output is correct |
45 |
Correct |
526 ms |
187308 KB |
Output is correct |
46 |
Correct |
465 ms |
187016 KB |
Output is correct |
47 |
Correct |
275 ms |
149676 KB |
Output is correct |
48 |
Correct |
291 ms |
162884 KB |
Output is correct |
49 |
Correct |
327 ms |
176232 KB |
Output is correct |
50 |
Correct |
347 ms |
167608 KB |
Output is correct |
51 |
Correct |
346 ms |
180528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4500 ms |
715508 KB |
Output is correct |
2 |
Runtime error |
3487 ms |
1048576 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3781 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
12 ms |
17364 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
12 ms |
15928 KB |
Output is correct |
9 |
Correct |
10 ms |
14420 KB |
Output is correct |
10 |
Correct |
14 ms |
17720 KB |
Output is correct |
11 |
Correct |
9 ms |
15572 KB |
Output is correct |
12 |
Correct |
12 ms |
17108 KB |
Output is correct |
13 |
Correct |
9 ms |
14804 KB |
Output is correct |
14 |
Correct |
10 ms |
15584 KB |
Output is correct |
15 |
Correct |
9 ms |
15572 KB |
Output is correct |
16 |
Correct |
9 ms |
15316 KB |
Output is correct |
17 |
Correct |
10 ms |
15956 KB |
Output is correct |
18 |
Correct |
9 ms |
15552 KB |
Output is correct |
19 |
Correct |
9 ms |
15316 KB |
Output is correct |
20 |
Correct |
10 ms |
15956 KB |
Output is correct |
21 |
Correct |
8 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
9 ms |
15184 KB |
Output is correct |
24 |
Correct |
9 ms |
15572 KB |
Output is correct |
25 |
Correct |
10 ms |
16044 KB |
Output is correct |
26 |
Correct |
10 ms |
16172 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
12 ms |
15672 KB |
Output is correct |
29 |
Correct |
9 ms |
15688 KB |
Output is correct |
30 |
Correct |
9 ms |
15060 KB |
Output is correct |
31 |
Correct |
1105 ms |
344792 KB |
Output is correct |
32 |
Correct |
77 ms |
19652 KB |
Output is correct |
33 |
Correct |
1134 ms |
347916 KB |
Output is correct |
34 |
Correct |
1143 ms |
329128 KB |
Output is correct |
35 |
Correct |
1131 ms |
354044 KB |
Output is correct |
36 |
Correct |
1161 ms |
358836 KB |
Output is correct |
37 |
Correct |
888 ms |
340272 KB |
Output is correct |
38 |
Correct |
868 ms |
344632 KB |
Output is correct |
39 |
Correct |
585 ms |
332744 KB |
Output is correct |
40 |
Correct |
671 ms |
340112 KB |
Output is correct |
41 |
Correct |
555 ms |
186296 KB |
Output is correct |
42 |
Correct |
514 ms |
186808 KB |
Output is correct |
43 |
Correct |
68 ms |
21484 KB |
Output is correct |
44 |
Correct |
541 ms |
186904 KB |
Output is correct |
45 |
Correct |
526 ms |
187308 KB |
Output is correct |
46 |
Correct |
465 ms |
187016 KB |
Output is correct |
47 |
Correct |
275 ms |
149676 KB |
Output is correct |
48 |
Correct |
291 ms |
162884 KB |
Output is correct |
49 |
Correct |
327 ms |
176232 KB |
Output is correct |
50 |
Correct |
347 ms |
167608 KB |
Output is correct |
51 |
Correct |
346 ms |
180528 KB |
Output is correct |
52 |
Correct |
175 ms |
26996 KB |
Output is correct |
53 |
Correct |
168 ms |
21684 KB |
Output is correct |
54 |
Correct |
532 ms |
153136 KB |
Output is correct |
55 |
Correct |
441 ms |
139176 KB |
Output is correct |
56 |
Correct |
439 ms |
114104 KB |
Output is correct |
57 |
Correct |
504 ms |
172276 KB |
Output is correct |
58 |
Correct |
496 ms |
136796 KB |
Output is correct |
59 |
Correct |
377 ms |
110740 KB |
Output is correct |
60 |
Correct |
541 ms |
172104 KB |
Output is correct |
61 |
Correct |
129 ms |
27308 KB |
Output is correct |
62 |
Correct |
170 ms |
27112 KB |
Output is correct |
63 |
Correct |
416 ms |
103404 KB |
Output is correct |
64 |
Correct |
509 ms |
129088 KB |
Output is correct |
65 |
Correct |
591 ms |
168476 KB |
Output is correct |
66 |
Correct |
551 ms |
185924 KB |
Output is correct |
67 |
Correct |
238 ms |
20268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
12 ms |
17364 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
12 ms |
15928 KB |
Output is correct |
9 |
Correct |
10 ms |
14420 KB |
Output is correct |
10 |
Correct |
14 ms |
17720 KB |
Output is correct |
11 |
Correct |
9 ms |
15572 KB |
Output is correct |
12 |
Correct |
12 ms |
17108 KB |
Output is correct |
13 |
Correct |
9 ms |
14804 KB |
Output is correct |
14 |
Correct |
10 ms |
15584 KB |
Output is correct |
15 |
Correct |
9 ms |
15572 KB |
Output is correct |
16 |
Correct |
9 ms |
15316 KB |
Output is correct |
17 |
Correct |
10 ms |
15956 KB |
Output is correct |
18 |
Correct |
9 ms |
15552 KB |
Output is correct |
19 |
Correct |
9 ms |
15316 KB |
Output is correct |
20 |
Correct |
10 ms |
15956 KB |
Output is correct |
21 |
Correct |
8 ms |
14420 KB |
Output is correct |
22 |
Correct |
8 ms |
14420 KB |
Output is correct |
23 |
Correct |
9 ms |
15184 KB |
Output is correct |
24 |
Correct |
9 ms |
15572 KB |
Output is correct |
25 |
Correct |
10 ms |
16044 KB |
Output is correct |
26 |
Correct |
10 ms |
16172 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
12 ms |
15672 KB |
Output is correct |
29 |
Correct |
9 ms |
15688 KB |
Output is correct |
30 |
Correct |
9 ms |
15060 KB |
Output is correct |
31 |
Correct |
1105 ms |
344792 KB |
Output is correct |
32 |
Correct |
77 ms |
19652 KB |
Output is correct |
33 |
Correct |
1134 ms |
347916 KB |
Output is correct |
34 |
Correct |
1143 ms |
329128 KB |
Output is correct |
35 |
Correct |
1131 ms |
354044 KB |
Output is correct |
36 |
Correct |
1161 ms |
358836 KB |
Output is correct |
37 |
Correct |
888 ms |
340272 KB |
Output is correct |
38 |
Correct |
868 ms |
344632 KB |
Output is correct |
39 |
Correct |
585 ms |
332744 KB |
Output is correct |
40 |
Correct |
671 ms |
340112 KB |
Output is correct |
41 |
Correct |
555 ms |
186296 KB |
Output is correct |
42 |
Correct |
514 ms |
186808 KB |
Output is correct |
43 |
Correct |
68 ms |
21484 KB |
Output is correct |
44 |
Correct |
541 ms |
186904 KB |
Output is correct |
45 |
Correct |
526 ms |
187308 KB |
Output is correct |
46 |
Correct |
465 ms |
187016 KB |
Output is correct |
47 |
Correct |
275 ms |
149676 KB |
Output is correct |
48 |
Correct |
291 ms |
162884 KB |
Output is correct |
49 |
Correct |
327 ms |
176232 KB |
Output is correct |
50 |
Correct |
347 ms |
167608 KB |
Output is correct |
51 |
Correct |
346 ms |
180528 KB |
Output is correct |
52 |
Correct |
4500 ms |
715508 KB |
Output is correct |
53 |
Runtime error |
3487 ms |
1048576 KB |
Execution killed with signal 9 |
54 |
Halted |
0 ms |
0 KB |
- |