답안 #367978

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
367978 2021-02-19T05:24:20 Z arnold518 새 집 (APIO18_new_home) C++14
100 / 100
4456 ms 176076 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 3e5;
const int SEGN = 1048576;
const int INF = 2e8;

int N, Q, K;

struct Data
{
	int x, t, a, b;
};

struct Query
{
	int l, y, p;
};

struct Data2
{
	int t, p, q;
};

Data A[MAXN+10];
Query B[MAXN+10];

set<int> S[MAXN+10];
multiset<int> S2[MAXN+10];
vector<pii> comp;

int ans[MAXN+10];

struct SEG
{
	int tree[SEGN*2+10];
	void init()
	{
		for(int i=0; i<SEGN*2+10; i++) tree[i]=-INF;
	}
	void update(int p, int q)
	{
		p+=SEGN;
		tree[p]=q;
		for(int node=p>>1; node>=1; node>>=1) tree[node]=max(tree[node<<1], tree[node<<1|1]);
	}
	int query(int l, int r)
	{
		l+=SEGN; r+=SEGN; r++;
		int ret=-INF;
		for(; l<r; l>>=1, r>>=1)
		{
			if(l&1) ret=max(ret, tree[l++]);
			if(r&1) ret=max(ret, tree[--r]);
		}
		return ret;
	}
	void push(int x, int y)
	{
		update(x, y);
	}
	void pop(int x, int y)
	{
		update(x, -INF);
	}
}seg1, seg2;

void push(int x, int y, int k)
{
	int x2=lower_bound(comp.begin(), comp.end(), pii(x, k))-comp.begin()+1;
	seg1.push(x2, y-x);
	seg2.push(x2, x+y);
}

void pop(int x, int y, int k)
{
	int x2=lower_bound(comp.begin(), comp.end(), pii(x, k))-comp.begin()+1;
	seg1.pop(x2, y-x);
	seg2.pop(x2, x+y);
}

int main()
{
	scanf("%d%d%d", &N, &K, &Q);
	for(int i=1; i<=N; i++)
	{
		scanf("%d%d%d%d", &A[i].x, &A[i].t, &A[i].a, &A[i].b);
		A[i].x*=2;
	}
	for(int i=1; i<=Q; i++)
	{
		scanf("%d%d", &B[i].l, &B[i].y);
		B[i].l*=2;
		B[i].p=i;
	}

	vector<Data2> V;
	for(int i=1; i<=N; i++)
	{
		V.push_back({A[i].a, -1, i});
		V.push_back({A[i].b, 1, i});
	}
	for(int i=1; i<=Q; i++)
	{
		V.push_back({B[i].y, 0, i});
	}

	sort(V.begin(), V.end(), [&](const Data2 &p, const Data2 &q)
	{
		if(p.t!=q.t) return p.t<q.t;
		return p.p<q.p;
	});

	for(int i=1; i<=K; i++)
	{
		S[i].insert(-INF);
		S[i].insert(INF*2);
		S2[i].insert(-INF);
		S2[i].insert(INF*2);
		comp.push_back({INF/2, i});
	}

	for(auto it : V)
	{
		if(it.p<0)
		{
			auto now = A[it.q];
			S2[now.t].insert(now.x);
			if(S[now.t].find(now.x)!=S[now.t].end()) continue;

			auto pt=S[now.t].lower_bound(now.x);
			int l=*prev(pt), r=*pt;
			comp.push_back({(l+now.x)>>1, now.t});
			comp.push_back({(now.x+r)>>1, now.t});
			S[now.t].insert(now.x);
		}
		else if(it.p>0)
		{
			auto now = A[it.q];
			S2[now.t].erase(S2[now.t].find(now.x));
			if(S2[now.t].find(now.x)!=S2[now.t].end()) continue;

			auto pt=S[now.t].lower_bound(now.x);
			int l=*prev(pt), r=*next(pt);
			comp.push_back({(l+r)>>1, now.t});
			S[now.t].erase(now.x);
		}
	}

	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end());

	for(int i=1; i<=K; i++)
	{
		S[i].clear(); S2[i].clear();
		S[i].insert(-INF);
		S[i].insert(INF*2);
		S2[i].insert(-INF);
		S2[i].insert(INF*2);
		push(INF/2, INF+INF/2, i);
	}

	seg1.init();
	seg2.init();

	int cnt=0;
	for(auto it : V)
	{
		if(it.p<0)
		{
			auto now = A[it.q];
			if(S2[now.t].size()==2) cnt++;
			S2[now.t].insert(now.x);
			if(S[now.t].find(now.x)!=S[now.t].end()) continue;

			auto pt=S[now.t].lower_bound(now.x);
			int l=*prev(pt), r=*pt;

			pop((l+r)/2, (r-l)/2, now.t);
			push((l+now.x)/2, (now.x-l)/2, now.t);
			push((now.x+r)/2, (r-now.x)/2, now.t);

			S[now.t].insert(now.x);
		}
		else if(it.p>0)
		{
			auto now = A[it.q];
			S2[now.t].erase(S2[now.t].find(now.x));
			if(S2[now.t].size()==2) cnt--;
			if(S2[now.t].find(now.x)!=S2[now.t].end()) continue;

			auto pt=S[now.t].lower_bound(now.x);
			int l=*prev(pt), r=*next(pt);

			push((l+r)/2, (r-l)/2, now.t);
			pop((l+now.x)/2, (now.x-l)/2, now.t);
			pop((now.x+r)/2, (r-now.x)/2, now.t);

			S[now.t].erase(now.x);
		}
		else
		{
			auto now = B[it.q];
			if(cnt!=K)
			{
				ans[it.q]=-1;
				continue;
			}
			int val=-INF;
			int x=lower_bound(comp.begin(), comp.end(), pii(now.l, 0))-comp.begin()+1;
			val=max(val, seg1.query(x, comp.size())+now.l);
			x=upper_bound(comp.begin(), comp.end(), pii(now.l, INF))-comp.begin();
			val=max(val, seg2.query(1, x)-now.l);
			ans[it.q]=val/2;
		}	
	}
	for(int i=1; i<=Q; i++) printf("%d\n", ans[i]);
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:88:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   88 |  scanf("%d%d%d", &N, &K, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   91 |   scanf("%d%d%d%d", &A[i].x, &A[i].t, &A[i].a, &A[i].b);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   96 |   scanf("%d%d", &B[i].l, &B[i].y);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 44908 KB Output is correct
2 Correct 29 ms 45000 KB Output is correct
3 Correct 30 ms 44928 KB Output is correct
4 Correct 30 ms 44908 KB Output is correct
5 Correct 30 ms 45036 KB Output is correct
6 Correct 33 ms 45036 KB Output is correct
7 Correct 37 ms 45036 KB Output is correct
8 Correct 31 ms 45036 KB Output is correct
9 Correct 35 ms 45036 KB Output is correct
10 Correct 32 ms 45164 KB Output is correct
11 Correct 31 ms 45036 KB Output is correct
12 Correct 31 ms 45036 KB Output is correct
13 Correct 31 ms 45036 KB Output is correct
14 Correct 34 ms 45036 KB Output is correct
15 Correct 35 ms 45036 KB Output is correct
16 Correct 32 ms 45036 KB Output is correct
17 Correct 33 ms 45292 KB Output is correct
18 Correct 32 ms 45036 KB Output is correct
19 Correct 32 ms 45036 KB Output is correct
20 Correct 31 ms 45036 KB Output is correct
21 Correct 31 ms 45164 KB Output is correct
22 Correct 32 ms 45036 KB Output is correct
23 Correct 32 ms 45036 KB Output is correct
24 Correct 32 ms 45036 KB Output is correct
25 Correct 31 ms 45036 KB Output is correct
26 Correct 31 ms 45036 KB Output is correct
27 Correct 31 ms 45036 KB Output is correct
28 Correct 31 ms 45036 KB Output is correct
29 Correct 31 ms 45036 KB Output is correct
30 Correct 30 ms 45036 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 44908 KB Output is correct
2 Correct 29 ms 45000 KB Output is correct
3 Correct 30 ms 44928 KB Output is correct
4 Correct 30 ms 44908 KB Output is correct
5 Correct 30 ms 45036 KB Output is correct
6 Correct 33 ms 45036 KB Output is correct
7 Correct 37 ms 45036 KB Output is correct
8 Correct 31 ms 45036 KB Output is correct
9 Correct 35 ms 45036 KB Output is correct
10 Correct 32 ms 45164 KB Output is correct
11 Correct 31 ms 45036 KB Output is correct
12 Correct 31 ms 45036 KB Output is correct
13 Correct 31 ms 45036 KB Output is correct
14 Correct 34 ms 45036 KB Output is correct
15 Correct 35 ms 45036 KB Output is correct
16 Correct 32 ms 45036 KB Output is correct
17 Correct 33 ms 45292 KB Output is correct
18 Correct 32 ms 45036 KB Output is correct
19 Correct 32 ms 45036 KB Output is correct
20 Correct 31 ms 45036 KB Output is correct
21 Correct 31 ms 45164 KB Output is correct
22 Correct 32 ms 45036 KB Output is correct
23 Correct 32 ms 45036 KB Output is correct
24 Correct 32 ms 45036 KB Output is correct
25 Correct 31 ms 45036 KB Output is correct
26 Correct 31 ms 45036 KB Output is correct
27 Correct 31 ms 45036 KB Output is correct
28 Correct 31 ms 45036 KB Output is correct
29 Correct 31 ms 45036 KB Output is correct
30 Correct 30 ms 45036 KB Output is correct
31 Correct 593 ms 57564 KB Output is correct
32 Correct 151 ms 50284 KB Output is correct
33 Correct 548 ms 54244 KB Output is correct
34 Correct 510 ms 54528 KB Output is correct
35 Correct 623 ms 57540 KB Output is correct
36 Correct 632 ms 57564 KB Output is correct
37 Correct 379 ms 53084 KB Output is correct
38 Correct 400 ms 53084 KB Output is correct
39 Correct 317 ms 52828 KB Output is correct
40 Correct 354 ms 52820 KB Output is correct
41 Correct 360 ms 52956 KB Output is correct
42 Correct 370 ms 52824 KB Output is correct
43 Correct 120 ms 52444 KB Output is correct
44 Correct 358 ms 53084 KB Output is correct
45 Correct 354 ms 52956 KB Output is correct
46 Correct 344 ms 53084 KB Output is correct
47 Correct 224 ms 52572 KB Output is correct
48 Correct 224 ms 52572 KB Output is correct
49 Correct 242 ms 52700 KB Output is correct
50 Correct 252 ms 52848 KB Output is correct
51 Correct 258 ms 52700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3118 ms 114840 KB Output is correct
2 Correct 3917 ms 102268 KB Output is correct
3 Correct 3026 ms 175460 KB Output is correct
4 Correct 3111 ms 136332 KB Output is correct
5 Correct 3606 ms 114356 KB Output is correct
6 Correct 3659 ms 114780 KB Output is correct
7 Correct 2077 ms 175472 KB Output is correct
8 Correct 2161 ms 136080 KB Output is correct
9 Correct 2218 ms 122336 KB Output is correct
10 Correct 2694 ms 115776 KB Output is correct
11 Correct 1676 ms 114716 KB Output is correct
12 Correct 1790 ms 115684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3794 ms 104016 KB Output is correct
2 Correct 772 ms 83932 KB Output is correct
3 Correct 4377 ms 114736 KB Output is correct
4 Correct 3301 ms 173504 KB Output is correct
5 Correct 3174 ms 126156 KB Output is correct
6 Correct 3127 ms 133952 KB Output is correct
7 Correct 4430 ms 113984 KB Output is correct
8 Correct 4350 ms 114368 KB Output is correct
9 Correct 2799 ms 174576 KB Output is correct
10 Correct 2786 ms 130492 KB Output is correct
11 Correct 3078 ms 118848 KB Output is correct
12 Correct 3665 ms 115392 KB Output is correct
13 Correct 1624 ms 113704 KB Output is correct
14 Correct 1626 ms 113216 KB Output is correct
15 Correct 1797 ms 114480 KB Output is correct
16 Correct 2005 ms 115524 KB Output is correct
17 Correct 1975 ms 114148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 44908 KB Output is correct
2 Correct 29 ms 45000 KB Output is correct
3 Correct 30 ms 44928 KB Output is correct
4 Correct 30 ms 44908 KB Output is correct
5 Correct 30 ms 45036 KB Output is correct
6 Correct 33 ms 45036 KB Output is correct
7 Correct 37 ms 45036 KB Output is correct
8 Correct 31 ms 45036 KB Output is correct
9 Correct 35 ms 45036 KB Output is correct
10 Correct 32 ms 45164 KB Output is correct
11 Correct 31 ms 45036 KB Output is correct
12 Correct 31 ms 45036 KB Output is correct
13 Correct 31 ms 45036 KB Output is correct
14 Correct 34 ms 45036 KB Output is correct
15 Correct 35 ms 45036 KB Output is correct
16 Correct 32 ms 45036 KB Output is correct
17 Correct 33 ms 45292 KB Output is correct
18 Correct 32 ms 45036 KB Output is correct
19 Correct 32 ms 45036 KB Output is correct
20 Correct 31 ms 45036 KB Output is correct
21 Correct 31 ms 45164 KB Output is correct
22 Correct 32 ms 45036 KB Output is correct
23 Correct 32 ms 45036 KB Output is correct
24 Correct 32 ms 45036 KB Output is correct
25 Correct 31 ms 45036 KB Output is correct
26 Correct 31 ms 45036 KB Output is correct
27 Correct 31 ms 45036 KB Output is correct
28 Correct 31 ms 45036 KB Output is correct
29 Correct 31 ms 45036 KB Output is correct
30 Correct 30 ms 45036 KB Output is correct
31 Correct 593 ms 57564 KB Output is correct
32 Correct 151 ms 50284 KB Output is correct
33 Correct 548 ms 54244 KB Output is correct
34 Correct 510 ms 54528 KB Output is correct
35 Correct 623 ms 57540 KB Output is correct
36 Correct 632 ms 57564 KB Output is correct
37 Correct 379 ms 53084 KB Output is correct
38 Correct 400 ms 53084 KB Output is correct
39 Correct 317 ms 52828 KB Output is correct
40 Correct 354 ms 52820 KB Output is correct
41 Correct 360 ms 52956 KB Output is correct
42 Correct 370 ms 52824 KB Output is correct
43 Correct 120 ms 52444 KB Output is correct
44 Correct 358 ms 53084 KB Output is correct
45 Correct 354 ms 52956 KB Output is correct
46 Correct 344 ms 53084 KB Output is correct
47 Correct 224 ms 52572 KB Output is correct
48 Correct 224 ms 52572 KB Output is correct
49 Correct 242 ms 52700 KB Output is correct
50 Correct 252 ms 52848 KB Output is correct
51 Correct 258 ms 52700 KB Output is correct
52 Correct 506 ms 67932 KB Output is correct
53 Correct 492 ms 65244 KB Output is correct
54 Correct 481 ms 60764 KB Output is correct
55 Correct 394 ms 58408 KB Output is correct
56 Correct 417 ms 60636 KB Output is correct
57 Correct 372 ms 54620 KB Output is correct
58 Correct 414 ms 57692 KB Output is correct
59 Correct 435 ms 60508 KB Output is correct
60 Correct 370 ms 54364 KB Output is correct
61 Correct 284 ms 68316 KB Output is correct
62 Correct 513 ms 68116 KB Output is correct
63 Correct 496 ms 62072 KB Output is correct
64 Correct 489 ms 59396 KB Output is correct
65 Correct 446 ms 55132 KB Output is correct
66 Correct 379 ms 53148 KB Output is correct
67 Correct 434 ms 54748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 44908 KB Output is correct
2 Correct 29 ms 45000 KB Output is correct
3 Correct 30 ms 44928 KB Output is correct
4 Correct 30 ms 44908 KB Output is correct
5 Correct 30 ms 45036 KB Output is correct
6 Correct 33 ms 45036 KB Output is correct
7 Correct 37 ms 45036 KB Output is correct
8 Correct 31 ms 45036 KB Output is correct
9 Correct 35 ms 45036 KB Output is correct
10 Correct 32 ms 45164 KB Output is correct
11 Correct 31 ms 45036 KB Output is correct
12 Correct 31 ms 45036 KB Output is correct
13 Correct 31 ms 45036 KB Output is correct
14 Correct 34 ms 45036 KB Output is correct
15 Correct 35 ms 45036 KB Output is correct
16 Correct 32 ms 45036 KB Output is correct
17 Correct 33 ms 45292 KB Output is correct
18 Correct 32 ms 45036 KB Output is correct
19 Correct 32 ms 45036 KB Output is correct
20 Correct 31 ms 45036 KB Output is correct
21 Correct 31 ms 45164 KB Output is correct
22 Correct 32 ms 45036 KB Output is correct
23 Correct 32 ms 45036 KB Output is correct
24 Correct 32 ms 45036 KB Output is correct
25 Correct 31 ms 45036 KB Output is correct
26 Correct 31 ms 45036 KB Output is correct
27 Correct 31 ms 45036 KB Output is correct
28 Correct 31 ms 45036 KB Output is correct
29 Correct 31 ms 45036 KB Output is correct
30 Correct 30 ms 45036 KB Output is correct
31 Correct 593 ms 57564 KB Output is correct
32 Correct 151 ms 50284 KB Output is correct
33 Correct 548 ms 54244 KB Output is correct
34 Correct 510 ms 54528 KB Output is correct
35 Correct 623 ms 57540 KB Output is correct
36 Correct 632 ms 57564 KB Output is correct
37 Correct 379 ms 53084 KB Output is correct
38 Correct 400 ms 53084 KB Output is correct
39 Correct 317 ms 52828 KB Output is correct
40 Correct 354 ms 52820 KB Output is correct
41 Correct 360 ms 52956 KB Output is correct
42 Correct 370 ms 52824 KB Output is correct
43 Correct 120 ms 52444 KB Output is correct
44 Correct 358 ms 53084 KB Output is correct
45 Correct 354 ms 52956 KB Output is correct
46 Correct 344 ms 53084 KB Output is correct
47 Correct 224 ms 52572 KB Output is correct
48 Correct 224 ms 52572 KB Output is correct
49 Correct 242 ms 52700 KB Output is correct
50 Correct 252 ms 52848 KB Output is correct
51 Correct 258 ms 52700 KB Output is correct
52 Correct 3118 ms 114840 KB Output is correct
53 Correct 3917 ms 102268 KB Output is correct
54 Correct 3026 ms 175460 KB Output is correct
55 Correct 3111 ms 136332 KB Output is correct
56 Correct 3606 ms 114356 KB Output is correct
57 Correct 3659 ms 114780 KB Output is correct
58 Correct 2077 ms 175472 KB Output is correct
59 Correct 2161 ms 136080 KB Output is correct
60 Correct 2218 ms 122336 KB Output is correct
61 Correct 2694 ms 115776 KB Output is correct
62 Correct 1676 ms 114716 KB Output is correct
63 Correct 1790 ms 115684 KB Output is correct
64 Correct 3794 ms 104016 KB Output is correct
65 Correct 772 ms 83932 KB Output is correct
66 Correct 4377 ms 114736 KB Output is correct
67 Correct 3301 ms 173504 KB Output is correct
68 Correct 3174 ms 126156 KB Output is correct
69 Correct 3127 ms 133952 KB Output is correct
70 Correct 4430 ms 113984 KB Output is correct
71 Correct 4350 ms 114368 KB Output is correct
72 Correct 2799 ms 174576 KB Output is correct
73 Correct 2786 ms 130492 KB Output is correct
74 Correct 3078 ms 118848 KB Output is correct
75 Correct 3665 ms 115392 KB Output is correct
76 Correct 1624 ms 113704 KB Output is correct
77 Correct 1626 ms 113216 KB Output is correct
78 Correct 1797 ms 114480 KB Output is correct
79 Correct 2005 ms 115524 KB Output is correct
80 Correct 1975 ms 114148 KB Output is correct
81 Correct 506 ms 67932 KB Output is correct
82 Correct 492 ms 65244 KB Output is correct
83 Correct 481 ms 60764 KB Output is correct
84 Correct 394 ms 58408 KB Output is correct
85 Correct 417 ms 60636 KB Output is correct
86 Correct 372 ms 54620 KB Output is correct
87 Correct 414 ms 57692 KB Output is correct
88 Correct 435 ms 60508 KB Output is correct
89 Correct 370 ms 54364 KB Output is correct
90 Correct 284 ms 68316 KB Output is correct
91 Correct 513 ms 68116 KB Output is correct
92 Correct 496 ms 62072 KB Output is correct
93 Correct 489 ms 59396 KB Output is correct
94 Correct 446 ms 55132 KB Output is correct
95 Correct 379 ms 53148 KB Output is correct
96 Correct 434 ms 54748 KB Output is correct
97 Correct 3341 ms 175384 KB Output is correct
98 Correct 795 ms 76224 KB Output is correct
99 Correct 4185 ms 102732 KB Output is correct
100 Correct 3212 ms 159164 KB Output is correct
101 Correct 3183 ms 136080 KB Output is correct
102 Correct 4456 ms 116772 KB Output is correct
103 Correct 2736 ms 98168 KB Output is correct
104 Correct 2724 ms 97352 KB Output is correct
105 Correct 1708 ms 96140 KB Output is correct
106 Correct 1829 ms 96376 KB Output is correct
107 Correct 2716 ms 123408 KB Output is correct
108 Correct 2804 ms 137676 KB Output is correct
109 Correct 2700 ms 106184 KB Output is correct
110 Correct 2775 ms 122888 KB Output is correct
111 Correct 2880 ms 136536 KB Output is correct
112 Correct 2696 ms 104584 KB Output is correct
113 Correct 1362 ms 174312 KB Output is correct
114 Correct 3317 ms 176076 KB Output is correct
115 Correct 3209 ms 141868 KB Output is correct
116 Correct 3246 ms 130368 KB Output is correct
117 Correct 3339 ms 108440 KB Output is correct
118 Correct 2994 ms 98756 KB Output is correct
119 Correct 3065 ms 97284 KB Output is correct
120 Correct 978 ms 93124 KB Output is correct
121 Correct 1102 ms 94864 KB Output is correct
122 Correct 1088 ms 94904 KB Output is correct
123 Correct 1185 ms 95892 KB Output is correct
124 Correct 1284 ms 96516 KB Output is correct
125 Correct 1258 ms 95944 KB Output is correct
126 Correct 1331 ms 97016 KB Output is correct