Submission #222231

# Submission time Handle Problem Language Result Execution time Memory
222231 2020-04-12T12:24:59 Z Bruteforceman New Home (APIO18_new_home) C++11
100 / 100
4671 ms 217528 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 5 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 7 ms 640 KB Output is correct
8 Correct 7 ms 512 KB Output is correct
9 Correct 6 ms 692 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 640 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 7 ms 768 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 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 512 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 6 ms 640 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 572 KB Output is correct
29 Correct 6 ms 640 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 5 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 7 ms 640 KB Output is correct
8 Correct 7 ms 512 KB Output is correct
9 Correct 6 ms 692 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 640 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 7 ms 768 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 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 512 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 6 ms 640 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 572 KB Output is correct
29 Correct 6 ms 640 KB Output is correct
30 Correct 6 ms 512 KB Output is correct
31 Correct 495 ms 27948 KB Output is correct
32 Correct 68 ms 5572 KB Output is correct
33 Correct 479 ms 27180 KB Output is correct
34 Correct 515 ms 27816 KB Output is correct
35 Correct 527 ms 26544 KB Output is correct
36 Correct 528 ms 28252 KB Output is correct
37 Correct 345 ms 26668 KB Output is correct
38 Correct 322 ms 26540 KB Output is correct
39 Correct 299 ms 26440 KB Output is correct
40 Correct 293 ms 26444 KB Output is correct
41 Correct 525 ms 26652 KB Output is correct
42 Correct 474 ms 26532 KB Output is correct
43 Correct 61 ms 7364 KB Output is correct
44 Correct 509 ms 26660 KB Output is correct
45 Correct 501 ms 26672 KB Output is correct
46 Correct 442 ms 26540 KB Output is correct
47 Correct 257 ms 25828 KB Output is correct
48 Correct 243 ms 25392 KB Output is correct
49 Correct 250 ms 25876 KB Output is correct
50 Correct 258 ms 26308 KB Output is correct
51 Correct 255 ms 25772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2275 ms 102612 KB Output is correct
2 Correct 1991 ms 89600 KB Output is correct
3 Correct 2157 ms 166064 KB Output is correct
4 Correct 2264 ms 109676 KB Output is correct
5 Correct 1884 ms 89080 KB Output is correct
6 Correct 1932 ms 89720 KB Output is correct
7 Correct 1690 ms 165780 KB Output is correct
8 Correct 1932 ms 109640 KB Output is correct
9 Correct 1965 ms 97216 KB Output is correct
10 Correct 1963 ms 90756 KB Output is correct
11 Correct 940 ms 88568 KB Output is correct
12 Correct 1049 ms 90300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3952 ms 133804 KB Output is correct
2 Correct 258 ms 27788 KB Output is correct
3 Correct 3468 ms 120008 KB Output is correct
4 Correct 3651 ms 215516 KB Output is correct
5 Correct 3479 ms 131044 KB Output is correct
6 Correct 3568 ms 145408 KB Output is correct
7 Correct 3171 ms 119536 KB Output is correct
8 Correct 3335 ms 120012 KB Output is correct
9 Correct 3043 ms 211804 KB Output is correct
10 Correct 3406 ms 134812 KB Output is correct
11 Correct 3537 ms 130432 KB Output is correct
12 Correct 3503 ms 120928 KB Output is correct
13 Correct 1326 ms 117240 KB Output is correct
14 Correct 1268 ms 116048 KB Output is correct
15 Correct 1498 ms 118536 KB Output is correct
16 Correct 1630 ms 124876 KB Output is correct
17 Correct 1623 ms 122404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 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 7 ms 640 KB Output is correct
8 Correct 7 ms 512 KB Output is correct
9 Correct 6 ms 692 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 640 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 7 ms 768 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 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 512 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 6 ms 640 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 572 KB Output is correct
29 Correct 6 ms 640 KB Output is correct
30 Correct 6 ms 512 KB Output is correct
31 Correct 495 ms 27948 KB Output is correct
32 Correct 68 ms 5572 KB Output is correct
33 Correct 479 ms 27180 KB Output is correct
34 Correct 515 ms 27816 KB Output is correct
35 Correct 527 ms 26544 KB Output is correct
36 Correct 528 ms 28252 KB Output is correct
37 Correct 345 ms 26668 KB Output is correct
38 Correct 322 ms 26540 KB Output is correct
39 Correct 299 ms 26440 KB Output is correct
40 Correct 293 ms 26444 KB Output is correct
41 Correct 525 ms 26652 KB Output is correct
42 Correct 474 ms 26532 KB Output is correct
43 Correct 61 ms 7364 KB Output is correct
44 Correct 509 ms 26660 KB Output is correct
45 Correct 501 ms 26672 KB Output is correct
46 Correct 442 ms 26540 KB Output is correct
47 Correct 257 ms 25828 KB Output is correct
48 Correct 243 ms 25392 KB Output is correct
49 Correct 250 ms 25876 KB Output is correct
50 Correct 258 ms 26308 KB Output is correct
51 Correct 255 ms 25772 KB Output is correct
52 Correct 450 ms 37064 KB Output is correct
53 Correct 469 ms 38088 KB Output is correct
54 Correct 411 ms 29936 KB Output is correct
55 Correct 412 ms 30420 KB Output is correct
56 Correct 406 ms 33388 KB Output is correct
57 Correct 458 ms 28296 KB Output is correct
58 Correct 422 ms 30956 KB Output is correct
59 Correct 451 ms 33380 KB Output is correct
60 Correct 423 ms 27784 KB Output is correct
61 Correct 204 ms 29784 KB Output is correct
62 Correct 490 ms 38212 KB Output is correct
63 Correct 436 ms 33132 KB Output is correct
64 Correct 444 ms 30188 KB Output is correct
65 Correct 467 ms 27780 KB Output is correct
66 Correct 446 ms 26872 KB Output is correct
67 Correct 386 ms 25988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 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 7 ms 640 KB Output is correct
8 Correct 7 ms 512 KB Output is correct
9 Correct 6 ms 692 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 640 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 7 ms 768 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 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 512 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 6 ms 640 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 572 KB Output is correct
29 Correct 6 ms 640 KB Output is correct
30 Correct 6 ms 512 KB Output is correct
31 Correct 495 ms 27948 KB Output is correct
32 Correct 68 ms 5572 KB Output is correct
33 Correct 479 ms 27180 KB Output is correct
34 Correct 515 ms 27816 KB Output is correct
35 Correct 527 ms 26544 KB Output is correct
36 Correct 528 ms 28252 KB Output is correct
37 Correct 345 ms 26668 KB Output is correct
38 Correct 322 ms 26540 KB Output is correct
39 Correct 299 ms 26440 KB Output is correct
40 Correct 293 ms 26444 KB Output is correct
41 Correct 525 ms 26652 KB Output is correct
42 Correct 474 ms 26532 KB Output is correct
43 Correct 61 ms 7364 KB Output is correct
44 Correct 509 ms 26660 KB Output is correct
45 Correct 501 ms 26672 KB Output is correct
46 Correct 442 ms 26540 KB Output is correct
47 Correct 257 ms 25828 KB Output is correct
48 Correct 243 ms 25392 KB Output is correct
49 Correct 250 ms 25876 KB Output is correct
50 Correct 258 ms 26308 KB Output is correct
51 Correct 255 ms 25772 KB Output is correct
52 Correct 2275 ms 102612 KB Output is correct
53 Correct 1991 ms 89600 KB Output is correct
54 Correct 2157 ms 166064 KB Output is correct
55 Correct 2264 ms 109676 KB Output is correct
56 Correct 1884 ms 89080 KB Output is correct
57 Correct 1932 ms 89720 KB Output is correct
58 Correct 1690 ms 165780 KB Output is correct
59 Correct 1932 ms 109640 KB Output is correct
60 Correct 1965 ms 97216 KB Output is correct
61 Correct 1963 ms 90756 KB Output is correct
62 Correct 940 ms 88568 KB Output is correct
63 Correct 1049 ms 90300 KB Output is correct
64 Correct 3952 ms 133804 KB Output is correct
65 Correct 258 ms 27788 KB Output is correct
66 Correct 3468 ms 120008 KB Output is correct
67 Correct 3651 ms 215516 KB Output is correct
68 Correct 3479 ms 131044 KB Output is correct
69 Correct 3568 ms 145408 KB Output is correct
70 Correct 3171 ms 119536 KB Output is correct
71 Correct 3335 ms 120012 KB Output is correct
72 Correct 3043 ms 211804 KB Output is correct
73 Correct 3406 ms 134812 KB Output is correct
74 Correct 3537 ms 130432 KB Output is correct
75 Correct 3503 ms 120928 KB Output is correct
76 Correct 1326 ms 117240 KB Output is correct
77 Correct 1268 ms 116048 KB Output is correct
78 Correct 1498 ms 118536 KB Output is correct
79 Correct 1630 ms 124876 KB Output is correct
80 Correct 1623 ms 122404 KB Output is correct
81 Correct 450 ms 37064 KB Output is correct
82 Correct 469 ms 38088 KB Output is correct
83 Correct 411 ms 29936 KB Output is correct
84 Correct 412 ms 30420 KB Output is correct
85 Correct 406 ms 33388 KB Output is correct
86 Correct 458 ms 28296 KB Output is correct
87 Correct 422 ms 30956 KB Output is correct
88 Correct 451 ms 33380 KB Output is correct
89 Correct 423 ms 27784 KB Output is correct
90 Correct 204 ms 29784 KB Output is correct
91 Correct 490 ms 38212 KB Output is correct
92 Correct 436 ms 33132 KB Output is correct
93 Correct 444 ms 30188 KB Output is correct
94 Correct 467 ms 27780 KB Output is correct
95 Correct 446 ms 26872 KB Output is correct
96 Correct 386 ms 25988 KB Output is correct
97 Correct 3527 ms 212572 KB Output is correct
98 Correct 291 ms 28300 KB Output is correct
99 Correct 3509 ms 127196 KB Output is correct
100 Correct 3708 ms 215860 KB Output is correct
101 Correct 3646 ms 145656 KB Output is correct
102 Correct 3587 ms 128788 KB Output is correct
103 Correct 2363 ms 123340 KB Output is correct
104 Correct 2281 ms 122884 KB Output is correct
105 Correct 1565 ms 121948 KB Output is correct
106 Correct 1632 ms 122060 KB Output is correct
107 Correct 3545 ms 141700 KB Output is correct
108 Correct 3550 ms 157404 KB Output is correct
109 Correct 3664 ms 129424 KB Output is correct
110 Correct 3468 ms 141320 KB Output is correct
111 Correct 3607 ms 156900 KB Output is correct
112 Correct 3607 ms 130128 KB Output is correct
113 Correct 979 ms 164636 KB Output is correct
114 Correct 4152 ms 217528 KB Output is correct
115 Correct 3956 ms 157340 KB Output is correct
116 Correct 3959 ms 146544 KB Output is correct
117 Correct 4032 ms 132856 KB Output is correct
118 Correct 4043 ms 124256 KB Output is correct
119 Correct 3135 ms 120288 KB Output is correct
120 Correct 4671 ms 114628 KB Output is correct
121 Correct 1278 ms 118844 KB Output is correct
122 Correct 1297 ms 118080 KB Output is correct
123 Correct 1418 ms 120384 KB Output is correct
124 Correct 1551 ms 122064 KB Output is correct
125 Correct 1525 ms 120764 KB Output is correct
126 Correct 1550 ms 122392 KB Output is correct