#include "bits/stdc++.h"
using namespace std;
#define ar array
#define pq priority_queue
const int N = 3e5 + 5;
const int M = N * 20;
const int MX = 1e8 + 5;
struct ST{
pq<int> tree[M][2], bad[M][2];
int f[M][2], last;
ST(){
last = 0;
}
void make(int x){
if(!f[x][0]) f[x][0] = ++last;
if(!f[x][1]) f[x][1] = ++last;
//~ assert(last < M);
}
void add(int l, int r, int s, int t, int lx = 0, int rx = MX, int x = 0){
if(lx > r || rx < l) return;
if(lx >= l && rx <= r){
if(t == 1){
tree[x][0].push(s + (lx - l));
} else {
tree[x][1].push(s - (lx - l));
} return;
} int m = (lx + rx) >> 1;
make(x);
add(l, r, s, t, lx, m, f[x][0]), add(l, r, s, t, m+1, rx, f[x][1]);
}
void bal(pq<int>& a, pq<int>& b){
while(!a.empty() && !b.empty() && a.top() == b.top()){
a.pop();
b.pop();
}
}
void del(int l, int r, int s, int t, int lx = 0, int rx = MX, int x = 0){
if(lx > r || rx < l) return;
if(lx >= l && rx <= r){
if(t == 1){
bad[x][0].push(s + (lx - l));
} else {
bad[x][1].push(s - (lx - l));
} return;
} int m = (lx + rx) >> 1;
make(x);
del(l, r, s, t, lx, m, f[x][0]), del(l, r, s, t, m+1, rx, f[x][1]);
}
int get(int i, int lx = 0, int rx = MX, int x = 0){
int res = 0;
bal(tree[x][0], bad[x][0]);
bal(tree[x][1], bad[x][1]);
if(!tree[x][0].empty()) res = max(res, tree[x][0].top() + (i - lx));
if(!tree[x][1].empty()) res = max(res, tree[x][1].top() - (i - lx));
if(lx == rx) return res;
int m = (lx + rx) >> 1;
if(i <= m) return max((f[x][0] ? get(i, lx, m, f[x][0]) : 0), res);
else return max((f[x][1] ? get(i, m+1, rx, f[x][1]) : 0), res);
}
}tree;
/*
4 2 4
3 1 1 10
9 2 2 4
7 2 5 7
4 1 8 10
5 3
5 6
5 9
1 10
2 1 3
1 1 1 4
1 1 2 6
1 3
1 5
1 7
1 1 1
100000000 1 1 1
1 1
*/
int x[N], t[N], a[N], b[N], l[N], y[N];
vector<int> stor[N];
multiset<int> ss[N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, k, q; cin>>n>>k>>q;
for(int i=0;i<n;i++){
cin>>x[i]>>t[i]>>a[i]>>b[i];
stor[--t[i]].push_back(i);
}
auto add = [&](int l, int r){
if(l == r) return;
int m = (l + r) >> 1;
tree.add(l, m, 0, 1);
tree.add(m + 1, r, r - m - 1, -1);
}; auto inc = [&](int x) { tree.add(x, MX, 0, 1); };
auto dec = [&](int x) { tree.add(0, x, x, -1); };
auto dadd = [&](int l, int r){
if(l == r) return;
int m = (l + r) >> 1;
tree.del(l, m, 0, 1);
tree.del(m + 1, r, r - m - 1, -1);
}; auto dinc = [&](int x) { tree.del(x, MX, 0, 1); };
auto ddec = [&](int x) { tree.del(0, x, x, -1); };
//~ bool is = 1;
//~ for(int i=0;i<k;i++){
//~ if(stor[i].empty()) { is = 0; break; }
//~ sort(stor[i].begin(), stor[i].end(), [&](int i, int j){
//~ return (x[i] < x[j]);
//~ });
//~ for(int j=1;j<(int)stor[i].size();j++){
//~ add(x[stor[i][j-1]], x[stor[i][j]]);
//~ }
//~ dec(x[stor[i][0]]);
//~ inc(x[stor[i].back()]);
//~ }
vector<int> in(n); iota(in.begin(), in.end(), 0);
vector<int> out(n); iota(out.begin(), out.end(), 0);
sort(in.begin(), in.end(), [&](int i, int j){
return (a[i] < a[j]);
});
sort(out.begin(), out.end(), [&](int i, int j){
return (b[i] < b[j]);
});
vector<int> p(q);
for(int i=0;i<q;i++){ p[i] = i;
cin>>l[i]>>y[i];
}
sort(p.begin(), p.end(), [&](int i, int j){
return (y[i] < y[j]);
});
int I = 0, O = 0, c = k;
auto NEW = [&](int i){
auto it = ss[t[i]].upper_bound(x[i]);
if(ss[t[i]].empty()) c--;
if(it == ss[t[i]].end()){
if(it != ss[t[i]].begin()){
it--;
dinc(*it);
add(*it, x[i]);
inc(x[i]);
} else {
inc(x[i]);
dec(x[i]);
}
} else {
if(it == ss[t[i]].begin()){
ddec(*it);
add(x[i], *it);
dec(x[i]);
} else {
auto R = it; it--;
dadd(*it, *R);
add(*it, x[i]);
add(x[i], *R);
}
}
ss[t[i]].insert(x[i]);
};
auto DEL = [&](int i){
ss[t[i]].erase(ss[t[i]].find(x[i]));
if(ss[t[i]].empty()) c++;
auto it = ss[t[i]].upper_bound(x[i]);
if(it == ss[t[i]].end()){
if(it == ss[t[i]].begin()){
dinc(x[i]);
ddec(x[i]);
} else {
it--;
dadd(*it, x[i]);
dinc(x[i]);
inc(*it);
}
} else {
if(it == ss[t[i]].begin()){
dadd(x[i], *it);
ddec(x[i]);
dec(*it);
} else {
auto R = it; it--;
dadd(*it, x[i]);
dadd(x[i], *R);
add(*it, *R);
}
}
};
vector<int> res(q);
for(auto i : p){
while(I < n && a[in[I]] <= y[i]){
NEW(in[I]);
I++;
}
while(O < n && b[out[O]] < y[i]){
DEL(out[O]);
O++;
}
if(!c) res[i] = tree.get(l[i]);
else res[i] = -1;
}
for(int i=0;i<q;i++) cout<<res[i]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
772888 KB |
Output is correct |
2 |
Correct |
340 ms |
772916 KB |
Output is correct |
3 |
Correct |
333 ms |
773036 KB |
Output is correct |
4 |
Correct |
344 ms |
773040 KB |
Output is correct |
5 |
Correct |
366 ms |
773044 KB |
Output is correct |
6 |
Correct |
354 ms |
774988 KB |
Output is correct |
7 |
Correct |
344 ms |
773576 KB |
Output is correct |
8 |
Correct |
362 ms |
774220 KB |
Output is correct |
9 |
Correct |
337 ms |
773792 KB |
Output is correct |
10 |
Correct |
341 ms |
774936 KB |
Output is correct |
11 |
Correct |
342 ms |
774076 KB |
Output is correct |
12 |
Correct |
344 ms |
774700 KB |
Output is correct |
13 |
Correct |
345 ms |
773712 KB |
Output is correct |
14 |
Correct |
345 ms |
774108 KB |
Output is correct |
15 |
Correct |
346 ms |
774068 KB |
Output is correct |
16 |
Correct |
344 ms |
773976 KB |
Output is correct |
17 |
Correct |
365 ms |
774336 KB |
Output is correct |
18 |
Correct |
339 ms |
774008 KB |
Output is correct |
19 |
Correct |
343 ms |
773968 KB |
Output is correct |
20 |
Correct |
356 ms |
774304 KB |
Output is correct |
21 |
Correct |
346 ms |
773096 KB |
Output is correct |
22 |
Correct |
342 ms |
773724 KB |
Output is correct |
23 |
Correct |
387 ms |
773956 KB |
Output is correct |
24 |
Correct |
341 ms |
774084 KB |
Output is correct |
25 |
Correct |
333 ms |
774292 KB |
Output is correct |
26 |
Correct |
350 ms |
774356 KB |
Output is correct |
27 |
Correct |
339 ms |
773068 KB |
Output is correct |
28 |
Correct |
357 ms |
774096 KB |
Output is correct |
29 |
Correct |
369 ms |
774084 KB |
Output is correct |
30 |
Correct |
335 ms |
773928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
772888 KB |
Output is correct |
2 |
Correct |
340 ms |
772916 KB |
Output is correct |
3 |
Correct |
333 ms |
773036 KB |
Output is correct |
4 |
Correct |
344 ms |
773040 KB |
Output is correct |
5 |
Correct |
366 ms |
773044 KB |
Output is correct |
6 |
Correct |
354 ms |
774988 KB |
Output is correct |
7 |
Correct |
344 ms |
773576 KB |
Output is correct |
8 |
Correct |
362 ms |
774220 KB |
Output is correct |
9 |
Correct |
337 ms |
773792 KB |
Output is correct |
10 |
Correct |
341 ms |
774936 KB |
Output is correct |
11 |
Correct |
342 ms |
774076 KB |
Output is correct |
12 |
Correct |
344 ms |
774700 KB |
Output is correct |
13 |
Correct |
345 ms |
773712 KB |
Output is correct |
14 |
Correct |
345 ms |
774108 KB |
Output is correct |
15 |
Correct |
346 ms |
774068 KB |
Output is correct |
16 |
Correct |
344 ms |
773976 KB |
Output is correct |
17 |
Correct |
365 ms |
774336 KB |
Output is correct |
18 |
Correct |
339 ms |
774008 KB |
Output is correct |
19 |
Correct |
343 ms |
773968 KB |
Output is correct |
20 |
Correct |
356 ms |
774304 KB |
Output is correct |
21 |
Correct |
346 ms |
773096 KB |
Output is correct |
22 |
Correct |
342 ms |
773724 KB |
Output is correct |
23 |
Correct |
387 ms |
773956 KB |
Output is correct |
24 |
Correct |
341 ms |
774084 KB |
Output is correct |
25 |
Correct |
333 ms |
774292 KB |
Output is correct |
26 |
Correct |
350 ms |
774356 KB |
Output is correct |
27 |
Correct |
339 ms |
773068 KB |
Output is correct |
28 |
Correct |
357 ms |
774096 KB |
Output is correct |
29 |
Correct |
369 ms |
774084 KB |
Output is correct |
30 |
Correct |
335 ms |
773928 KB |
Output is correct |
31 |
Correct |
3728 ms |
981436 KB |
Output is correct |
32 |
Correct |
520 ms |
779260 KB |
Output is correct |
33 |
Correct |
3135 ms |
970480 KB |
Output is correct |
34 |
Correct |
3505 ms |
979872 KB |
Output is correct |
35 |
Correct |
3369 ms |
979444 KB |
Output is correct |
36 |
Correct |
2876 ms |
969964 KB |
Output is correct |
37 |
Correct |
2582 ms |
970804 KB |
Output is correct |
38 |
Correct |
2267 ms |
965520 KB |
Output is correct |
39 |
Correct |
1538 ms |
962716 KB |
Output is correct |
40 |
Correct |
1688 ms |
963596 KB |
Output is correct |
41 |
Correct |
2481 ms |
904432 KB |
Output is correct |
42 |
Correct |
2436 ms |
909628 KB |
Output is correct |
43 |
Correct |
411 ms |
780708 KB |
Output is correct |
44 |
Correct |
2412 ms |
904460 KB |
Output is correct |
45 |
Correct |
2197 ms |
902248 KB |
Output is correct |
46 |
Correct |
1861 ms |
905224 KB |
Output is correct |
47 |
Correct |
1091 ms |
888640 KB |
Output is correct |
48 |
Correct |
1136 ms |
895160 KB |
Output is correct |
49 |
Correct |
1272 ms |
901488 KB |
Output is correct |
50 |
Correct |
1382 ms |
902404 KB |
Output is correct |
51 |
Correct |
1394 ms |
903208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3058 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3242 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
772888 KB |
Output is correct |
2 |
Correct |
340 ms |
772916 KB |
Output is correct |
3 |
Correct |
333 ms |
773036 KB |
Output is correct |
4 |
Correct |
344 ms |
773040 KB |
Output is correct |
5 |
Correct |
366 ms |
773044 KB |
Output is correct |
6 |
Correct |
354 ms |
774988 KB |
Output is correct |
7 |
Correct |
344 ms |
773576 KB |
Output is correct |
8 |
Correct |
362 ms |
774220 KB |
Output is correct |
9 |
Correct |
337 ms |
773792 KB |
Output is correct |
10 |
Correct |
341 ms |
774936 KB |
Output is correct |
11 |
Correct |
342 ms |
774076 KB |
Output is correct |
12 |
Correct |
344 ms |
774700 KB |
Output is correct |
13 |
Correct |
345 ms |
773712 KB |
Output is correct |
14 |
Correct |
345 ms |
774108 KB |
Output is correct |
15 |
Correct |
346 ms |
774068 KB |
Output is correct |
16 |
Correct |
344 ms |
773976 KB |
Output is correct |
17 |
Correct |
365 ms |
774336 KB |
Output is correct |
18 |
Correct |
339 ms |
774008 KB |
Output is correct |
19 |
Correct |
343 ms |
773968 KB |
Output is correct |
20 |
Correct |
356 ms |
774304 KB |
Output is correct |
21 |
Correct |
346 ms |
773096 KB |
Output is correct |
22 |
Correct |
342 ms |
773724 KB |
Output is correct |
23 |
Correct |
387 ms |
773956 KB |
Output is correct |
24 |
Correct |
341 ms |
774084 KB |
Output is correct |
25 |
Correct |
333 ms |
774292 KB |
Output is correct |
26 |
Correct |
350 ms |
774356 KB |
Output is correct |
27 |
Correct |
339 ms |
773068 KB |
Output is correct |
28 |
Correct |
357 ms |
774096 KB |
Output is correct |
29 |
Correct |
369 ms |
774084 KB |
Output is correct |
30 |
Correct |
335 ms |
773928 KB |
Output is correct |
31 |
Correct |
3728 ms |
981436 KB |
Output is correct |
32 |
Correct |
520 ms |
779260 KB |
Output is correct |
33 |
Correct |
3135 ms |
970480 KB |
Output is correct |
34 |
Correct |
3505 ms |
979872 KB |
Output is correct |
35 |
Correct |
3369 ms |
979444 KB |
Output is correct |
36 |
Correct |
2876 ms |
969964 KB |
Output is correct |
37 |
Correct |
2582 ms |
970804 KB |
Output is correct |
38 |
Correct |
2267 ms |
965520 KB |
Output is correct |
39 |
Correct |
1538 ms |
962716 KB |
Output is correct |
40 |
Correct |
1688 ms |
963596 KB |
Output is correct |
41 |
Correct |
2481 ms |
904432 KB |
Output is correct |
42 |
Correct |
2436 ms |
909628 KB |
Output is correct |
43 |
Correct |
411 ms |
780708 KB |
Output is correct |
44 |
Correct |
2412 ms |
904460 KB |
Output is correct |
45 |
Correct |
2197 ms |
902248 KB |
Output is correct |
46 |
Correct |
1861 ms |
905224 KB |
Output is correct |
47 |
Correct |
1091 ms |
888640 KB |
Output is correct |
48 |
Correct |
1136 ms |
895160 KB |
Output is correct |
49 |
Correct |
1272 ms |
901488 KB |
Output is correct |
50 |
Correct |
1382 ms |
902404 KB |
Output is correct |
51 |
Correct |
1394 ms |
903208 KB |
Output is correct |
52 |
Correct |
972 ms |
840320 KB |
Output is correct |
53 |
Correct |
988 ms |
839756 KB |
Output is correct |
54 |
Correct |
2120 ms |
902456 KB |
Output is correct |
55 |
Correct |
1954 ms |
887304 KB |
Output is correct |
56 |
Correct |
1667 ms |
875924 KB |
Output is correct |
57 |
Correct |
2283 ms |
901256 KB |
Output is correct |
58 |
Correct |
2189 ms |
888788 KB |
Output is correct |
59 |
Correct |
1858 ms |
877720 KB |
Output is correct |
60 |
Correct |
3061 ms |
904352 KB |
Output is correct |
61 |
Correct |
547 ms |
789208 KB |
Output is correct |
62 |
Correct |
1262 ms |
843160 KB |
Output is correct |
63 |
Correct |
2063 ms |
879300 KB |
Output is correct |
64 |
Correct |
2433 ms |
892444 KB |
Output is correct |
65 |
Correct |
2894 ms |
911168 KB |
Output is correct |
66 |
Correct |
3142 ms |
910140 KB |
Output is correct |
67 |
Correct |
716 ms |
792048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
772888 KB |
Output is correct |
2 |
Correct |
340 ms |
772916 KB |
Output is correct |
3 |
Correct |
333 ms |
773036 KB |
Output is correct |
4 |
Correct |
344 ms |
773040 KB |
Output is correct |
5 |
Correct |
366 ms |
773044 KB |
Output is correct |
6 |
Correct |
354 ms |
774988 KB |
Output is correct |
7 |
Correct |
344 ms |
773576 KB |
Output is correct |
8 |
Correct |
362 ms |
774220 KB |
Output is correct |
9 |
Correct |
337 ms |
773792 KB |
Output is correct |
10 |
Correct |
341 ms |
774936 KB |
Output is correct |
11 |
Correct |
342 ms |
774076 KB |
Output is correct |
12 |
Correct |
344 ms |
774700 KB |
Output is correct |
13 |
Correct |
345 ms |
773712 KB |
Output is correct |
14 |
Correct |
345 ms |
774108 KB |
Output is correct |
15 |
Correct |
346 ms |
774068 KB |
Output is correct |
16 |
Correct |
344 ms |
773976 KB |
Output is correct |
17 |
Correct |
365 ms |
774336 KB |
Output is correct |
18 |
Correct |
339 ms |
774008 KB |
Output is correct |
19 |
Correct |
343 ms |
773968 KB |
Output is correct |
20 |
Correct |
356 ms |
774304 KB |
Output is correct |
21 |
Correct |
346 ms |
773096 KB |
Output is correct |
22 |
Correct |
342 ms |
773724 KB |
Output is correct |
23 |
Correct |
387 ms |
773956 KB |
Output is correct |
24 |
Correct |
341 ms |
774084 KB |
Output is correct |
25 |
Correct |
333 ms |
774292 KB |
Output is correct |
26 |
Correct |
350 ms |
774356 KB |
Output is correct |
27 |
Correct |
339 ms |
773068 KB |
Output is correct |
28 |
Correct |
357 ms |
774096 KB |
Output is correct |
29 |
Correct |
369 ms |
774084 KB |
Output is correct |
30 |
Correct |
335 ms |
773928 KB |
Output is correct |
31 |
Correct |
3728 ms |
981436 KB |
Output is correct |
32 |
Correct |
520 ms |
779260 KB |
Output is correct |
33 |
Correct |
3135 ms |
970480 KB |
Output is correct |
34 |
Correct |
3505 ms |
979872 KB |
Output is correct |
35 |
Correct |
3369 ms |
979444 KB |
Output is correct |
36 |
Correct |
2876 ms |
969964 KB |
Output is correct |
37 |
Correct |
2582 ms |
970804 KB |
Output is correct |
38 |
Correct |
2267 ms |
965520 KB |
Output is correct |
39 |
Correct |
1538 ms |
962716 KB |
Output is correct |
40 |
Correct |
1688 ms |
963596 KB |
Output is correct |
41 |
Correct |
2481 ms |
904432 KB |
Output is correct |
42 |
Correct |
2436 ms |
909628 KB |
Output is correct |
43 |
Correct |
411 ms |
780708 KB |
Output is correct |
44 |
Correct |
2412 ms |
904460 KB |
Output is correct |
45 |
Correct |
2197 ms |
902248 KB |
Output is correct |
46 |
Correct |
1861 ms |
905224 KB |
Output is correct |
47 |
Correct |
1091 ms |
888640 KB |
Output is correct |
48 |
Correct |
1136 ms |
895160 KB |
Output is correct |
49 |
Correct |
1272 ms |
901488 KB |
Output is correct |
50 |
Correct |
1382 ms |
902404 KB |
Output is correct |
51 |
Correct |
1394 ms |
903208 KB |
Output is correct |
52 |
Runtime error |
3058 ms |
1048576 KB |
Execution killed with signal 9 |
53 |
Halted |
0 ms |
0 KB |
- |