Submission #172055

# Submission time Handle Problem Language Result Execution time Memory
172055 2019-12-31T02:42:00 Z AngusRitossa New Home (APIO18_new_home) C++14
5 / 100
5000 ms 193392 KB
#include <bits/stdc++.h>
using namespace std;
#define MXN 30010
#define MXNODES MXN*20
#ifdef DEBUG
	#define D(x...) printf(x)
#else
	#define D(x...)
#endif
set<pair<int, int> > segments[MXNODES][2];
int l[MXNODES], r[MXNODES], upto;
void addsegment(int s, int e, pair<int, int> val, int whichone, int curr = 1, int cstart = 0, int cend = 1e8)
{
	if (e < cstart || s > cend) return;
	if (s <= cstart && cend <= e)
	{
		segments[curr][whichone].insert(val);
		return;
	}
	int mid = (cstart+cend)/2;
	if (e <= mid) 
	{
		if (!l[curr]) l[curr] = ++upto;
		addsegment(s, e, val, whichone, l[curr], cstart, mid);
	}
	else if (s > mid)
	{
		if (!r[curr]) r[curr] = ++upto;
		addsegment(s, e, val, whichone, r[curr], mid+1, cend);
	}
	else
	{
		if (!l[curr]) l[curr] = ++upto;
		if (!r[curr]) r[curr] = ++upto;
		addsegment(s, e, val, whichone, l[curr], cstart, mid);
		addsegment(s, e, val, whichone, r[curr], mid+1, cend);
	}
}
void removesegment(int s, int e, pair<int, int> val, int whichone, int curr = 1, int cstart = 0, int cend = 1e8)
{
	if (e < cstart || s > cend) return;
	if (s <= cstart && cend <= e)
	{
		segments[curr][whichone].erase(val);
		return;
	}
	int mid = (cstart+cend)/2;
	if (e <= mid) 
	{
		removesegment(s, e, val, whichone, l[curr], cstart, mid);
	}
	else if (s > mid)
	{
		removesegment(s, e, val, whichone, r[curr], mid+1, cend);
	}
	else
	{
		removesegment(s, e, val, whichone, l[curr], cstart, mid);
		removesegment(s, e, val, whichone, 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][0].size()) ans.first = segments[curr][0].begin()->first;
	if (segments[curr][1].size()) ans.second = segments[curr][1].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, 0);
			mid = (before.first+after.first+1)/2;
			removesegment(mid, after.first, after, 1);
			// 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, 0);
			mid = (before.first+x[i]+1)/2;
			D("adding range second %d %d\n", mid, x[i]);
			addsegment(mid, x[i], { x[i], i }, 1);
			mid = (x[i]+after.first)/2;
			D("adding range first %d %d\n", x[i], mid);
			addsegment(x[i], mid, { x[i], i }, 0);
			mid = (x[i]+after.first+1)/2;
			D("adding range second %d %d\n", mid, after.first);
			addsegment(mid, after.first, after, 1);
		}
		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, 0);
			mid = (before.first+x[i]+1)/2;
			removesegment(mid, x[i], { x[i], i }, 1);
			mid = (x[i]+after.first)/2;
			removesegment(x[i], mid, { x[i], i }, 0);
			mid = (x[i]+after.first+1)/2;
			removesegment(mid, after.first, after, 1);

			// add new ranges
			mid = (before.first+after.first)/2;
			addsegment(before.first, mid, before, 0);
			mid = (before.first+after.first+1)/2;
			addsegment(mid, after.first, after, 1);
			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:98: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:108: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:114: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 55 ms 58232 KB Output is correct
2 Correct 55 ms 58232 KB Output is correct
3 Correct 55 ms 58232 KB Output is correct
4 Correct 55 ms 58232 KB Output is correct
5 Correct 56 ms 58232 KB Output is correct
6 Correct 63 ms 58744 KB Output is correct
7 Correct 58 ms 58488 KB Output is correct
8 Correct 62 ms 59128 KB Output is correct
9 Correct 58 ms 58872 KB Output is correct
10 Correct 65 ms 59256 KB Output is correct
11 Correct 59 ms 58488 KB Output is correct
12 Correct 61 ms 58564 KB Output is correct
13 Correct 58 ms 58360 KB Output is correct
14 Correct 58 ms 58360 KB Output is correct
15 Correct 61 ms 58576 KB Output is correct
16 Correct 60 ms 58616 KB Output is correct
17 Correct 60 ms 58580 KB Output is correct
18 Correct 60 ms 58688 KB Output is correct
19 Correct 59 ms 58744 KB Output is correct
20 Correct 61 ms 58508 KB Output is correct
21 Correct 55 ms 58204 KB Output is correct
22 Correct 58 ms 58892 KB Output is correct
23 Correct 60 ms 58900 KB Output is correct
24 Correct 62 ms 58844 KB Output is correct
25 Correct 61 ms 58592 KB Output is correct
26 Correct 60 ms 58488 KB Output is correct
27 Correct 57 ms 58272 KB Output is correct
28 Correct 59 ms 58376 KB Output is correct
29 Correct 59 ms 58364 KB Output is correct
30 Correct 57 ms 58360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 58232 KB Output is correct
2 Correct 55 ms 58232 KB Output is correct
3 Correct 55 ms 58232 KB Output is correct
4 Correct 55 ms 58232 KB Output is correct
5 Correct 56 ms 58232 KB Output is correct
6 Correct 63 ms 58744 KB Output is correct
7 Correct 58 ms 58488 KB Output is correct
8 Correct 62 ms 59128 KB Output is correct
9 Correct 58 ms 58872 KB Output is correct
10 Correct 65 ms 59256 KB Output is correct
11 Correct 59 ms 58488 KB Output is correct
12 Correct 61 ms 58564 KB Output is correct
13 Correct 58 ms 58360 KB Output is correct
14 Correct 58 ms 58360 KB Output is correct
15 Correct 61 ms 58576 KB Output is correct
16 Correct 60 ms 58616 KB Output is correct
17 Correct 60 ms 58580 KB Output is correct
18 Correct 60 ms 58688 KB Output is correct
19 Correct 59 ms 58744 KB Output is correct
20 Correct 61 ms 58508 KB Output is correct
21 Correct 55 ms 58204 KB Output is correct
22 Correct 58 ms 58892 KB Output is correct
23 Correct 60 ms 58900 KB Output is correct
24 Correct 62 ms 58844 KB Output is correct
25 Correct 61 ms 58592 KB Output is correct
26 Correct 60 ms 58488 KB Output is correct
27 Correct 57 ms 58272 KB Output is correct
28 Correct 59 ms 58376 KB Output is correct
29 Correct 59 ms 58364 KB Output is correct
30 Correct 57 ms 58360 KB Output is correct
31 Execution timed out 5102 ms 193392 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 139 ms 123244 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 183 ms 122248 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 55 ms 58232 KB Output is correct
2 Correct 55 ms 58232 KB Output is correct
3 Correct 55 ms 58232 KB Output is correct
4 Correct 55 ms 58232 KB Output is correct
5 Correct 56 ms 58232 KB Output is correct
6 Correct 63 ms 58744 KB Output is correct
7 Correct 58 ms 58488 KB Output is correct
8 Correct 62 ms 59128 KB Output is correct
9 Correct 58 ms 58872 KB Output is correct
10 Correct 65 ms 59256 KB Output is correct
11 Correct 59 ms 58488 KB Output is correct
12 Correct 61 ms 58564 KB Output is correct
13 Correct 58 ms 58360 KB Output is correct
14 Correct 58 ms 58360 KB Output is correct
15 Correct 61 ms 58576 KB Output is correct
16 Correct 60 ms 58616 KB Output is correct
17 Correct 60 ms 58580 KB Output is correct
18 Correct 60 ms 58688 KB Output is correct
19 Correct 59 ms 58744 KB Output is correct
20 Correct 61 ms 58508 KB Output is correct
21 Correct 55 ms 58204 KB Output is correct
22 Correct 58 ms 58892 KB Output is correct
23 Correct 60 ms 58900 KB Output is correct
24 Correct 62 ms 58844 KB Output is correct
25 Correct 61 ms 58592 KB Output is correct
26 Correct 60 ms 58488 KB Output is correct
27 Correct 57 ms 58272 KB Output is correct
28 Correct 59 ms 58376 KB Output is correct
29 Correct 59 ms 58364 KB Output is correct
30 Correct 57 ms 58360 KB Output is correct
31 Execution timed out 5102 ms 193392 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 55 ms 58232 KB Output is correct
2 Correct 55 ms 58232 KB Output is correct
3 Correct 55 ms 58232 KB Output is correct
4 Correct 55 ms 58232 KB Output is correct
5 Correct 56 ms 58232 KB Output is correct
6 Correct 63 ms 58744 KB Output is correct
7 Correct 58 ms 58488 KB Output is correct
8 Correct 62 ms 59128 KB Output is correct
9 Correct 58 ms 58872 KB Output is correct
10 Correct 65 ms 59256 KB Output is correct
11 Correct 59 ms 58488 KB Output is correct
12 Correct 61 ms 58564 KB Output is correct
13 Correct 58 ms 58360 KB Output is correct
14 Correct 58 ms 58360 KB Output is correct
15 Correct 61 ms 58576 KB Output is correct
16 Correct 60 ms 58616 KB Output is correct
17 Correct 60 ms 58580 KB Output is correct
18 Correct 60 ms 58688 KB Output is correct
19 Correct 59 ms 58744 KB Output is correct
20 Correct 61 ms 58508 KB Output is correct
21 Correct 55 ms 58204 KB Output is correct
22 Correct 58 ms 58892 KB Output is correct
23 Correct 60 ms 58900 KB Output is correct
24 Correct 62 ms 58844 KB Output is correct
25 Correct 61 ms 58592 KB Output is correct
26 Correct 60 ms 58488 KB Output is correct
27 Correct 57 ms 58272 KB Output is correct
28 Correct 59 ms 58376 KB Output is correct
29 Correct 59 ms 58364 KB Output is correct
30 Correct 57 ms 58360 KB Output is correct
31 Execution timed out 5102 ms 193392 KB Time limit exceeded
32 Halted 0 ms 0 KB -