Submission #217325

# Submission time Handle Problem Language Result Execution time Memory
217325 2020-03-29T12:14:47 Z theStaticMind New Home (APIO18_new_home) C++14
100 / 100
4233 ms 217696 KB
#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 1e9
#define modulo 1000000007
#define mod 998244353
//#define int long long int
using namespace std;

int S;

struct MaxSeg{
	vector<int> seg;

	void build(){
		S = (1 << (int)ceil(log2(sz(seg))));
		int l = S - sz(seg);
		for(int i = 0; i < l; i++) seg.pb(-INF);
		reverse(all(seg));
		for(int i = 1; i < sz(seg); i += 2) seg.pb(max(seg[i - 1], seg[i]));
		seg.pb(0);
		reverse(all(seg));
	}

	MaxSeg(int n){
		seg = vector<int>(n, -INF);
		build();
	}

	int query(int a, int b, int j = 1, int l = 0, int r = S - 1){
		if(r < a || b < l) return -INF;
		if(a <= l && r <= b) return seg[j];
		return max(query(a,b,j*2,l,(l+r)/2), query(a,b,j*2+1,(l+r)/2+1,r));
	}
	void update(int j, int v){
		j += S;
		seg[j] = v;
		j /= 2;
		while(j != 0){
			seg[j] = max(seg[j * 2], seg[j * 2 + 1]);
			j /= 2;
		}
	}
};

struct Store{
	int x, type, tl, tr, id;
};
struct Query{
	int x, time, id;
};
struct Event{
	int t, l, r, i, id; 
	Event(int a, int b, int c, int d, int e){
		t=a,l=b,r=c,i=d,id=e;
	}
	int mid(){
		return (1ll * l + 1ll * r) / 2;
	}
	bool operator<=(Event& A){
		if(mid() < A.mid()) return true;
		if(mid() == A.mid()){
			if(id <= A.id) return true;
		}
		return false;
	}

} ;
vector<Event> E;

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
  
	int n, k, q;
	cin >> n >> k >> q;

	vector<Store> X, Y;
	vector<Query> Q;
	vector<int> ans(q);
	vector<int> cnt(k + 1, 0);
	map<int, int> K[k + 1];
	int c = k;

	for(int i = 1; i <= k; i++) {
		K[i][1e9]++, K[i][-1e9]++;
		E.pb(Event(-1, -1e9, 1e9, 1, i));
	}

	for(int i = 0; i < n; i++){
		int a, b, c, d; 
		cin >> a >> b >> c >> d; a*=2;
		X.pb({a, b, c, d, i});
		Y.pb({a, b, c, d, i});
	}
	for(int i = 0; i < q; i++){
		int a, b; 
		cin >> a >> b; a*=2;
		Q.pb({a, b, i});
	}

	sort(all(X), [&](Store& A, Store& B){return A.tl < B.tl;});
	sort(all(Y), [&](Store& A, Store& B){return A.tr < B.tr;});
	sort(all(Q), [&](Query& A, Query& B){return A.time < B.time;});

	int a = 0, b = 0;

	for(auto& curr : Q){
		while(a < n && X[a].tl <= curr.time){
			int t = X[a].type, x = X[a].x;
			if(++cnt[t] == 1) c--;

			if(!K[t].count(x)){
				auto next = K[t].upper_bound(x);
				auto prev = std::prev(next);

				E.pb(Event(X[a].tl, prev->first, next->first, 0, t));
				E.pb(Event(X[a].tl, prev->first, x, 1, t));
				E.pb(Event(X[a].tl, x, next->first, 1, t));

			}
			K[t][x]++;

			a++;
		}
		while(b < n && Y[b].tr < curr.time){
			int t = Y[b].type, x = Y[b].x;
			if(--cnt[Y[b].type] == 0) c++;

			if(K[t][x] == 1){	
				K[t].erase(x);

				auto next = K[t].upper_bound(x);
				auto prev = std::prev(next);
				
				E.pb(Event(Y[b].tr+1, prev->first, x, 0, t));
				E.pb(Event(Y[b].tr+1, x, next->first, 0, t));
				E.pb(Event(Y[b].tr+1, prev->first, next->first, 1, t));
			}
			else{
				K[t][x]--;
			}

			b++;
		}
		if(c > 0){
			ans[curr.id] = -2;
			continue;
		}

	}

	MaxSeg L(sz(E)), R(sz(E));

	a = 0;

	vector<int> seq;
	for(int i = 0; i < sz(E); i++) if(E[i].i) seq.pb(i);
	sort(all(seq), [&](int x, int y){return E[x] <= E[y];});

	auto lower = [&](Event x){
		int l = 0, r = sz(seq) - 1;
		int ret = 0;
		while(l <= r){
			int mid = (l + r) / 2;
			if(x <= E[seq[mid]]){
				ret = mid;
				r = mid - 1;
			}
			else l = mid + 1;
		}
		return ret;
	};
	int w;
	for(auto& curr : Q){
		while(a < sz(E) && E[a].t <= curr.time){
			w = lower(E[a]);
			if(E[a].i){
				R.update(w, -E[a].l);
				L.update(w, E[a].r);
			}
			else{
				R.update(w, -INF);
				L.update(w, -INF);
			}
			a++;
		}
		if(ans[curr.id] == -2){
			continue;
		}
		int x = curr.x;
		w = lower(Event(0,x,x,1,-1));
		ans[curr.id] = max(L.query(0, w - 1) - x, R.query(w, S-1) + x);

	}

	for(int i = 0; i < q; i++) cout << ans[i] / 2 << ' ';

}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 6 ms 556 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 640 KB Output is correct
20 Correct 6 ms 640 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 640 KB Output is correct
23 Correct 6 ms 640 KB Output is correct
24 Correct 6 ms 552 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 6 ms 512 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 512 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 6 ms 556 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 640 KB Output is correct
20 Correct 6 ms 640 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 640 KB Output is correct
23 Correct 6 ms 640 KB Output is correct
24 Correct 6 ms 552 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 6 ms 512 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 512 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 5 ms 512 KB Output is correct
31 Correct 483 ms 27944 KB Output is correct
32 Correct 55 ms 5448 KB Output is correct
33 Correct 471 ms 27264 KB Output is correct
34 Correct 500 ms 27820 KB Output is correct
35 Correct 476 ms 26548 KB Output is correct
36 Correct 466 ms 28328 KB Output is correct
37 Correct 319 ms 26668 KB Output is correct
38 Correct 332 ms 26540 KB Output is correct
39 Correct 274 ms 26432 KB Output is correct
40 Correct 295 ms 26388 KB Output is correct
41 Correct 530 ms 26672 KB Output is correct
42 Correct 493 ms 26552 KB Output is correct
43 Correct 61 ms 7340 KB Output is correct
44 Correct 508 ms 26688 KB Output is correct
45 Correct 499 ms 26544 KB Output is correct
46 Correct 439 ms 26668 KB Output is correct
47 Correct 245 ms 25648 KB Output is correct
48 Correct 234 ms 25516 KB Output is correct
49 Correct 240 ms 25900 KB Output is correct
50 Correct 259 ms 26304 KB Output is correct
51 Correct 250 ms 25776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2246 ms 93192 KB Output is correct
2 Correct 1962 ms 81272 KB Output is correct
3 Correct 2097 ms 156188 KB Output is correct
4 Correct 2229 ms 100144 KB Output is correct
5 Correct 1826 ms 80760 KB Output is correct
6 Correct 1900 ms 81272 KB Output is correct
7 Correct 1653 ms 156172 KB Output is correct
8 Correct 1885 ms 100164 KB Output is correct
9 Correct 1928 ms 87868 KB Output is correct
10 Correct 1953 ms 81900 KB Output is correct
11 Correct 932 ms 80376 KB Output is correct
12 Correct 1006 ms 81528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4233 ms 124916 KB Output is correct
2 Correct 279 ms 27864 KB Output is correct
3 Correct 3565 ms 112748 KB Output is correct
4 Correct 3790 ms 206428 KB Output is correct
5 Correct 3570 ms 122144 KB Output is correct
6 Correct 3622 ms 136320 KB Output is correct
7 Correct 3335 ms 112072 KB Output is correct
8 Correct 3663 ms 112524 KB Output is correct
9 Correct 3241 ms 202640 KB Output is correct
10 Correct 3694 ms 125920 KB Output is correct
11 Correct 3688 ms 121676 KB Output is correct
12 Correct 3612 ms 112860 KB Output is correct
13 Correct 1339 ms 110172 KB Output is correct
14 Correct 1296 ms 109388 KB Output is correct
15 Correct 1456 ms 111264 KB Output is correct
16 Correct 1591 ms 116812 KB Output is correct
17 Correct 1595 ms 115540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 6 ms 556 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 640 KB Output is correct
20 Correct 6 ms 640 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 640 KB Output is correct
23 Correct 6 ms 640 KB Output is correct
24 Correct 6 ms 552 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 6 ms 512 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 512 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 5 ms 512 KB Output is correct
31 Correct 483 ms 27944 KB Output is correct
32 Correct 55 ms 5448 KB Output is correct
33 Correct 471 ms 27264 KB Output is correct
34 Correct 500 ms 27820 KB Output is correct
35 Correct 476 ms 26548 KB Output is correct
36 Correct 466 ms 28328 KB Output is correct
37 Correct 319 ms 26668 KB Output is correct
38 Correct 332 ms 26540 KB Output is correct
39 Correct 274 ms 26432 KB Output is correct
40 Correct 295 ms 26388 KB Output is correct
41 Correct 530 ms 26672 KB Output is correct
42 Correct 493 ms 26552 KB Output is correct
43 Correct 61 ms 7340 KB Output is correct
44 Correct 508 ms 26688 KB Output is correct
45 Correct 499 ms 26544 KB Output is correct
46 Correct 439 ms 26668 KB Output is correct
47 Correct 245 ms 25648 KB Output is correct
48 Correct 234 ms 25516 KB Output is correct
49 Correct 240 ms 25900 KB Output is correct
50 Correct 259 ms 26304 KB Output is correct
51 Correct 250 ms 25776 KB Output is correct
52 Correct 467 ms 37096 KB Output is correct
53 Correct 480 ms 38084 KB Output is correct
54 Correct 429 ms 29936 KB Output is correct
55 Correct 427 ms 30292 KB Output is correct
56 Correct 409 ms 33388 KB Output is correct
57 Correct 443 ms 28292 KB Output is correct
58 Correct 436 ms 30832 KB Output is correct
59 Correct 431 ms 33368 KB Output is correct
60 Correct 440 ms 27784 KB Output is correct
61 Correct 160 ms 29780 KB Output is correct
62 Correct 476 ms 38212 KB Output is correct
63 Correct 439 ms 33136 KB Output is correct
64 Correct 446 ms 30172 KB Output is correct
65 Correct 469 ms 27780 KB Output is correct
66 Correct 461 ms 26740 KB Output is correct
67 Correct 373 ms 25860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 6 ms 512 KB Output is correct
7 Correct 6 ms 640 KB Output is correct
8 Correct 6 ms 640 KB Output is correct
9 Correct 6 ms 640 KB Output is correct
10 Correct 6 ms 512 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 6 ms 512 KB Output is correct
15 Correct 6 ms 556 KB Output is correct
16 Correct 6 ms 640 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 640 KB Output is correct
20 Correct 6 ms 640 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 640 KB Output is correct
23 Correct 6 ms 640 KB Output is correct
24 Correct 6 ms 552 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 6 ms 512 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 512 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 5 ms 512 KB Output is correct
31 Correct 483 ms 27944 KB Output is correct
32 Correct 55 ms 5448 KB Output is correct
33 Correct 471 ms 27264 KB Output is correct
34 Correct 500 ms 27820 KB Output is correct
35 Correct 476 ms 26548 KB Output is correct
36 Correct 466 ms 28328 KB Output is correct
37 Correct 319 ms 26668 KB Output is correct
38 Correct 332 ms 26540 KB Output is correct
39 Correct 274 ms 26432 KB Output is correct
40 Correct 295 ms 26388 KB Output is correct
41 Correct 530 ms 26672 KB Output is correct
42 Correct 493 ms 26552 KB Output is correct
43 Correct 61 ms 7340 KB Output is correct
44 Correct 508 ms 26688 KB Output is correct
45 Correct 499 ms 26544 KB Output is correct
46 Correct 439 ms 26668 KB Output is correct
47 Correct 245 ms 25648 KB Output is correct
48 Correct 234 ms 25516 KB Output is correct
49 Correct 240 ms 25900 KB Output is correct
50 Correct 259 ms 26304 KB Output is correct
51 Correct 250 ms 25776 KB Output is correct
52 Correct 2246 ms 93192 KB Output is correct
53 Correct 1962 ms 81272 KB Output is correct
54 Correct 2097 ms 156188 KB Output is correct
55 Correct 2229 ms 100144 KB Output is correct
56 Correct 1826 ms 80760 KB Output is correct
57 Correct 1900 ms 81272 KB Output is correct
58 Correct 1653 ms 156172 KB Output is correct
59 Correct 1885 ms 100164 KB Output is correct
60 Correct 1928 ms 87868 KB Output is correct
61 Correct 1953 ms 81900 KB Output is correct
62 Correct 932 ms 80376 KB Output is correct
63 Correct 1006 ms 81528 KB Output is correct
64 Correct 4233 ms 124916 KB Output is correct
65 Correct 279 ms 27864 KB Output is correct
66 Correct 3565 ms 112748 KB Output is correct
67 Correct 3790 ms 206428 KB Output is correct
68 Correct 3570 ms 122144 KB Output is correct
69 Correct 3622 ms 136320 KB Output is correct
70 Correct 3335 ms 112072 KB Output is correct
71 Correct 3663 ms 112524 KB Output is correct
72 Correct 3241 ms 202640 KB Output is correct
73 Correct 3694 ms 125920 KB Output is correct
74 Correct 3688 ms 121676 KB Output is correct
75 Correct 3612 ms 112860 KB Output is correct
76 Correct 1339 ms 110172 KB Output is correct
77 Correct 1296 ms 109388 KB Output is correct
78 Correct 1456 ms 111264 KB Output is correct
79 Correct 1591 ms 116812 KB Output is correct
80 Correct 1595 ms 115540 KB Output is correct
81 Correct 467 ms 37096 KB Output is correct
82 Correct 480 ms 38084 KB Output is correct
83 Correct 429 ms 29936 KB Output is correct
84 Correct 427 ms 30292 KB Output is correct
85 Correct 409 ms 33388 KB Output is correct
86 Correct 443 ms 28292 KB Output is correct
87 Correct 436 ms 30832 KB Output is correct
88 Correct 431 ms 33368 KB Output is correct
89 Correct 440 ms 27784 KB Output is correct
90 Correct 160 ms 29780 KB Output is correct
91 Correct 476 ms 38212 KB Output is correct
92 Correct 439 ms 33136 KB Output is correct
93 Correct 446 ms 30172 KB Output is correct
94 Correct 469 ms 27780 KB Output is correct
95 Correct 461 ms 26740 KB Output is correct
96 Correct 373 ms 25860 KB Output is correct
97 Correct 3524 ms 212744 KB Output is correct
98 Correct 272 ms 28432 KB Output is correct
99 Correct 3383 ms 127252 KB Output is correct
100 Correct 3677 ms 215684 KB Output is correct
101 Correct 3586 ms 145568 KB Output is correct
102 Correct 3556 ms 128844 KB Output is correct
103 Correct 2355 ms 123628 KB Output is correct
104 Correct 2262 ms 122956 KB Output is correct
105 Correct 1621 ms 121960 KB Output is correct
106 Correct 1687 ms 121936 KB Output is correct
107 Correct 3445 ms 141928 KB Output is correct
108 Correct 3445 ms 157520 KB Output is correct
109 Correct 3471 ms 129496 KB Output is correct
110 Correct 3423 ms 141176 KB Output is correct
111 Correct 3406 ms 156988 KB Output is correct
112 Correct 3394 ms 130224 KB Output is correct
113 Correct 918 ms 164416 KB Output is correct
114 Correct 3725 ms 217696 KB Output is correct
115 Correct 3678 ms 157412 KB Output is correct
116 Correct 3756 ms 146716 KB Output is correct
117 Correct 3802 ms 132896 KB Output is correct
118 Correct 3768 ms 124364 KB Output is correct
119 Correct 2871 ms 120336 KB Output is correct
120 Correct 4181 ms 114624 KB Output is correct
121 Correct 1270 ms 118984 KB Output is correct
122 Correct 1242 ms 118076 KB Output is correct
123 Correct 1367 ms 120320 KB Output is correct
124 Correct 1489 ms 122072 KB Output is correct
125 Correct 1510 ms 120768 KB Output is correct
126 Correct 1511 ms 122240 KB Output is correct