#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int INF = 1e9 + 100;
struct SegLeft{
vector<int> X;
int tree[600600], sz;
pair<int, int> buf[10001000];
int lsz;
inline void push(int i, int x){
if (tree[i] <= x) return;
buf[lsz++] = {i, tree[i]};
tree[i] = x;
}
inline void pop(){
--lsz;
tree[buf[lsz].first] = buf[lsz].second;
}
void init(const vector<int> &_X){
X = _X;
sz = X.size();
lsz = 0;
fill(tree, tree+sz*2, INF);
}
void update(int l, int r, int x){
if (l==-INF) l = 0;
else l = lower_bound(X.begin(), X.end(), l) - X.begin();
if (r==INF) r = (int)X.size();
else r = upper_bound(X.begin(), X.end(), r) - X.begin();
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1){
push(l, x);
l++;
}
if (r&1){
--r;
push(r, x);
}
}
}
int query(int p){
p = lower_bound(X.begin(), X.end(), p) - X.begin();
int ret = INF;
for (p+=sz;p;p>>=1) ret = min(ret, tree[p]);
return ret;
}
void rollback(int lsz0){
while(lsz > lsz0) pop();
}
}treeL;
struct SegRight{
vector<int> X;
int tree[600600], sz;
pair<int, int> buf[10001000];
int lsz;
inline void push(int i, int x){
if (tree[i] >= x) return;
buf[lsz++] = {i, tree[i]};
tree[i] = x;
}
inline void pop(){
--lsz;
tree[buf[lsz].first] = buf[lsz].second;
}
void init(const vector<int> &_X){
X = _X;
sz = X.size();
lsz = 0;
fill(tree, tree+sz*2, -INF);
}
void update(int l, int r, int x){
if (l==-INF) l = 0;
else l = lower_bound(X.begin(), X.end(), l) - X.begin();
if (r==INF) r = (int)X.size();
else r = upper_bound(X.begin(), X.end(), r) - X.begin();
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1){
push(l, x);
l++;
}
if (r&1){
--r;
push(r, x);
}
}
}
int query(int p){
p = lower_bound(X.begin(), X.end(), p) - X.begin();
int ret = -INF;
for (p+=sz;p;p>>=1) ret = max(ret, tree[p]);
return ret;
}
void rollback(int lsz0){
while(lsz > lsz0) pop();
}
}treeR;
int pos[300300], col[300300], a[300300], b[300300];
int posQ[300300], tQ[300300], ans[300300];
vector<int> T, X, buf[1201200], bufQ[300300];
multiset<int> st[300300];
void insert(int i, int l, int r, int s, int e, int x){
if (r<s || e<l) return;
if (s<=l && r<=e){
buf[i].push_back(x);
return;
}
int m = (l+r)>>1;
insert(i<<1, l, m, s, e, x);
insert(i<<1|1, m+1, r, s, e, x);
}
void dnc(int i, int l, int r){
int szL = treeL.lsz, szR = treeR.lsz;
for (auto &idx:buf[i]){
int c = col[idx], p = pos[idx];
auto iter = st[c].find(p);
auto niter = next(iter);
if (iter==st[c].begin()){
if (niter==st[c].end()){
treeL.update(-INF, INF, -INF);
}
else{
treeR.update(-INF, *niter, *niter);
}
}
else{
auto piter = prev(iter);
if (niter==st[c].end()){
treeL.update(*piter, INF, *piter);
}
else{
int mid = (*piter + *niter) / 2;
treeL.update(*piter, mid, *piter);
treeR.update(mid+1, *niter, *niter);
}
}
st[c].erase(iter);
}
if (l==r){
for (auto &idx:bufQ[l]){
int pl = treeL.query(posQ[idx]);
int pr = treeR.query(posQ[idx]);
if (pl==-INF || pr==INF) ans[idx] = -1;
else ans[idx] = max(posQ[idx]-pl, pr-posQ[idx]);
}
}
else{
int m = (l+r)>>1;
dnc(i<<1, l, m);
dnc(i<<1|1, m+1, r);
}
for (auto &idx:buf[i]) st[col[idx]].insert(pos[idx]);
treeL.rollback(szL);
treeR.rollback(szR);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, q;
cin >> n >> k >> q;
for (int i=1;i<=n;i++){
cin >> pos[i] >> col[i] >> a[i] >> b[i];
}
for (int i=1;i<=q;i++){
cin >> posQ[i] >> tQ[i];
T.push_back(tQ[i]);
X.push_back(posQ[i]);
}
sort(T.begin(), T.end());
T.erase(unique(T.begin(), T.end()), T.end());
sort(X.begin(), X.end());
X.erase(unique(X.begin(), X.end()), X.end());
treeL.init(X); treeR.init(X);
for (int i=1;i<=q;i++){
tQ[i] = lower_bound(T.begin(), T.end(), tQ[i]) - T.begin() + 1;
bufQ[tQ[i]].push_back(i);
// printf(" ok t = %d\n", i);
}
for (int i=1;i<=n;i++){
a[i] = lower_bound(T.begin(), T.end(), a[i]) - T.begin() + 1;
b[i] = upper_bound(T.begin(), T.end(), b[i]) - T.begin() + 1;
// printf(" ok remove (%d, %d) and (%d, %d)\n", 1, a[i]-1, b[i], (int)T.size());
insert(1, 1, T.size(), 1, a[i]-1, i);
insert(1, 1, T.size(), b[i], T.size(), i);
}
for (int i=1;i<=n;i++){
st[col[i]].insert(pos[i]);
}
for (int i=1;i<=k;i++){
if (st[i].empty()) {treeL.update(-INF, INF, -INF); continue;}
int p = -INF;
for (auto x:st[i]){
if (p==-INF) treeR.update(-INF, x, x);
else{
int mid = (p+x)/2;
treeL.update(p, mid, p);
treeR.update(mid+1, x, x);
}
p = x;
}
treeL.update(p, INF, p);
}
dnc(1, 1, T.size());
for (int i=1;i<=q;i++) printf("%d\n", ans[i]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
211024 KB |
Output is correct |
2 |
Correct |
56 ms |
211032 KB |
Output is correct |
3 |
Correct |
56 ms |
210976 KB |
Output is correct |
4 |
Correct |
56 ms |
211068 KB |
Output is correct |
5 |
Correct |
58 ms |
211028 KB |
Output is correct |
6 |
Correct |
58 ms |
211028 KB |
Output is correct |
7 |
Correct |
61 ms |
211028 KB |
Output is correct |
8 |
Correct |
57 ms |
211028 KB |
Output is correct |
9 |
Correct |
63 ms |
211284 KB |
Output is correct |
10 |
Correct |
56 ms |
211028 KB |
Output is correct |
11 |
Correct |
57 ms |
210972 KB |
Output is correct |
12 |
Correct |
57 ms |
211028 KB |
Output is correct |
13 |
Correct |
64 ms |
211140 KB |
Output is correct |
14 |
Correct |
56 ms |
211024 KB |
Output is correct |
15 |
Correct |
57 ms |
211080 KB |
Output is correct |
16 |
Correct |
58 ms |
211024 KB |
Output is correct |
17 |
Correct |
57 ms |
211028 KB |
Output is correct |
18 |
Correct |
59 ms |
211044 KB |
Output is correct |
19 |
Correct |
66 ms |
211148 KB |
Output is correct |
20 |
Correct |
56 ms |
211024 KB |
Output is correct |
21 |
Correct |
60 ms |
211112 KB |
Output is correct |
22 |
Correct |
56 ms |
211028 KB |
Output is correct |
23 |
Correct |
58 ms |
211172 KB |
Output is correct |
24 |
Correct |
73 ms |
211028 KB |
Output is correct |
25 |
Correct |
57 ms |
210924 KB |
Output is correct |
26 |
Correct |
58 ms |
211012 KB |
Output is correct |
27 |
Correct |
67 ms |
211028 KB |
Output is correct |
28 |
Correct |
58 ms |
211028 KB |
Output is correct |
29 |
Correct |
57 ms |
211028 KB |
Output is correct |
30 |
Correct |
56 ms |
211120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
211024 KB |
Output is correct |
2 |
Correct |
56 ms |
211032 KB |
Output is correct |
3 |
Correct |
56 ms |
210976 KB |
Output is correct |
4 |
Correct |
56 ms |
211068 KB |
Output is correct |
5 |
Correct |
58 ms |
211028 KB |
Output is correct |
6 |
Correct |
58 ms |
211028 KB |
Output is correct |
7 |
Correct |
61 ms |
211028 KB |
Output is correct |
8 |
Correct |
57 ms |
211028 KB |
Output is correct |
9 |
Correct |
63 ms |
211284 KB |
Output is correct |
10 |
Correct |
56 ms |
211028 KB |
Output is correct |
11 |
Correct |
57 ms |
210972 KB |
Output is correct |
12 |
Correct |
57 ms |
211028 KB |
Output is correct |
13 |
Correct |
64 ms |
211140 KB |
Output is correct |
14 |
Correct |
56 ms |
211024 KB |
Output is correct |
15 |
Correct |
57 ms |
211080 KB |
Output is correct |
16 |
Correct |
58 ms |
211024 KB |
Output is correct |
17 |
Correct |
57 ms |
211028 KB |
Output is correct |
18 |
Correct |
59 ms |
211044 KB |
Output is correct |
19 |
Correct |
66 ms |
211148 KB |
Output is correct |
20 |
Correct |
56 ms |
211024 KB |
Output is correct |
21 |
Correct |
60 ms |
211112 KB |
Output is correct |
22 |
Correct |
56 ms |
211028 KB |
Output is correct |
23 |
Correct |
58 ms |
211172 KB |
Output is correct |
24 |
Correct |
73 ms |
211028 KB |
Output is correct |
25 |
Correct |
57 ms |
210924 KB |
Output is correct |
26 |
Correct |
58 ms |
211012 KB |
Output is correct |
27 |
Correct |
67 ms |
211028 KB |
Output is correct |
28 |
Correct |
58 ms |
211028 KB |
Output is correct |
29 |
Correct |
57 ms |
211028 KB |
Output is correct |
30 |
Correct |
56 ms |
211120 KB |
Output is correct |
31 |
Correct |
1010 ms |
226764 KB |
Output is correct |
32 |
Correct |
191 ms |
217908 KB |
Output is correct |
33 |
Correct |
970 ms |
227436 KB |
Output is correct |
34 |
Correct |
951 ms |
227760 KB |
Output is correct |
35 |
Correct |
955 ms |
226712 KB |
Output is correct |
36 |
Correct |
901 ms |
226760 KB |
Output is correct |
37 |
Correct |
782 ms |
227532 KB |
Output is correct |
38 |
Correct |
756 ms |
227640 KB |
Output is correct |
39 |
Correct |
628 ms |
227508 KB |
Output is correct |
40 |
Correct |
669 ms |
227428 KB |
Output is correct |
41 |
Correct |
723 ms |
227800 KB |
Output is correct |
42 |
Correct |
720 ms |
227568 KB |
Output is correct |
43 |
Correct |
92 ms |
218828 KB |
Output is correct |
44 |
Correct |
725 ms |
227868 KB |
Output is correct |
45 |
Correct |
805 ms |
227792 KB |
Output is correct |
46 |
Correct |
825 ms |
228044 KB |
Output is correct |
47 |
Correct |
472 ms |
226944 KB |
Output is correct |
48 |
Correct |
485 ms |
226820 KB |
Output is correct |
49 |
Correct |
545 ms |
227244 KB |
Output is correct |
50 |
Correct |
539 ms |
227928 KB |
Output is correct |
51 |
Correct |
585 ms |
227052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
818 ms |
260784 KB |
Output is correct |
2 |
Correct |
690 ms |
258612 KB |
Output is correct |
3 |
Correct |
752 ms |
260800 KB |
Output is correct |
4 |
Correct |
901 ms |
261168 KB |
Output is correct |
5 |
Correct |
636 ms |
258220 KB |
Output is correct |
6 |
Correct |
671 ms |
258460 KB |
Output is correct |
7 |
Correct |
655 ms |
260916 KB |
Output is correct |
8 |
Correct |
744 ms |
260948 KB |
Output is correct |
9 |
Correct |
750 ms |
260848 KB |
Output is correct |
10 |
Correct |
727 ms |
259364 KB |
Output is correct |
11 |
Correct |
487 ms |
255376 KB |
Output is correct |
12 |
Correct |
590 ms |
256432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5073 ms |
276424 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
211024 KB |
Output is correct |
2 |
Correct |
56 ms |
211032 KB |
Output is correct |
3 |
Correct |
56 ms |
210976 KB |
Output is correct |
4 |
Correct |
56 ms |
211068 KB |
Output is correct |
5 |
Correct |
58 ms |
211028 KB |
Output is correct |
6 |
Correct |
58 ms |
211028 KB |
Output is correct |
7 |
Correct |
61 ms |
211028 KB |
Output is correct |
8 |
Correct |
57 ms |
211028 KB |
Output is correct |
9 |
Correct |
63 ms |
211284 KB |
Output is correct |
10 |
Correct |
56 ms |
211028 KB |
Output is correct |
11 |
Correct |
57 ms |
210972 KB |
Output is correct |
12 |
Correct |
57 ms |
211028 KB |
Output is correct |
13 |
Correct |
64 ms |
211140 KB |
Output is correct |
14 |
Correct |
56 ms |
211024 KB |
Output is correct |
15 |
Correct |
57 ms |
211080 KB |
Output is correct |
16 |
Correct |
58 ms |
211024 KB |
Output is correct |
17 |
Correct |
57 ms |
211028 KB |
Output is correct |
18 |
Correct |
59 ms |
211044 KB |
Output is correct |
19 |
Correct |
66 ms |
211148 KB |
Output is correct |
20 |
Correct |
56 ms |
211024 KB |
Output is correct |
21 |
Correct |
60 ms |
211112 KB |
Output is correct |
22 |
Correct |
56 ms |
211028 KB |
Output is correct |
23 |
Correct |
58 ms |
211172 KB |
Output is correct |
24 |
Correct |
73 ms |
211028 KB |
Output is correct |
25 |
Correct |
57 ms |
210924 KB |
Output is correct |
26 |
Correct |
58 ms |
211012 KB |
Output is correct |
27 |
Correct |
67 ms |
211028 KB |
Output is correct |
28 |
Correct |
58 ms |
211028 KB |
Output is correct |
29 |
Correct |
57 ms |
211028 KB |
Output is correct |
30 |
Correct |
56 ms |
211120 KB |
Output is correct |
31 |
Correct |
1010 ms |
226764 KB |
Output is correct |
32 |
Correct |
191 ms |
217908 KB |
Output is correct |
33 |
Correct |
970 ms |
227436 KB |
Output is correct |
34 |
Correct |
951 ms |
227760 KB |
Output is correct |
35 |
Correct |
955 ms |
226712 KB |
Output is correct |
36 |
Correct |
901 ms |
226760 KB |
Output is correct |
37 |
Correct |
782 ms |
227532 KB |
Output is correct |
38 |
Correct |
756 ms |
227640 KB |
Output is correct |
39 |
Correct |
628 ms |
227508 KB |
Output is correct |
40 |
Correct |
669 ms |
227428 KB |
Output is correct |
41 |
Correct |
723 ms |
227800 KB |
Output is correct |
42 |
Correct |
720 ms |
227568 KB |
Output is correct |
43 |
Correct |
92 ms |
218828 KB |
Output is correct |
44 |
Correct |
725 ms |
227868 KB |
Output is correct |
45 |
Correct |
805 ms |
227792 KB |
Output is correct |
46 |
Correct |
825 ms |
228044 KB |
Output is correct |
47 |
Correct |
472 ms |
226944 KB |
Output is correct |
48 |
Correct |
485 ms |
226820 KB |
Output is correct |
49 |
Correct |
545 ms |
227244 KB |
Output is correct |
50 |
Correct |
539 ms |
227928 KB |
Output is correct |
51 |
Correct |
585 ms |
227052 KB |
Output is correct |
52 |
Correct |
332 ms |
226764 KB |
Output is correct |
53 |
Correct |
325 ms |
227660 KB |
Output is correct |
54 |
Correct |
562 ms |
226704 KB |
Output is correct |
55 |
Correct |
548 ms |
227988 KB |
Output is correct |
56 |
Correct |
463 ms |
227296 KB |
Output is correct |
57 |
Correct |
702 ms |
228096 KB |
Output is correct |
58 |
Correct |
566 ms |
228304 KB |
Output is correct |
59 |
Correct |
486 ms |
227788 KB |
Output is correct |
60 |
Correct |
686 ms |
228048 KB |
Output is correct |
61 |
Correct |
86 ms |
218936 KB |
Output is correct |
62 |
Correct |
285 ms |
226928 KB |
Output is correct |
63 |
Correct |
423 ms |
227332 KB |
Output is correct |
64 |
Correct |
470 ms |
227628 KB |
Output is correct |
65 |
Correct |
603 ms |
228456 KB |
Output is correct |
66 |
Correct |
709 ms |
228308 KB |
Output is correct |
67 |
Correct |
173 ms |
219088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
211024 KB |
Output is correct |
2 |
Correct |
56 ms |
211032 KB |
Output is correct |
3 |
Correct |
56 ms |
210976 KB |
Output is correct |
4 |
Correct |
56 ms |
211068 KB |
Output is correct |
5 |
Correct |
58 ms |
211028 KB |
Output is correct |
6 |
Correct |
58 ms |
211028 KB |
Output is correct |
7 |
Correct |
61 ms |
211028 KB |
Output is correct |
8 |
Correct |
57 ms |
211028 KB |
Output is correct |
9 |
Correct |
63 ms |
211284 KB |
Output is correct |
10 |
Correct |
56 ms |
211028 KB |
Output is correct |
11 |
Correct |
57 ms |
210972 KB |
Output is correct |
12 |
Correct |
57 ms |
211028 KB |
Output is correct |
13 |
Correct |
64 ms |
211140 KB |
Output is correct |
14 |
Correct |
56 ms |
211024 KB |
Output is correct |
15 |
Correct |
57 ms |
211080 KB |
Output is correct |
16 |
Correct |
58 ms |
211024 KB |
Output is correct |
17 |
Correct |
57 ms |
211028 KB |
Output is correct |
18 |
Correct |
59 ms |
211044 KB |
Output is correct |
19 |
Correct |
66 ms |
211148 KB |
Output is correct |
20 |
Correct |
56 ms |
211024 KB |
Output is correct |
21 |
Correct |
60 ms |
211112 KB |
Output is correct |
22 |
Correct |
56 ms |
211028 KB |
Output is correct |
23 |
Correct |
58 ms |
211172 KB |
Output is correct |
24 |
Correct |
73 ms |
211028 KB |
Output is correct |
25 |
Correct |
57 ms |
210924 KB |
Output is correct |
26 |
Correct |
58 ms |
211012 KB |
Output is correct |
27 |
Correct |
67 ms |
211028 KB |
Output is correct |
28 |
Correct |
58 ms |
211028 KB |
Output is correct |
29 |
Correct |
57 ms |
211028 KB |
Output is correct |
30 |
Correct |
56 ms |
211120 KB |
Output is correct |
31 |
Correct |
1010 ms |
226764 KB |
Output is correct |
32 |
Correct |
191 ms |
217908 KB |
Output is correct |
33 |
Correct |
970 ms |
227436 KB |
Output is correct |
34 |
Correct |
951 ms |
227760 KB |
Output is correct |
35 |
Correct |
955 ms |
226712 KB |
Output is correct |
36 |
Correct |
901 ms |
226760 KB |
Output is correct |
37 |
Correct |
782 ms |
227532 KB |
Output is correct |
38 |
Correct |
756 ms |
227640 KB |
Output is correct |
39 |
Correct |
628 ms |
227508 KB |
Output is correct |
40 |
Correct |
669 ms |
227428 KB |
Output is correct |
41 |
Correct |
723 ms |
227800 KB |
Output is correct |
42 |
Correct |
720 ms |
227568 KB |
Output is correct |
43 |
Correct |
92 ms |
218828 KB |
Output is correct |
44 |
Correct |
725 ms |
227868 KB |
Output is correct |
45 |
Correct |
805 ms |
227792 KB |
Output is correct |
46 |
Correct |
825 ms |
228044 KB |
Output is correct |
47 |
Correct |
472 ms |
226944 KB |
Output is correct |
48 |
Correct |
485 ms |
226820 KB |
Output is correct |
49 |
Correct |
545 ms |
227244 KB |
Output is correct |
50 |
Correct |
539 ms |
227928 KB |
Output is correct |
51 |
Correct |
585 ms |
227052 KB |
Output is correct |
52 |
Correct |
818 ms |
260784 KB |
Output is correct |
53 |
Correct |
690 ms |
258612 KB |
Output is correct |
54 |
Correct |
752 ms |
260800 KB |
Output is correct |
55 |
Correct |
901 ms |
261168 KB |
Output is correct |
56 |
Correct |
636 ms |
258220 KB |
Output is correct |
57 |
Correct |
671 ms |
258460 KB |
Output is correct |
58 |
Correct |
655 ms |
260916 KB |
Output is correct |
59 |
Correct |
744 ms |
260948 KB |
Output is correct |
60 |
Correct |
750 ms |
260848 KB |
Output is correct |
61 |
Correct |
727 ms |
259364 KB |
Output is correct |
62 |
Correct |
487 ms |
255376 KB |
Output is correct |
63 |
Correct |
590 ms |
256432 KB |
Output is correct |
64 |
Execution timed out |
5073 ms |
276424 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |