답안 #48857

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
48857 2018-05-19T10:43:44 Z kriii 새 집 (APIO18_new_home) C++17
57 / 100
5000 ms 277912 KB
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <set>
using namespace std;

int N,K,Q;
int C,XP[1001001];
map<int, pair<vector<pair<int, int> >, vector<pair<int, int> > > > query;
map<int, int> make[300300];
int ans[300300];

const int non = 500000000;
const int Z = 1<<20;
multiset<int> upc[Z];
multiset<int, greater<int> > downc[Z];
int up[Z*2],down[Z*2];

void push(int v, int u, int d, bool in)
{
	int p = lower_bound(XP, XP+C, v) - XP;
	{
		int x = p;
		if (in) upc[x].insert(u);
		else upc[x].erase(upc[x].find(u));

		if (upc[x].empty()) up[x+Z] = non;
		else up[x+Z] = *upc[x].begin();

		x = (x + Z) / 2;
		while (x){
			up[x] = min(up[x*2], up[x*2+1]);
			x /= 2;
		}
	}

	{
		int x = p;
		if (in) downc[x].insert(d);
		else downc[x].erase(downc[x].find(d));
		if (downc[x].empty()) down[x+Z] = -non;
		else down[x+Z] = *downc[x].begin();

		x = (x + Z) / 2;
		while (x){
			down[x] = max(down[x*2], down[x*2+1]);
			x /= 2;
		}
	}
}

int out(int v)
{
	int u = non, d = -non;
	{
		int x = lower_bound(XP,XP+C,v) - XP + Z, y = C-1 + Z;
		while (x < y){
			if (x & 1) u = min(u, up[x++]);
			if (~y & 1) u = min(u, up[y--]);
			x /= 2; y /= 2;
		}
		if (x == y) u = min(u, up[x]);
	}
	{
		int x = Z, y = upper_bound(XP,XP+C,v) - XP + Z - 1;
		while (x < y){
			if (x & 1) d = max(d, down[x++]);
			if (~y & 1) d = max(d, down[y--]);
			x /= 2; y /= 2;
		}
		if (x == y) d = max(d, down[x]);
	}

	return max(v-u,-v+d);
}

void mt(int s, int e, bool in)
{
	push((s+e)/2,s,e,in);
}

int main()
{
	scanf ("%d %d %d",&N,&K,&Q);
	for (int i=0;i<N;i++){
		int x,t,a,b; scanf ("%d %d %d %d",&x,&t,&a,&b);
		x *= 2;
		query[a].first.push_back({x,t});
		query[b+1].first.push_back({x,-t});
	}
	for (int i=0;i<Q;i++){
		int l,y; scanf ("%d %d",&l,&y);
		l *= 2;
		query[y].second.push_back({l,i});
	}

	for (int i=1;i<=K;i++){
		make[i][-non] = make[i][non] = 1;
	}
	XP[C++] = -non;
	XP[C++] = non;
	XP[C++] = 0;

	for (auto &p : query){
		auto &v = p.second.first;
		for (auto &q : v){
			int x = q.first;
			if (q.second > 0){
				int t = q.second;
				int &c = make[t][x];
				if (c == 0){
					auto I = make[t].find(x);
					auto J = I, K = I; J--; K++;
					XP[C++] = (J->first + I->first) / 2;
					XP[C++] = (I->first + K->first) / 2;
				}
				c++;
			}
			else{
				int t = -q.second;
				int &c = make[t][x];
				if (c == 1){
					auto I = make[t].find(x);
					auto J = I, K = I; J--; K++;
					XP[C++] = (J->first + K->first) / 2;
					make[t].erase(I);
				}
				else c--;
			}
		}
	}

	sort(XP,XP+C);
	C = unique(XP,XP+C) - XP;

	int v = lower_bound(XP,XP+C,0) - XP;
	for (int i=1;i<K;i++){
		upc[v].insert(-non);
		downc[v].insert(non);
	}
	mt(-non,non,true);

	for (int i=1;i<Z*2;i++) up[i] = non, down[i] = -non;

	int have = 0;
	for (auto &p : query){
		auto &v = p.second.first;
		for (auto &q : v){
			int x = q.first;
			if (q.second > 0){
				int t = q.second;
				if (make[t].size() == 2) have++;
				int &c = make[t][x];
				if (c == 0){
					auto I = make[t].find(x);
					auto J = I, K = I; J--; K++;
					mt(J->first,K->first,false);
					mt(J->first,I->first,true);
					mt(I->first,K->first,true);
				}
				c++;
			}
			else{
				int t = -q.second;
				int &c = make[t][x];
				if (c == 1){
					auto I = make[t].find(x);
					auto J = I, K = I; J--; K++;
					mt(J->first,I->first,false);
					mt(I->first,K->first,false);
					mt(J->first,K->first,true);
					make[t].erase(I);
					if (make[t].size() == 2) have--;
				}
				else c--;
			}
		}
		auto &w = p.second.second;
		for (auto &q : w){
			ans[q.second] = have == K ? out(q.first) / 2 : -1;
		}
	}

	for (int i=0;i<Q;i++){
		printf ("%d\n",ans[i]);
	}

	return 0;
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:86:8: 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:88:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x,t,a,b; scanf ("%d %d %d %d",&x,&t,&a,&b);
                ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:94:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int l,y; scanf ("%d %d",&l,&y);
            ~~~~~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 129400 KB Output is correct
2 Correct 112 ms 129504 KB Output is correct
3 Correct 124 ms 129580 KB Output is correct
4 Correct 110 ms 129580 KB Output is correct
5 Correct 115 ms 129580 KB Output is correct
6 Correct 119 ms 129800 KB Output is correct
7 Correct 111 ms 129800 KB Output is correct
8 Correct 114 ms 129800 KB Output is correct
9 Correct 125 ms 129840 KB Output is correct
10 Correct 118 ms 129840 KB Output is correct
11 Correct 107 ms 129840 KB Output is correct
12 Correct 118 ms 129840 KB Output is correct
13 Correct 108 ms 129840 KB Output is correct
14 Correct 114 ms 129840 KB Output is correct
15 Correct 112 ms 129840 KB Output is correct
16 Correct 119 ms 129888 KB Output is correct
17 Correct 107 ms 129896 KB Output is correct
18 Correct 114 ms 129896 KB Output is correct
19 Correct 122 ms 129896 KB Output is correct
20 Correct 114 ms 129896 KB Output is correct
21 Correct 127 ms 129896 KB Output is correct
22 Correct 108 ms 129916 KB Output is correct
23 Correct 111 ms 129948 KB Output is correct
24 Correct 110 ms 129948 KB Output is correct
25 Correct 107 ms 129948 KB Output is correct
26 Correct 108 ms 129948 KB Output is correct
27 Correct 116 ms 129948 KB Output is correct
28 Correct 119 ms 129948 KB Output is correct
29 Correct 109 ms 129948 KB Output is correct
30 Correct 117 ms 129948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 129400 KB Output is correct
2 Correct 112 ms 129504 KB Output is correct
3 Correct 124 ms 129580 KB Output is correct
4 Correct 110 ms 129580 KB Output is correct
5 Correct 115 ms 129580 KB Output is correct
6 Correct 119 ms 129800 KB Output is correct
7 Correct 111 ms 129800 KB Output is correct
8 Correct 114 ms 129800 KB Output is correct
9 Correct 125 ms 129840 KB Output is correct
10 Correct 118 ms 129840 KB Output is correct
11 Correct 107 ms 129840 KB Output is correct
12 Correct 118 ms 129840 KB Output is correct
13 Correct 108 ms 129840 KB Output is correct
14 Correct 114 ms 129840 KB Output is correct
15 Correct 112 ms 129840 KB Output is correct
16 Correct 119 ms 129888 KB Output is correct
17 Correct 107 ms 129896 KB Output is correct
18 Correct 114 ms 129896 KB Output is correct
19 Correct 122 ms 129896 KB Output is correct
20 Correct 114 ms 129896 KB Output is correct
21 Correct 127 ms 129896 KB Output is correct
22 Correct 108 ms 129916 KB Output is correct
23 Correct 111 ms 129948 KB Output is correct
24 Correct 110 ms 129948 KB Output is correct
25 Correct 107 ms 129948 KB Output is correct
26 Correct 108 ms 129948 KB Output is correct
27 Correct 116 ms 129948 KB Output is correct
28 Correct 119 ms 129948 KB Output is correct
29 Correct 109 ms 129948 KB Output is correct
30 Correct 117 ms 129948 KB Output is correct
31 Correct 1080 ms 162172 KB Output is correct
32 Correct 157 ms 162172 KB Output is correct
33 Correct 945 ms 162172 KB Output is correct
34 Correct 1104 ms 162172 KB Output is correct
35 Correct 1016 ms 162172 KB Output is correct
36 Correct 969 ms 162172 KB Output is correct
37 Correct 665 ms 162172 KB Output is correct
38 Correct 746 ms 162172 KB Output is correct
39 Correct 560 ms 162172 KB Output is correct
40 Correct 571 ms 162172 KB Output is correct
41 Correct 720 ms 162172 KB Output is correct
42 Correct 751 ms 162172 KB Output is correct
43 Correct 155 ms 162172 KB Output is correct
44 Correct 843 ms 162172 KB Output is correct
45 Correct 828 ms 162172 KB Output is correct
46 Correct 725 ms 162172 KB Output is correct
47 Correct 420 ms 162172 KB Output is correct
48 Correct 403 ms 162172 KB Output is correct
49 Correct 480 ms 162172 KB Output is correct
50 Correct 525 ms 162172 KB Output is correct
51 Correct 537 ms 162172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4446 ms 232720 KB Output is correct
2 Correct 4786 ms 232720 KB Output is correct
3 Correct 3827 ms 277812 KB Output is correct
4 Correct 3916 ms 277812 KB Output is correct
5 Correct 4220 ms 277812 KB Output is correct
6 Correct 4393 ms 277812 KB Output is correct
7 Correct 3272 ms 277912 KB Output is correct
8 Correct 3311 ms 277912 KB Output is correct
9 Correct 3236 ms 277912 KB Output is correct
10 Correct 3411 ms 277912 KB Output is correct
11 Correct 2149 ms 277912 KB Output is correct
12 Correct 2217 ms 277912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 5113 ms 277912 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 129400 KB Output is correct
2 Correct 112 ms 129504 KB Output is correct
3 Correct 124 ms 129580 KB Output is correct
4 Correct 110 ms 129580 KB Output is correct
5 Correct 115 ms 129580 KB Output is correct
6 Correct 119 ms 129800 KB Output is correct
7 Correct 111 ms 129800 KB Output is correct
8 Correct 114 ms 129800 KB Output is correct
9 Correct 125 ms 129840 KB Output is correct
10 Correct 118 ms 129840 KB Output is correct
11 Correct 107 ms 129840 KB Output is correct
12 Correct 118 ms 129840 KB Output is correct
13 Correct 108 ms 129840 KB Output is correct
14 Correct 114 ms 129840 KB Output is correct
15 Correct 112 ms 129840 KB Output is correct
16 Correct 119 ms 129888 KB Output is correct
17 Correct 107 ms 129896 KB Output is correct
18 Correct 114 ms 129896 KB Output is correct
19 Correct 122 ms 129896 KB Output is correct
20 Correct 114 ms 129896 KB Output is correct
21 Correct 127 ms 129896 KB Output is correct
22 Correct 108 ms 129916 KB Output is correct
23 Correct 111 ms 129948 KB Output is correct
24 Correct 110 ms 129948 KB Output is correct
25 Correct 107 ms 129948 KB Output is correct
26 Correct 108 ms 129948 KB Output is correct
27 Correct 116 ms 129948 KB Output is correct
28 Correct 119 ms 129948 KB Output is correct
29 Correct 109 ms 129948 KB Output is correct
30 Correct 117 ms 129948 KB Output is correct
31 Correct 1080 ms 162172 KB Output is correct
32 Correct 157 ms 162172 KB Output is correct
33 Correct 945 ms 162172 KB Output is correct
34 Correct 1104 ms 162172 KB Output is correct
35 Correct 1016 ms 162172 KB Output is correct
36 Correct 969 ms 162172 KB Output is correct
37 Correct 665 ms 162172 KB Output is correct
38 Correct 746 ms 162172 KB Output is correct
39 Correct 560 ms 162172 KB Output is correct
40 Correct 571 ms 162172 KB Output is correct
41 Correct 720 ms 162172 KB Output is correct
42 Correct 751 ms 162172 KB Output is correct
43 Correct 155 ms 162172 KB Output is correct
44 Correct 843 ms 162172 KB Output is correct
45 Correct 828 ms 162172 KB Output is correct
46 Correct 725 ms 162172 KB Output is correct
47 Correct 420 ms 162172 KB Output is correct
48 Correct 403 ms 162172 KB Output is correct
49 Correct 480 ms 162172 KB Output is correct
50 Correct 525 ms 162172 KB Output is correct
51 Correct 537 ms 162172 KB Output is correct
52 Correct 1022 ms 277912 KB Output is correct
53 Correct 934 ms 277912 KB Output is correct
54 Correct 1103 ms 277912 KB Output is correct
55 Correct 1014 ms 277912 KB Output is correct
56 Correct 942 ms 277912 KB Output is correct
57 Correct 887 ms 277912 KB Output is correct
58 Correct 819 ms 277912 KB Output is correct
59 Correct 919 ms 277912 KB Output is correct
60 Correct 779 ms 277912 KB Output is correct
61 Correct 399 ms 277912 KB Output is correct
62 Correct 960 ms 277912 KB Output is correct
63 Correct 882 ms 277912 KB Output is correct
64 Correct 872 ms 277912 KB Output is correct
65 Correct 942 ms 277912 KB Output is correct
66 Correct 856 ms 277912 KB Output is correct
67 Correct 465 ms 277912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 108 ms 129400 KB Output is correct
2 Correct 112 ms 129504 KB Output is correct
3 Correct 124 ms 129580 KB Output is correct
4 Correct 110 ms 129580 KB Output is correct
5 Correct 115 ms 129580 KB Output is correct
6 Correct 119 ms 129800 KB Output is correct
7 Correct 111 ms 129800 KB Output is correct
8 Correct 114 ms 129800 KB Output is correct
9 Correct 125 ms 129840 KB Output is correct
10 Correct 118 ms 129840 KB Output is correct
11 Correct 107 ms 129840 KB Output is correct
12 Correct 118 ms 129840 KB Output is correct
13 Correct 108 ms 129840 KB Output is correct
14 Correct 114 ms 129840 KB Output is correct
15 Correct 112 ms 129840 KB Output is correct
16 Correct 119 ms 129888 KB Output is correct
17 Correct 107 ms 129896 KB Output is correct
18 Correct 114 ms 129896 KB Output is correct
19 Correct 122 ms 129896 KB Output is correct
20 Correct 114 ms 129896 KB Output is correct
21 Correct 127 ms 129896 KB Output is correct
22 Correct 108 ms 129916 KB Output is correct
23 Correct 111 ms 129948 KB Output is correct
24 Correct 110 ms 129948 KB Output is correct
25 Correct 107 ms 129948 KB Output is correct
26 Correct 108 ms 129948 KB Output is correct
27 Correct 116 ms 129948 KB Output is correct
28 Correct 119 ms 129948 KB Output is correct
29 Correct 109 ms 129948 KB Output is correct
30 Correct 117 ms 129948 KB Output is correct
31 Correct 1080 ms 162172 KB Output is correct
32 Correct 157 ms 162172 KB Output is correct
33 Correct 945 ms 162172 KB Output is correct
34 Correct 1104 ms 162172 KB Output is correct
35 Correct 1016 ms 162172 KB Output is correct
36 Correct 969 ms 162172 KB Output is correct
37 Correct 665 ms 162172 KB Output is correct
38 Correct 746 ms 162172 KB Output is correct
39 Correct 560 ms 162172 KB Output is correct
40 Correct 571 ms 162172 KB Output is correct
41 Correct 720 ms 162172 KB Output is correct
42 Correct 751 ms 162172 KB Output is correct
43 Correct 155 ms 162172 KB Output is correct
44 Correct 843 ms 162172 KB Output is correct
45 Correct 828 ms 162172 KB Output is correct
46 Correct 725 ms 162172 KB Output is correct
47 Correct 420 ms 162172 KB Output is correct
48 Correct 403 ms 162172 KB Output is correct
49 Correct 480 ms 162172 KB Output is correct
50 Correct 525 ms 162172 KB Output is correct
51 Correct 537 ms 162172 KB Output is correct
52 Correct 4446 ms 232720 KB Output is correct
53 Correct 4786 ms 232720 KB Output is correct
54 Correct 3827 ms 277812 KB Output is correct
55 Correct 3916 ms 277812 KB Output is correct
56 Correct 4220 ms 277812 KB Output is correct
57 Correct 4393 ms 277812 KB Output is correct
58 Correct 3272 ms 277912 KB Output is correct
59 Correct 3311 ms 277912 KB Output is correct
60 Correct 3236 ms 277912 KB Output is correct
61 Correct 3411 ms 277912 KB Output is correct
62 Correct 2149 ms 277912 KB Output is correct
63 Correct 2217 ms 277912 KB Output is correct
64 Execution timed out 5113 ms 277912 KB Time limit exceeded
65 Halted 0 ms 0 KB -