답안 #985063

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
985063 2024-05-17T10:12:08 Z qwerasdfzxcl 새 집 (APIO18_new_home) C++17
47 / 100
916 ms 486376 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[600600], 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]);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 197204 KB Output is correct
2 Correct 39 ms 198992 KB Output is correct
3 Correct 37 ms 197180 KB Output is correct
4 Correct 35 ms 197204 KB Output is correct
5 Correct 37 ms 199120 KB Output is correct
6 Correct 42 ms 197404 KB Output is correct
7 Correct 38 ms 197212 KB Output is correct
8 Correct 38 ms 197200 KB Output is correct
9 Correct 37 ms 197320 KB Output is correct
10 Correct 37 ms 197200 KB Output is correct
11 Correct 37 ms 197204 KB Output is correct
12 Correct 40 ms 197212 KB Output is correct
13 Correct 37 ms 197212 KB Output is correct
14 Correct 40 ms 197192 KB Output is correct
15 Correct 43 ms 197212 KB Output is correct
16 Correct 39 ms 197224 KB Output is correct
17 Correct 38 ms 197200 KB Output is correct
18 Correct 39 ms 197244 KB Output is correct
19 Correct 38 ms 197200 KB Output is correct
20 Correct 39 ms 197208 KB Output is correct
21 Correct 39 ms 197264 KB Output is correct
22 Correct 39 ms 197360 KB Output is correct
23 Correct 38 ms 197200 KB Output is correct
24 Correct 38 ms 197200 KB Output is correct
25 Correct 40 ms 197332 KB Output is correct
26 Correct 39 ms 197224 KB Output is correct
27 Correct 38 ms 197204 KB Output is correct
28 Correct 39 ms 199004 KB Output is correct
29 Correct 37 ms 198992 KB Output is correct
30 Correct 38 ms 199004 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 197204 KB Output is correct
2 Correct 39 ms 198992 KB Output is correct
3 Correct 37 ms 197180 KB Output is correct
4 Correct 35 ms 197204 KB Output is correct
5 Correct 37 ms 199120 KB Output is correct
6 Correct 42 ms 197404 KB Output is correct
7 Correct 38 ms 197212 KB Output is correct
8 Correct 38 ms 197200 KB Output is correct
9 Correct 37 ms 197320 KB Output is correct
10 Correct 37 ms 197200 KB Output is correct
11 Correct 37 ms 197204 KB Output is correct
12 Correct 40 ms 197212 KB Output is correct
13 Correct 37 ms 197212 KB Output is correct
14 Correct 40 ms 197192 KB Output is correct
15 Correct 43 ms 197212 KB Output is correct
16 Correct 39 ms 197224 KB Output is correct
17 Correct 38 ms 197200 KB Output is correct
18 Correct 39 ms 197244 KB Output is correct
19 Correct 38 ms 197200 KB Output is correct
20 Correct 39 ms 197208 KB Output is correct
21 Correct 39 ms 197264 KB Output is correct
22 Correct 39 ms 197360 KB Output is correct
23 Correct 38 ms 197200 KB Output is correct
24 Correct 38 ms 197200 KB Output is correct
25 Correct 40 ms 197332 KB Output is correct
26 Correct 39 ms 197224 KB Output is correct
27 Correct 38 ms 197204 KB Output is correct
28 Correct 39 ms 199004 KB Output is correct
29 Correct 37 ms 198992 KB Output is correct
30 Correct 38 ms 199004 KB Output is correct
31 Correct 916 ms 214460 KB Output is correct
32 Correct 168 ms 205524 KB Output is correct
33 Correct 896 ms 215244 KB Output is correct
34 Correct 898 ms 215248 KB Output is correct
35 Correct 916 ms 214556 KB Output is correct
36 Correct 911 ms 214600 KB Output is correct
37 Correct 779 ms 215252 KB Output is correct
38 Correct 735 ms 215304 KB Output is correct
39 Correct 601 ms 215224 KB Output is correct
40 Correct 616 ms 215184 KB Output is correct
41 Correct 697 ms 215524 KB Output is correct
42 Correct 699 ms 215300 KB Output is correct
43 Correct 69 ms 206540 KB Output is correct
44 Correct 762 ms 215708 KB Output is correct
45 Correct 777 ms 215500 KB Output is correct
46 Correct 798 ms 215264 KB Output is correct
47 Correct 445 ms 214552 KB Output is correct
48 Correct 459 ms 214540 KB Output is correct
49 Correct 514 ms 214828 KB Output is correct
50 Correct 517 ms 215472 KB Output is correct
51 Correct 572 ms 215164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 796 ms 486376 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 423 ms 457140 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 197204 KB Output is correct
2 Correct 39 ms 198992 KB Output is correct
3 Correct 37 ms 197180 KB Output is correct
4 Correct 35 ms 197204 KB Output is correct
5 Correct 37 ms 199120 KB Output is correct
6 Correct 42 ms 197404 KB Output is correct
7 Correct 38 ms 197212 KB Output is correct
8 Correct 38 ms 197200 KB Output is correct
9 Correct 37 ms 197320 KB Output is correct
10 Correct 37 ms 197200 KB Output is correct
11 Correct 37 ms 197204 KB Output is correct
12 Correct 40 ms 197212 KB Output is correct
13 Correct 37 ms 197212 KB Output is correct
14 Correct 40 ms 197192 KB Output is correct
15 Correct 43 ms 197212 KB Output is correct
16 Correct 39 ms 197224 KB Output is correct
17 Correct 38 ms 197200 KB Output is correct
18 Correct 39 ms 197244 KB Output is correct
19 Correct 38 ms 197200 KB Output is correct
20 Correct 39 ms 197208 KB Output is correct
21 Correct 39 ms 197264 KB Output is correct
22 Correct 39 ms 197360 KB Output is correct
23 Correct 38 ms 197200 KB Output is correct
24 Correct 38 ms 197200 KB Output is correct
25 Correct 40 ms 197332 KB Output is correct
26 Correct 39 ms 197224 KB Output is correct
27 Correct 38 ms 197204 KB Output is correct
28 Correct 39 ms 199004 KB Output is correct
29 Correct 37 ms 198992 KB Output is correct
30 Correct 38 ms 199004 KB Output is correct
31 Correct 916 ms 214460 KB Output is correct
32 Correct 168 ms 205524 KB Output is correct
33 Correct 896 ms 215244 KB Output is correct
34 Correct 898 ms 215248 KB Output is correct
35 Correct 916 ms 214556 KB Output is correct
36 Correct 911 ms 214600 KB Output is correct
37 Correct 779 ms 215252 KB Output is correct
38 Correct 735 ms 215304 KB Output is correct
39 Correct 601 ms 215224 KB Output is correct
40 Correct 616 ms 215184 KB Output is correct
41 Correct 697 ms 215524 KB Output is correct
42 Correct 699 ms 215300 KB Output is correct
43 Correct 69 ms 206540 KB Output is correct
44 Correct 762 ms 215708 KB Output is correct
45 Correct 777 ms 215500 KB Output is correct
46 Correct 798 ms 215264 KB Output is correct
47 Correct 445 ms 214552 KB Output is correct
48 Correct 459 ms 214540 KB Output is correct
49 Correct 514 ms 214828 KB Output is correct
50 Correct 517 ms 215472 KB Output is correct
51 Correct 572 ms 215164 KB Output is correct
52 Correct 310 ms 214516 KB Output is correct
53 Correct 309 ms 214988 KB Output is correct
54 Correct 519 ms 214260 KB Output is correct
55 Correct 532 ms 215308 KB Output is correct
56 Correct 462 ms 214340 KB Output is correct
57 Correct 641 ms 215244 KB Output is correct
58 Correct 560 ms 215656 KB Output is correct
59 Correct 492 ms 215244 KB Output is correct
60 Correct 665 ms 215472 KB Output is correct
61 Correct 67 ms 206544 KB Output is correct
62 Correct 291 ms 214224 KB Output is correct
63 Correct 406 ms 214756 KB Output is correct
64 Correct 452 ms 214992 KB Output is correct
65 Correct 560 ms 215712 KB Output is correct
66 Correct 675 ms 215568 KB Output is correct
67 Correct 161 ms 206776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 90 ms 197204 KB Output is correct
2 Correct 39 ms 198992 KB Output is correct
3 Correct 37 ms 197180 KB Output is correct
4 Correct 35 ms 197204 KB Output is correct
5 Correct 37 ms 199120 KB Output is correct
6 Correct 42 ms 197404 KB Output is correct
7 Correct 38 ms 197212 KB Output is correct
8 Correct 38 ms 197200 KB Output is correct
9 Correct 37 ms 197320 KB Output is correct
10 Correct 37 ms 197200 KB Output is correct
11 Correct 37 ms 197204 KB Output is correct
12 Correct 40 ms 197212 KB Output is correct
13 Correct 37 ms 197212 KB Output is correct
14 Correct 40 ms 197192 KB Output is correct
15 Correct 43 ms 197212 KB Output is correct
16 Correct 39 ms 197224 KB Output is correct
17 Correct 38 ms 197200 KB Output is correct
18 Correct 39 ms 197244 KB Output is correct
19 Correct 38 ms 197200 KB Output is correct
20 Correct 39 ms 197208 KB Output is correct
21 Correct 39 ms 197264 KB Output is correct
22 Correct 39 ms 197360 KB Output is correct
23 Correct 38 ms 197200 KB Output is correct
24 Correct 38 ms 197200 KB Output is correct
25 Correct 40 ms 197332 KB Output is correct
26 Correct 39 ms 197224 KB Output is correct
27 Correct 38 ms 197204 KB Output is correct
28 Correct 39 ms 199004 KB Output is correct
29 Correct 37 ms 198992 KB Output is correct
30 Correct 38 ms 199004 KB Output is correct
31 Correct 916 ms 214460 KB Output is correct
32 Correct 168 ms 205524 KB Output is correct
33 Correct 896 ms 215244 KB Output is correct
34 Correct 898 ms 215248 KB Output is correct
35 Correct 916 ms 214556 KB Output is correct
36 Correct 911 ms 214600 KB Output is correct
37 Correct 779 ms 215252 KB Output is correct
38 Correct 735 ms 215304 KB Output is correct
39 Correct 601 ms 215224 KB Output is correct
40 Correct 616 ms 215184 KB Output is correct
41 Correct 697 ms 215524 KB Output is correct
42 Correct 699 ms 215300 KB Output is correct
43 Correct 69 ms 206540 KB Output is correct
44 Correct 762 ms 215708 KB Output is correct
45 Correct 777 ms 215500 KB Output is correct
46 Correct 798 ms 215264 KB Output is correct
47 Correct 445 ms 214552 KB Output is correct
48 Correct 459 ms 214540 KB Output is correct
49 Correct 514 ms 214828 KB Output is correct
50 Correct 517 ms 215472 KB Output is correct
51 Correct 572 ms 215164 KB Output is correct
52 Runtime error 796 ms 486376 KB Execution killed with signal 11
53 Halted 0 ms 0 KB -