답안 #49578

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
49578 2018-05-31T12:25:27 Z Diuven 새 집 (APIO18_new_home) C++11
100 / 100
4849 ms 370900 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef multiset<int>::iterator mit;
const int MX=300010, inf=1<<29;

struct SHOP {
	int x, type, s, e, idx;
} S[MX];

struct QUERY {
	int x, t, idx, ans;
} Q[MX];

inline int max(int x, int y){ return x>y ? x : y; }

int n, q, k;

void input(){
	cin>>n>>k>>q;
	for(int i=1; i<=n; i++){
		int t, x, s, e;
		cin>>x>>t>>s>>e;
		S[i]={x,t,s,e,i};
	}
	for(int i=1; i<=q; i++){
		int x, t;
		cin>>x>>t;
		Q[i]={x,t,i};
	}
}

vector<int> X;
int xmax;

int lo(int x){
	return upper_bound(X.begin(), X.end(), x)-X.begin()-1;
}
int hi(int x){
	return lower_bound(X.begin(), X.end(), x)-X.begin();
}

void prec(){
	for(int i=1; i<=n; i++){
		S[i].x*=2;
	}
	for(int i=1; i<=q; i++){
		Q[i].x*=2;
		X.push_back(Q[i].x);
	}
	sort(X.begin(), X.end());
	X.resize(unique(X.begin(), X.end())-X.begin());
	xmax=X.size()-1U;
}

//////

multiset<int> pos[MX];
int nonzero_cnt;

struct SegTree {
	int tree[4*MX]={};
	multiset<int> leaf[MX];
	// [0, xmax]

	void init(){
		for(int i=1; i<=4*xmax; i++) tree[i]=-inf;
	}

	int mx(int v, int s, int e, int l, int r){
		if(r<s || e<l) return -inf;
		if(l<=s && e<=r) return tree[v];
		return max(mx(v*2, s, (s+e)/2, l, r), mx(v*2+1, (s+e)/2+1, e, l, r));
	}
	int mx(int l, int r){
		return mx(1,0,xmax,l,r);
	}
	void upt(int v, int s, int e, int x){
		if(x<s || e<x) return;
		if(s==e){
			if(leaf[x].empty()) tree[v]=-inf;
			else tree[v]=*leaf[x].rbegin();
			return;
		}
		upt(v*2, s, (s+e)/2, x);
		upt(v*2+1, (s+e)/2+1, e, x);
		tree[v]=max(tree[v*2], tree[v*2+1]);
	}
	void put(int x, int val){
		if(x<0 || xmax<x) return;
		leaf[x].insert(val);
		upt(1,0,xmax,x);
	}
	void pop(int x, int val){
		if(x<0 || xmax<x) return;
		leaf[x].erase(leaf[x].find(val));
		upt(1,0,xmax,x);
	}
} Seg1, Seg2;

// 1이 /, 2가 \
//1: max(y-x), 2: max(y+x)

void add(int x1, int x2){
	int mx=(0LL+x1+x2)/2, my=(0LL+x2-x1)/2;
	int pos=hi(mx);
	Seg1.put(pos-1, my-mx);
	Seg2.put(pos, my+mx);
}

void del(int x1, int x2){
	int mx=(0LL+x1+x2)/2, my=(0LL+x2-x1)/2;
	int pos=hi(mx);
	Seg1.pop(pos-1, my-mx);
	Seg2.pop(pos, my+mx);
}

int find(int qx){
	if(nonzero_cnt<k) return -2;
	int x=lo(qx);
	int ans=max(Seg1.mx(x, xmax)+qx, Seg2.mx(0, x)-qx);
	return ans;
}

////

void add_store(int idx){
	multiset<int> &stores = pos[S[idx].type];

	if(stores.size()==2U) nonzero_cnt++;

	int x=S[idx].x;

	if(stores.find(x)==stores.end()){
		mit it1=stores.lower_bound(x), it2=it1; it2--;
		int lx=*it2, rx=*it1;
		del(lx, rx);
		add(lx, x); add(x, rx);
	}

	stores.insert(x);
}

void del_store(int idx){
	multiset<int> &stores = pos[S[idx].type];

	if(stores.size()==3U) nonzero_cnt--;

	int x=S[idx].x;

	stores.erase(stores.find(x));

	if(stores.find(x)!=stores.end())
		return;

	mit it1=stores.lower_bound(x), it2=it1; it2--;
	int lx=*it2, rx=*it1;
	del(lx, x); del(x, rx);
	add(lx, rx);
}

void init(){
	Seg1.init();
	Seg2.init();
	for(int i=1; i<=k; i++){
		pos[i].insert(inf);
		pos[i].insert(-inf);
		add(-inf, inf);
	}
}

void solve(){
	vector<pii> in, out;
	for(int i=1; i<=n; i++){
		in.push_back({S[i].s, i});
		out.push_back({S[i].e, i});
	}
	sort(Q+1, Q+q+1, [](QUERY &a, QUERY &b){ return a.t<b.t; });
	sort(in.begin(), in.end());
	sort(out.begin(), out.end());

	init();

	for(int i=1, a=0, b=0; i<=q; i++){
		int qt=Q[i].t, qx=Q[i].x;
		while(a<n){
			int t,idx; tie(t,idx)=in[a];
			if(qt<t) break;
			a++;
			add_store(idx);
		}
		while(b<n){
			int t,idx; tie(t,idx)=out[b];
			if(qt<=t) break;
			b++;
			del_store(idx);
		}
		Q[i].ans=find(qx);
	}
}


int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	input();
	prec();
	solve();

	sort(Q+1, Q+q+1, [](QUERY &a, QUERY &b){ return a.idx<b.idx; });
	for(int i=1; i<=q; i++){
		cout<<Q[i].ans/2<<'\n';
	}
	return 0;
}

Compilation message

new_home.cpp:102:1: warning: multi-line comment [-Wcomment]
 // 1이 /, 2가 \
 ^
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 51964 KB Output is correct
2 Correct 47 ms 52068 KB Output is correct
3 Correct 47 ms 52144 KB Output is correct
4 Correct 53 ms 52144 KB Output is correct
5 Correct 46 ms 52160 KB Output is correct
6 Correct 48 ms 52208 KB Output is correct
7 Correct 47 ms 52268 KB Output is correct
8 Correct 45 ms 52272 KB Output is correct
9 Correct 47 ms 52424 KB Output is correct
10 Correct 48 ms 52424 KB Output is correct
11 Correct 47 ms 52424 KB Output is correct
12 Correct 47 ms 52424 KB Output is correct
13 Correct 47 ms 52424 KB Output is correct
14 Correct 47 ms 52424 KB Output is correct
15 Correct 52 ms 52424 KB Output is correct
16 Correct 51 ms 52424 KB Output is correct
17 Correct 50 ms 52424 KB Output is correct
18 Correct 49 ms 52424 KB Output is correct
19 Correct 49 ms 52424 KB Output is correct
20 Correct 50 ms 52424 KB Output is correct
21 Correct 45 ms 52424 KB Output is correct
22 Correct 44 ms 52424 KB Output is correct
23 Correct 47 ms 52424 KB Output is correct
24 Correct 47 ms 52424 KB Output is correct
25 Correct 48 ms 52424 KB Output is correct
26 Correct 46 ms 52424 KB Output is correct
27 Correct 47 ms 52424 KB Output is correct
28 Correct 48 ms 52476 KB Output is correct
29 Correct 46 ms 52476 KB Output is correct
30 Correct 47 ms 52476 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 51964 KB Output is correct
2 Correct 47 ms 52068 KB Output is correct
3 Correct 47 ms 52144 KB Output is correct
4 Correct 53 ms 52144 KB Output is correct
5 Correct 46 ms 52160 KB Output is correct
6 Correct 48 ms 52208 KB Output is correct
7 Correct 47 ms 52268 KB Output is correct
8 Correct 45 ms 52272 KB Output is correct
9 Correct 47 ms 52424 KB Output is correct
10 Correct 48 ms 52424 KB Output is correct
11 Correct 47 ms 52424 KB Output is correct
12 Correct 47 ms 52424 KB Output is correct
13 Correct 47 ms 52424 KB Output is correct
14 Correct 47 ms 52424 KB Output is correct
15 Correct 52 ms 52424 KB Output is correct
16 Correct 51 ms 52424 KB Output is correct
17 Correct 50 ms 52424 KB Output is correct
18 Correct 49 ms 52424 KB Output is correct
19 Correct 49 ms 52424 KB Output is correct
20 Correct 50 ms 52424 KB Output is correct
21 Correct 45 ms 52424 KB Output is correct
22 Correct 44 ms 52424 KB Output is correct
23 Correct 47 ms 52424 KB Output is correct
24 Correct 47 ms 52424 KB Output is correct
25 Correct 48 ms 52424 KB Output is correct
26 Correct 46 ms 52424 KB Output is correct
27 Correct 47 ms 52424 KB Output is correct
28 Correct 48 ms 52476 KB Output is correct
29 Correct 46 ms 52476 KB Output is correct
30 Correct 47 ms 52476 KB Output is correct
31 Correct 762 ms 64040 KB Output is correct
32 Correct 189 ms 64040 KB Output is correct
33 Correct 734 ms 64040 KB Output is correct
34 Correct 862 ms 64040 KB Output is correct
35 Correct 840 ms 64144 KB Output is correct
36 Correct 901 ms 64272 KB Output is correct
37 Correct 508 ms 64272 KB Output is correct
38 Correct 469 ms 64272 KB Output is correct
39 Correct 403 ms 64272 KB Output is correct
40 Correct 390 ms 64272 KB Output is correct
41 Correct 364 ms 64272 KB Output is correct
42 Correct 363 ms 64272 KB Output is correct
43 Correct 108 ms 64272 KB Output is correct
44 Correct 373 ms 64272 KB Output is correct
45 Correct 375 ms 64272 KB Output is correct
46 Correct 376 ms 64272 KB Output is correct
47 Correct 257 ms 64272 KB Output is correct
48 Correct 264 ms 64272 KB Output is correct
49 Correct 281 ms 64272 KB Output is correct
50 Correct 308 ms 64272 KB Output is correct
51 Correct 396 ms 64272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2779 ms 116800 KB Output is correct
2 Correct 2807 ms 116800 KB Output is correct
3 Correct 2133 ms 139276 KB Output is correct
4 Correct 2536 ms 139276 KB Output is correct
5 Correct 2677 ms 139276 KB Output is correct
6 Correct 2875 ms 139276 KB Output is correct
7 Correct 2052 ms 139392 KB Output is correct
8 Correct 1892 ms 139392 KB Output is correct
9 Correct 1878 ms 139392 KB Output is correct
10 Correct 2260 ms 139392 KB Output is correct
11 Correct 1349 ms 139392 KB Output is correct
12 Correct 1492 ms 139392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4849 ms 139392 KB Output is correct
2 Correct 648 ms 139392 KB Output is correct
3 Correct 4177 ms 139392 KB Output is correct
4 Correct 3031 ms 153000 KB Output is correct
5 Correct 3791 ms 153000 KB Output is correct
6 Correct 3713 ms 160720 KB Output is correct
7 Correct 4287 ms 163560 KB Output is correct
8 Correct 4577 ms 175712 KB Output is correct
9 Correct 3420 ms 217552 KB Output is correct
10 Correct 3352 ms 217552 KB Output is correct
11 Correct 3898 ms 217552 KB Output is correct
12 Correct 4474 ms 228576 KB Output is correct
13 Correct 1869 ms 238416 KB Output is correct
14 Correct 1661 ms 249688 KB Output is correct
15 Correct 1967 ms 263304 KB Output is correct
16 Correct 2645 ms 276600 KB Output is correct
17 Correct 2373 ms 276600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 51964 KB Output is correct
2 Correct 47 ms 52068 KB Output is correct
3 Correct 47 ms 52144 KB Output is correct
4 Correct 53 ms 52144 KB Output is correct
5 Correct 46 ms 52160 KB Output is correct
6 Correct 48 ms 52208 KB Output is correct
7 Correct 47 ms 52268 KB Output is correct
8 Correct 45 ms 52272 KB Output is correct
9 Correct 47 ms 52424 KB Output is correct
10 Correct 48 ms 52424 KB Output is correct
11 Correct 47 ms 52424 KB Output is correct
12 Correct 47 ms 52424 KB Output is correct
13 Correct 47 ms 52424 KB Output is correct
14 Correct 47 ms 52424 KB Output is correct
15 Correct 52 ms 52424 KB Output is correct
16 Correct 51 ms 52424 KB Output is correct
17 Correct 50 ms 52424 KB Output is correct
18 Correct 49 ms 52424 KB Output is correct
19 Correct 49 ms 52424 KB Output is correct
20 Correct 50 ms 52424 KB Output is correct
21 Correct 45 ms 52424 KB Output is correct
22 Correct 44 ms 52424 KB Output is correct
23 Correct 47 ms 52424 KB Output is correct
24 Correct 47 ms 52424 KB Output is correct
25 Correct 48 ms 52424 KB Output is correct
26 Correct 46 ms 52424 KB Output is correct
27 Correct 47 ms 52424 KB Output is correct
28 Correct 48 ms 52476 KB Output is correct
29 Correct 46 ms 52476 KB Output is correct
30 Correct 47 ms 52476 KB Output is correct
31 Correct 762 ms 64040 KB Output is correct
32 Correct 189 ms 64040 KB Output is correct
33 Correct 734 ms 64040 KB Output is correct
34 Correct 862 ms 64040 KB Output is correct
35 Correct 840 ms 64144 KB Output is correct
36 Correct 901 ms 64272 KB Output is correct
37 Correct 508 ms 64272 KB Output is correct
38 Correct 469 ms 64272 KB Output is correct
39 Correct 403 ms 64272 KB Output is correct
40 Correct 390 ms 64272 KB Output is correct
41 Correct 364 ms 64272 KB Output is correct
42 Correct 363 ms 64272 KB Output is correct
43 Correct 108 ms 64272 KB Output is correct
44 Correct 373 ms 64272 KB Output is correct
45 Correct 375 ms 64272 KB Output is correct
46 Correct 376 ms 64272 KB Output is correct
47 Correct 257 ms 64272 KB Output is correct
48 Correct 264 ms 64272 KB Output is correct
49 Correct 281 ms 64272 KB Output is correct
50 Correct 308 ms 64272 KB Output is correct
51 Correct 396 ms 64272 KB Output is correct
52 Correct 575 ms 276600 KB Output is correct
53 Correct 431 ms 276600 KB Output is correct
54 Correct 677 ms 276600 KB Output is correct
55 Correct 417 ms 276600 KB Output is correct
56 Correct 438 ms 276600 KB Output is correct
57 Correct 391 ms 276600 KB Output is correct
58 Correct 541 ms 276600 KB Output is correct
59 Correct 480 ms 276600 KB Output is correct
60 Correct 459 ms 276600 KB Output is correct
61 Correct 200 ms 276600 KB Output is correct
62 Correct 612 ms 276600 KB Output is correct
63 Correct 644 ms 276600 KB Output is correct
64 Correct 670 ms 276600 KB Output is correct
65 Correct 557 ms 276600 KB Output is correct
66 Correct 378 ms 276600 KB Output is correct
67 Correct 352 ms 276600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 51964 KB Output is correct
2 Correct 47 ms 52068 KB Output is correct
3 Correct 47 ms 52144 KB Output is correct
4 Correct 53 ms 52144 KB Output is correct
5 Correct 46 ms 52160 KB Output is correct
6 Correct 48 ms 52208 KB Output is correct
7 Correct 47 ms 52268 KB Output is correct
8 Correct 45 ms 52272 KB Output is correct
9 Correct 47 ms 52424 KB Output is correct
10 Correct 48 ms 52424 KB Output is correct
11 Correct 47 ms 52424 KB Output is correct
12 Correct 47 ms 52424 KB Output is correct
13 Correct 47 ms 52424 KB Output is correct
14 Correct 47 ms 52424 KB Output is correct
15 Correct 52 ms 52424 KB Output is correct
16 Correct 51 ms 52424 KB Output is correct
17 Correct 50 ms 52424 KB Output is correct
18 Correct 49 ms 52424 KB Output is correct
19 Correct 49 ms 52424 KB Output is correct
20 Correct 50 ms 52424 KB Output is correct
21 Correct 45 ms 52424 KB Output is correct
22 Correct 44 ms 52424 KB Output is correct
23 Correct 47 ms 52424 KB Output is correct
24 Correct 47 ms 52424 KB Output is correct
25 Correct 48 ms 52424 KB Output is correct
26 Correct 46 ms 52424 KB Output is correct
27 Correct 47 ms 52424 KB Output is correct
28 Correct 48 ms 52476 KB Output is correct
29 Correct 46 ms 52476 KB Output is correct
30 Correct 47 ms 52476 KB Output is correct
31 Correct 762 ms 64040 KB Output is correct
32 Correct 189 ms 64040 KB Output is correct
33 Correct 734 ms 64040 KB Output is correct
34 Correct 862 ms 64040 KB Output is correct
35 Correct 840 ms 64144 KB Output is correct
36 Correct 901 ms 64272 KB Output is correct
37 Correct 508 ms 64272 KB Output is correct
38 Correct 469 ms 64272 KB Output is correct
39 Correct 403 ms 64272 KB Output is correct
40 Correct 390 ms 64272 KB Output is correct
41 Correct 364 ms 64272 KB Output is correct
42 Correct 363 ms 64272 KB Output is correct
43 Correct 108 ms 64272 KB Output is correct
44 Correct 373 ms 64272 KB Output is correct
45 Correct 375 ms 64272 KB Output is correct
46 Correct 376 ms 64272 KB Output is correct
47 Correct 257 ms 64272 KB Output is correct
48 Correct 264 ms 64272 KB Output is correct
49 Correct 281 ms 64272 KB Output is correct
50 Correct 308 ms 64272 KB Output is correct
51 Correct 396 ms 64272 KB Output is correct
52 Correct 2779 ms 116800 KB Output is correct
53 Correct 2807 ms 116800 KB Output is correct
54 Correct 2133 ms 139276 KB Output is correct
55 Correct 2536 ms 139276 KB Output is correct
56 Correct 2677 ms 139276 KB Output is correct
57 Correct 2875 ms 139276 KB Output is correct
58 Correct 2052 ms 139392 KB Output is correct
59 Correct 1892 ms 139392 KB Output is correct
60 Correct 1878 ms 139392 KB Output is correct
61 Correct 2260 ms 139392 KB Output is correct
62 Correct 1349 ms 139392 KB Output is correct
63 Correct 1492 ms 139392 KB Output is correct
64 Correct 4849 ms 139392 KB Output is correct
65 Correct 648 ms 139392 KB Output is correct
66 Correct 4177 ms 139392 KB Output is correct
67 Correct 3031 ms 153000 KB Output is correct
68 Correct 3791 ms 153000 KB Output is correct
69 Correct 3713 ms 160720 KB Output is correct
70 Correct 4287 ms 163560 KB Output is correct
71 Correct 4577 ms 175712 KB Output is correct
72 Correct 3420 ms 217552 KB Output is correct
73 Correct 3352 ms 217552 KB Output is correct
74 Correct 3898 ms 217552 KB Output is correct
75 Correct 4474 ms 228576 KB Output is correct
76 Correct 1869 ms 238416 KB Output is correct
77 Correct 1661 ms 249688 KB Output is correct
78 Correct 1967 ms 263304 KB Output is correct
79 Correct 2645 ms 276600 KB Output is correct
80 Correct 2373 ms 276600 KB Output is correct
81 Correct 575 ms 276600 KB Output is correct
82 Correct 431 ms 276600 KB Output is correct
83 Correct 677 ms 276600 KB Output is correct
84 Correct 417 ms 276600 KB Output is correct
85 Correct 438 ms 276600 KB Output is correct
86 Correct 391 ms 276600 KB Output is correct
87 Correct 541 ms 276600 KB Output is correct
88 Correct 480 ms 276600 KB Output is correct
89 Correct 459 ms 276600 KB Output is correct
90 Correct 200 ms 276600 KB Output is correct
91 Correct 612 ms 276600 KB Output is correct
92 Correct 644 ms 276600 KB Output is correct
93 Correct 670 ms 276600 KB Output is correct
94 Correct 557 ms 276600 KB Output is correct
95 Correct 378 ms 276600 KB Output is correct
96 Correct 352 ms 276600 KB Output is correct
97 Correct 3097 ms 320936 KB Output is correct
98 Correct 639 ms 320936 KB Output is correct
99 Correct 4040 ms 320936 KB Output is correct
100 Correct 2854 ms 337708 KB Output is correct
101 Correct 3495 ms 352172 KB Output is correct
102 Correct 4295 ms 352172 KB Output is correct
103 Correct 3503 ms 352172 KB Output is correct
104 Correct 3681 ms 352172 KB Output is correct
105 Correct 1999 ms 352172 KB Output is correct
106 Correct 2455 ms 352172 KB Output is correct
107 Correct 3038 ms 352172 KB Output is correct
108 Correct 3358 ms 352172 KB Output is correct
109 Correct 3354 ms 352172 KB Output is correct
110 Correct 3541 ms 352172 KB Output is correct
111 Correct 3385 ms 352172 KB Output is correct
112 Correct 2796 ms 352172 KB Output is correct
113 Correct 945 ms 370900 KB Output is correct
114 Correct 3208 ms 370900 KB Output is correct
115 Correct 4115 ms 370900 KB Output is correct
116 Correct 3618 ms 370900 KB Output is correct
117 Correct 4010 ms 370900 KB Output is correct
118 Correct 2428 ms 370900 KB Output is correct
119 Correct 2833 ms 370900 KB Output is correct
120 Correct 949 ms 370900 KB Output is correct
121 Correct 1104 ms 370900 KB Output is correct
122 Correct 1174 ms 370900 KB Output is correct
123 Correct 1277 ms 370900 KB Output is correct
124 Correct 1568 ms 370900 KB Output is correct
125 Correct 1411 ms 370900 KB Output is correct
126 Correct 1568 ms 370900 KB Output is correct