Submission #111238

# Submission time Handle Problem Language Result Execution time Memory
111238 2019-05-14T12:06:30 Z diamond_duke New Home (APIO18_new_home) C++11
57 / 100
5000 ms 190540 KB
#include <algorithm>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
constexpr int INF = 2e8 + 5;
int seg[20000005], lson[20000005], rson[20000005], t_cnt;
void modify(int &u, int l, int r, int pos, int val)
{
	if (!u)
		u = ++t_cnt;
	if (l == r)
	{
		seg[u] = val;
		return;
	}
	int m = l + r >> 1;
	if (pos <= m)
		modify(lson[u], l, m, pos, val);
	else
		modify(rson[u], m + 1, r, pos, val);
	seg[u] = std::min(seg[lson[u]], seg[rson[u]]);
}
int query(int u, int l, int r, int pos, int mn)
{
	if (l == r)
		return l;
	int m = l + r >> 1;
	if (m >= pos && std::min(mn, seg[rson[u]]) >= pos - (m - pos))
		return query(lson[u], l, m, pos, std::min(mn, seg[rson[u]]));
	return query(rson[u], m + 1, r, pos, mn);
}
struct { int pos, tp, l, r; } shop[300005];
int app[600005], ans[300005], rt;
std::multiset<int> all[300005];
std::map<int, std::multiset<int> > pre;
std::vector<int> vec[600005];
std::vector<std::pair<int, int> > qry[600005];
void add_pre(int x, int y)
{
	auto &se = pre[x];
	se.insert(y);
	modify(rt, 0, INF, x, *se.begin());
}
void del_pre(int x, int y)
{
	auto &se = pre[x];
	se.erase(se.find(y));
	modify(rt, 0, INF, x, se.empty() ? INF : *se.begin());
}
int main()
{
	// freopen("uoj-414.in", "r", stdin);
	seg[0] = INF;
	int n, k, q, cnt = 0;
	scanf("%d%d%d", &n, &k, &q);
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d%d%d", &shop[i].pos, &shop[i].tp, &shop[i].l, &shop[i].r);
		app[cnt++] = shop[i].l;
		app[cnt++] = shop[i].r + 1;
		shop[i].tp--;
	}
	std::sort(app, app + cnt);
	cnt = std::unique(app, app + cnt) - app;
	for (int i = 0; i < n; i++)
	{
		int l = std::lower_bound(app, app + cnt, shop[i].l) - app;
		int r = std::lower_bound(app, app + cnt, shop[i].r + 1) - app;
		vec[l].push_back(i);
		vec[r].push_back(~i);
	}
	for (int i = 0; i < q; i++)
	{
		int pos, t;
		scanf("%d%d", &pos, &t);
		t = std::upper_bound(app, app + cnt, t) - app - 1;
		if (t < 0)
			ans[i] = -1;
		else
			qry[t].emplace_back(pos, i);
	}
	for (int i = 0; i < k; i++)
	{
		add_pre(INF, -INF);
		all[i] = {INF};
	}
	for (int i = 0; i < cnt; i++)
	{
		for (int u : vec[i])
		{
			int x = -INF, y = INF;
			auto get_adj = [&] (std::multiset<int> &se, int pos)
			{
				auto it = se.lower_bound(pos);
				if (it != se.end())
					y = *it;
				if (it != se.begin())
					x = *--it;
			};
			if (u >= 0)
			{
				int pos = shop[u].pos;
				auto &se = all[shop[u].tp];
				get_adj(se, pos);
				se.insert(pos);
				del_pre(y, x);
				add_pre(y, pos);
				add_pre(pos, x);
			}
			else
			{
				u = ~u;
				int pos = shop[u].pos;
				auto &se = all[shop[u].tp];
				se.erase(se.find(pos));
				get_adj(se, pos);
				del_pre(y, pos);
				del_pre(pos, x);
				add_pre(y, x);
			}
		}
		for (auto &it : qry[i])
		{
			int idx = it.second, pos = it.first;
			ans[idx] = query(rt, 0, INF, pos, INF);
			if (ans[idx] == INF)
				ans[idx] = -1;
			else
				ans[idx] -= pos;
		}
	}
	for (int i = 0; i < q; i++)
		printf("%d\n", ans[i]);
	return 0;
}

Compilation message

new_home.cpp: In function 'void modify(int&, int, int, int, int)':
new_home.cpp:17:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int m = l + r >> 1;
          ~~^~~
new_home.cpp: In function 'int query(int, int, int, int, int)':
new_home.cpp:28:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  int m = l + r >> 1;
          ~~^~~
new_home.cpp: In function 'int main()':
new_home.cpp:56: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:59:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &shop[i].pos, &shop[i].tp, &shop[i].l, &shop[i].r);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
new_home.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d", &pos, &t);
   ~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 37 ms 42616 KB Output is correct
2 Correct 39 ms 42624 KB Output is correct
3 Correct 36 ms 42616 KB Output is correct
4 Correct 36 ms 42604 KB Output is correct
5 Correct 38 ms 42744 KB Output is correct
6 Correct 40 ms 42872 KB Output is correct
7 Correct 40 ms 42876 KB Output is correct
8 Correct 42 ms 43004 KB Output is correct
9 Correct 40 ms 42872 KB Output is correct
10 Correct 41 ms 42880 KB Output is correct
11 Correct 38 ms 42872 KB Output is correct
12 Correct 41 ms 42744 KB Output is correct
13 Correct 44 ms 42872 KB Output is correct
14 Correct 43 ms 42872 KB Output is correct
15 Correct 41 ms 42872 KB Output is correct
16 Correct 38 ms 42872 KB Output is correct
17 Correct 39 ms 42872 KB Output is correct
18 Correct 38 ms 42872 KB Output is correct
19 Correct 40 ms 42872 KB Output is correct
20 Correct 43 ms 42872 KB Output is correct
21 Correct 39 ms 42872 KB Output is correct
22 Correct 38 ms 43000 KB Output is correct
23 Correct 39 ms 42872 KB Output is correct
24 Correct 40 ms 42812 KB Output is correct
25 Correct 42 ms 43000 KB Output is correct
26 Correct 39 ms 42744 KB Output is correct
27 Correct 38 ms 42744 KB Output is correct
28 Correct 41 ms 42872 KB Output is correct
29 Correct 39 ms 42744 KB Output is correct
30 Correct 38 ms 42744 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 42616 KB Output is correct
2 Correct 39 ms 42624 KB Output is correct
3 Correct 36 ms 42616 KB Output is correct
4 Correct 36 ms 42604 KB Output is correct
5 Correct 38 ms 42744 KB Output is correct
6 Correct 40 ms 42872 KB Output is correct
7 Correct 40 ms 42876 KB Output is correct
8 Correct 42 ms 43004 KB Output is correct
9 Correct 40 ms 42872 KB Output is correct
10 Correct 41 ms 42880 KB Output is correct
11 Correct 38 ms 42872 KB Output is correct
12 Correct 41 ms 42744 KB Output is correct
13 Correct 44 ms 42872 KB Output is correct
14 Correct 43 ms 42872 KB Output is correct
15 Correct 41 ms 42872 KB Output is correct
16 Correct 38 ms 42872 KB Output is correct
17 Correct 39 ms 42872 KB Output is correct
18 Correct 38 ms 42872 KB Output is correct
19 Correct 40 ms 42872 KB Output is correct
20 Correct 43 ms 42872 KB Output is correct
21 Correct 39 ms 42872 KB Output is correct
22 Correct 38 ms 43000 KB Output is correct
23 Correct 39 ms 42872 KB Output is correct
24 Correct 40 ms 42812 KB Output is correct
25 Correct 42 ms 43000 KB Output is correct
26 Correct 39 ms 42744 KB Output is correct
27 Correct 38 ms 42744 KB Output is correct
28 Correct 41 ms 42872 KB Output is correct
29 Correct 39 ms 42744 KB Output is correct
30 Correct 38 ms 42744 KB Output is correct
31 Correct 857 ms 72524 KB Output is correct
32 Correct 278 ms 48760 KB Output is correct
33 Correct 900 ms 68176 KB Output is correct
34 Correct 854 ms 68344 KB Output is correct
35 Correct 992 ms 72404 KB Output is correct
36 Correct 967 ms 72204 KB Output is correct
37 Correct 564 ms 67164 KB Output is correct
38 Correct 619 ms 66936 KB Output is correct
39 Correct 355 ms 66688 KB Output is correct
40 Correct 387 ms 66724 KB Output is correct
41 Correct 526 ms 66936 KB Output is correct
42 Correct 588 ms 66936 KB Output is correct
43 Correct 213 ms 53744 KB Output is correct
44 Correct 557 ms 66936 KB Output is correct
45 Correct 493 ms 66808 KB Output is correct
46 Correct 443 ms 67060 KB Output is correct
47 Correct 291 ms 65532 KB Output is correct
48 Correct 272 ms 65528 KB Output is correct
49 Correct 297 ms 66040 KB Output is correct
50 Correct 373 ms 66504 KB Output is correct
51 Correct 305 ms 66040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4030 ms 167748 KB Output is correct
2 Correct 4681 ms 159852 KB Output is correct
3 Correct 3541 ms 190540 KB Output is correct
4 Correct 4078 ms 171424 KB Output is correct
5 Correct 4599 ms 159216 KB Output is correct
6 Correct 4665 ms 159608 KB Output is correct
7 Correct 3708 ms 190440 KB Output is correct
8 Correct 3267 ms 171376 KB Output is correct
9 Correct 2660 ms 164700 KB Output is correct
10 Correct 3209 ms 160744 KB Output is correct
11 Correct 1443 ms 157728 KB Output is correct
12 Correct 1606 ms 159936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 5043 ms 170728 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 37 ms 42616 KB Output is correct
2 Correct 39 ms 42624 KB Output is correct
3 Correct 36 ms 42616 KB Output is correct
4 Correct 36 ms 42604 KB Output is correct
5 Correct 38 ms 42744 KB Output is correct
6 Correct 40 ms 42872 KB Output is correct
7 Correct 40 ms 42876 KB Output is correct
8 Correct 42 ms 43004 KB Output is correct
9 Correct 40 ms 42872 KB Output is correct
10 Correct 41 ms 42880 KB Output is correct
11 Correct 38 ms 42872 KB Output is correct
12 Correct 41 ms 42744 KB Output is correct
13 Correct 44 ms 42872 KB Output is correct
14 Correct 43 ms 42872 KB Output is correct
15 Correct 41 ms 42872 KB Output is correct
16 Correct 38 ms 42872 KB Output is correct
17 Correct 39 ms 42872 KB Output is correct
18 Correct 38 ms 42872 KB Output is correct
19 Correct 40 ms 42872 KB Output is correct
20 Correct 43 ms 42872 KB Output is correct
21 Correct 39 ms 42872 KB Output is correct
22 Correct 38 ms 43000 KB Output is correct
23 Correct 39 ms 42872 KB Output is correct
24 Correct 40 ms 42812 KB Output is correct
25 Correct 42 ms 43000 KB Output is correct
26 Correct 39 ms 42744 KB Output is correct
27 Correct 38 ms 42744 KB Output is correct
28 Correct 41 ms 42872 KB Output is correct
29 Correct 39 ms 42744 KB Output is correct
30 Correct 38 ms 42744 KB Output is correct
31 Correct 857 ms 72524 KB Output is correct
32 Correct 278 ms 48760 KB Output is correct
33 Correct 900 ms 68176 KB Output is correct
34 Correct 854 ms 68344 KB Output is correct
35 Correct 992 ms 72404 KB Output is correct
36 Correct 967 ms 72204 KB Output is correct
37 Correct 564 ms 67164 KB Output is correct
38 Correct 619 ms 66936 KB Output is correct
39 Correct 355 ms 66688 KB Output is correct
40 Correct 387 ms 66724 KB Output is correct
41 Correct 526 ms 66936 KB Output is correct
42 Correct 588 ms 66936 KB Output is correct
43 Correct 213 ms 53744 KB Output is correct
44 Correct 557 ms 66936 KB Output is correct
45 Correct 493 ms 66808 KB Output is correct
46 Correct 443 ms 67060 KB Output is correct
47 Correct 291 ms 65532 KB Output is correct
48 Correct 272 ms 65528 KB Output is correct
49 Correct 297 ms 66040 KB Output is correct
50 Correct 373 ms 66504 KB Output is correct
51 Correct 305 ms 66040 KB Output is correct
52 Correct 967 ms 78032 KB Output is correct
53 Correct 867 ms 74488 KB Output is correct
54 Correct 959 ms 74280 KB Output is correct
55 Correct 651 ms 70540 KB Output is correct
56 Correct 747 ms 72280 KB Output is correct
57 Correct 686 ms 68300 KB Output is correct
58 Correct 812 ms 70380 KB Output is correct
59 Correct 737 ms 72076 KB Output is correct
60 Correct 646 ms 68024 KB Output is correct
61 Correct 274 ms 59604 KB Output is correct
62 Correct 878 ms 77884 KB Output is correct
63 Correct 826 ms 74460 KB Output is correct
64 Correct 923 ms 72456 KB Output is correct
65 Correct 906 ms 68728 KB Output is correct
66 Correct 669 ms 67044 KB Output is correct
67 Correct 280 ms 48888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 42616 KB Output is correct
2 Correct 39 ms 42624 KB Output is correct
3 Correct 36 ms 42616 KB Output is correct
4 Correct 36 ms 42604 KB Output is correct
5 Correct 38 ms 42744 KB Output is correct
6 Correct 40 ms 42872 KB Output is correct
7 Correct 40 ms 42876 KB Output is correct
8 Correct 42 ms 43004 KB Output is correct
9 Correct 40 ms 42872 KB Output is correct
10 Correct 41 ms 42880 KB Output is correct
11 Correct 38 ms 42872 KB Output is correct
12 Correct 41 ms 42744 KB Output is correct
13 Correct 44 ms 42872 KB Output is correct
14 Correct 43 ms 42872 KB Output is correct
15 Correct 41 ms 42872 KB Output is correct
16 Correct 38 ms 42872 KB Output is correct
17 Correct 39 ms 42872 KB Output is correct
18 Correct 38 ms 42872 KB Output is correct
19 Correct 40 ms 42872 KB Output is correct
20 Correct 43 ms 42872 KB Output is correct
21 Correct 39 ms 42872 KB Output is correct
22 Correct 38 ms 43000 KB Output is correct
23 Correct 39 ms 42872 KB Output is correct
24 Correct 40 ms 42812 KB Output is correct
25 Correct 42 ms 43000 KB Output is correct
26 Correct 39 ms 42744 KB Output is correct
27 Correct 38 ms 42744 KB Output is correct
28 Correct 41 ms 42872 KB Output is correct
29 Correct 39 ms 42744 KB Output is correct
30 Correct 38 ms 42744 KB Output is correct
31 Correct 857 ms 72524 KB Output is correct
32 Correct 278 ms 48760 KB Output is correct
33 Correct 900 ms 68176 KB Output is correct
34 Correct 854 ms 68344 KB Output is correct
35 Correct 992 ms 72404 KB Output is correct
36 Correct 967 ms 72204 KB Output is correct
37 Correct 564 ms 67164 KB Output is correct
38 Correct 619 ms 66936 KB Output is correct
39 Correct 355 ms 66688 KB Output is correct
40 Correct 387 ms 66724 KB Output is correct
41 Correct 526 ms 66936 KB Output is correct
42 Correct 588 ms 66936 KB Output is correct
43 Correct 213 ms 53744 KB Output is correct
44 Correct 557 ms 66936 KB Output is correct
45 Correct 493 ms 66808 KB Output is correct
46 Correct 443 ms 67060 KB Output is correct
47 Correct 291 ms 65532 KB Output is correct
48 Correct 272 ms 65528 KB Output is correct
49 Correct 297 ms 66040 KB Output is correct
50 Correct 373 ms 66504 KB Output is correct
51 Correct 305 ms 66040 KB Output is correct
52 Correct 4030 ms 167748 KB Output is correct
53 Correct 4681 ms 159852 KB Output is correct
54 Correct 3541 ms 190540 KB Output is correct
55 Correct 4078 ms 171424 KB Output is correct
56 Correct 4599 ms 159216 KB Output is correct
57 Correct 4665 ms 159608 KB Output is correct
58 Correct 3708 ms 190440 KB Output is correct
59 Correct 3267 ms 171376 KB Output is correct
60 Correct 2660 ms 164700 KB Output is correct
61 Correct 3209 ms 160744 KB Output is correct
62 Correct 1443 ms 157728 KB Output is correct
63 Correct 1606 ms 159936 KB Output is correct
64 Execution timed out 5043 ms 170728 KB Time limit exceeded
65 Halted 0 ms 0 KB -