Submission #172053

# Submission time Handle Problem Language Result Execution time Memory
172053 2019-12-31T02:33:22 Z AngusRitossa New Home (APIO18_new_home) C++14
5 / 100
5000 ms 873540 KB
#include <bits/stdc++.h>
using namespace std;
#define MXN 300010
#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 = { 1e9, -1e9 };
	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)
{
	D("\nbeginning query %d\n", 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({ -1e9, -1 }), shops[i].insert({ 1e9, -2 });
		// dumbie stuff
		addsegment(-1e9, 0, {-1e9, -1}, 0);
		addsegment(0, 1e9, {1e9, -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:99: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:109: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:115: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 997 ms 578252 KB Output is correct
2 Correct 525 ms 578040 KB Output is correct
3 Correct 522 ms 578044 KB Output is correct
4 Correct 545 ms 578120 KB Output is correct
5 Correct 524 ms 578168 KB Output is correct
6 Correct 529 ms 578760 KB Output is correct
7 Correct 524 ms 578572 KB Output is correct
8 Correct 537 ms 579064 KB Output is correct
9 Correct 529 ms 578760 KB Output is correct
10 Correct 593 ms 579320 KB Output is correct
11 Correct 558 ms 578464 KB Output is correct
12 Correct 526 ms 578612 KB Output is correct
13 Correct 525 ms 578248 KB Output is correct
14 Correct 522 ms 578544 KB Output is correct
15 Correct 527 ms 578680 KB Output is correct
16 Correct 531 ms 578692 KB Output is correct
17 Correct 529 ms 578552 KB Output is correct
18 Correct 526 ms 578552 KB Output is correct
19 Correct 526 ms 578732 KB Output is correct
20 Correct 530 ms 578488 KB Output is correct
21 Correct 527 ms 578184 KB Output is correct
22 Correct 532 ms 578776 KB Output is correct
23 Correct 529 ms 578832 KB Output is correct
24 Correct 531 ms 578780 KB Output is correct
25 Correct 525 ms 578552 KB Output is correct
26 Correct 574 ms 578396 KB Output is correct
27 Correct 523 ms 578212 KB Output is correct
28 Correct 521 ms 578300 KB Output is correct
29 Correct 543 ms 578344 KB Output is correct
30 Correct 535 ms 578296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 997 ms 578252 KB Output is correct
2 Correct 525 ms 578040 KB Output is correct
3 Correct 522 ms 578044 KB Output is correct
4 Correct 545 ms 578120 KB Output is correct
5 Correct 524 ms 578168 KB Output is correct
6 Correct 529 ms 578760 KB Output is correct
7 Correct 524 ms 578572 KB Output is correct
8 Correct 537 ms 579064 KB Output is correct
9 Correct 529 ms 578760 KB Output is correct
10 Correct 593 ms 579320 KB Output is correct
11 Correct 558 ms 578464 KB Output is correct
12 Correct 526 ms 578612 KB Output is correct
13 Correct 525 ms 578248 KB Output is correct
14 Correct 522 ms 578544 KB Output is correct
15 Correct 527 ms 578680 KB Output is correct
16 Correct 531 ms 578692 KB Output is correct
17 Correct 529 ms 578552 KB Output is correct
18 Correct 526 ms 578552 KB Output is correct
19 Correct 526 ms 578732 KB Output is correct
20 Correct 530 ms 578488 KB Output is correct
21 Correct 527 ms 578184 KB Output is correct
22 Correct 532 ms 578776 KB Output is correct
23 Correct 529 ms 578832 KB Output is correct
24 Correct 531 ms 578780 KB Output is correct
25 Correct 525 ms 578552 KB Output is correct
26 Correct 574 ms 578396 KB Output is correct
27 Correct 523 ms 578212 KB Output is correct
28 Correct 521 ms 578300 KB Output is correct
29 Correct 543 ms 578344 KB Output is correct
30 Correct 535 ms 578296 KB Output is correct
31 Execution timed out 5015 ms 713724 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5096 ms 873540 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5061 ms 813476 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 997 ms 578252 KB Output is correct
2 Correct 525 ms 578040 KB Output is correct
3 Correct 522 ms 578044 KB Output is correct
4 Correct 545 ms 578120 KB Output is correct
5 Correct 524 ms 578168 KB Output is correct
6 Correct 529 ms 578760 KB Output is correct
7 Correct 524 ms 578572 KB Output is correct
8 Correct 537 ms 579064 KB Output is correct
9 Correct 529 ms 578760 KB Output is correct
10 Correct 593 ms 579320 KB Output is correct
11 Correct 558 ms 578464 KB Output is correct
12 Correct 526 ms 578612 KB Output is correct
13 Correct 525 ms 578248 KB Output is correct
14 Correct 522 ms 578544 KB Output is correct
15 Correct 527 ms 578680 KB Output is correct
16 Correct 531 ms 578692 KB Output is correct
17 Correct 529 ms 578552 KB Output is correct
18 Correct 526 ms 578552 KB Output is correct
19 Correct 526 ms 578732 KB Output is correct
20 Correct 530 ms 578488 KB Output is correct
21 Correct 527 ms 578184 KB Output is correct
22 Correct 532 ms 578776 KB Output is correct
23 Correct 529 ms 578832 KB Output is correct
24 Correct 531 ms 578780 KB Output is correct
25 Correct 525 ms 578552 KB Output is correct
26 Correct 574 ms 578396 KB Output is correct
27 Correct 523 ms 578212 KB Output is correct
28 Correct 521 ms 578300 KB Output is correct
29 Correct 543 ms 578344 KB Output is correct
30 Correct 535 ms 578296 KB Output is correct
31 Execution timed out 5015 ms 713724 KB Time limit exceeded
32 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 997 ms 578252 KB Output is correct
2 Correct 525 ms 578040 KB Output is correct
3 Correct 522 ms 578044 KB Output is correct
4 Correct 545 ms 578120 KB Output is correct
5 Correct 524 ms 578168 KB Output is correct
6 Correct 529 ms 578760 KB Output is correct
7 Correct 524 ms 578572 KB Output is correct
8 Correct 537 ms 579064 KB Output is correct
9 Correct 529 ms 578760 KB Output is correct
10 Correct 593 ms 579320 KB Output is correct
11 Correct 558 ms 578464 KB Output is correct
12 Correct 526 ms 578612 KB Output is correct
13 Correct 525 ms 578248 KB Output is correct
14 Correct 522 ms 578544 KB Output is correct
15 Correct 527 ms 578680 KB Output is correct
16 Correct 531 ms 578692 KB Output is correct
17 Correct 529 ms 578552 KB Output is correct
18 Correct 526 ms 578552 KB Output is correct
19 Correct 526 ms 578732 KB Output is correct
20 Correct 530 ms 578488 KB Output is correct
21 Correct 527 ms 578184 KB Output is correct
22 Correct 532 ms 578776 KB Output is correct
23 Correct 529 ms 578832 KB Output is correct
24 Correct 531 ms 578780 KB Output is correct
25 Correct 525 ms 578552 KB Output is correct
26 Correct 574 ms 578396 KB Output is correct
27 Correct 523 ms 578212 KB Output is correct
28 Correct 521 ms 578300 KB Output is correct
29 Correct 543 ms 578344 KB Output is correct
30 Correct 535 ms 578296 KB Output is correct
31 Execution timed out 5015 ms 713724 KB Time limit exceeded
32 Halted 0 ms 0 KB -