답안 #172052

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
172052 2019-12-31T02:32:43 Z AngusRitossa 새 집 (APIO18_new_home) C++14
5 / 100
21 ms 13304 KB
#include <bits/stdc++.h>
using namespace std;
#define MXN 3010
#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);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 6136 KB Output is correct
2 Correct 7 ms 6012 KB Output is correct
3 Correct 8 ms 6136 KB Output is correct
4 Correct 7 ms 6008 KB Output is correct
5 Correct 9 ms 6264 KB Output is correct
6 Correct 14 ms 6776 KB Output is correct
7 Correct 10 ms 6520 KB Output is correct
8 Correct 14 ms 7160 KB Output is correct
9 Correct 11 ms 6904 KB Output is correct
10 Correct 16 ms 7288 KB Output is correct
11 Correct 11 ms 6520 KB Output is correct
12 Correct 14 ms 6520 KB Output is correct
13 Correct 9 ms 6392 KB Output is correct
14 Correct 10 ms 6392 KB Output is correct
15 Correct 13 ms 6648 KB Output is correct
16 Correct 12 ms 6776 KB Output is correct
17 Correct 13 ms 6520 KB Output is correct
18 Correct 13 ms 6648 KB Output is correct
19 Correct 12 ms 6776 KB Output is correct
20 Correct 13 ms 6520 KB Output is correct
21 Correct 8 ms 6264 KB Output is correct
22 Correct 11 ms 6876 KB Output is correct
23 Correct 12 ms 6904 KB Output is correct
24 Correct 15 ms 6904 KB Output is correct
25 Correct 13 ms 6520 KB Output is correct
26 Correct 18 ms 6520 KB Output is correct
27 Correct 12 ms 6264 KB Output is correct
28 Correct 11 ms 6392 KB Output is correct
29 Correct 11 ms 6392 KB Output is correct
30 Correct 9 ms 6264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 6136 KB Output is correct
2 Correct 7 ms 6012 KB Output is correct
3 Correct 8 ms 6136 KB Output is correct
4 Correct 7 ms 6008 KB Output is correct
5 Correct 9 ms 6264 KB Output is correct
6 Correct 14 ms 6776 KB Output is correct
7 Correct 10 ms 6520 KB Output is correct
8 Correct 14 ms 7160 KB Output is correct
9 Correct 11 ms 6904 KB Output is correct
10 Correct 16 ms 7288 KB Output is correct
11 Correct 11 ms 6520 KB Output is correct
12 Correct 14 ms 6520 KB Output is correct
13 Correct 9 ms 6392 KB Output is correct
14 Correct 10 ms 6392 KB Output is correct
15 Correct 13 ms 6648 KB Output is correct
16 Correct 12 ms 6776 KB Output is correct
17 Correct 13 ms 6520 KB Output is correct
18 Correct 13 ms 6648 KB Output is correct
19 Correct 12 ms 6776 KB Output is correct
20 Correct 13 ms 6520 KB Output is correct
21 Correct 8 ms 6264 KB Output is correct
22 Correct 11 ms 6876 KB Output is correct
23 Correct 12 ms 6904 KB Output is correct
24 Correct 15 ms 6904 KB Output is correct
25 Correct 13 ms 6520 KB Output is correct
26 Correct 18 ms 6520 KB Output is correct
27 Correct 12 ms 6264 KB Output is correct
28 Correct 11 ms 6392 KB Output is correct
29 Correct 11 ms 6392 KB Output is correct
30 Correct 9 ms 6264 KB Output is correct
31 Runtime error 21 ms 13304 KB Execution killed with signal 11 (could be triggered by violating memory limits)
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 15 ms 12796 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 15 ms 12792 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 6136 KB Output is correct
2 Correct 7 ms 6012 KB Output is correct
3 Correct 8 ms 6136 KB Output is correct
4 Correct 7 ms 6008 KB Output is correct
5 Correct 9 ms 6264 KB Output is correct
6 Correct 14 ms 6776 KB Output is correct
7 Correct 10 ms 6520 KB Output is correct
8 Correct 14 ms 7160 KB Output is correct
9 Correct 11 ms 6904 KB Output is correct
10 Correct 16 ms 7288 KB Output is correct
11 Correct 11 ms 6520 KB Output is correct
12 Correct 14 ms 6520 KB Output is correct
13 Correct 9 ms 6392 KB Output is correct
14 Correct 10 ms 6392 KB Output is correct
15 Correct 13 ms 6648 KB Output is correct
16 Correct 12 ms 6776 KB Output is correct
17 Correct 13 ms 6520 KB Output is correct
18 Correct 13 ms 6648 KB Output is correct
19 Correct 12 ms 6776 KB Output is correct
20 Correct 13 ms 6520 KB Output is correct
21 Correct 8 ms 6264 KB Output is correct
22 Correct 11 ms 6876 KB Output is correct
23 Correct 12 ms 6904 KB Output is correct
24 Correct 15 ms 6904 KB Output is correct
25 Correct 13 ms 6520 KB Output is correct
26 Correct 18 ms 6520 KB Output is correct
27 Correct 12 ms 6264 KB Output is correct
28 Correct 11 ms 6392 KB Output is correct
29 Correct 11 ms 6392 KB Output is correct
30 Correct 9 ms 6264 KB Output is correct
31 Runtime error 21 ms 13304 KB Execution killed with signal 11 (could be triggered by violating memory limits)
32 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 6136 KB Output is correct
2 Correct 7 ms 6012 KB Output is correct
3 Correct 8 ms 6136 KB Output is correct
4 Correct 7 ms 6008 KB Output is correct
5 Correct 9 ms 6264 KB Output is correct
6 Correct 14 ms 6776 KB Output is correct
7 Correct 10 ms 6520 KB Output is correct
8 Correct 14 ms 7160 KB Output is correct
9 Correct 11 ms 6904 KB Output is correct
10 Correct 16 ms 7288 KB Output is correct
11 Correct 11 ms 6520 KB Output is correct
12 Correct 14 ms 6520 KB Output is correct
13 Correct 9 ms 6392 KB Output is correct
14 Correct 10 ms 6392 KB Output is correct
15 Correct 13 ms 6648 KB Output is correct
16 Correct 12 ms 6776 KB Output is correct
17 Correct 13 ms 6520 KB Output is correct
18 Correct 13 ms 6648 KB Output is correct
19 Correct 12 ms 6776 KB Output is correct
20 Correct 13 ms 6520 KB Output is correct
21 Correct 8 ms 6264 KB Output is correct
22 Correct 11 ms 6876 KB Output is correct
23 Correct 12 ms 6904 KB Output is correct
24 Correct 15 ms 6904 KB Output is correct
25 Correct 13 ms 6520 KB Output is correct
26 Correct 18 ms 6520 KB Output is correct
27 Correct 12 ms 6264 KB Output is correct
28 Correct 11 ms 6392 KB Output is correct
29 Correct 11 ms 6392 KB Output is correct
30 Correct 9 ms 6264 KB Output is correct
31 Runtime error 21 ms 13304 KB Execution killed with signal 11 (could be triggered by violating memory limits)
32 Halted 0 ms 0 KB -