답안 #545664

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
545664 2022-04-05T06:36:33 Z 8e7 새 집 (APIO18_new_home) C++17
100 / 100
1426 ms 100332 KB
//Challenge: Accepted
#include <bits/stdc++.h>
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
	while (l != r) cout << *l << " ", l++;
	cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 300005
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
const int inf = 2e8;
struct store{
	int x, a, b;
	store(){x = a = b = 0;}
	store(int u, int v, int w){x = u, a = v, b = w;}
};
vector<store> seg[maxn];
 
struct query{
	int x, ti, id;
	query(){x = ti = id = 0;}
} que[maxn];
 
int ans[maxn];
struct event{
	int x, val, l, r;
	event(){x = val = l = r = 0;}
	event(int a, int b, int c, int d){x = a, val = b, l = c, r = d;}
};
vector<event> lev, rev;
struct segtree{
	int type; //1: max, 0:min
	int seg[4*maxn];
	void init(int cur, int l, int r) {
		if (r <= l) return;
		if (type == 0) seg[cur] = inf;
		if (r - l == 1) return;
		int m = (l + r) / 2;
		init(cur*2, l, m), init(cur*2+1, m, r);
	}
	void modify(int cur, int l, int r, int ql, int qr, int v) {
		if (r <= l || ql >= r || qr <= l || qr == ql) return;
		if (ql <= l && qr >= r) {
			if (type) seg[cur] = max(seg[cur], v);
			else seg[cur] = min(seg[cur], v);
			return;
		}
		int m = (l + r) / 2;
		modify(cur*2, l, m, ql, qr, v);
		modify(cur*2+1, m, r, ql, qr, v);
	}	
	int query(int cur, int l, int r, int ind) {
		if (r <= l || ind >= r || ind < l) return type ? -inf : inf;
		int val = seg[cur];
		if (r - l == 1) return val;
		int m = (l + r) / 2, rec;
		if (ind < m) rec = query(cur*2, l, m, ind);
		else rec = query(cur*2+1, m, r, ind);
		return type ? max(val, rec) : min(val, rec);
	}	
} ltr, rtr;
 
 
int main() {
	io
	int n, k, q;
	cin >> n >> k >> q;
	vector<int> tv;
	for (int i = 1;i <= k;i++) seg[i].push_back(store(-inf, 0, inf));
	for (int i = 0;i < n;i++) {
		store s;
		int ty;
		cin >> s.x >> ty >> s.a >> s.b;		
		s.b++;
		//tv.push_back(s.a), tv.push_back(s.b);	
		seg[ty].push_back(s);
	}
	for (int i = 1;i <= k;i++) seg[i].push_back(store(inf, 0, inf));
	for (int i = 0;i < q;i++) {
		cin >> que[i].x >> que[i].ti;
		que[i].id = i;
		tv.push_back(que[i].ti);
	}
	sort(tv.begin(), tv.end());
	tv.resize(int(unique(tv.begin(), tv.end()) - tv.begin()));
	//pary(tv.begin(), tv.end());
	auto conv = [&] (int x){
		return lower_bound(tv.begin(), tv.end(), x) - tv.begin();
	};
	for (int i = 1;i <= k;i++) {
		set<pii> se;
 
		auto add_seg = [&] (int l, int r, int u, int d) {
			if (d <= u) return;
			int m = (l + r + 1) / 2;	
			debug("seg", l, r, u, d);
			lev.push_back(event(m-1, l, u, d));
			rev.push_back(event(m, r, u, d));
		};
		sort(seg[i].begin(), seg[i].end(), [&] (store x, store y){return x.x < y.x;});
		for (auto &p:seg[i]) {
			p.a = conv(p.a), p.b = conv(p.b);	
			if (se.empty()) {
				se.insert({p.a, p.x});
				se.insert({p.b+1, p.x});
			} else {
				if (p.a == p.b) continue;
				auto it = se.lower_bound(make_pair(p.a, -inf));	
				int cur = p.a;
				while (it != se.end() && it->ff <= p.b) {
					if (it != se.begin()) add_seg(prev(it)->ss, p.x, cur, it->ff);
					cur = it->ff;
					it = next(it);
				}
				if (it != se.begin()) add_seg(prev(it)->ss, p.x, cur, p.b);
 
				it = se.lower_bound(make_pair(p.a, -inf));
				int last = prev(it)->ss;
				while (it != se.end() && it->ff < p.b) {
					last = it->ss;	
					se.erase(it);
					it = se.lower_bound(make_pair(p.a, -inf));
				}
				se.insert(make_pair(p.a, p.x));
				if (it->ff > p.b) {
					se.insert(make_pair(p.b, last));	
				}
			}
		}	
	}
	for (int i = 0;i < q;i++) que[i].ti = conv(que[i].ti);	
	sort(que, que + q, [&] (query x, query y){return x.x < y.x;});		
	sort(lev.begin(), lev.end(), [&] (event x, event y){return x.x > y.x;});
	sort(rev.begin(), rev.end(), [&] (event x, event y){return x.x < y.x;});
 
	int lid = 0, rid = 0;
	rtr.type = 1;
	ltr.init(1, 0, q);
	for (int i = 0;i < q;i++) {
		query cur = que[i];
		while (rid < rev.size() && rev[rid].x <= cur.x) {
			rtr.modify(1, 0, q, rev[rid].l, rev[rid].r, rev[rid].val);
			rid++;
		}
		ans[cur.id] = rtr.query(1, 0, q, cur.ti) - cur.x;		
	}
	for (int i = q - 1;i >= 0;i--) {
		query cur = que[i];
		while (lid < lev.size() && lev[lid].x >= cur.x) {
			ltr.modify(1, 0, q, lev[lid].l, lev[lid].r, lev[lid].val);
			lid++;
		}
		ans[cur.id] = max(ans[cur.id], cur.x - ltr.query(1, 0, q, cur.ti));
	}
	for (int i = 0;i < q;i++) {
		cout << (ans[i] >= inf/2 ? -1 : ans[i]) << "\n";
	}
}

Compilation message

new_home.cpp: In lambda function:
new_home.cpp:12:20: warning: statement has no effect [-Wunused-value]
   12 | #define debug(...) 0
      |                    ^
new_home.cpp:106:4: note: in expansion of macro 'debug'
  106 |    debug("seg", l, r, u, d);
      |    ^~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:151:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  151 |   while (rid < rev.size() && rev[rid].x <= cur.x) {
      |          ~~~~^~~~~~~~~~~~
new_home.cpp:159:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<event>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  159 |   while (lid < lev.size() && lev[lid].x >= cur.x) {
      |          ~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 5 ms 10868 KB Output is correct
3 Correct 6 ms 10952 KB Output is correct
4 Correct 8 ms 10836 KB Output is correct
5 Correct 6 ms 10964 KB Output is correct
6 Correct 7 ms 11040 KB Output is correct
7 Correct 7 ms 11040 KB Output is correct
8 Correct 8 ms 11148 KB Output is correct
9 Correct 9 ms 11044 KB Output is correct
10 Correct 7 ms 10964 KB Output is correct
11 Correct 9 ms 10988 KB Output is correct
12 Correct 7 ms 10932 KB Output is correct
13 Correct 7 ms 10916 KB Output is correct
14 Correct 6 ms 10916 KB Output is correct
15 Correct 7 ms 11020 KB Output is correct
16 Correct 7 ms 10964 KB Output is correct
17 Correct 7 ms 11040 KB Output is correct
18 Correct 8 ms 11048 KB Output is correct
19 Correct 7 ms 10964 KB Output is correct
20 Correct 7 ms 11044 KB Output is correct
21 Correct 8 ms 10916 KB Output is correct
22 Correct 8 ms 11068 KB Output is correct
23 Correct 8 ms 10964 KB Output is correct
24 Correct 8 ms 11040 KB Output is correct
25 Correct 8 ms 11172 KB Output is correct
26 Correct 7 ms 10964 KB Output is correct
27 Correct 6 ms 10964 KB Output is correct
28 Correct 7 ms 11048 KB Output is correct
29 Correct 6 ms 10912 KB Output is correct
30 Correct 6 ms 10964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 5 ms 10868 KB Output is correct
3 Correct 6 ms 10952 KB Output is correct
4 Correct 8 ms 10836 KB Output is correct
5 Correct 6 ms 10964 KB Output is correct
6 Correct 7 ms 11040 KB Output is correct
7 Correct 7 ms 11040 KB Output is correct
8 Correct 8 ms 11148 KB Output is correct
9 Correct 9 ms 11044 KB Output is correct
10 Correct 7 ms 10964 KB Output is correct
11 Correct 9 ms 10988 KB Output is correct
12 Correct 7 ms 10932 KB Output is correct
13 Correct 7 ms 10916 KB Output is correct
14 Correct 6 ms 10916 KB Output is correct
15 Correct 7 ms 11020 KB Output is correct
16 Correct 7 ms 10964 KB Output is correct
17 Correct 7 ms 11040 KB Output is correct
18 Correct 8 ms 11048 KB Output is correct
19 Correct 7 ms 10964 KB Output is correct
20 Correct 7 ms 11044 KB Output is correct
21 Correct 8 ms 10916 KB Output is correct
22 Correct 8 ms 11068 KB Output is correct
23 Correct 8 ms 10964 KB Output is correct
24 Correct 8 ms 11040 KB Output is correct
25 Correct 8 ms 11172 KB Output is correct
26 Correct 7 ms 10964 KB Output is correct
27 Correct 6 ms 10964 KB Output is correct
28 Correct 7 ms 11048 KB Output is correct
29 Correct 6 ms 10912 KB Output is correct
30 Correct 6 ms 10964 KB Output is correct
31 Correct 242 ms 23164 KB Output is correct
32 Correct 111 ms 18132 KB Output is correct
33 Correct 233 ms 22324 KB Output is correct
34 Correct 238 ms 23280 KB Output is correct
35 Correct 248 ms 22500 KB Output is correct
36 Correct 231 ms 22460 KB Output is correct
37 Correct 232 ms 22352 KB Output is correct
38 Correct 207 ms 21976 KB Output is correct
39 Correct 178 ms 22212 KB Output is correct
40 Correct 202 ms 22192 KB Output is correct
41 Correct 212 ms 22744 KB Output is correct
42 Correct 218 ms 22540 KB Output is correct
43 Correct 63 ms 18156 KB Output is correct
44 Correct 208 ms 22768 KB Output is correct
45 Correct 237 ms 22516 KB Output is correct
46 Correct 201 ms 22244 KB Output is correct
47 Correct 143 ms 21684 KB Output is correct
48 Correct 154 ms 21372 KB Output is correct
49 Correct 172 ms 22064 KB Output is correct
50 Correct 179 ms 22652 KB Output is correct
51 Correct 196 ms 22068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 569 ms 43164 KB Output is correct
2 Correct 534 ms 35188 KB Output is correct
3 Correct 813 ms 76124 KB Output is correct
4 Correct 664 ms 60036 KB Output is correct
5 Correct 541 ms 51096 KB Output is correct
6 Correct 533 ms 47704 KB Output is correct
7 Correct 744 ms 71728 KB Output is correct
8 Correct 678 ms 60272 KB Output is correct
9 Correct 581 ms 53764 KB Output is correct
10 Correct 546 ms 46520 KB Output is correct
11 Correct 417 ms 45200 KB Output is correct
12 Correct 474 ms 50096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 916 ms 51660 KB Output is correct
2 Correct 399 ms 40240 KB Output is correct
3 Correct 938 ms 57960 KB Output is correct
4 Correct 1043 ms 87644 KB Output is correct
5 Correct 970 ms 68132 KB Output is correct
6 Correct 941 ms 71256 KB Output is correct
7 Correct 982 ms 64380 KB Output is correct
8 Correct 947 ms 60184 KB Output is correct
9 Correct 891 ms 81492 KB Output is correct
10 Correct 930 ms 68708 KB Output is correct
11 Correct 916 ms 64968 KB Output is correct
12 Correct 955 ms 59220 KB Output is correct
13 Correct 634 ms 60124 KB Output is correct
14 Correct 656 ms 59780 KB Output is correct
15 Correct 736 ms 61136 KB Output is correct
16 Correct 756 ms 63044 KB Output is correct
17 Correct 729 ms 57076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 5 ms 10868 KB Output is correct
3 Correct 6 ms 10952 KB Output is correct
4 Correct 8 ms 10836 KB Output is correct
5 Correct 6 ms 10964 KB Output is correct
6 Correct 7 ms 11040 KB Output is correct
7 Correct 7 ms 11040 KB Output is correct
8 Correct 8 ms 11148 KB Output is correct
9 Correct 9 ms 11044 KB Output is correct
10 Correct 7 ms 10964 KB Output is correct
11 Correct 9 ms 10988 KB Output is correct
12 Correct 7 ms 10932 KB Output is correct
13 Correct 7 ms 10916 KB Output is correct
14 Correct 6 ms 10916 KB Output is correct
15 Correct 7 ms 11020 KB Output is correct
16 Correct 7 ms 10964 KB Output is correct
17 Correct 7 ms 11040 KB Output is correct
18 Correct 8 ms 11048 KB Output is correct
19 Correct 7 ms 10964 KB Output is correct
20 Correct 7 ms 11044 KB Output is correct
21 Correct 8 ms 10916 KB Output is correct
22 Correct 8 ms 11068 KB Output is correct
23 Correct 8 ms 10964 KB Output is correct
24 Correct 8 ms 11040 KB Output is correct
25 Correct 8 ms 11172 KB Output is correct
26 Correct 7 ms 10964 KB Output is correct
27 Correct 6 ms 10964 KB Output is correct
28 Correct 7 ms 11048 KB Output is correct
29 Correct 6 ms 10912 KB Output is correct
30 Correct 6 ms 10964 KB Output is correct
31 Correct 242 ms 23164 KB Output is correct
32 Correct 111 ms 18132 KB Output is correct
33 Correct 233 ms 22324 KB Output is correct
34 Correct 238 ms 23280 KB Output is correct
35 Correct 248 ms 22500 KB Output is correct
36 Correct 231 ms 22460 KB Output is correct
37 Correct 232 ms 22352 KB Output is correct
38 Correct 207 ms 21976 KB Output is correct
39 Correct 178 ms 22212 KB Output is correct
40 Correct 202 ms 22192 KB Output is correct
41 Correct 212 ms 22744 KB Output is correct
42 Correct 218 ms 22540 KB Output is correct
43 Correct 63 ms 18156 KB Output is correct
44 Correct 208 ms 22768 KB Output is correct
45 Correct 237 ms 22516 KB Output is correct
46 Correct 201 ms 22244 KB Output is correct
47 Correct 143 ms 21684 KB Output is correct
48 Correct 154 ms 21372 KB Output is correct
49 Correct 172 ms 22064 KB Output is correct
50 Correct 179 ms 22652 KB Output is correct
51 Correct 196 ms 22068 KB Output is correct
52 Correct 208 ms 27448 KB Output is correct
53 Correct 204 ms 27460 KB Output is correct
54 Correct 234 ms 24200 KB Output is correct
55 Correct 205 ms 24360 KB Output is correct
56 Correct 199 ms 25360 KB Output is correct
57 Correct 229 ms 23212 KB Output is correct
58 Correct 212 ms 24548 KB Output is correct
59 Correct 210 ms 24908 KB Output is correct
60 Correct 223 ms 23252 KB Output is correct
61 Correct 86 ms 22780 KB Output is correct
62 Correct 194 ms 27284 KB Output is correct
63 Correct 259 ms 25092 KB Output is correct
64 Correct 202 ms 24376 KB Output is correct
65 Correct 216 ms 23248 KB Output is correct
66 Correct 219 ms 22764 KB Output is correct
67 Correct 120 ms 20332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10836 KB Output is correct
2 Correct 5 ms 10868 KB Output is correct
3 Correct 6 ms 10952 KB Output is correct
4 Correct 8 ms 10836 KB Output is correct
5 Correct 6 ms 10964 KB Output is correct
6 Correct 7 ms 11040 KB Output is correct
7 Correct 7 ms 11040 KB Output is correct
8 Correct 8 ms 11148 KB Output is correct
9 Correct 9 ms 11044 KB Output is correct
10 Correct 7 ms 10964 KB Output is correct
11 Correct 9 ms 10988 KB Output is correct
12 Correct 7 ms 10932 KB Output is correct
13 Correct 7 ms 10916 KB Output is correct
14 Correct 6 ms 10916 KB Output is correct
15 Correct 7 ms 11020 KB Output is correct
16 Correct 7 ms 10964 KB Output is correct
17 Correct 7 ms 11040 KB Output is correct
18 Correct 8 ms 11048 KB Output is correct
19 Correct 7 ms 10964 KB Output is correct
20 Correct 7 ms 11044 KB Output is correct
21 Correct 8 ms 10916 KB Output is correct
22 Correct 8 ms 11068 KB Output is correct
23 Correct 8 ms 10964 KB Output is correct
24 Correct 8 ms 11040 KB Output is correct
25 Correct 8 ms 11172 KB Output is correct
26 Correct 7 ms 10964 KB Output is correct
27 Correct 6 ms 10964 KB Output is correct
28 Correct 7 ms 11048 KB Output is correct
29 Correct 6 ms 10912 KB Output is correct
30 Correct 6 ms 10964 KB Output is correct
31 Correct 242 ms 23164 KB Output is correct
32 Correct 111 ms 18132 KB Output is correct
33 Correct 233 ms 22324 KB Output is correct
34 Correct 238 ms 23280 KB Output is correct
35 Correct 248 ms 22500 KB Output is correct
36 Correct 231 ms 22460 KB Output is correct
37 Correct 232 ms 22352 KB Output is correct
38 Correct 207 ms 21976 KB Output is correct
39 Correct 178 ms 22212 KB Output is correct
40 Correct 202 ms 22192 KB Output is correct
41 Correct 212 ms 22744 KB Output is correct
42 Correct 218 ms 22540 KB Output is correct
43 Correct 63 ms 18156 KB Output is correct
44 Correct 208 ms 22768 KB Output is correct
45 Correct 237 ms 22516 KB Output is correct
46 Correct 201 ms 22244 KB Output is correct
47 Correct 143 ms 21684 KB Output is correct
48 Correct 154 ms 21372 KB Output is correct
49 Correct 172 ms 22064 KB Output is correct
50 Correct 179 ms 22652 KB Output is correct
51 Correct 196 ms 22068 KB Output is correct
52 Correct 569 ms 43164 KB Output is correct
53 Correct 534 ms 35188 KB Output is correct
54 Correct 813 ms 76124 KB Output is correct
55 Correct 664 ms 60036 KB Output is correct
56 Correct 541 ms 51096 KB Output is correct
57 Correct 533 ms 47704 KB Output is correct
58 Correct 744 ms 71728 KB Output is correct
59 Correct 678 ms 60272 KB Output is correct
60 Correct 581 ms 53764 KB Output is correct
61 Correct 546 ms 46520 KB Output is correct
62 Correct 417 ms 45200 KB Output is correct
63 Correct 474 ms 50096 KB Output is correct
64 Correct 916 ms 51660 KB Output is correct
65 Correct 399 ms 40240 KB Output is correct
66 Correct 938 ms 57960 KB Output is correct
67 Correct 1043 ms 87644 KB Output is correct
68 Correct 970 ms 68132 KB Output is correct
69 Correct 941 ms 71256 KB Output is correct
70 Correct 982 ms 64380 KB Output is correct
71 Correct 947 ms 60184 KB Output is correct
72 Correct 891 ms 81492 KB Output is correct
73 Correct 930 ms 68708 KB Output is correct
74 Correct 916 ms 64968 KB Output is correct
75 Correct 955 ms 59220 KB Output is correct
76 Correct 634 ms 60124 KB Output is correct
77 Correct 656 ms 59780 KB Output is correct
78 Correct 736 ms 61136 KB Output is correct
79 Correct 756 ms 63044 KB Output is correct
80 Correct 729 ms 57076 KB Output is correct
81 Correct 208 ms 27448 KB Output is correct
82 Correct 204 ms 27460 KB Output is correct
83 Correct 234 ms 24200 KB Output is correct
84 Correct 205 ms 24360 KB Output is correct
85 Correct 199 ms 25360 KB Output is correct
86 Correct 229 ms 23212 KB Output is correct
87 Correct 212 ms 24548 KB Output is correct
88 Correct 210 ms 24908 KB Output is correct
89 Correct 223 ms 23252 KB Output is correct
90 Correct 86 ms 22780 KB Output is correct
91 Correct 194 ms 27284 KB Output is correct
92 Correct 259 ms 25092 KB Output is correct
93 Correct 202 ms 24376 KB Output is correct
94 Correct 216 ms 23248 KB Output is correct
95 Correct 219 ms 22764 KB Output is correct
96 Correct 120 ms 20332 KB Output is correct
97 Correct 1331 ms 100296 KB Output is correct
98 Correct 491 ms 46124 KB Output is correct
99 Correct 1339 ms 69464 KB Output is correct
100 Correct 1295 ms 100332 KB Output is correct
101 Correct 1310 ms 82544 KB Output is correct
102 Correct 1426 ms 69704 KB Output is correct
103 Correct 1201 ms 73176 KB Output is correct
104 Correct 1160 ms 68988 KB Output is correct
105 Correct 1069 ms 68996 KB Output is correct
106 Correct 1072 ms 71492 KB Output is correct
107 Correct 1224 ms 79872 KB Output is correct
108 Correct 1198 ms 88368 KB Output is correct
109 Correct 1261 ms 79152 KB Output is correct
110 Correct 1270 ms 79880 KB Output is correct
111 Correct 1160 ms 92164 KB Output is correct
112 Correct 1243 ms 77816 KB Output is correct
113 Correct 503 ms 70332 KB Output is correct
114 Correct 1165 ms 95816 KB Output is correct
115 Correct 1300 ms 93008 KB Output is correct
116 Correct 1234 ms 83888 KB Output is correct
117 Correct 1228 ms 78188 KB Output is correct
118 Correct 1200 ms 76116 KB Output is correct
119 Correct 594 ms 61968 KB Output is correct
120 Correct 681 ms 65404 KB Output is correct
121 Correct 789 ms 67352 KB Output is correct
122 Correct 784 ms 66368 KB Output is correct
123 Correct 911 ms 71664 KB Output is correct
124 Correct 992 ms 74012 KB Output is correct
125 Correct 933 ms 68556 KB Output is correct
126 Correct 988 ms 75688 KB Output is correct