Submission #172063

# Submission time Handle Problem Language Result Execution time Memory
172063 2019-12-31T03:20:55 Z AngusRitossa New Home (APIO18_new_home) C++14
47 / 100
4804 ms 482152 KB
#include <bits/stdc++.h>
using namespace std;
#define MXN 60010
#define MXNODES MXN*80
#ifdef DEBUG
	#define D(x...) printf(x)
#else
	#define D(x...)
#endif
set<pair<int, int> > segments[MXNODES];
int l[MXNODES], r[MXNODES], upto;
void addsegment(int s, int e, pair<int, int> val, int curr = 1, int cstart = 0, int cend = 1e8)
{
	if (e < cstart || s > cend) return;
	if (s <= cstart && cend <= e)
	{
		segments[curr].insert(val);
		return;
	}
	int mid = (cstart+cend)/2;
	if (e <= mid) 
	{
		if (!l[curr]) l[curr] = ++upto;
		addsegment(s, e, val, l[curr], cstart, mid);
	}
	else if (s > mid)
	{
		if (!r[curr]) r[curr] = ++upto;
		addsegment(s, e, val, r[curr], mid+1, cend);
	}
	else
	{
		if (!l[curr]) l[curr] = ++upto;
		if (!r[curr]) r[curr] = ++upto;
		addsegment(s, e, val, l[curr], cstart, mid);
		addsegment(s, e, val, r[curr], mid+1, cend);
	}
}
void removesegment(int s, int e, pair<int, int> val, int curr = 1, int cstart = 0, int cend = 1e8)
{
	if (e < cstart || s > cend) return;
	if (s <= cstart && cend <= e)
	{
		segments[curr].erase(val);
		return;
	}
	int mid = (cstart+cend)/2;
	if (e <= mid) 
	{
		removesegment(s, e, val, l[curr], cstart, mid);
	}
	else if (s > mid)
	{
		removesegment(s, e, val, r[curr], mid+1, cend);
	}
	else
	{
		removesegment(s, e, val, l[curr], cstart, mid);
		removesegment(s, e, val, r[curr], mid+1, cend);
	}
}
pair<int, int> query(int node, int curr = 1, int cstart = 0, int cend = 1e8)
{
	pair<int, int> ans = { 2e8, -2e8 };
	if (segments[curr].size()) ans.first = segments[curr].begin()->first, ans.second = segments[curr].rbegin()->first;
	if (cstart == cend) return ans;
	int mid = (cstart+cend)/2;
	pair<int, int> child;
	if (node <= mid) child = query(node, l[curr], cstart, mid);
	else child = query(node, r[curr], mid+1, cend);
	ans.first = min(ans.first, child.first);
	ans.second = max(ans.second, child.second);
	return ans;
}
int n, k, q, x[MXN], t[MXN], a[MXN], b[MXN], amwithshop;
vector<pair<int, int> > events;
set<pair<int, int> > shops[MXN]; 
pair<int, int> queries[MXN];
int sq[MXN], ans[MXN];
int doQuery(int loc)
{
	if (amwithshop != k)
	{
		return -1;
	}
	// jdi
	auto res = query(loc);
	D("%d %d\n", res.first, res.second);
	int before = loc-res.first;
	int after = res.second-loc;
	int ans = max(before, after);
	return ans;
}
int main()
{
	++upto;
	scanf("%d%d%d", &n, &k, &q);
	for (int i = 1; i <= k; i++) 
	{
		shops[i].insert({ -2e8, -1 }), shops[i].insert({ 2e8, -2 });
		// dumbie stuff
		addsegment(-2e8, 0, {-2e8, -1}, 0);
		addsegment(0, 2e8, {2e8, -2}, 1);
	}
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d%d%d", &x[i], &t[i], &a[i], &b[i]);
		events.emplace_back(i, 0);
		events.emplace_back(i, 1);
	}
	for (int i = 0; i < q; i++)
	{
		scanf("%d%d", &queries[i].second, &queries[i].first);
	}
	iota(sq, sq+q, 0);
	sort(sq, sq+q, [&](int a, int b) { return queries[a].first < queries[b].first;});
	int qupto = 0;

	sort(events.begin(), events.end(), [](const pair<int, int> &i, const pair<int, int> &j)
	{
		int val1 = i.second ? b[i.first] : a[i.first];
		int val2 = j.second ? b[j.first] : a[j.first];
		return make_pair(val1, i.second) < make_pair(val2, j.second);
	});
	for (auto f : events)
	{
		int i = f.first;
		int eventType = f.second;
		int timeOfEvent = eventType ? b[i]+1 : a[i];
		while (qupto < q && queries[sq[qupto]].first < timeOfEvent)
		{
			// do query
			ans[sq[qupto]] = doQuery(queries[sq[qupto]].second);
			qupto++;
		}
		D("\nprocessing %d %d : %d (time: %d)\n", i, eventType, t[i], eventType ? b[i] : a[i]);
		if (!eventType) // new thing
		{
			D("new thing\n");
			if (shops[t[i]].size() == 2) amwithshop++;
			auto it = shops[t[i]].lower_bound({x[i], i});
			auto after = *it;
			auto before = *prev(it);
			// remove the ranges 
			int mid = (before.first+after.first)/2;
			removesegment(before.first, mid, before);
			mid = (before.first+after.first+1)/2;
			removesegment(mid, after.first, after);
			// add in, then add ranges (4)
			shops[t[i]].insert({x[i], i});
			mid = (before.first+x[i])/2;
			D("adding range first %d %d\n", before.first, mid);
			addsegment(before.first, mid, before);
			mid = (before.first+x[i]+1)/2;
			D("adding range second %d %d\n", mid, x[i]);
			addsegment(mid, x[i], { x[i], i });
			mid = (x[i]+after.first)/2;
			D("adding range first %d %d\n", x[i], mid);
			addsegment(x[i], mid, { x[i], i });
			mid = (x[i]+after.first+1)/2;
			D("adding range second %d %d\n", mid, after.first);
			addsegment(mid, after.first, after);
		}
		else // end of thing
		{
			D("end of thing\n");
			shops[t[i]].erase({x[i], i});
			auto it = shops[t[i]].lower_bound({x[i], i});
			auto after = *it;
			auto before = *prev(it);
			// remove ranges
			int mid = (before.first+x[i])/2;
			removesegment(before.first, mid, before);
			mid = (before.first+x[i]+1)/2;
			removesegment(mid, x[i], { x[i], i });
			mid = (x[i]+after.first)/2;
			removesegment(x[i], mid, { x[i], i });
			mid = (x[i]+after.first+1)/2;
			removesegment(mid, after.first, after);

			// add new ranges
			mid = (before.first+after.first)/2;
			addsegment(before.first, mid, before);
			mid = (before.first+after.first+1)/2;
			addsegment(mid, after.first, after);
			if (shops[t[i]].size() == 2) amwithshop--;
		}
	}
	while (qupto < q)
	{
		ans[sq[qupto]] = doQuery(queries[sq[qupto]].second);
		qupto++;
	}
	for (int i = 0; i < q; i++) printf("%d\n", ans[i]);
}

Compilation message

new_home.cpp: In function 'int main()':
new_home.cpp:97:7: 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:107:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &x[i], &t[i], &a[i], &b[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &queries[i].second, &queries[i].first);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 209 ms 228648 KB Output is correct
2 Correct 212 ms 228660 KB Output is correct
3 Correct 212 ms 228648 KB Output is correct
4 Correct 212 ms 228636 KB Output is correct
5 Correct 217 ms 228748 KB Output is correct
6 Correct 223 ms 229268 KB Output is correct
7 Correct 217 ms 229072 KB Output is correct
8 Correct 244 ms 229604 KB Output is correct
9 Correct 218 ms 229392 KB Output is correct
10 Correct 219 ms 229820 KB Output is correct
11 Correct 211 ms 229040 KB Output is correct
12 Correct 215 ms 229100 KB Output is correct
13 Correct 210 ms 228836 KB Output is correct
14 Correct 213 ms 228880 KB Output is correct
15 Correct 215 ms 229096 KB Output is correct
16 Correct 216 ms 229192 KB Output is correct
17 Correct 215 ms 229024 KB Output is correct
18 Correct 214 ms 229208 KB Output is correct
19 Correct 215 ms 229212 KB Output is correct
20 Correct 215 ms 229128 KB Output is correct
21 Correct 209 ms 228892 KB Output is correct
22 Correct 212 ms 229436 KB Output is correct
23 Correct 213 ms 229448 KB Output is correct
24 Correct 215 ms 229280 KB Output is correct
25 Correct 215 ms 229112 KB Output is correct
26 Correct 213 ms 228984 KB Output is correct
27 Correct 211 ms 228728 KB Output is correct
28 Correct 213 ms 228856 KB Output is correct
29 Correct 215 ms 228940 KB Output is correct
30 Correct 212 ms 228772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 228648 KB Output is correct
2 Correct 212 ms 228660 KB Output is correct
3 Correct 212 ms 228648 KB Output is correct
4 Correct 212 ms 228636 KB Output is correct
5 Correct 217 ms 228748 KB Output is correct
6 Correct 223 ms 229268 KB Output is correct
7 Correct 217 ms 229072 KB Output is correct
8 Correct 244 ms 229604 KB Output is correct
9 Correct 218 ms 229392 KB Output is correct
10 Correct 219 ms 229820 KB Output is correct
11 Correct 211 ms 229040 KB Output is correct
12 Correct 215 ms 229100 KB Output is correct
13 Correct 210 ms 228836 KB Output is correct
14 Correct 213 ms 228880 KB Output is correct
15 Correct 215 ms 229096 KB Output is correct
16 Correct 216 ms 229192 KB Output is correct
17 Correct 215 ms 229024 KB Output is correct
18 Correct 214 ms 229208 KB Output is correct
19 Correct 215 ms 229212 KB Output is correct
20 Correct 215 ms 229128 KB Output is correct
21 Correct 209 ms 228892 KB Output is correct
22 Correct 212 ms 229436 KB Output is correct
23 Correct 213 ms 229448 KB Output is correct
24 Correct 215 ms 229280 KB Output is correct
25 Correct 215 ms 229112 KB Output is correct
26 Correct 213 ms 228984 KB Output is correct
27 Correct 211 ms 228728 KB Output is correct
28 Correct 213 ms 228856 KB Output is correct
29 Correct 215 ms 228940 KB Output is correct
30 Correct 212 ms 228772 KB Output is correct
31 Correct 4804 ms 361392 KB Output is correct
32 Correct 533 ms 234816 KB Output is correct
33 Correct 2769 ms 292840 KB Output is correct
34 Correct 4578 ms 302440 KB Output is correct
35 Correct 4110 ms 356160 KB Output is correct
36 Correct 2794 ms 335972 KB Output is correct
37 Correct 2223 ms 269216 KB Output is correct
38 Correct 1810 ms 268360 KB Output is correct
39 Correct 1231 ms 262904 KB Output is correct
40 Correct 1287 ms 263696 KB Output is correct
41 Correct 1825 ms 254740 KB Output is correct
42 Correct 1947 ms 255064 KB Output is correct
43 Correct 473 ms 240068 KB Output is correct
44 Correct 1776 ms 254572 KB Output is correct
45 Correct 1534 ms 254144 KB Output is correct
46 Correct 1190 ms 253956 KB Output is correct
47 Correct 735 ms 250816 KB Output is correct
48 Correct 715 ms 251756 KB Output is correct
49 Correct 947 ms 252812 KB Output is correct
50 Correct 1292 ms 253244 KB Output is correct
51 Correct 912 ms 253064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 609 ms 482152 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 603 ms 473916 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 209 ms 228648 KB Output is correct
2 Correct 212 ms 228660 KB Output is correct
3 Correct 212 ms 228648 KB Output is correct
4 Correct 212 ms 228636 KB Output is correct
5 Correct 217 ms 228748 KB Output is correct
6 Correct 223 ms 229268 KB Output is correct
7 Correct 217 ms 229072 KB Output is correct
8 Correct 244 ms 229604 KB Output is correct
9 Correct 218 ms 229392 KB Output is correct
10 Correct 219 ms 229820 KB Output is correct
11 Correct 211 ms 229040 KB Output is correct
12 Correct 215 ms 229100 KB Output is correct
13 Correct 210 ms 228836 KB Output is correct
14 Correct 213 ms 228880 KB Output is correct
15 Correct 215 ms 229096 KB Output is correct
16 Correct 216 ms 229192 KB Output is correct
17 Correct 215 ms 229024 KB Output is correct
18 Correct 214 ms 229208 KB Output is correct
19 Correct 215 ms 229212 KB Output is correct
20 Correct 215 ms 229128 KB Output is correct
21 Correct 209 ms 228892 KB Output is correct
22 Correct 212 ms 229436 KB Output is correct
23 Correct 213 ms 229448 KB Output is correct
24 Correct 215 ms 229280 KB Output is correct
25 Correct 215 ms 229112 KB Output is correct
26 Correct 213 ms 228984 KB Output is correct
27 Correct 211 ms 228728 KB Output is correct
28 Correct 213 ms 228856 KB Output is correct
29 Correct 215 ms 228940 KB Output is correct
30 Correct 212 ms 228772 KB Output is correct
31 Correct 4804 ms 361392 KB Output is correct
32 Correct 533 ms 234816 KB Output is correct
33 Correct 2769 ms 292840 KB Output is correct
34 Correct 4578 ms 302440 KB Output is correct
35 Correct 4110 ms 356160 KB Output is correct
36 Correct 2794 ms 335972 KB Output is correct
37 Correct 2223 ms 269216 KB Output is correct
38 Correct 1810 ms 268360 KB Output is correct
39 Correct 1231 ms 262904 KB Output is correct
40 Correct 1287 ms 263696 KB Output is correct
41 Correct 1825 ms 254740 KB Output is correct
42 Correct 1947 ms 255064 KB Output is correct
43 Correct 473 ms 240068 KB Output is correct
44 Correct 1776 ms 254572 KB Output is correct
45 Correct 1534 ms 254144 KB Output is correct
46 Correct 1190 ms 253956 KB Output is correct
47 Correct 735 ms 250816 KB Output is correct
48 Correct 715 ms 251756 KB Output is correct
49 Correct 947 ms 252812 KB Output is correct
50 Correct 1292 ms 253244 KB Output is correct
51 Correct 912 ms 253064 KB Output is correct
52 Correct 1532 ms 324668 KB Output is correct
53 Correct 1411 ms 275520 KB Output is correct
54 Correct 3872 ms 361576 KB Output is correct
55 Correct 2116 ms 276296 KB Output is correct
56 Correct 2096 ms 288388 KB Output is correct
57 Correct 2246 ms 259460 KB Output is correct
58 Correct 2231 ms 276584 KB Output is correct
59 Correct 2174 ms 288724 KB Output is correct
60 Correct 2530 ms 259456 KB Output is correct
61 Correct 403 ms 246060 KB Output is correct
62 Correct 1652 ms 324764 KB Output is correct
63 Correct 2903 ms 330856 KB Output is correct
64 Correct 3361 ms 318536 KB Output is correct
65 Correct 3581 ms 273784 KB Output is correct
66 Correct 2321 ms 253004 KB Output is correct
67 Correct 1519 ms 242220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 209 ms 228648 KB Output is correct
2 Correct 212 ms 228660 KB Output is correct
3 Correct 212 ms 228648 KB Output is correct
4 Correct 212 ms 228636 KB Output is correct
5 Correct 217 ms 228748 KB Output is correct
6 Correct 223 ms 229268 KB Output is correct
7 Correct 217 ms 229072 KB Output is correct
8 Correct 244 ms 229604 KB Output is correct
9 Correct 218 ms 229392 KB Output is correct
10 Correct 219 ms 229820 KB Output is correct
11 Correct 211 ms 229040 KB Output is correct
12 Correct 215 ms 229100 KB Output is correct
13 Correct 210 ms 228836 KB Output is correct
14 Correct 213 ms 228880 KB Output is correct
15 Correct 215 ms 229096 KB Output is correct
16 Correct 216 ms 229192 KB Output is correct
17 Correct 215 ms 229024 KB Output is correct
18 Correct 214 ms 229208 KB Output is correct
19 Correct 215 ms 229212 KB Output is correct
20 Correct 215 ms 229128 KB Output is correct
21 Correct 209 ms 228892 KB Output is correct
22 Correct 212 ms 229436 KB Output is correct
23 Correct 213 ms 229448 KB Output is correct
24 Correct 215 ms 229280 KB Output is correct
25 Correct 215 ms 229112 KB Output is correct
26 Correct 213 ms 228984 KB Output is correct
27 Correct 211 ms 228728 KB Output is correct
28 Correct 213 ms 228856 KB Output is correct
29 Correct 215 ms 228940 KB Output is correct
30 Correct 212 ms 228772 KB Output is correct
31 Correct 4804 ms 361392 KB Output is correct
32 Correct 533 ms 234816 KB Output is correct
33 Correct 2769 ms 292840 KB Output is correct
34 Correct 4578 ms 302440 KB Output is correct
35 Correct 4110 ms 356160 KB Output is correct
36 Correct 2794 ms 335972 KB Output is correct
37 Correct 2223 ms 269216 KB Output is correct
38 Correct 1810 ms 268360 KB Output is correct
39 Correct 1231 ms 262904 KB Output is correct
40 Correct 1287 ms 263696 KB Output is correct
41 Correct 1825 ms 254740 KB Output is correct
42 Correct 1947 ms 255064 KB Output is correct
43 Correct 473 ms 240068 KB Output is correct
44 Correct 1776 ms 254572 KB Output is correct
45 Correct 1534 ms 254144 KB Output is correct
46 Correct 1190 ms 253956 KB Output is correct
47 Correct 735 ms 250816 KB Output is correct
48 Correct 715 ms 251756 KB Output is correct
49 Correct 947 ms 252812 KB Output is correct
50 Correct 1292 ms 253244 KB Output is correct
51 Correct 912 ms 253064 KB Output is correct
52 Runtime error 609 ms 482152 KB Execution killed with signal 11 (could be triggered by violating memory limits)
53 Halted 0 ms 0 KB -