Submission #48869

# Submission time Handle Problem Language Result Execution time Memory
48869 2018-05-19T11:19:03 Z kriii New Home (APIO18_new_home) C++17
100 / 100
4988 ms 249688 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];

struct query{
	int y,o,x,t;
	bool operator <(const query &t)const{
		if (y == t.y) return o < t.o;
		return y < t.y;
	};
}qs[900900]; int qc;

map<int, int> make[300300];
int ans[300300];

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

void push(int v, int u, int d, bool in)
{
	int x = lower_bound(XP, XP+C, v) - XP;

	if (in) upc[x][u]++;
	else{
		if (--upc[x][u] == 0) upc[x].erase(upc[x].find(u));
	}
	if (upc[x].empty()) up[x+Z] = non;
	else up[x+Z] = upc[x].begin()->first;

	if (in) downc[x][d]++;
	else{
		if (--downc[x][d] == 0) downc[x].erase(downc[x].find(d));
	}
	if (downc[x].empty()) down[x+Z] = -non;
	else down[x+Z] = downc[x].begin()->first;

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

int out(int v)
{
	int u = non, d = -non, p = lower_bound(XP,XP+C,v) - XP;
	{
		int x = p + 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 = p + 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;
		qs[qc++] = {a,0,x,t};
		qs[qc++] = {b+1,0,x,-t};
	}
	for (int i=0;i<Q;i++){
		int l,y; scanf ("%d %d",&l,&y);
		l *= 2;
		qs[qc++] = {y,1,l,i};
	}
	sort(qs,qs+qc);

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

	for (int i=0;i<qc;i++){
		auto &q = qs[i];
		if (q.o == 0){
			int x = q.x, t = q.t;
			if (t > 0){
				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{
				t = -t;
				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;
	upc[v][-non] = downc[v][non] = K-1;
	mt(-non,non,true);

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

	int have = 0;
	for (int i=0;i<qc;i++){
		auto &q = qs[i];
		if (q.o == 0){
			int x = q.x, t = q.t;
			if (t > 0){
				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{
				t = -t;
				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--;
			}
		}
		else{
			ans[q.t] = have == K ? out(q.x) / 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:87: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:89: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:95: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);
            ~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 108 ms 129404 KB Output is correct
2 Correct 105 ms 129508 KB Output is correct
3 Correct 103 ms 129508 KB Output is correct
4 Correct 108 ms 129568 KB Output is correct
5 Correct 122 ms 129628 KB Output is correct
6 Correct 135 ms 129628 KB Output is correct
7 Correct 124 ms 129776 KB Output is correct
8 Correct 122 ms 129776 KB Output is correct
9 Correct 114 ms 129808 KB Output is correct
10 Correct 120 ms 129808 KB Output is correct
11 Correct 117 ms 129808 KB Output is correct
12 Correct 115 ms 129960 KB Output is correct
13 Correct 262 ms 129960 KB Output is correct
14 Correct 106 ms 129960 KB Output is correct
15 Correct 105 ms 129960 KB Output is correct
16 Correct 106 ms 129960 KB Output is correct
17 Correct 123 ms 129960 KB Output is correct
18 Correct 117 ms 129960 KB Output is correct
19 Correct 116 ms 129960 KB Output is correct
20 Correct 114 ms 129960 KB Output is correct
21 Correct 137 ms 129960 KB Output is correct
22 Correct 116 ms 129960 KB Output is correct
23 Correct 131 ms 129960 KB Output is correct
24 Correct 118 ms 129960 KB Output is correct
25 Correct 108 ms 129960 KB Output is correct
26 Correct 109 ms 129960 KB Output is correct
27 Correct 106 ms 129960 KB Output is correct
28 Correct 103 ms 129960 KB Output is correct
29 Correct 124 ms 129960 KB Output is correct
30 Correct 107 ms 129960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 129404 KB Output is correct
2 Correct 105 ms 129508 KB Output is correct
3 Correct 103 ms 129508 KB Output is correct
4 Correct 108 ms 129568 KB Output is correct
5 Correct 122 ms 129628 KB Output is correct
6 Correct 135 ms 129628 KB Output is correct
7 Correct 124 ms 129776 KB Output is correct
8 Correct 122 ms 129776 KB Output is correct
9 Correct 114 ms 129808 KB Output is correct
10 Correct 120 ms 129808 KB Output is correct
11 Correct 117 ms 129808 KB Output is correct
12 Correct 115 ms 129960 KB Output is correct
13 Correct 262 ms 129960 KB Output is correct
14 Correct 106 ms 129960 KB Output is correct
15 Correct 105 ms 129960 KB Output is correct
16 Correct 106 ms 129960 KB Output is correct
17 Correct 123 ms 129960 KB Output is correct
18 Correct 117 ms 129960 KB Output is correct
19 Correct 116 ms 129960 KB Output is correct
20 Correct 114 ms 129960 KB Output is correct
21 Correct 137 ms 129960 KB Output is correct
22 Correct 116 ms 129960 KB Output is correct
23 Correct 131 ms 129960 KB Output is correct
24 Correct 118 ms 129960 KB Output is correct
25 Correct 108 ms 129960 KB Output is correct
26 Correct 109 ms 129960 KB Output is correct
27 Correct 106 ms 129960 KB Output is correct
28 Correct 103 ms 129960 KB Output is correct
29 Correct 124 ms 129960 KB Output is correct
30 Correct 107 ms 129960 KB Output is correct
31 Correct 755 ms 142520 KB Output is correct
32 Correct 178 ms 142520 KB Output is correct
33 Correct 742 ms 142520 KB Output is correct
34 Correct 731 ms 142520 KB Output is correct
35 Correct 818 ms 142520 KB Output is correct
36 Correct 863 ms 142520 KB Output is correct
37 Correct 529 ms 142520 KB Output is correct
38 Correct 544 ms 142520 KB Output is correct
39 Correct 424 ms 142520 KB Output is correct
40 Correct 425 ms 142520 KB Output is correct
41 Correct 573 ms 142520 KB Output is correct
42 Correct 512 ms 142520 KB Output is correct
43 Correct 158 ms 142520 KB Output is correct
44 Correct 498 ms 142520 KB Output is correct
45 Correct 470 ms 142520 KB Output is correct
46 Correct 482 ms 142520 KB Output is correct
47 Correct 298 ms 142520 KB Output is correct
48 Correct 314 ms 142520 KB Output is correct
49 Correct 357 ms 142520 KB Output is correct
50 Correct 343 ms 142520 KB Output is correct
51 Correct 338 ms 142520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3939 ms 204624 KB Output is correct
2 Correct 4173 ms 204624 KB Output is correct
3 Correct 2467 ms 249648 KB Output is correct
4 Correct 3325 ms 249648 KB Output is correct
5 Correct 4239 ms 249648 KB Output is correct
6 Correct 4555 ms 249648 KB Output is correct
7 Correct 2498 ms 249688 KB Output is correct
8 Correct 2688 ms 249688 KB Output is correct
9 Correct 2794 ms 249688 KB Output is correct
10 Correct 3667 ms 249688 KB Output is correct
11 Correct 1843 ms 249688 KB Output is correct
12 Correct 2310 ms 249688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4415 ms 249688 KB Output is correct
2 Correct 431 ms 249688 KB Output is correct
3 Correct 4445 ms 249688 KB Output is correct
4 Correct 3039 ms 249688 KB Output is correct
5 Correct 4257 ms 249688 KB Output is correct
6 Correct 3805 ms 249688 KB Output is correct
7 Correct 4861 ms 249688 KB Output is correct
8 Correct 4988 ms 249688 KB Output is correct
9 Correct 2673 ms 249688 KB Output is correct
10 Correct 3070 ms 249688 KB Output is correct
11 Correct 3777 ms 249688 KB Output is correct
12 Correct 4017 ms 249688 KB Output is correct
13 Correct 1819 ms 249688 KB Output is correct
14 Correct 1728 ms 249688 KB Output is correct
15 Correct 2031 ms 249688 KB Output is correct
16 Correct 2311 ms 249688 KB Output is correct
17 Correct 2266 ms 249688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 129404 KB Output is correct
2 Correct 105 ms 129508 KB Output is correct
3 Correct 103 ms 129508 KB Output is correct
4 Correct 108 ms 129568 KB Output is correct
5 Correct 122 ms 129628 KB Output is correct
6 Correct 135 ms 129628 KB Output is correct
7 Correct 124 ms 129776 KB Output is correct
8 Correct 122 ms 129776 KB Output is correct
9 Correct 114 ms 129808 KB Output is correct
10 Correct 120 ms 129808 KB Output is correct
11 Correct 117 ms 129808 KB Output is correct
12 Correct 115 ms 129960 KB Output is correct
13 Correct 262 ms 129960 KB Output is correct
14 Correct 106 ms 129960 KB Output is correct
15 Correct 105 ms 129960 KB Output is correct
16 Correct 106 ms 129960 KB Output is correct
17 Correct 123 ms 129960 KB Output is correct
18 Correct 117 ms 129960 KB Output is correct
19 Correct 116 ms 129960 KB Output is correct
20 Correct 114 ms 129960 KB Output is correct
21 Correct 137 ms 129960 KB Output is correct
22 Correct 116 ms 129960 KB Output is correct
23 Correct 131 ms 129960 KB Output is correct
24 Correct 118 ms 129960 KB Output is correct
25 Correct 108 ms 129960 KB Output is correct
26 Correct 109 ms 129960 KB Output is correct
27 Correct 106 ms 129960 KB Output is correct
28 Correct 103 ms 129960 KB Output is correct
29 Correct 124 ms 129960 KB Output is correct
30 Correct 107 ms 129960 KB Output is correct
31 Correct 755 ms 142520 KB Output is correct
32 Correct 178 ms 142520 KB Output is correct
33 Correct 742 ms 142520 KB Output is correct
34 Correct 731 ms 142520 KB Output is correct
35 Correct 818 ms 142520 KB Output is correct
36 Correct 863 ms 142520 KB Output is correct
37 Correct 529 ms 142520 KB Output is correct
38 Correct 544 ms 142520 KB Output is correct
39 Correct 424 ms 142520 KB Output is correct
40 Correct 425 ms 142520 KB Output is correct
41 Correct 573 ms 142520 KB Output is correct
42 Correct 512 ms 142520 KB Output is correct
43 Correct 158 ms 142520 KB Output is correct
44 Correct 498 ms 142520 KB Output is correct
45 Correct 470 ms 142520 KB Output is correct
46 Correct 482 ms 142520 KB Output is correct
47 Correct 298 ms 142520 KB Output is correct
48 Correct 314 ms 142520 KB Output is correct
49 Correct 357 ms 142520 KB Output is correct
50 Correct 343 ms 142520 KB Output is correct
51 Correct 338 ms 142520 KB Output is correct
52 Correct 599 ms 249688 KB Output is correct
53 Correct 490 ms 249688 KB Output is correct
54 Correct 614 ms 249688 KB Output is correct
55 Correct 479 ms 249688 KB Output is correct
56 Correct 553 ms 249688 KB Output is correct
57 Correct 514 ms 249688 KB Output is correct
58 Correct 519 ms 249688 KB Output is correct
59 Correct 487 ms 249688 KB Output is correct
60 Correct 486 ms 249688 KB Output is correct
61 Correct 266 ms 249688 KB Output is correct
62 Correct 535 ms 249688 KB Output is correct
63 Correct 714 ms 249688 KB Output is correct
64 Correct 612 ms 249688 KB Output is correct
65 Correct 567 ms 249688 KB Output is correct
66 Correct 504 ms 249688 KB Output is correct
67 Correct 294 ms 249688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 129404 KB Output is correct
2 Correct 105 ms 129508 KB Output is correct
3 Correct 103 ms 129508 KB Output is correct
4 Correct 108 ms 129568 KB Output is correct
5 Correct 122 ms 129628 KB Output is correct
6 Correct 135 ms 129628 KB Output is correct
7 Correct 124 ms 129776 KB Output is correct
8 Correct 122 ms 129776 KB Output is correct
9 Correct 114 ms 129808 KB Output is correct
10 Correct 120 ms 129808 KB Output is correct
11 Correct 117 ms 129808 KB Output is correct
12 Correct 115 ms 129960 KB Output is correct
13 Correct 262 ms 129960 KB Output is correct
14 Correct 106 ms 129960 KB Output is correct
15 Correct 105 ms 129960 KB Output is correct
16 Correct 106 ms 129960 KB Output is correct
17 Correct 123 ms 129960 KB Output is correct
18 Correct 117 ms 129960 KB Output is correct
19 Correct 116 ms 129960 KB Output is correct
20 Correct 114 ms 129960 KB Output is correct
21 Correct 137 ms 129960 KB Output is correct
22 Correct 116 ms 129960 KB Output is correct
23 Correct 131 ms 129960 KB Output is correct
24 Correct 118 ms 129960 KB Output is correct
25 Correct 108 ms 129960 KB Output is correct
26 Correct 109 ms 129960 KB Output is correct
27 Correct 106 ms 129960 KB Output is correct
28 Correct 103 ms 129960 KB Output is correct
29 Correct 124 ms 129960 KB Output is correct
30 Correct 107 ms 129960 KB Output is correct
31 Correct 755 ms 142520 KB Output is correct
32 Correct 178 ms 142520 KB Output is correct
33 Correct 742 ms 142520 KB Output is correct
34 Correct 731 ms 142520 KB Output is correct
35 Correct 818 ms 142520 KB Output is correct
36 Correct 863 ms 142520 KB Output is correct
37 Correct 529 ms 142520 KB Output is correct
38 Correct 544 ms 142520 KB Output is correct
39 Correct 424 ms 142520 KB Output is correct
40 Correct 425 ms 142520 KB Output is correct
41 Correct 573 ms 142520 KB Output is correct
42 Correct 512 ms 142520 KB Output is correct
43 Correct 158 ms 142520 KB Output is correct
44 Correct 498 ms 142520 KB Output is correct
45 Correct 470 ms 142520 KB Output is correct
46 Correct 482 ms 142520 KB Output is correct
47 Correct 298 ms 142520 KB Output is correct
48 Correct 314 ms 142520 KB Output is correct
49 Correct 357 ms 142520 KB Output is correct
50 Correct 343 ms 142520 KB Output is correct
51 Correct 338 ms 142520 KB Output is correct
52 Correct 3939 ms 204624 KB Output is correct
53 Correct 4173 ms 204624 KB Output is correct
54 Correct 2467 ms 249648 KB Output is correct
55 Correct 3325 ms 249648 KB Output is correct
56 Correct 4239 ms 249648 KB Output is correct
57 Correct 4555 ms 249648 KB Output is correct
58 Correct 2498 ms 249688 KB Output is correct
59 Correct 2688 ms 249688 KB Output is correct
60 Correct 2794 ms 249688 KB Output is correct
61 Correct 3667 ms 249688 KB Output is correct
62 Correct 1843 ms 249688 KB Output is correct
63 Correct 2310 ms 249688 KB Output is correct
64 Correct 4415 ms 249688 KB Output is correct
65 Correct 431 ms 249688 KB Output is correct
66 Correct 4445 ms 249688 KB Output is correct
67 Correct 3039 ms 249688 KB Output is correct
68 Correct 4257 ms 249688 KB Output is correct
69 Correct 3805 ms 249688 KB Output is correct
70 Correct 4861 ms 249688 KB Output is correct
71 Correct 4988 ms 249688 KB Output is correct
72 Correct 2673 ms 249688 KB Output is correct
73 Correct 3070 ms 249688 KB Output is correct
74 Correct 3777 ms 249688 KB Output is correct
75 Correct 4017 ms 249688 KB Output is correct
76 Correct 1819 ms 249688 KB Output is correct
77 Correct 1728 ms 249688 KB Output is correct
78 Correct 2031 ms 249688 KB Output is correct
79 Correct 2311 ms 249688 KB Output is correct
80 Correct 2266 ms 249688 KB Output is correct
81 Correct 599 ms 249688 KB Output is correct
82 Correct 490 ms 249688 KB Output is correct
83 Correct 614 ms 249688 KB Output is correct
84 Correct 479 ms 249688 KB Output is correct
85 Correct 553 ms 249688 KB Output is correct
86 Correct 514 ms 249688 KB Output is correct
87 Correct 519 ms 249688 KB Output is correct
88 Correct 487 ms 249688 KB Output is correct
89 Correct 486 ms 249688 KB Output is correct
90 Correct 266 ms 249688 KB Output is correct
91 Correct 535 ms 249688 KB Output is correct
92 Correct 714 ms 249688 KB Output is correct
93 Correct 612 ms 249688 KB Output is correct
94 Correct 567 ms 249688 KB Output is correct
95 Correct 504 ms 249688 KB Output is correct
96 Correct 294 ms 249688 KB Output is correct
97 Correct 2832 ms 249688 KB Output is correct
98 Correct 439 ms 249688 KB Output is correct
99 Correct 4265 ms 249688 KB Output is correct
100 Correct 2583 ms 249688 KB Output is correct
101 Correct 3641 ms 249688 KB Output is correct
102 Correct 4573 ms 249688 KB Output is correct
103 Correct 2869 ms 249688 KB Output is correct
104 Correct 3062 ms 249688 KB Output is correct
105 Correct 1932 ms 249688 KB Output is correct
106 Correct 2190 ms 249688 KB Output is correct
107 Correct 3187 ms 249688 KB Output is correct
108 Correct 2940 ms 249688 KB Output is correct
109 Correct 2963 ms 249688 KB Output is correct
110 Correct 3008 ms 249688 KB Output is correct
111 Correct 2985 ms 249688 KB Output is correct
112 Correct 3161 ms 249688 KB Output is correct
113 Correct 1016 ms 249688 KB Output is correct
114 Correct 3213 ms 249688 KB Output is correct
115 Correct 3688 ms 249688 KB Output is correct
116 Correct 3821 ms 249688 KB Output is correct
117 Correct 4014 ms 249688 KB Output is correct
118 Correct 3761 ms 249688 KB Output is correct
119 Correct 1413 ms 249688 KB Output is correct
120 Correct 1125 ms 249688 KB Output is correct
121 Correct 1360 ms 249688 KB Output is correct
122 Correct 1363 ms 249688 KB Output is correct
123 Correct 1383 ms 249688 KB Output is correct
124 Correct 1716 ms 249688 KB Output is correct
125 Correct 1612 ms 249688 KB Output is correct
126 Correct 1718 ms 249688 KB Output is correct