답안 #49635

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
49635 2018-06-01T12:09:23 Z gs14004 새 집 (APIO18_new_home) C++17
100 / 100
2974 ms 325660 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int MAXN = 300005;
 
struct query{
	int pos, t, idx;
	bool operator<(const query &q)const{
		return t < q.t;
	};
}qry[MAXN];
 
struct query2{
	int t, mode, val;
	bool operator<(const query2 &q)const{
		return pi(t, mode) < pi(q.t, q.mode);
	};
};
 
struct rect{ int st, et, y, mode, rpos; };
// mode 1 : [0, y]
// mode -1 : [y, inf]
struct intv{ int s, e, x; };
vector<intv> house[MAXN];
vector<rect> rec;
int n, k, q, ans[MAXN];
const int inf = 500000005;
 
struct rect2{
	int sx, ex, ins;
	bool operator<(const rect2 &r)const{
		return sx < r.sx;
	}
};
 
void process(int h){
	vector<query2> v;
	for(auto &i : house[h]){
		i.s = lower_bound(qry, qry + q, (query){-1, i.s, -1}) - qry;
		i.e = upper_bound(qry, qry + q, (query){-1, i.e, -1}) - qry;
		if(i.s == i.e) continue;
		v.push_back({i.s, 1, i.x});
		v.push_back({i.e, -1, i.x});
	}
	sort(v.begin(), v.end());
	map<int, int> cnt;
	set<rect2> r;
	r.insert({-1, inf, 0});
	auto insert_rect = [&](int st, int et, int sy, int ey){
		if(st == et) return;
		et--;
		if(sy == -1){
			rec.push_back({st, et, -1, -1, ey});
			return;
		}
		if(ey == inf){
			rec.push_back({st, et, inf, 1, sy});
			return;
		}
		int my = (sy + ey) / 2;
		rec.push_back({st, et, my, 1, sy});
		rec.push_back({st, et, my+1, -1, ey});
	};
	for(int i=0; i<v.size(); i++){
		auto insert_intv = [&](int pos){
			auto l = --r.upper_bound({pos, -1, -1});
			insert_rect(l->ins, v[i].t, l->sx, l->ex);
			rect2 ins1 = {l->sx, pos, v[i].t};
			rect2 ins2 = {pos, l->ex, v[i].t};
			r.erase(l);
			r.insert(ins1);
			r.insert(ins2);
		};
		auto remove_intv = [&](int pos){
			auto nxt = r.lower_bound({pos, -1, -1});
			auto prv = prev(nxt);
			insert_rect(prv->ins, v[i].t, prv->sx, prv->ex);
			insert_rect(nxt->ins, v[i].t, nxt->sx, nxt->ex);
			rect2 ins = {prv->sx, nxt->ex, v[i].t};
			r.erase(prv);
			r.erase(nxt);
			r.insert(ins);
		};
		for(int j=i; j<i+1; j++){
			if(cnt[v[j].val] == 0) insert_intv(v[j].val);
			cnt[v[j].val] += v[j].mode;
			if(cnt[v[j].val] == 0) remove_intv(v[j].val);
		}
	}
}
 
struct query3{
	int pos, l, r, v;
	bool operator<(const query3 &q)const{
		return pos < q.pos;
	}
};
 
vector<query3> mode1,mode2;
// mode1 : disappear after y + 1
// mode2 : appear after y
 
struct seg{
	int seg[1050000], lim;
	void init(int n){
		for(lim = 1; lim <= n; lim <<= 1);
		fill(seg, seg + 1050000, 1e9);
	}
	void add(int l, int r, int val){
		l += lim;
		r += lim;
		while(l < r){
			if(l%2 == 1) seg[l] = min(seg[l], val), l++;
			if(r%2 == 0) seg[r] = min(seg[r], val), r--;
			l >>= 1;
			r >>= 1;
		}
		if(l == r) seg[l] = min(seg[l], val);
	}
	int query(int pos){
		pos += lim;
		int ans = 1e9;
		while(pos){
			ans = min(ans, seg[pos]);
			pos >>= 1;
		}
		return ans;
	}
}seg;
 
int main(){
	scanf("%d %d %d",&n,&k,&q);
	for(int i=0; i<n; i++){
		int x, t, s, e;
		x = rand()%100000000 + 1;
		t = rand()%k +1;
		s = 1;
		e = 100000000;
		scanf("%d %d %d %d",&x,&t,&s,&e);
		house[t].push_back({s, e, x});
	}
	for(int i=1; i<=k; i++){
		house[i].push_back({1, 100000000, 500000000});
	}
	for(int i=0; i<q; i++){
		qry[i].pos = rand()%100000000 + 1;
		qry[i].t = 1;
		scanf("%d %d",&qry[i].pos,&qry[i].t);
		qry[i].idx = i;
	}
	sort(qry, qry + q);
	for(int i=1; i<=k; i++) process(i);
	for(auto &j : rec){
		if(j.mode == 1){
			mode1.push_back({j.y, j.st, j.et, j.rpos});
		}
		else{
			mode2.push_back({j.y, j.st, j.et, j.rpos});
		}
	}
	for(int i=0; i<q; i++) qry[i].t = i;
	sort(qry, qry + q, [&](const query &a, const query &b){
		return a.pos < b.pos;
	});
	sort(mode1.begin(), mode1.end());
	sort(mode2.begin(), mode2.end());
	int ptr = 0;
	seg.init(q + 1);
	for(int i=0; i<q; i++){
		while(ptr < mode2.size() && mode2[ptr].pos <= qry[i].pos){
			seg.add(mode2[ptr].l, mode2[ptr].r, -mode2[ptr].v);
			ptr++;
		}
		int query2 = -seg.query(qry[i].t) - qry[i].pos;
		ans[qry[i].idx] = max(ans[qry[i].idx], query2);
	}
	seg.init(q + 1);
	ptr = mode1.size();
	for(int i=q-1; i>=0; i--){
		while(ptr > 0 && mode1[ptr-1].pos >= qry[i].pos){
			ptr--;
			seg.add(mode1[ptr].l, mode1[ptr].r, mode1[ptr].v);
		}
		int query1 = qry[i].pos - seg.query(qry[i].t);
		ans[qry[i].idx] = max(ans[qry[i].idx], query1);
	}
	for(int i=0; i<q; i++){
		if(ans[i] > 2e8) ans[i] = -1;
		printf("%d\n", ans[i]);
	}
}

Compilation message

new_home.cpp: In function 'void process(int)':
new_home.cpp:66:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0; i<v.size(); i++){
               ~^~~~~~~~~
new_home.cpp: In function 'int main()':
new_home.cpp:172:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ptr < mode2.size() && mode2[ptr].pos <= qry[i].pos){
         ~~~~^~~~~~~~~~~~~~
new_home.cpp:134:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d",&n,&k,&q);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~
new_home.cpp:141:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d %d",&x,&t,&s,&e);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:150:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&qry[i].pos,&qry[i].t);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 11512 KB Output is correct
2 Correct 13 ms 11512 KB Output is correct
3 Correct 14 ms 11596 KB Output is correct
4 Correct 12 ms 11596 KB Output is correct
5 Correct 17 ms 11620 KB Output is correct
6 Correct 15 ms 11804 KB Output is correct
7 Correct 14 ms 11804 KB Output is correct
8 Correct 15 ms 11804 KB Output is correct
9 Correct 15 ms 11896 KB Output is correct
10 Correct 16 ms 11896 KB Output is correct
11 Correct 14 ms 11992 KB Output is correct
12 Correct 18 ms 11992 KB Output is correct
13 Correct 13 ms 11992 KB Output is correct
14 Correct 14 ms 11992 KB Output is correct
15 Correct 14 ms 11992 KB Output is correct
16 Correct 14 ms 11992 KB Output is correct
17 Correct 13 ms 11992 KB Output is correct
18 Correct 14 ms 11992 KB Output is correct
19 Correct 14 ms 11992 KB Output is correct
20 Correct 17 ms 11992 KB Output is correct
21 Correct 16 ms 11992 KB Output is correct
22 Correct 12 ms 11992 KB Output is correct
23 Correct 12 ms 11992 KB Output is correct
24 Correct 13 ms 11992 KB Output is correct
25 Correct 13 ms 11992 KB Output is correct
26 Correct 12 ms 11992 KB Output is correct
27 Correct 12 ms 11992 KB Output is correct
28 Correct 14 ms 11992 KB Output is correct
29 Correct 14 ms 11992 KB Output is correct
30 Correct 17 ms 11992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 11512 KB Output is correct
2 Correct 13 ms 11512 KB Output is correct
3 Correct 14 ms 11596 KB Output is correct
4 Correct 12 ms 11596 KB Output is correct
5 Correct 17 ms 11620 KB Output is correct
6 Correct 15 ms 11804 KB Output is correct
7 Correct 14 ms 11804 KB Output is correct
8 Correct 15 ms 11804 KB Output is correct
9 Correct 15 ms 11896 KB Output is correct
10 Correct 16 ms 11896 KB Output is correct
11 Correct 14 ms 11992 KB Output is correct
12 Correct 18 ms 11992 KB Output is correct
13 Correct 13 ms 11992 KB Output is correct
14 Correct 14 ms 11992 KB Output is correct
15 Correct 14 ms 11992 KB Output is correct
16 Correct 14 ms 11992 KB Output is correct
17 Correct 13 ms 11992 KB Output is correct
18 Correct 14 ms 11992 KB Output is correct
19 Correct 14 ms 11992 KB Output is correct
20 Correct 17 ms 11992 KB Output is correct
21 Correct 16 ms 11992 KB Output is correct
22 Correct 12 ms 11992 KB Output is correct
23 Correct 12 ms 11992 KB Output is correct
24 Correct 13 ms 11992 KB Output is correct
25 Correct 13 ms 11992 KB Output is correct
26 Correct 12 ms 11992 KB Output is correct
27 Correct 12 ms 11992 KB Output is correct
28 Correct 14 ms 11992 KB Output is correct
29 Correct 14 ms 11992 KB Output is correct
30 Correct 17 ms 11992 KB Output is correct
31 Correct 295 ms 31136 KB Output is correct
32 Correct 87 ms 31136 KB Output is correct
33 Correct 396 ms 36160 KB Output is correct
34 Correct 283 ms 37848 KB Output is correct
35 Correct 298 ms 41812 KB Output is correct
36 Correct 336 ms 43400 KB Output is correct
37 Correct 295 ms 46256 KB Output is correct
38 Correct 322 ms 50320 KB Output is correct
39 Correct 311 ms 52740 KB Output is correct
40 Correct 295 ms 55700 KB Output is correct
41 Correct 316 ms 55700 KB Output is correct
42 Correct 241 ms 58340 KB Output is correct
43 Correct 79 ms 58340 KB Output is correct
44 Correct 241 ms 65024 KB Output is correct
45 Correct 270 ms 67860 KB Output is correct
46 Correct 223 ms 68536 KB Output is correct
47 Correct 187 ms 70764 KB Output is correct
48 Correct 193 ms 72944 KB Output is correct
49 Correct 212 ms 78196 KB Output is correct
50 Correct 220 ms 80520 KB Output is correct
51 Correct 207 ms 80520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 938 ms 114664 KB Output is correct
2 Correct 1592 ms 116324 KB Output is correct
3 Correct 1072 ms 166600 KB Output is correct
4 Correct 1010 ms 166600 KB Output is correct
5 Correct 2896 ms 166600 KB Output is correct
6 Correct 1792 ms 166600 KB Output is correct
7 Correct 976 ms 166600 KB Output is correct
8 Correct 792 ms 166600 KB Output is correct
9 Correct 827 ms 166600 KB Output is correct
10 Correct 930 ms 166600 KB Output is correct
11 Correct 993 ms 166600 KB Output is correct
12 Correct 905 ms 166600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1407 ms 166600 KB Output is correct
2 Correct 329 ms 166600 KB Output is correct
3 Correct 2048 ms 176548 KB Output is correct
4 Correct 1268 ms 205280 KB Output is correct
5 Correct 1188 ms 205544 KB Output is correct
6 Correct 1404 ms 218084 KB Output is correct
7 Correct 2974 ms 248744 KB Output is correct
8 Correct 2108 ms 248744 KB Output is correct
9 Correct 1156 ms 271028 KB Output is correct
10 Correct 1193 ms 271028 KB Output is correct
11 Correct 1370 ms 282328 KB Output is correct
12 Correct 1463 ms 285776 KB Output is correct
13 Correct 1183 ms 285776 KB Output is correct
14 Correct 1084 ms 285776 KB Output is correct
15 Correct 1209 ms 287420 KB Output is correct
16 Correct 1273 ms 289012 KB Output is correct
17 Correct 1473 ms 289012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 11512 KB Output is correct
2 Correct 13 ms 11512 KB Output is correct
3 Correct 14 ms 11596 KB Output is correct
4 Correct 12 ms 11596 KB Output is correct
5 Correct 17 ms 11620 KB Output is correct
6 Correct 15 ms 11804 KB Output is correct
7 Correct 14 ms 11804 KB Output is correct
8 Correct 15 ms 11804 KB Output is correct
9 Correct 15 ms 11896 KB Output is correct
10 Correct 16 ms 11896 KB Output is correct
11 Correct 14 ms 11992 KB Output is correct
12 Correct 18 ms 11992 KB Output is correct
13 Correct 13 ms 11992 KB Output is correct
14 Correct 14 ms 11992 KB Output is correct
15 Correct 14 ms 11992 KB Output is correct
16 Correct 14 ms 11992 KB Output is correct
17 Correct 13 ms 11992 KB Output is correct
18 Correct 14 ms 11992 KB Output is correct
19 Correct 14 ms 11992 KB Output is correct
20 Correct 17 ms 11992 KB Output is correct
21 Correct 16 ms 11992 KB Output is correct
22 Correct 12 ms 11992 KB Output is correct
23 Correct 12 ms 11992 KB Output is correct
24 Correct 13 ms 11992 KB Output is correct
25 Correct 13 ms 11992 KB Output is correct
26 Correct 12 ms 11992 KB Output is correct
27 Correct 12 ms 11992 KB Output is correct
28 Correct 14 ms 11992 KB Output is correct
29 Correct 14 ms 11992 KB Output is correct
30 Correct 17 ms 11992 KB Output is correct
31 Correct 295 ms 31136 KB Output is correct
32 Correct 87 ms 31136 KB Output is correct
33 Correct 396 ms 36160 KB Output is correct
34 Correct 283 ms 37848 KB Output is correct
35 Correct 298 ms 41812 KB Output is correct
36 Correct 336 ms 43400 KB Output is correct
37 Correct 295 ms 46256 KB Output is correct
38 Correct 322 ms 50320 KB Output is correct
39 Correct 311 ms 52740 KB Output is correct
40 Correct 295 ms 55700 KB Output is correct
41 Correct 316 ms 55700 KB Output is correct
42 Correct 241 ms 58340 KB Output is correct
43 Correct 79 ms 58340 KB Output is correct
44 Correct 241 ms 65024 KB Output is correct
45 Correct 270 ms 67860 KB Output is correct
46 Correct 223 ms 68536 KB Output is correct
47 Correct 187 ms 70764 KB Output is correct
48 Correct 193 ms 72944 KB Output is correct
49 Correct 212 ms 78196 KB Output is correct
50 Correct 220 ms 80520 KB Output is correct
51 Correct 207 ms 80520 KB Output is correct
52 Correct 283 ms 289012 KB Output is correct
53 Correct 280 ms 289012 KB Output is correct
54 Correct 248 ms 289012 KB Output is correct
55 Correct 231 ms 289012 KB Output is correct
56 Correct 240 ms 289012 KB Output is correct
57 Correct 225 ms 289012 KB Output is correct
58 Correct 236 ms 289012 KB Output is correct
59 Correct 233 ms 289012 KB Output is correct
60 Correct 227 ms 289012 KB Output is correct
61 Correct 162 ms 289012 KB Output is correct
62 Correct 329 ms 289012 KB Output is correct
63 Correct 251 ms 289012 KB Output is correct
64 Correct 229 ms 289012 KB Output is correct
65 Correct 284 ms 289012 KB Output is correct
66 Correct 233 ms 289012 KB Output is correct
67 Correct 186 ms 289012 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 11512 KB Output is correct
2 Correct 13 ms 11512 KB Output is correct
3 Correct 14 ms 11596 KB Output is correct
4 Correct 12 ms 11596 KB Output is correct
5 Correct 17 ms 11620 KB Output is correct
6 Correct 15 ms 11804 KB Output is correct
7 Correct 14 ms 11804 KB Output is correct
8 Correct 15 ms 11804 KB Output is correct
9 Correct 15 ms 11896 KB Output is correct
10 Correct 16 ms 11896 KB Output is correct
11 Correct 14 ms 11992 KB Output is correct
12 Correct 18 ms 11992 KB Output is correct
13 Correct 13 ms 11992 KB Output is correct
14 Correct 14 ms 11992 KB Output is correct
15 Correct 14 ms 11992 KB Output is correct
16 Correct 14 ms 11992 KB Output is correct
17 Correct 13 ms 11992 KB Output is correct
18 Correct 14 ms 11992 KB Output is correct
19 Correct 14 ms 11992 KB Output is correct
20 Correct 17 ms 11992 KB Output is correct
21 Correct 16 ms 11992 KB Output is correct
22 Correct 12 ms 11992 KB Output is correct
23 Correct 12 ms 11992 KB Output is correct
24 Correct 13 ms 11992 KB Output is correct
25 Correct 13 ms 11992 KB Output is correct
26 Correct 12 ms 11992 KB Output is correct
27 Correct 12 ms 11992 KB Output is correct
28 Correct 14 ms 11992 KB Output is correct
29 Correct 14 ms 11992 KB Output is correct
30 Correct 17 ms 11992 KB Output is correct
31 Correct 295 ms 31136 KB Output is correct
32 Correct 87 ms 31136 KB Output is correct
33 Correct 396 ms 36160 KB Output is correct
34 Correct 283 ms 37848 KB Output is correct
35 Correct 298 ms 41812 KB Output is correct
36 Correct 336 ms 43400 KB Output is correct
37 Correct 295 ms 46256 KB Output is correct
38 Correct 322 ms 50320 KB Output is correct
39 Correct 311 ms 52740 KB Output is correct
40 Correct 295 ms 55700 KB Output is correct
41 Correct 316 ms 55700 KB Output is correct
42 Correct 241 ms 58340 KB Output is correct
43 Correct 79 ms 58340 KB Output is correct
44 Correct 241 ms 65024 KB Output is correct
45 Correct 270 ms 67860 KB Output is correct
46 Correct 223 ms 68536 KB Output is correct
47 Correct 187 ms 70764 KB Output is correct
48 Correct 193 ms 72944 KB Output is correct
49 Correct 212 ms 78196 KB Output is correct
50 Correct 220 ms 80520 KB Output is correct
51 Correct 207 ms 80520 KB Output is correct
52 Correct 938 ms 114664 KB Output is correct
53 Correct 1592 ms 116324 KB Output is correct
54 Correct 1072 ms 166600 KB Output is correct
55 Correct 1010 ms 166600 KB Output is correct
56 Correct 2896 ms 166600 KB Output is correct
57 Correct 1792 ms 166600 KB Output is correct
58 Correct 976 ms 166600 KB Output is correct
59 Correct 792 ms 166600 KB Output is correct
60 Correct 827 ms 166600 KB Output is correct
61 Correct 930 ms 166600 KB Output is correct
62 Correct 993 ms 166600 KB Output is correct
63 Correct 905 ms 166600 KB Output is correct
64 Correct 1407 ms 166600 KB Output is correct
65 Correct 329 ms 166600 KB Output is correct
66 Correct 2048 ms 176548 KB Output is correct
67 Correct 1268 ms 205280 KB Output is correct
68 Correct 1188 ms 205544 KB Output is correct
69 Correct 1404 ms 218084 KB Output is correct
70 Correct 2974 ms 248744 KB Output is correct
71 Correct 2108 ms 248744 KB Output is correct
72 Correct 1156 ms 271028 KB Output is correct
73 Correct 1193 ms 271028 KB Output is correct
74 Correct 1370 ms 282328 KB Output is correct
75 Correct 1463 ms 285776 KB Output is correct
76 Correct 1183 ms 285776 KB Output is correct
77 Correct 1084 ms 285776 KB Output is correct
78 Correct 1209 ms 287420 KB Output is correct
79 Correct 1273 ms 289012 KB Output is correct
80 Correct 1473 ms 289012 KB Output is correct
81 Correct 283 ms 289012 KB Output is correct
82 Correct 280 ms 289012 KB Output is correct
83 Correct 248 ms 289012 KB Output is correct
84 Correct 231 ms 289012 KB Output is correct
85 Correct 240 ms 289012 KB Output is correct
86 Correct 225 ms 289012 KB Output is correct
87 Correct 236 ms 289012 KB Output is correct
88 Correct 233 ms 289012 KB Output is correct
89 Correct 227 ms 289012 KB Output is correct
90 Correct 162 ms 289012 KB Output is correct
91 Correct 329 ms 289012 KB Output is correct
92 Correct 251 ms 289012 KB Output is correct
93 Correct 229 ms 289012 KB Output is correct
94 Correct 284 ms 289012 KB Output is correct
95 Correct 233 ms 289012 KB Output is correct
96 Correct 186 ms 289012 KB Output is correct
97 Correct 1602 ms 316300 KB Output is correct
98 Correct 420 ms 316300 KB Output is correct
99 Correct 2167 ms 316300 KB Output is correct
100 Correct 1547 ms 325660 KB Output is correct
101 Correct 1408 ms 325660 KB Output is correct
102 Correct 2126 ms 325660 KB Output is correct
103 Correct 1751 ms 325660 KB Output is correct
104 Correct 1961 ms 325660 KB Output is correct
105 Correct 1409 ms 325660 KB Output is correct
106 Correct 1649 ms 325660 KB Output is correct
107 Correct 1274 ms 325660 KB Output is correct
108 Correct 1339 ms 325660 KB Output is correct
109 Correct 1255 ms 325660 KB Output is correct
110 Correct 1353 ms 325660 KB Output is correct
111 Correct 1455 ms 325660 KB Output is correct
112 Correct 1447 ms 325660 KB Output is correct
113 Correct 916 ms 325660 KB Output is correct
114 Correct 1522 ms 325660 KB Output is correct
115 Correct 1496 ms 325660 KB Output is correct
116 Correct 1810 ms 325660 KB Output is correct
117 Correct 1533 ms 325660 KB Output is correct
118 Correct 1424 ms 325660 KB Output is correct
119 Correct 1203 ms 325660 KB Output is correct
120 Correct 928 ms 325660 KB Output is correct
121 Correct 975 ms 325660 KB Output is correct
122 Correct 1082 ms 325660 KB Output is correct
123 Correct 1092 ms 325660 KB Output is correct
124 Correct 1172 ms 325660 KB Output is correct
125 Correct 1067 ms 325660 KB Output is correct
126 Correct 1398 ms 325660 KB Output is correct