Submission #217278

# Submission time Handle Problem Language Result Execution time Memory
217278 2020-03-29T10:48:29 Z theStaticMind New Home (APIO18_new_home) C++14
57 / 100
4646 ms 231760 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;

int ind = 0;

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++) 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 4 ms 384 KB Output is correct
4 Correct 4 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 7 ms 768 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 512 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 512 KB Output is correct
21 Correct 5 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 512 KB Output is correct
25 Correct 6 ms 512 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 6 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 4 ms 384 KB Output is correct
4 Correct 4 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 7 ms 768 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 512 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 512 KB Output is correct
21 Correct 5 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 512 KB Output is correct
25 Correct 6 ms 512 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 6 ms 512 KB Output is correct
31 Correct 566 ms 24976 KB Output is correct
32 Correct 60 ms 4548 KB Output is correct
33 Correct 559 ms 24360 KB Output is correct
34 Correct 574 ms 24876 KB Output is correct
35 Correct 573 ms 23732 KB Output is correct
36 Correct 532 ms 25384 KB Output is correct
37 Correct 355 ms 23852 KB Output is correct
38 Correct 351 ms 23720 KB Output is correct
39 Correct 320 ms 23752 KB Output is correct
40 Correct 311 ms 23504 KB Output is correct
41 Correct 550 ms 23748 KB Output is correct
42 Correct 510 ms 23636 KB Output is correct
43 Correct 63 ms 5060 KB Output is correct
44 Correct 544 ms 23764 KB Output is correct
45 Correct 536 ms 23728 KB Output is correct
46 Correct 510 ms 23852 KB Output is correct
47 Correct 347 ms 22828 KB Output is correct
48 Correct 290 ms 22700 KB Output is correct
49 Correct 277 ms 23084 KB Output is correct
50 Correct 292 ms 23360 KB Output is correct
51 Correct 296 ms 23056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2433 ms 88820 KB Output is correct
2 Correct 2092 ms 77092 KB Output is correct
3 Correct 2158 ms 156596 KB Output is correct
4 Correct 2403 ms 95948 KB Output is correct
5 Correct 1931 ms 76664 KB Output is correct
6 Correct 2009 ms 77048 KB Output is correct
7 Correct 1544 ms 156592 KB Output is correct
8 Correct 2013 ms 95944 KB Output is correct
9 Correct 2081 ms 83520 KB Output is correct
10 Correct 2094 ms 77640 KB Output is correct
11 Correct 994 ms 76152 KB Output is correct
12 Correct 1104 ms 77432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4646 ms 120688 KB Output is correct
2 Correct 254 ms 27796 KB Output is correct
3 Correct 4010 ms 119968 KB Output is correct
4 Correct 4540 ms 215644 KB Output is correct
5 Correct 4180 ms 130852 KB Output is correct
6 Correct 4254 ms 145048 KB Output is correct
7 Correct 3686 ms 119372 KB Output is correct
8 Correct 3885 ms 119924 KB Output is correct
9 Correct 3906 ms 211940 KB Output is correct
10 Correct 4102 ms 134868 KB Output is correct
11 Correct 4227 ms 130700 KB Output is correct
12 Correct 4072 ms 121016 KB Output is correct
13 Correct 1462 ms 117232 KB Output is correct
14 Correct 1449 ms 116176 KB Output is correct
15 Correct 1631 ms 118352 KB Output is correct
16 Correct 1880 ms 124976 KB Output is correct
17 Runtime error 905 ms 231760 KB Execution killed with signal 11 (could be triggered by violating memory limits)
# 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 4 ms 384 KB Output is correct
4 Correct 4 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 7 ms 768 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 512 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 512 KB Output is correct
21 Correct 5 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 512 KB Output is correct
25 Correct 6 ms 512 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 6 ms 512 KB Output is correct
31 Correct 566 ms 24976 KB Output is correct
32 Correct 60 ms 4548 KB Output is correct
33 Correct 559 ms 24360 KB Output is correct
34 Correct 574 ms 24876 KB Output is correct
35 Correct 573 ms 23732 KB Output is correct
36 Correct 532 ms 25384 KB Output is correct
37 Correct 355 ms 23852 KB Output is correct
38 Correct 351 ms 23720 KB Output is correct
39 Correct 320 ms 23752 KB Output is correct
40 Correct 311 ms 23504 KB Output is correct
41 Correct 550 ms 23748 KB Output is correct
42 Correct 510 ms 23636 KB Output is correct
43 Correct 63 ms 5060 KB Output is correct
44 Correct 544 ms 23764 KB Output is correct
45 Correct 536 ms 23728 KB Output is correct
46 Correct 510 ms 23852 KB Output is correct
47 Correct 347 ms 22828 KB Output is correct
48 Correct 290 ms 22700 KB Output is correct
49 Correct 277 ms 23084 KB Output is correct
50 Correct 292 ms 23360 KB Output is correct
51 Correct 296 ms 23056 KB Output is correct
52 Correct 547 ms 33860 KB Output is correct
53 Correct 539 ms 34980 KB Output is correct
54 Correct 525 ms 26864 KB Output is correct
55 Correct 481 ms 27212 KB Output is correct
56 Correct 472 ms 30500 KB Output is correct
57 Correct 507 ms 25224 KB Output is correct
58 Correct 497 ms 27888 KB Output is correct
59 Correct 519 ms 30360 KB Output is correct
60 Correct 505 ms 24712 KB Output is correct
61 Correct 171 ms 27348 KB Output is correct
62 Correct 558 ms 35148 KB Output is correct
63 Correct 511 ms 30068 KB Output is correct
64 Correct 528 ms 27120 KB Output is correct
65 Correct 483 ms 24836 KB Output is correct
66 Correct 523 ms 23772 KB Output is correct
67 Correct 457 ms 24712 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 4 ms 384 KB Output is correct
4 Correct 4 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 7 ms 768 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 512 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 512 KB Output is correct
21 Correct 5 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 512 KB Output is correct
25 Correct 6 ms 512 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 6 ms 512 KB Output is correct
31 Correct 566 ms 24976 KB Output is correct
32 Correct 60 ms 4548 KB Output is correct
33 Correct 559 ms 24360 KB Output is correct
34 Correct 574 ms 24876 KB Output is correct
35 Correct 573 ms 23732 KB Output is correct
36 Correct 532 ms 25384 KB Output is correct
37 Correct 355 ms 23852 KB Output is correct
38 Correct 351 ms 23720 KB Output is correct
39 Correct 320 ms 23752 KB Output is correct
40 Correct 311 ms 23504 KB Output is correct
41 Correct 550 ms 23748 KB Output is correct
42 Correct 510 ms 23636 KB Output is correct
43 Correct 63 ms 5060 KB Output is correct
44 Correct 544 ms 23764 KB Output is correct
45 Correct 536 ms 23728 KB Output is correct
46 Correct 510 ms 23852 KB Output is correct
47 Correct 347 ms 22828 KB Output is correct
48 Correct 290 ms 22700 KB Output is correct
49 Correct 277 ms 23084 KB Output is correct
50 Correct 292 ms 23360 KB Output is correct
51 Correct 296 ms 23056 KB Output is correct
52 Correct 2433 ms 88820 KB Output is correct
53 Correct 2092 ms 77092 KB Output is correct
54 Correct 2158 ms 156596 KB Output is correct
55 Correct 2403 ms 95948 KB Output is correct
56 Correct 1931 ms 76664 KB Output is correct
57 Correct 2009 ms 77048 KB Output is correct
58 Correct 1544 ms 156592 KB Output is correct
59 Correct 2013 ms 95944 KB Output is correct
60 Correct 2081 ms 83520 KB Output is correct
61 Correct 2094 ms 77640 KB Output is correct
62 Correct 994 ms 76152 KB Output is correct
63 Correct 1104 ms 77432 KB Output is correct
64 Correct 4646 ms 120688 KB Output is correct
65 Correct 254 ms 27796 KB Output is correct
66 Correct 4010 ms 119968 KB Output is correct
67 Correct 4540 ms 215644 KB Output is correct
68 Correct 4180 ms 130852 KB Output is correct
69 Correct 4254 ms 145048 KB Output is correct
70 Correct 3686 ms 119372 KB Output is correct
71 Correct 3885 ms 119924 KB Output is correct
72 Correct 3906 ms 211940 KB Output is correct
73 Correct 4102 ms 134868 KB Output is correct
74 Correct 4227 ms 130700 KB Output is correct
75 Correct 4072 ms 121016 KB Output is correct
76 Correct 1462 ms 117232 KB Output is correct
77 Correct 1449 ms 116176 KB Output is correct
78 Correct 1631 ms 118352 KB Output is correct
79 Correct 1880 ms 124976 KB Output is correct
80 Runtime error 905 ms 231760 KB Execution killed with signal 11 (could be triggered by violating memory limits)