Submission #111875

# Submission time Handle Problem Language Result Execution time Memory
111875 2019-05-16T13:19:24 Z square1001 New Home (APIO18_new_home) C++14
100 / 100
2041 ms 78176 KB
#include <set>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1012345678;
class segment {
	public:
	int l, r, x;
	segment() : l(0), r(0), x(0) {};
	segment(int l_, int r_, int x_) : l(l_), r(r_), x(x_) {};
	bool operator<(const segment& s) const {
		if(l != s.l) return l < s.l;
		if(r != s.r) return r < s.r;
		return x < s.x;
	}
};
class query {
	public:
	int l, r, x, w;
	query() : l(0), r(0), x(0), w(0) {};
	query(int l_, int r_, int x_, int w_) : l(l_), r(r_), x(x_), w(w_) {};
	bool operator<(const query& q) const {
		if(x != q.x) return x < q.x;
		return w < q.w;
	}
};
class segtree {
	private:
	int sz;
	vector<int> val;
	public:
	segtree() : sz(0), val(vector<int>()) {};
	segtree(int sz_) {
		sz = 1;
		while(sz < sz_) sz *= 2;
		val = vector<int>(sz * 2, -inf);
	}
	void update(int l, int r, int x) {
		l += sz;
		r += sz;
		while(l != r) {
			if(l & 1) val[l] = max(val[l], x), ++l;
			if(r & 1) --r, val[r] = max(val[r], x);
			l >>= 1, r >>= 1;
		}
	}
	int get(int pos) {
		pos += sz;
		int ans = -inf;
		while(pos >= 1) {
			ans = max(ans, val[pos]);
			pos >>= 1;
		}
		return ans;
	}
};
vector<int> solve(int N, int K, int Q, vector<int> X, vector<int> T, vector<int> A, vector<int> B, vector<int> L, vector<int> Y) {
	vector<int> comp = { 0, inf };
	vector<vector<int> > G(K);
	for(int i = 0; i < N; ++i) {
		++B[i], --T[i];
		comp.push_back(A[i]);
		comp.push_back(B[i]);
		X[i] *= 2;
		G[T[i]].push_back(i);
	}
	for(int i = 0; i < Q; ++i) {
		L[i] *= 2;
	}
	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end());
	int S = comp.size();
	for(int i = 0; i < N; ++i) {
		A[i] = lower_bound(comp.begin(), comp.end(), A[i]) - comp.begin();
		B[i] = lower_bound(comp.begin(), comp.end(), B[i]) - comp.begin();
	}
	for(int i = 0; i < Q; ++i) {
		Y[i] = lower_bound(comp.begin(), comp.end(), Y[i] + 1) - comp.begin() - 1;
	}
	vector<query> qs;
	for(int i = 0; i < K; ++i) {
		set<segment> st;
		st.insert(segment(0, S, -inf));
		vector<segment> iniqs;
		for(int j : G[i]) {
			iniqs.push_back(segment(A[j], B[j], X[j]));
		}
		sort(iniqs.begin(), iniqs.end(), [](segment s1, segment s2) { return s1.x < s2.x; });
		int cnt = 0;
		for(segment s : iniqs) {
			set<segment>::iterator it = st.lower_bound(segment(s.l, s.r, s.x));
			if(it != st.begin()) --it;
			vector<segment> nst;
			while(it != st.end() && it->l < s.r) {
				segment t = *it;
				int al = max(t.l, s.l), ar = min(t.r, s.r);
				if(al >= ar) {
					++it;
					continue;
				}
				++cnt;
				it = st.erase(it);
				qs.push_back(query(al, ar, (s.x + t.x) / 2, (s.x - t.x) / 2));
				nst.push_back(segment(al, ar, s.x));
				if(t.l < al) nst.push_back(segment(t.l, al, t.x));
				if(ar < t.r) nst.push_back(segment(ar, t.r, t.x));
			}
			if(!nst.empty()) {
				sort(nst.begin(), nst.end());
				int pre = nst[0].l;
				vector<segment> nnst;
				for(int i = 1; i <= nst.size(); ++i) {
					if(i == nst.size() || nst[i - 1].x != nst[i].x) {
						nnst.push_back(segment(pre, nst[i - 1].r, nst[i - 1].x));
						pre = nst[i - 1].r;
					}
				}
				st.insert(nnst.begin(), nnst.end());
			}
		}
		for(segment s : st) {
			qs.push_back(query(s.l, s.r, (inf + s.x) / 2, (inf - s.x) / 2));
		}
	}
	for(int i = 0; i < Q; ++i) {
		qs.push_back(query(Y[i], Y[i] + 1, L[i], -(i + 1)));
	}
	sort(qs.begin(), qs.end());
	segtree seg(S);
	for(query i : qs) {
		if(i.w >= 0 && i.x - i.w == -inf) {
			seg.update(i.l, i.r, i.x + i.w);
		}
	}
	vector<int> ans(Q, -1);
	for(query i : qs) {
		if(i.w < 0) {
			ans[-i.w - 1] = max(ans[-i.w - 1], seg.get(i.l) - i.x);
		}
		else {
			seg.update(i.l, i.r, i.x + i.w);
		}
	}
	reverse(qs.begin(), qs.end());
	seg = segtree(S);
	for(query i : qs) {
		if(i.w >= 0 && i.x + i.w == inf) {
			seg.update(i.l, i.r, -(i.x - i.w));
		}
	}
	for(query i : qs) {
		if(i.w < 0) {
			ans[-i.w - 1] = max(ans[-i.w - 1], i.x - (-seg.get(i.l)));
		}
		else {
			seg.update(i.l, i.r, -(i.x - i.w));
		}
	}
	for(int i = 0; i < Q; ++i) {
		if(ans[i] > inf / 3) ans[i] = -1;
		else ans[i] /= 2;
	}
	return ans;
}
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N, K, Q;
	cin >> N >> K >> Q;
	vector<int> X(N), T(N), A(N), B(N), L(Q), Y(Q);
	for(int i = 0; i < N; ++i) {
		cin >> X[i] >> T[i] >> A[i] >> B[i];
	}
	for(int i = 0; i < Q; ++i) {
		cin >> L[i] >> Y[i];
	}
	vector<int> ans = solve(N, K, Q, X, T, A, B, L, Y);
	for(int i = 0; i < Q; ++i) {
		cout << ans[i] << '\n';
	}
	return 0;
}

Compilation message

new_home.cpp: In function 'std::vector<int> solve(int, int, int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
new_home.cpp:114:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1; i <= nst.size(); ++i) {
                    ~~^~~~~~~~~~~~~
new_home.cpp:115:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
      if(i == nst.size() || nst[i - 1].x != nst[i].x) {
         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 4 ms 512 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 4 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 4 ms 512 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 4 ms 512 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 4 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 4 ms 512 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 225 ms 13024 KB Output is correct
32 Correct 123 ms 7372 KB Output is correct
33 Correct 215 ms 12804 KB Output is correct
34 Correct 223 ms 13208 KB Output is correct
35 Correct 229 ms 13012 KB Output is correct
36 Correct 222 ms 12976 KB Output is correct
37 Correct 209 ms 12896 KB Output is correct
38 Correct 188 ms 12952 KB Output is correct
39 Correct 187 ms 13020 KB Output is correct
40 Correct 167 ms 12980 KB Output is correct
41 Correct 206 ms 12924 KB Output is correct
42 Correct 198 ms 13008 KB Output is correct
43 Correct 78 ms 8576 KB Output is correct
44 Correct 216 ms 13168 KB Output is correct
45 Correct 183 ms 12980 KB Output is correct
46 Correct 187 ms 12896 KB Output is correct
47 Correct 169 ms 12612 KB Output is correct
48 Correct 163 ms 12540 KB Output is correct
49 Correct 190 ms 12720 KB Output is correct
50 Correct 222 ms 13056 KB Output is correct
51 Correct 196 ms 12644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 610 ms 42204 KB Output is correct
2 Correct 446 ms 39784 KB Output is correct
3 Correct 982 ms 70664 KB Output is correct
4 Correct 670 ms 43216 KB Output is correct
5 Correct 440 ms 43724 KB Output is correct
6 Correct 420 ms 40308 KB Output is correct
7 Correct 690 ms 70332 KB Output is correct
8 Correct 707 ms 43088 KB Output is correct
9 Correct 512 ms 40320 KB Output is correct
10 Correct 475 ms 39340 KB Output is correct
11 Correct 450 ms 38956 KB Output is correct
12 Correct 425 ms 39240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1106 ms 46988 KB Output is correct
2 Correct 357 ms 36152 KB Output is correct
3 Correct 948 ms 42392 KB Output is correct
4 Correct 1483 ms 70080 KB Output is correct
5 Correct 1065 ms 50348 KB Output is correct
6 Correct 1052 ms 59324 KB Output is correct
7 Correct 846 ms 46852 KB Output is correct
8 Correct 904 ms 45872 KB Output is correct
9 Correct 1450 ms 70092 KB Output is correct
10 Correct 1074 ms 58328 KB Output is correct
11 Correct 1050 ms 46756 KB Output is correct
12 Correct 992 ms 45832 KB Output is correct
13 Correct 691 ms 44696 KB Output is correct
14 Correct 645 ms 44800 KB Output is correct
15 Correct 627 ms 45084 KB Output is correct
16 Correct 649 ms 45608 KB Output is correct
17 Correct 702 ms 45040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 4 ms 512 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 4 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 4 ms 512 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 225 ms 13024 KB Output is correct
32 Correct 123 ms 7372 KB Output is correct
33 Correct 215 ms 12804 KB Output is correct
34 Correct 223 ms 13208 KB Output is correct
35 Correct 229 ms 13012 KB Output is correct
36 Correct 222 ms 12976 KB Output is correct
37 Correct 209 ms 12896 KB Output is correct
38 Correct 188 ms 12952 KB Output is correct
39 Correct 187 ms 13020 KB Output is correct
40 Correct 167 ms 12980 KB Output is correct
41 Correct 206 ms 12924 KB Output is correct
42 Correct 198 ms 13008 KB Output is correct
43 Correct 78 ms 8576 KB Output is correct
44 Correct 216 ms 13168 KB Output is correct
45 Correct 183 ms 12980 KB Output is correct
46 Correct 187 ms 12896 KB Output is correct
47 Correct 169 ms 12612 KB Output is correct
48 Correct 163 ms 12540 KB Output is correct
49 Correct 190 ms 12720 KB Output is correct
50 Correct 222 ms 13056 KB Output is correct
51 Correct 196 ms 12644 KB Output is correct
52 Correct 337 ms 18412 KB Output is correct
53 Correct 284 ms 18456 KB Output is correct
54 Correct 258 ms 14284 KB Output is correct
55 Correct 217 ms 14392 KB Output is correct
56 Correct 248 ms 16960 KB Output is correct
57 Correct 249 ms 13356 KB Output is correct
58 Correct 277 ms 14452 KB Output is correct
59 Correct 242 ms 16864 KB Output is correct
60 Correct 243 ms 13584 KB Output is correct
61 Correct 114 ms 17804 KB Output is correct
62 Correct 266 ms 18400 KB Output is correct
63 Correct 278 ms 16740 KB Output is correct
64 Correct 257 ms 14176 KB Output is correct
65 Correct 275 ms 13460 KB Output is correct
66 Correct 237 ms 12992 KB Output is correct
67 Correct 145 ms 9688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 4 ms 512 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 4 ms 512 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 4 ms 512 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 4 ms 384 KB Output is correct
16 Correct 3 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 4 ms 512 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 4 ms 512 KB Output is correct
21 Correct 3 ms 512 KB Output is correct
22 Correct 3 ms 512 KB Output is correct
23 Correct 4 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 4 ms 512 KB Output is correct
26 Correct 3 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 2 ms 512 KB Output is correct
31 Correct 225 ms 13024 KB Output is correct
32 Correct 123 ms 7372 KB Output is correct
33 Correct 215 ms 12804 KB Output is correct
34 Correct 223 ms 13208 KB Output is correct
35 Correct 229 ms 13012 KB Output is correct
36 Correct 222 ms 12976 KB Output is correct
37 Correct 209 ms 12896 KB Output is correct
38 Correct 188 ms 12952 KB Output is correct
39 Correct 187 ms 13020 KB Output is correct
40 Correct 167 ms 12980 KB Output is correct
41 Correct 206 ms 12924 KB Output is correct
42 Correct 198 ms 13008 KB Output is correct
43 Correct 78 ms 8576 KB Output is correct
44 Correct 216 ms 13168 KB Output is correct
45 Correct 183 ms 12980 KB Output is correct
46 Correct 187 ms 12896 KB Output is correct
47 Correct 169 ms 12612 KB Output is correct
48 Correct 163 ms 12540 KB Output is correct
49 Correct 190 ms 12720 KB Output is correct
50 Correct 222 ms 13056 KB Output is correct
51 Correct 196 ms 12644 KB Output is correct
52 Correct 610 ms 42204 KB Output is correct
53 Correct 446 ms 39784 KB Output is correct
54 Correct 982 ms 70664 KB Output is correct
55 Correct 670 ms 43216 KB Output is correct
56 Correct 440 ms 43724 KB Output is correct
57 Correct 420 ms 40308 KB Output is correct
58 Correct 690 ms 70332 KB Output is correct
59 Correct 707 ms 43088 KB Output is correct
60 Correct 512 ms 40320 KB Output is correct
61 Correct 475 ms 39340 KB Output is correct
62 Correct 450 ms 38956 KB Output is correct
63 Correct 425 ms 39240 KB Output is correct
64 Correct 1106 ms 46988 KB Output is correct
65 Correct 357 ms 36152 KB Output is correct
66 Correct 948 ms 42392 KB Output is correct
67 Correct 1483 ms 70080 KB Output is correct
68 Correct 1065 ms 50348 KB Output is correct
69 Correct 1052 ms 59324 KB Output is correct
70 Correct 846 ms 46852 KB Output is correct
71 Correct 904 ms 45872 KB Output is correct
72 Correct 1450 ms 70092 KB Output is correct
73 Correct 1074 ms 58328 KB Output is correct
74 Correct 1050 ms 46756 KB Output is correct
75 Correct 992 ms 45832 KB Output is correct
76 Correct 691 ms 44696 KB Output is correct
77 Correct 645 ms 44800 KB Output is correct
78 Correct 627 ms 45084 KB Output is correct
79 Correct 649 ms 45608 KB Output is correct
80 Correct 702 ms 45040 KB Output is correct
81 Correct 337 ms 18412 KB Output is correct
82 Correct 284 ms 18456 KB Output is correct
83 Correct 258 ms 14284 KB Output is correct
84 Correct 217 ms 14392 KB Output is correct
85 Correct 248 ms 16960 KB Output is correct
86 Correct 249 ms 13356 KB Output is correct
87 Correct 277 ms 14452 KB Output is correct
88 Correct 242 ms 16864 KB Output is correct
89 Correct 243 ms 13584 KB Output is correct
90 Correct 114 ms 17804 KB Output is correct
91 Correct 266 ms 18400 KB Output is correct
92 Correct 278 ms 16740 KB Output is correct
93 Correct 257 ms 14176 KB Output is correct
94 Correct 275 ms 13460 KB Output is correct
95 Correct 237 ms 12992 KB Output is correct
96 Correct 145 ms 9688 KB Output is correct
97 Correct 2041 ms 78172 KB Output is correct
98 Correct 362 ms 39948 KB Output is correct
99 Correct 1268 ms 58620 KB Output is correct
100 Correct 1881 ms 78176 KB Output is correct
101 Correct 1699 ms 64192 KB Output is correct
102 Correct 1352 ms 58696 KB Output is correct
103 Correct 1031 ms 58632 KB Output is correct
104 Correct 1124 ms 61032 KB Output is correct
105 Correct 963 ms 58556 KB Output is correct
106 Correct 1150 ms 60856 KB Output is correct
107 Correct 1397 ms 64940 KB Output is correct
108 Correct 1493 ms 68284 KB Output is correct
109 Correct 1210 ms 61264 KB Output is correct
110 Correct 1256 ms 65308 KB Output is correct
111 Correct 1350 ms 68612 KB Output is correct
112 Correct 1459 ms 60616 KB Output is correct
113 Correct 597 ms 69864 KB Output is correct
114 Correct 1629 ms 77964 KB Output is correct
115 Correct 1480 ms 67516 KB Output is correct
116 Correct 1431 ms 63896 KB Output is correct
117 Correct 1449 ms 60416 KB Output is correct
118 Correct 1413 ms 58904 KB Output is correct
119 Correct 680 ms 56240 KB Output is correct
120 Correct 843 ms 57092 KB Output is correct
121 Correct 810 ms 57452 KB Output is correct
122 Correct 895 ms 58180 KB Output is correct
123 Correct 1002 ms 57800 KB Output is correct
124 Correct 1045 ms 58552 KB Output is correct
125 Correct 1075 ms 57920 KB Output is correct
126 Correct 1134 ms 58784 KB Output is correct