Submission #367977

# Submission time Handle Problem Language Result Execution time Memory
367977 2021-02-19T04:48:56 Z arnold518 New Home (APIO18_new_home) C++14
47 / 100
5000 ms 304240 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 = 1e6;
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<int> comp;

int ans[MAXN+10];

struct SEG
{
	multiset<int, greater<int>> val[MAXN+10];
	int tree[MAXN*4+10];
	void init(int node, int tl, int tr)
	{
		tree[node]=-INF;
		if(tl==tr) return;
		int mid=tl+tr>>1;
		init(node*2, tl, mid);
		init(node*2+1, mid+1, tr);
	}
	void update(int node, int tl, int tr, int p)
	{
		if(tl==tr)
		{
			if(!val[tl].empty()) tree[node]=*val[tl].begin();
			else tree[node]=-INF;
			return;
		}
		int mid=tl+tr>>1;
		if(p<=mid) update(node*2, tl, mid, p);
		else update(node*2+1, mid+1, tr, p);
		tree[node]=max(tree[node*2], tree[node*2+1]);
	}
	int query(int node, int tl, int tr, int l, int r)
	{
		if(l<=tl && tr<=r) return tree[node];
		if(r<tl || tr<l) return -INF;
		int mid=tl+tr>>1;
		return max(query(node*2, tl, mid, l, r), query(node*2+1, mid+1, tr, l, r));
	}
	void push(int x, int y)
	{
		val[x].insert(y);
		update(1, 1, comp.size(), x);
	}
	void pop(int x, int y)
	{
		assert(val[x].find(y)!=val[x].end());
		val[x].erase(val[x].find(y));
		update(1, 1, comp.size(), x);
	}
	int query(int l, int r)
	{
		return query(1, 1, comp.size(), l, r);
	}
}seg1, seg2;

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

void pop(int x, int y)
{
	int x2=lower_bound(comp.begin(), comp.end(), x)-comp.begin()+1;
	assert(comp[x2-1]==x);
	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);

	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);
			comp.push_back((now.x+r)>>1);
			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);
			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);
	}

	seg1.init(1, 1, comp.size());
	seg2.init(1, 1, comp.size());

	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);
			push((l+now.x)/2, (now.x-l)/2);
			push((now.x+r)/2, (r-now.x)/2);

			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);
			pop((l+now.x)/2, (now.x-l)/2);
			pop((now.x+r)/2, (r-now.x)/2);

			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(), now.l)-comp.begin()+1;
			val=max(val, seg1.query(x, comp.size())+now.l);
			x=upper_bound(comp.begin(), comp.end(), now.l)-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 member function 'void SEG::init(int, int, int)':
new_home.cpp:45:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |   int mid=tl+tr>>1;
      |           ~~^~~
new_home.cpp: In member function 'void SEG::update(int, int, int, int)':
new_home.cpp:57:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   57 |   int mid=tl+tr>>1;
      |           ~~^~~
new_home.cpp: In member function 'int SEG::query(int, int, int, int, int)':
new_home.cpp:66:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   66 |   int mid=tl+tr>>1;
      |           ~~^~~
new_home.cpp: In function 'int main()':
new_home.cpp:104:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  104 |  scanf("%d%d%d", &N, &K, &Q);
      |  ~~~~~^~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:107:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  107 |   scanf("%d%d%d%d", &A[i].x, &A[i].t, &A[i].a, &A[i].b);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  112 |   scanf("%d%d", &B[i].l, &B[i].y);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 120 ms 188268 KB Output is correct
2 Correct 120 ms 188268 KB Output is correct
3 Correct 122 ms 188268 KB Output is correct
4 Correct 125 ms 188396 KB Output is correct
5 Correct 127 ms 188268 KB Output is correct
6 Correct 128 ms 188396 KB Output is correct
7 Correct 121 ms 188524 KB Output is correct
8 Correct 136 ms 188524 KB Output is correct
9 Correct 135 ms 188524 KB Output is correct
10 Correct 123 ms 188396 KB Output is correct
11 Correct 122 ms 188268 KB Output is correct
12 Correct 123 ms 188268 KB Output is correct
13 Correct 131 ms 188268 KB Output is correct
14 Correct 122 ms 188268 KB Output is correct
15 Correct 122 ms 188416 KB Output is correct
16 Correct 122 ms 188396 KB Output is correct
17 Correct 122 ms 188396 KB Output is correct
18 Correct 121 ms 188396 KB Output is correct
19 Correct 124 ms 188524 KB Output is correct
20 Correct 122 ms 188396 KB Output is correct
21 Correct 121 ms 188396 KB Output is correct
22 Correct 121 ms 188524 KB Output is correct
23 Correct 119 ms 188396 KB Output is correct
24 Correct 122 ms 188396 KB Output is correct
25 Correct 124 ms 188384 KB Output is correct
26 Correct 121 ms 188268 KB Output is correct
27 Correct 123 ms 188268 KB Output is correct
28 Correct 125 ms 188268 KB Output is correct
29 Correct 120 ms 188268 KB Output is correct
30 Correct 121 ms 188352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 188268 KB Output is correct
2 Correct 120 ms 188268 KB Output is correct
3 Correct 122 ms 188268 KB Output is correct
4 Correct 125 ms 188396 KB Output is correct
5 Correct 127 ms 188268 KB Output is correct
6 Correct 128 ms 188396 KB Output is correct
7 Correct 121 ms 188524 KB Output is correct
8 Correct 136 ms 188524 KB Output is correct
9 Correct 135 ms 188524 KB Output is correct
10 Correct 123 ms 188396 KB Output is correct
11 Correct 122 ms 188268 KB Output is correct
12 Correct 123 ms 188268 KB Output is correct
13 Correct 131 ms 188268 KB Output is correct
14 Correct 122 ms 188268 KB Output is correct
15 Correct 122 ms 188416 KB Output is correct
16 Correct 122 ms 188396 KB Output is correct
17 Correct 122 ms 188396 KB Output is correct
18 Correct 121 ms 188396 KB Output is correct
19 Correct 124 ms 188524 KB Output is correct
20 Correct 122 ms 188396 KB Output is correct
21 Correct 121 ms 188396 KB Output is correct
22 Correct 121 ms 188524 KB Output is correct
23 Correct 119 ms 188396 KB Output is correct
24 Correct 122 ms 188396 KB Output is correct
25 Correct 124 ms 188384 KB Output is correct
26 Correct 121 ms 188268 KB Output is correct
27 Correct 123 ms 188268 KB Output is correct
28 Correct 125 ms 188268 KB Output is correct
29 Correct 120 ms 188268 KB Output is correct
30 Correct 121 ms 188352 KB Output is correct
31 Correct 996 ms 209040 KB Output is correct
32 Correct 253 ms 193628 KB Output is correct
33 Correct 911 ms 201564 KB Output is correct
34 Correct 889 ms 201820 KB Output is correct
35 Correct 999 ms 208840 KB Output is correct
36 Correct 1016 ms 208860 KB Output is correct
37 Correct 708 ms 199032 KB Output is correct
38 Correct 719 ms 198876 KB Output is correct
39 Correct 525 ms 198664 KB Output is correct
40 Correct 549 ms 198448 KB Output is correct
41 Correct 690 ms 198716 KB Output is correct
42 Correct 642 ms 198492 KB Output is correct
43 Correct 209 ms 195804 KB Output is correct
44 Correct 693 ms 198876 KB Output is correct
45 Correct 632 ms 198748 KB Output is correct
46 Correct 599 ms 198876 KB Output is correct
47 Correct 412 ms 198364 KB Output is correct
48 Correct 403 ms 198364 KB Output is correct
49 Correct 438 ms 198364 KB Output is correct
50 Correct 473 ms 198492 KB Output is correct
51 Correct 453 ms 198364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4648 ms 304240 KB Output is correct
2 Execution timed out 5108 ms 297128 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5090 ms 286196 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 120 ms 188268 KB Output is correct
2 Correct 120 ms 188268 KB Output is correct
3 Correct 122 ms 188268 KB Output is correct
4 Correct 125 ms 188396 KB Output is correct
5 Correct 127 ms 188268 KB Output is correct
6 Correct 128 ms 188396 KB Output is correct
7 Correct 121 ms 188524 KB Output is correct
8 Correct 136 ms 188524 KB Output is correct
9 Correct 135 ms 188524 KB Output is correct
10 Correct 123 ms 188396 KB Output is correct
11 Correct 122 ms 188268 KB Output is correct
12 Correct 123 ms 188268 KB Output is correct
13 Correct 131 ms 188268 KB Output is correct
14 Correct 122 ms 188268 KB Output is correct
15 Correct 122 ms 188416 KB Output is correct
16 Correct 122 ms 188396 KB Output is correct
17 Correct 122 ms 188396 KB Output is correct
18 Correct 121 ms 188396 KB Output is correct
19 Correct 124 ms 188524 KB Output is correct
20 Correct 122 ms 188396 KB Output is correct
21 Correct 121 ms 188396 KB Output is correct
22 Correct 121 ms 188524 KB Output is correct
23 Correct 119 ms 188396 KB Output is correct
24 Correct 122 ms 188396 KB Output is correct
25 Correct 124 ms 188384 KB Output is correct
26 Correct 121 ms 188268 KB Output is correct
27 Correct 123 ms 188268 KB Output is correct
28 Correct 125 ms 188268 KB Output is correct
29 Correct 120 ms 188268 KB Output is correct
30 Correct 121 ms 188352 KB Output is correct
31 Correct 996 ms 209040 KB Output is correct
32 Correct 253 ms 193628 KB Output is correct
33 Correct 911 ms 201564 KB Output is correct
34 Correct 889 ms 201820 KB Output is correct
35 Correct 999 ms 208840 KB Output is correct
36 Correct 1016 ms 208860 KB Output is correct
37 Correct 708 ms 199032 KB Output is correct
38 Correct 719 ms 198876 KB Output is correct
39 Correct 525 ms 198664 KB Output is correct
40 Correct 549 ms 198448 KB Output is correct
41 Correct 690 ms 198716 KB Output is correct
42 Correct 642 ms 198492 KB Output is correct
43 Correct 209 ms 195804 KB Output is correct
44 Correct 693 ms 198876 KB Output is correct
45 Correct 632 ms 198748 KB Output is correct
46 Correct 599 ms 198876 KB Output is correct
47 Correct 412 ms 198364 KB Output is correct
48 Correct 403 ms 198364 KB Output is correct
49 Correct 438 ms 198364 KB Output is correct
50 Correct 473 ms 198492 KB Output is correct
51 Correct 453 ms 198364 KB Output is correct
52 Correct 790 ms 223328 KB Output is correct
53 Correct 809 ms 216320 KB Output is correct
54 Correct 847 ms 214108 KB Output is correct
55 Correct 762 ms 207196 KB Output is correct
56 Correct 739 ms 211932 KB Output is correct
57 Correct 804 ms 200540 KB Output is correct
58 Correct 763 ms 207016 KB Output is correct
59 Correct 777 ms 211676 KB Output is correct
60 Correct 695 ms 200540 KB Output is correct
61 Correct 438 ms 221788 KB Output is correct
62 Correct 809 ms 223644 KB Output is correct
63 Correct 835 ms 215260 KB Output is correct
64 Correct 852 ms 210540 KB Output is correct
65 Correct 829 ms 201892 KB Output is correct
66 Correct 719 ms 199004 KB Output is correct
67 Correct 537 ms 197452 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 188268 KB Output is correct
2 Correct 120 ms 188268 KB Output is correct
3 Correct 122 ms 188268 KB Output is correct
4 Correct 125 ms 188396 KB Output is correct
5 Correct 127 ms 188268 KB Output is correct
6 Correct 128 ms 188396 KB Output is correct
7 Correct 121 ms 188524 KB Output is correct
8 Correct 136 ms 188524 KB Output is correct
9 Correct 135 ms 188524 KB Output is correct
10 Correct 123 ms 188396 KB Output is correct
11 Correct 122 ms 188268 KB Output is correct
12 Correct 123 ms 188268 KB Output is correct
13 Correct 131 ms 188268 KB Output is correct
14 Correct 122 ms 188268 KB Output is correct
15 Correct 122 ms 188416 KB Output is correct
16 Correct 122 ms 188396 KB Output is correct
17 Correct 122 ms 188396 KB Output is correct
18 Correct 121 ms 188396 KB Output is correct
19 Correct 124 ms 188524 KB Output is correct
20 Correct 122 ms 188396 KB Output is correct
21 Correct 121 ms 188396 KB Output is correct
22 Correct 121 ms 188524 KB Output is correct
23 Correct 119 ms 188396 KB Output is correct
24 Correct 122 ms 188396 KB Output is correct
25 Correct 124 ms 188384 KB Output is correct
26 Correct 121 ms 188268 KB Output is correct
27 Correct 123 ms 188268 KB Output is correct
28 Correct 125 ms 188268 KB Output is correct
29 Correct 120 ms 188268 KB Output is correct
30 Correct 121 ms 188352 KB Output is correct
31 Correct 996 ms 209040 KB Output is correct
32 Correct 253 ms 193628 KB Output is correct
33 Correct 911 ms 201564 KB Output is correct
34 Correct 889 ms 201820 KB Output is correct
35 Correct 999 ms 208840 KB Output is correct
36 Correct 1016 ms 208860 KB Output is correct
37 Correct 708 ms 199032 KB Output is correct
38 Correct 719 ms 198876 KB Output is correct
39 Correct 525 ms 198664 KB Output is correct
40 Correct 549 ms 198448 KB Output is correct
41 Correct 690 ms 198716 KB Output is correct
42 Correct 642 ms 198492 KB Output is correct
43 Correct 209 ms 195804 KB Output is correct
44 Correct 693 ms 198876 KB Output is correct
45 Correct 632 ms 198748 KB Output is correct
46 Correct 599 ms 198876 KB Output is correct
47 Correct 412 ms 198364 KB Output is correct
48 Correct 403 ms 198364 KB Output is correct
49 Correct 438 ms 198364 KB Output is correct
50 Correct 473 ms 198492 KB Output is correct
51 Correct 453 ms 198364 KB Output is correct
52 Correct 4648 ms 304240 KB Output is correct
53 Execution timed out 5108 ms 297128 KB Time limit exceeded
54 Halted 0 ms 0 KB -