Submission #985064

# Submission time Handle Problem Language Result Execution time Memory
985064 2024-05-17T10:13:25 Z qwerasdfzxcl New Home (APIO18_new_home) C++17
80 / 100
5000 ms 297468 KB
#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){
		l = lower_bound(X.begin(), X.end(), l) - X.begin();
		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){
		l = lower_bound(X.begin(), X.end(), l) - X.begin();
		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 44 ms 211024 KB Output is correct
2 Correct 39 ms 211152 KB Output is correct
3 Correct 42 ms 211120 KB Output is correct
4 Correct 40 ms 211036 KB Output is correct
5 Correct 41 ms 211028 KB Output is correct
6 Correct 43 ms 211180 KB Output is correct
7 Correct 46 ms 211116 KB Output is correct
8 Correct 40 ms 211028 KB Output is correct
9 Correct 40 ms 211028 KB Output is correct
10 Correct 44 ms 211060 KB Output is correct
11 Correct 42 ms 211140 KB Output is correct
12 Correct 48 ms 210968 KB Output is correct
13 Correct 42 ms 211028 KB Output is correct
14 Correct 40 ms 211032 KB Output is correct
15 Correct 44 ms 211040 KB Output is correct
16 Correct 41 ms 211036 KB Output is correct
17 Correct 41 ms 211032 KB Output is correct
18 Correct 40 ms 211036 KB Output is correct
19 Correct 40 ms 211028 KB Output is correct
20 Correct 41 ms 211440 KB Output is correct
21 Correct 39 ms 211028 KB Output is correct
22 Correct 39 ms 211028 KB Output is correct
23 Correct 40 ms 211080 KB Output is correct
24 Correct 40 ms 211000 KB Output is correct
25 Correct 45 ms 210984 KB Output is correct
26 Correct 40 ms 211060 KB Output is correct
27 Correct 39 ms 211028 KB Output is correct
28 Correct 43 ms 211024 KB Output is correct
29 Correct 40 ms 211024 KB Output is correct
30 Correct 39 ms 211200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 211024 KB Output is correct
2 Correct 39 ms 211152 KB Output is correct
3 Correct 42 ms 211120 KB Output is correct
4 Correct 40 ms 211036 KB Output is correct
5 Correct 41 ms 211028 KB Output is correct
6 Correct 43 ms 211180 KB Output is correct
7 Correct 46 ms 211116 KB Output is correct
8 Correct 40 ms 211028 KB Output is correct
9 Correct 40 ms 211028 KB Output is correct
10 Correct 44 ms 211060 KB Output is correct
11 Correct 42 ms 211140 KB Output is correct
12 Correct 48 ms 210968 KB Output is correct
13 Correct 42 ms 211028 KB Output is correct
14 Correct 40 ms 211032 KB Output is correct
15 Correct 44 ms 211040 KB Output is correct
16 Correct 41 ms 211036 KB Output is correct
17 Correct 41 ms 211032 KB Output is correct
18 Correct 40 ms 211036 KB Output is correct
19 Correct 40 ms 211028 KB Output is correct
20 Correct 41 ms 211440 KB Output is correct
21 Correct 39 ms 211028 KB Output is correct
22 Correct 39 ms 211028 KB Output is correct
23 Correct 40 ms 211080 KB Output is correct
24 Correct 40 ms 211000 KB Output is correct
25 Correct 45 ms 210984 KB Output is correct
26 Correct 40 ms 211060 KB Output is correct
27 Correct 39 ms 211028 KB Output is correct
28 Correct 43 ms 211024 KB Output is correct
29 Correct 40 ms 211024 KB Output is correct
30 Correct 39 ms 211200 KB Output is correct
31 Correct 903 ms 224156 KB Output is correct
32 Correct 152 ms 216784 KB Output is correct
33 Correct 891 ms 224868 KB Output is correct
34 Correct 944 ms 224868 KB Output is correct
35 Correct 931 ms 224204 KB Output is correct
36 Correct 912 ms 224208 KB Output is correct
37 Correct 868 ms 225264 KB Output is correct
38 Correct 749 ms 225148 KB Output is correct
39 Correct 614 ms 224976 KB Output is correct
40 Correct 618 ms 225036 KB Output is correct
41 Correct 696 ms 225236 KB Output is correct
42 Correct 707 ms 225008 KB Output is correct
43 Correct 72 ms 216524 KB Output is correct
44 Correct 717 ms 225140 KB Output is correct
45 Correct 746 ms 225488 KB Output is correct
46 Correct 813 ms 225484 KB Output is correct
47 Correct 454 ms 224312 KB Output is correct
48 Correct 456 ms 224540 KB Output is correct
49 Correct 545 ms 224896 KB Output is correct
50 Correct 518 ms 225248 KB Output is correct
51 Correct 575 ms 224704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 801 ms 250196 KB Output is correct
2 Correct 741 ms 261420 KB Output is correct
3 Correct 743 ms 264100 KB Output is correct
4 Correct 848 ms 263660 KB Output is correct
5 Correct 633 ms 261036 KB Output is correct
6 Correct 680 ms 261652 KB Output is correct
7 Correct 677 ms 263968 KB Output is correct
8 Correct 781 ms 263632 KB Output is correct
9 Correct 739 ms 263576 KB Output is correct
10 Correct 678 ms 262448 KB Output is correct
11 Correct 474 ms 261448 KB Output is correct
12 Correct 539 ms 262568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4729 ms 276164 KB Output is correct
2 Correct 540 ms 242096 KB Output is correct
3 Correct 4789 ms 281072 KB Output is correct
4 Correct 1847 ms 281920 KB Output is correct
5 Correct 3378 ms 281520 KB Output is correct
6 Correct 2873 ms 281464 KB Output is correct
7 Correct 4390 ms 280464 KB Output is correct
8 Correct 4876 ms 280976 KB Output is correct
9 Correct 1466 ms 277352 KB Output is correct
10 Correct 2643 ms 279156 KB Output is correct
11 Correct 3625 ms 281772 KB Output is correct
12 Correct 3866 ms 281868 KB Output is correct
13 Correct 1729 ms 278916 KB Output is correct
14 Correct 1662 ms 278684 KB Output is correct
15 Correct 2050 ms 280020 KB Output is correct
16 Correct 2449 ms 280916 KB Output is correct
17 Correct 2140 ms 279716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 211024 KB Output is correct
2 Correct 39 ms 211152 KB Output is correct
3 Correct 42 ms 211120 KB Output is correct
4 Correct 40 ms 211036 KB Output is correct
5 Correct 41 ms 211028 KB Output is correct
6 Correct 43 ms 211180 KB Output is correct
7 Correct 46 ms 211116 KB Output is correct
8 Correct 40 ms 211028 KB Output is correct
9 Correct 40 ms 211028 KB Output is correct
10 Correct 44 ms 211060 KB Output is correct
11 Correct 42 ms 211140 KB Output is correct
12 Correct 48 ms 210968 KB Output is correct
13 Correct 42 ms 211028 KB Output is correct
14 Correct 40 ms 211032 KB Output is correct
15 Correct 44 ms 211040 KB Output is correct
16 Correct 41 ms 211036 KB Output is correct
17 Correct 41 ms 211032 KB Output is correct
18 Correct 40 ms 211036 KB Output is correct
19 Correct 40 ms 211028 KB Output is correct
20 Correct 41 ms 211440 KB Output is correct
21 Correct 39 ms 211028 KB Output is correct
22 Correct 39 ms 211028 KB Output is correct
23 Correct 40 ms 211080 KB Output is correct
24 Correct 40 ms 211000 KB Output is correct
25 Correct 45 ms 210984 KB Output is correct
26 Correct 40 ms 211060 KB Output is correct
27 Correct 39 ms 211028 KB Output is correct
28 Correct 43 ms 211024 KB Output is correct
29 Correct 40 ms 211024 KB Output is correct
30 Correct 39 ms 211200 KB Output is correct
31 Correct 903 ms 224156 KB Output is correct
32 Correct 152 ms 216784 KB Output is correct
33 Correct 891 ms 224868 KB Output is correct
34 Correct 944 ms 224868 KB Output is correct
35 Correct 931 ms 224204 KB Output is correct
36 Correct 912 ms 224208 KB Output is correct
37 Correct 868 ms 225264 KB Output is correct
38 Correct 749 ms 225148 KB Output is correct
39 Correct 614 ms 224976 KB Output is correct
40 Correct 618 ms 225036 KB Output is correct
41 Correct 696 ms 225236 KB Output is correct
42 Correct 707 ms 225008 KB Output is correct
43 Correct 72 ms 216524 KB Output is correct
44 Correct 717 ms 225140 KB Output is correct
45 Correct 746 ms 225488 KB Output is correct
46 Correct 813 ms 225484 KB Output is correct
47 Correct 454 ms 224312 KB Output is correct
48 Correct 456 ms 224540 KB Output is correct
49 Correct 545 ms 224896 KB Output is correct
50 Correct 518 ms 225248 KB Output is correct
51 Correct 575 ms 224704 KB Output is correct
52 Correct 321 ms 232120 KB Output is correct
53 Correct 313 ms 232752 KB Output is correct
54 Correct 522 ms 232164 KB Output is correct
55 Correct 550 ms 233128 KB Output is correct
56 Correct 476 ms 232192 KB Output is correct
57 Correct 647 ms 233164 KB Output is correct
58 Correct 601 ms 233420 KB Output is correct
59 Correct 497 ms 232912 KB Output is correct
60 Correct 650 ms 233420 KB Output is correct
61 Correct 65 ms 222668 KB Output is correct
62 Correct 288 ms 232144 KB Output is correct
63 Correct 410 ms 232508 KB Output is correct
64 Correct 478 ms 232872 KB Output is correct
65 Correct 554 ms 233560 KB Output is correct
66 Correct 688 ms 233496 KB Output is correct
67 Correct 165 ms 223576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 211024 KB Output is correct
2 Correct 39 ms 211152 KB Output is correct
3 Correct 42 ms 211120 KB Output is correct
4 Correct 40 ms 211036 KB Output is correct
5 Correct 41 ms 211028 KB Output is correct
6 Correct 43 ms 211180 KB Output is correct
7 Correct 46 ms 211116 KB Output is correct
8 Correct 40 ms 211028 KB Output is correct
9 Correct 40 ms 211028 KB Output is correct
10 Correct 44 ms 211060 KB Output is correct
11 Correct 42 ms 211140 KB Output is correct
12 Correct 48 ms 210968 KB Output is correct
13 Correct 42 ms 211028 KB Output is correct
14 Correct 40 ms 211032 KB Output is correct
15 Correct 44 ms 211040 KB Output is correct
16 Correct 41 ms 211036 KB Output is correct
17 Correct 41 ms 211032 KB Output is correct
18 Correct 40 ms 211036 KB Output is correct
19 Correct 40 ms 211028 KB Output is correct
20 Correct 41 ms 211440 KB Output is correct
21 Correct 39 ms 211028 KB Output is correct
22 Correct 39 ms 211028 KB Output is correct
23 Correct 40 ms 211080 KB Output is correct
24 Correct 40 ms 211000 KB Output is correct
25 Correct 45 ms 210984 KB Output is correct
26 Correct 40 ms 211060 KB Output is correct
27 Correct 39 ms 211028 KB Output is correct
28 Correct 43 ms 211024 KB Output is correct
29 Correct 40 ms 211024 KB Output is correct
30 Correct 39 ms 211200 KB Output is correct
31 Correct 903 ms 224156 KB Output is correct
32 Correct 152 ms 216784 KB Output is correct
33 Correct 891 ms 224868 KB Output is correct
34 Correct 944 ms 224868 KB Output is correct
35 Correct 931 ms 224204 KB Output is correct
36 Correct 912 ms 224208 KB Output is correct
37 Correct 868 ms 225264 KB Output is correct
38 Correct 749 ms 225148 KB Output is correct
39 Correct 614 ms 224976 KB Output is correct
40 Correct 618 ms 225036 KB Output is correct
41 Correct 696 ms 225236 KB Output is correct
42 Correct 707 ms 225008 KB Output is correct
43 Correct 72 ms 216524 KB Output is correct
44 Correct 717 ms 225140 KB Output is correct
45 Correct 746 ms 225488 KB Output is correct
46 Correct 813 ms 225484 KB Output is correct
47 Correct 454 ms 224312 KB Output is correct
48 Correct 456 ms 224540 KB Output is correct
49 Correct 545 ms 224896 KB Output is correct
50 Correct 518 ms 225248 KB Output is correct
51 Correct 575 ms 224704 KB Output is correct
52 Correct 801 ms 250196 KB Output is correct
53 Correct 741 ms 261420 KB Output is correct
54 Correct 743 ms 264100 KB Output is correct
55 Correct 848 ms 263660 KB Output is correct
56 Correct 633 ms 261036 KB Output is correct
57 Correct 680 ms 261652 KB Output is correct
58 Correct 677 ms 263968 KB Output is correct
59 Correct 781 ms 263632 KB Output is correct
60 Correct 739 ms 263576 KB Output is correct
61 Correct 678 ms 262448 KB Output is correct
62 Correct 474 ms 261448 KB Output is correct
63 Correct 539 ms 262568 KB Output is correct
64 Correct 4729 ms 276164 KB Output is correct
65 Correct 540 ms 242096 KB Output is correct
66 Correct 4789 ms 281072 KB Output is correct
67 Correct 1847 ms 281920 KB Output is correct
68 Correct 3378 ms 281520 KB Output is correct
69 Correct 2873 ms 281464 KB Output is correct
70 Correct 4390 ms 280464 KB Output is correct
71 Correct 4876 ms 280976 KB Output is correct
72 Correct 1466 ms 277352 KB Output is correct
73 Correct 2643 ms 279156 KB Output is correct
74 Correct 3625 ms 281772 KB Output is correct
75 Correct 3866 ms 281868 KB Output is correct
76 Correct 1729 ms 278916 KB Output is correct
77 Correct 1662 ms 278684 KB Output is correct
78 Correct 2050 ms 280020 KB Output is correct
79 Correct 2449 ms 280916 KB Output is correct
80 Correct 2140 ms 279716 KB Output is correct
81 Correct 321 ms 232120 KB Output is correct
82 Correct 313 ms 232752 KB Output is correct
83 Correct 522 ms 232164 KB Output is correct
84 Correct 550 ms 233128 KB Output is correct
85 Correct 476 ms 232192 KB Output is correct
86 Correct 647 ms 233164 KB Output is correct
87 Correct 601 ms 233420 KB Output is correct
88 Correct 497 ms 232912 KB Output is correct
89 Correct 650 ms 233420 KB Output is correct
90 Correct 65 ms 222668 KB Output is correct
91 Correct 288 ms 232144 KB Output is correct
92 Correct 410 ms 232508 KB Output is correct
93 Correct 478 ms 232872 KB Output is correct
94 Correct 554 ms 233560 KB Output is correct
95 Correct 688 ms 233496 KB Output is correct
96 Correct 165 ms 223576 KB Output is correct
97 Correct 2677 ms 297468 KB Output is correct
98 Correct 729 ms 245268 KB Output is correct
99 Execution timed out 5035 ms 296984 KB Time limit exceeded
100 Halted 0 ms 0 KB -