답안 #466283

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
466283 2021-08-18T12:52:21 Z nonsensenonsense1 무지개나라 (APIO17_rainbow) C++17
100 / 100
1846 ms 96532 KB
#include "rainbow.h"
#include <algorithm>
#include <set>
#include <utility>
#include <vector>

const int N = 200005;
const int T = N * 400;
int size = N << 1, val[T], left[T];
std::vector<int> edge_h[N], edge_v[N], pt_h[N], pt_v[N];

void update_y(int ind, int i, int d, int tl = 0, int tr = N) 
{
	if (tr - tl != 1) {
		if (!left[ind]) {
			left[ind] = size;
			size += 2;
		}
		int m = tl + tr >> 1;
		if (i < m) update_y(left[ind], i, d, tl, m);
		else update_y(left[ind] | 1, i, d, m, tr);
	}
	val[ind] += d;
}

void update_x(int x, int y, int d) 
{
	for (x += N; x > 0; x >>= 1) update_y(x, y, d);
}

int query_y(int ind, int l, int r, int tl = 0, int tr = N) 
{
	if (tl >= r || tr <= l) return 0;
	if (tl >= l && tr <= r) return val[ind];
	if (!left[ind]) return 0;
	int m = tl + tr >> 1;
	return query_y(left[ind], l, r, tl, m) + query_y(left[ind] | 1, l, r, m, tr);
}

int query_x(int x1, int x2, int y1, int y2) 
{
	int ans = 0;
	for (x1 += N, x2 += N; x1 < x2; x1 >>= 1, x2 >>= 1) {
		if (x1 & 1) ans += query_y(x1++, y1, y2);
		if (x2 & 1) ans += query_y(--x2, y1, y2);
	}
	return ans;
}

void add(int x, int y, bool type) 
{
	if (type) edge_h[x].push_back(y);
	else edge_v[y].push_back(x);
	update_x(x, y, 1);
}

void add_point(int x, int y) 
{
	pt_h[x].push_back(y);
	pt_v[y].push_back(x);
	update_x(x, y, -1);
}

int start_x, start_y;

void init(int r, int c, int sr, int sc, int m, char *s) 
{
	--sr;
	--sc;
	start_x = sr;
	start_y = sc;
	std::set<std::pair<int, int> > u;
	for (int i = 0; i <= m; ++i) {
		if (!u.count(std::make_pair(sr, sc))) {
			u.insert(std::make_pair(sr, sc));
			update_x(sr, sc, -1);
			bool u_left = u.count(std::make_pair(sr, sc - 1)), u_up = u.count(std::make_pair(sr - 1, sc)), u_right = u.count(std::make_pair(sr, sc + 1)), u_down = u.count(std::make_pair(sr + 1, sc));
			if (!u_left) {
				add(sr, sc, 0);
				if (!u_up && !u.count(std::make_pair(sr - 1, sc - 1))) add_point(sr, sc);
			}
			if (!u_up) {
				add(sr, sc, 1);
				if (!u_right && !u.count(std::make_pair(sr - 1, sc + 1))) add_point(sr, sc + 1);
			}
			if (!u_right) {
				add(sr, sc + 1, 0);
				if (!u_down && !u.count(std::make_pair(sr + 1, sc + 1))) add_point(sr + 1, sc + 1);
			}
			if (!u_down) {
				add(sr + 1, sc, 1);
				if (!u_left && !u.count(std::make_pair(sr + 1, sc - 1))) add_point(sr + 1, sc);
			}
		}
		if (i < m) {
			if (s[i] == 'N') --sr;
			else if (s[i] == 'S') ++sr;
			else if (s[i] == 'W') --sc;
			else ++sc;
		}
	}
	for (int i = 0; i < N; ++i) {
		std::sort(edge_h[i].begin(), edge_h[i].end());
		std::sort(edge_v[i].begin(), edge_v[i].end());
		std::sort(pt_h[i].begin(), pt_h[i].end());
		std::sort(pt_v[i].begin(), pt_v[i].end());
	}
}

inline int count(std::vector<int> &v, int l, int r) 
{
	return std::lower_bound(v.begin(), v.end(), r) - std::lower_bound(v.begin(), v.end(), l);
}

int colour(int ar, int ac, int br, int bc) 
{
	--ar;
	--ac;
	return query_x(ar, br, ac, bc) - count(edge_h[ar], ac, bc) - count(edge_v[ac], ar, br) + count(pt_h[ar], ac, bc) + count(pt_v[ac], ar + 1, br) + 1 + (pt_h[ar].empty() && pt_h[br].empty() && pt_v[ac].empty() && pt_v[bc].empty() && start_x >= ar && start_x < br && start_y >= ac && start_y < bc);
}

Compilation message

rainbow.cpp: In function 'void update_y(int, int, int, int, int)':
rainbow.cpp:19:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   19 |   int m = tl + tr >> 1;
      |           ~~~^~~~
rainbow.cpp: In function 'int query_y(int, int, int, int, int)':
rainbow.cpp:36:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   36 |  int m = tl + tr >> 1;
      |          ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19148 KB Output is correct
2 Correct 24 ms 19328 KB Output is correct
3 Correct 18 ms 19216 KB Output is correct
4 Correct 19 ms 19292 KB Output is correct
5 Correct 26 ms 19412 KB Output is correct
6 Correct 12 ms 19076 KB Output is correct
7 Correct 12 ms 19160 KB Output is correct
8 Correct 12 ms 19148 KB Output is correct
9 Correct 12 ms 19096 KB Output is correct
10 Correct 12 ms 19148 KB Output is correct
11 Correct 17 ms 19304 KB Output is correct
12 Correct 21 ms 19216 KB Output is correct
13 Correct 32 ms 19404 KB Output is correct
14 Correct 34 ms 19460 KB Output is correct
15 Correct 13 ms 19148 KB Output is correct
16 Correct 14 ms 19184 KB Output is correct
17 Correct 12 ms 19084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 19184 KB Output is correct
2 Correct 12 ms 19084 KB Output is correct
3 Correct 1182 ms 45680 KB Output is correct
4 Correct 1846 ms 62564 KB Output is correct
5 Correct 1835 ms 60228 KB Output is correct
6 Correct 1377 ms 44568 KB Output is correct
7 Correct 1410 ms 44100 KB Output is correct
8 Correct 99 ms 20036 KB Output is correct
9 Correct 1833 ms 62504 KB Output is correct
10 Correct 1813 ms 60124 KB Output is correct
11 Correct 1420 ms 44544 KB Output is correct
12 Correct 1615 ms 59928 KB Output is correct
13 Correct 1736 ms 62696 KB Output is correct
14 Correct 1778 ms 60328 KB Output is correct
15 Correct 1302 ms 44732 KB Output is correct
16 Correct 1277 ms 50464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 19148 KB Output is correct
2 Correct 1707 ms 63168 KB Output is correct
3 Correct 1608 ms 92844 KB Output is correct
4 Correct 1625 ms 75856 KB Output is correct
5 Correct 1326 ms 65132 KB Output is correct
6 Correct 321 ms 22584 KB Output is correct
7 Correct 621 ms 25428 KB Output is correct
8 Correct 1506 ms 44404 KB Output is correct
9 Correct 1326 ms 40772 KB Output is correct
10 Correct 323 ms 23060 KB Output is correct
11 Correct 856 ms 37776 KB Output is correct
12 Correct 1711 ms 63200 KB Output is correct
13 Correct 1592 ms 92888 KB Output is correct
14 Correct 1560 ms 75960 KB Output is correct
15 Correct 1310 ms 65196 KB Output is correct
16 Correct 278 ms 21972 KB Output is correct
17 Correct 573 ms 25764 KB Output is correct
18 Correct 1468 ms 34236 KB Output is correct
19 Correct 1590 ms 75752 KB Output is correct
20 Correct 1568 ms 75680 KB Output is correct
21 Correct 1441 ms 44332 KB Output is correct
22 Correct 1306 ms 40832 KB Output is correct
23 Correct 316 ms 23064 KB Output is correct
24 Correct 851 ms 37776 KB Output is correct
25 Correct 1709 ms 63124 KB Output is correct
26 Correct 1610 ms 92964 KB Output is correct
27 Correct 1577 ms 75932 KB Output is correct
28 Correct 1295 ms 65192 KB Output is correct
29 Correct 268 ms 22016 KB Output is correct
30 Correct 572 ms 25824 KB Output is correct
31 Correct 1454 ms 34084 KB Output is correct
32 Correct 1566 ms 75684 KB Output is correct
33 Correct 1529 ms 75816 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19148 KB Output is correct
2 Correct 24 ms 19328 KB Output is correct
3 Correct 18 ms 19216 KB Output is correct
4 Correct 19 ms 19292 KB Output is correct
5 Correct 26 ms 19412 KB Output is correct
6 Correct 12 ms 19076 KB Output is correct
7 Correct 12 ms 19160 KB Output is correct
8 Correct 12 ms 19148 KB Output is correct
9 Correct 12 ms 19096 KB Output is correct
10 Correct 12 ms 19148 KB Output is correct
11 Correct 17 ms 19304 KB Output is correct
12 Correct 21 ms 19216 KB Output is correct
13 Correct 32 ms 19404 KB Output is correct
14 Correct 34 ms 19460 KB Output is correct
15 Correct 13 ms 19148 KB Output is correct
16 Correct 14 ms 19184 KB Output is correct
17 Correct 12 ms 19084 KB Output is correct
18 Correct 1151 ms 36496 KB Output is correct
19 Correct 181 ms 23372 KB Output is correct
20 Correct 155 ms 22680 KB Output is correct
21 Correct 193 ms 22928 KB Output is correct
22 Correct 192 ms 23164 KB Output is correct
23 Correct 182 ms 23380 KB Output is correct
24 Correct 171 ms 22788 KB Output is correct
25 Correct 190 ms 23044 KB Output is correct
26 Correct 196 ms 23108 KB Output is correct
27 Correct 734 ms 29888 KB Output is correct
28 Correct 432 ms 26276 KB Output is correct
29 Correct 694 ms 28872 KB Output is correct
30 Correct 1741 ms 37544 KB Output is correct
31 Correct 15 ms 19216 KB Output is correct
32 Correct 886 ms 29168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 19148 KB Output is correct
2 Correct 24 ms 19328 KB Output is correct
3 Correct 18 ms 19216 KB Output is correct
4 Correct 19 ms 19292 KB Output is correct
5 Correct 26 ms 19412 KB Output is correct
6 Correct 12 ms 19076 KB Output is correct
7 Correct 12 ms 19160 KB Output is correct
8 Correct 12 ms 19148 KB Output is correct
9 Correct 12 ms 19096 KB Output is correct
10 Correct 12 ms 19148 KB Output is correct
11 Correct 17 ms 19304 KB Output is correct
12 Correct 21 ms 19216 KB Output is correct
13 Correct 32 ms 19404 KB Output is correct
14 Correct 34 ms 19460 KB Output is correct
15 Correct 13 ms 19148 KB Output is correct
16 Correct 14 ms 19184 KB Output is correct
17 Correct 12 ms 19084 KB Output is correct
18 Correct 1151 ms 36496 KB Output is correct
19 Correct 181 ms 23372 KB Output is correct
20 Correct 155 ms 22680 KB Output is correct
21 Correct 193 ms 22928 KB Output is correct
22 Correct 192 ms 23164 KB Output is correct
23 Correct 182 ms 23380 KB Output is correct
24 Correct 171 ms 22788 KB Output is correct
25 Correct 190 ms 23044 KB Output is correct
26 Correct 196 ms 23108 KB Output is correct
27 Correct 734 ms 29888 KB Output is correct
28 Correct 432 ms 26276 KB Output is correct
29 Correct 694 ms 28872 KB Output is correct
30 Correct 1741 ms 37544 KB Output is correct
31 Correct 15 ms 19216 KB Output is correct
32 Correct 886 ms 29168 KB Output is correct
33 Correct 1707 ms 63168 KB Output is correct
34 Correct 1608 ms 92844 KB Output is correct
35 Correct 1625 ms 75856 KB Output is correct
36 Correct 1326 ms 65132 KB Output is correct
37 Correct 321 ms 22584 KB Output is correct
38 Correct 621 ms 25428 KB Output is correct
39 Correct 1506 ms 44404 KB Output is correct
40 Correct 1326 ms 40772 KB Output is correct
41 Correct 323 ms 23060 KB Output is correct
42 Correct 856 ms 37776 KB Output is correct
43 Correct 1711 ms 63200 KB Output is correct
44 Correct 1592 ms 92888 KB Output is correct
45 Correct 1560 ms 75960 KB Output is correct
46 Correct 1310 ms 65196 KB Output is correct
47 Correct 278 ms 21972 KB Output is correct
48 Correct 573 ms 25764 KB Output is correct
49 Correct 1468 ms 34236 KB Output is correct
50 Correct 1590 ms 75752 KB Output is correct
51 Correct 1568 ms 75680 KB Output is correct
52 Correct 1441 ms 44332 KB Output is correct
53 Correct 1306 ms 40832 KB Output is correct
54 Correct 316 ms 23064 KB Output is correct
55 Correct 851 ms 37776 KB Output is correct
56 Correct 1709 ms 63124 KB Output is correct
57 Correct 1610 ms 92964 KB Output is correct
58 Correct 1577 ms 75932 KB Output is correct
59 Correct 1295 ms 65192 KB Output is correct
60 Correct 268 ms 22016 KB Output is correct
61 Correct 572 ms 25824 KB Output is correct
62 Correct 1454 ms 34084 KB Output is correct
63 Correct 1566 ms 75684 KB Output is correct
64 Correct 1529 ms 75816 KB Output is correct
65 Correct 1182 ms 45680 KB Output is correct
66 Correct 1846 ms 62564 KB Output is correct
67 Correct 1835 ms 60228 KB Output is correct
68 Correct 1377 ms 44568 KB Output is correct
69 Correct 1410 ms 44100 KB Output is correct
70 Correct 99 ms 20036 KB Output is correct
71 Correct 1833 ms 62504 KB Output is correct
72 Correct 1813 ms 60124 KB Output is correct
73 Correct 1420 ms 44544 KB Output is correct
74 Correct 1615 ms 59928 KB Output is correct
75 Correct 1736 ms 62696 KB Output is correct
76 Correct 1778 ms 60328 KB Output is correct
77 Correct 1302 ms 44732 KB Output is correct
78 Correct 1277 ms 50464 KB Output is correct
79 Correct 1769 ms 47848 KB Output is correct
80 Correct 1581 ms 44152 KB Output is correct
81 Correct 459 ms 26552 KB Output is correct
82 Correct 1022 ms 41280 KB Output is correct
83 Correct 1842 ms 66960 KB Output is correct
84 Correct 1826 ms 96532 KB Output is correct
85 Correct 1712 ms 79604 KB Output is correct
86 Correct 1415 ms 68716 KB Output is correct
87 Correct 371 ms 25576 KB Output is correct
88 Correct 708 ms 29268 KB Output is correct
89 Correct 1551 ms 37760 KB Output is correct
90 Correct 1740 ms 79216 KB Output is correct
91 Correct 1795 ms 79168 KB Output is correct