답안 #274279

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
274279 2020-08-19T10:45:14 Z srvlt 무지개나라 (APIO17_rainbow) C++14
100 / 100
1691 ms 304704 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define all(x) begin(x), end(x)
#define SZ(x) (int)(x).size()
#define cps(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#include "rainbow.h"

int r, c;
map <array <int, 2>, int> used[3];

struct Node {
	int n;
	vector <int> xv, val, val_cell;
	vector <array <int, 2>> pt;
	void add(int x, int v) {
		xv.pb(x);
		pt.pb({x, v});
	}
	int pos(int x) {
		return (int)(lower_bound(all(xv), x) - begin(xv));
	}
	void build() {
		cps(xv);
		n = SZ(xv);
		val.resize(n), val_cell.resize(n);
		for (auto & i : pt) {
			int x = pos(i[0]);
			if (i[1] == 0) val[x]--;
			if (i[1] == 1) val[x]++;
			if (i[1] == 2) val_cell[x]++;
		}
		for (int i = 1; i < n; i++) {
			val[i] += val[i - 1];
			val_cell[i] += val_cell[i - 1];
		}
	}
	int get(int l, int r) {
		l = pos(l), r = pos(r + 1) - 1;
		if (l > r) return 0;
		if (l == 0) return val[r];
		return val[r] - val[l - 1];
	}
	int get_cell(int l, int r) {
		l = pos(l), r = pos(r + 1) - 1;
		if (l > r) return 0;
		if (l == 0) return val_cell[r];
		return val_cell[r] - val_cell[l - 1];
	}
};

struct SegTree {
	int n;
	vector <int> xv;
	vector <array <int, 3>> pt;
	vector <Node> nd;
	int pos(int x) {
		return (int)(lower_bound(all(xv), x) - xv.begin());
	}
	void add(int x, int y, int v) {
		for (x += n; x > 0; x >>= 1) nd[x].add(y, v);
	}
	void build() {
		for (auto & i : pt) xv.pb(i[0]);
		cps(xv);
		n = 1;
		while (n < SZ(xv)) n <<= 1;
		nd.resize(n << 1);
		for (auto & i : pt) {
			int x = pos(i[0]);
			add(x, i[1], i[2]);
		}
		for (int i = 1; i < (n << 1); i++) nd[i].build();
	}
	int get(int l, int r, int l2, int r2) {
		l = pos(l), r = pos(r + 1);
		if (l > r || l2 > r2) return 0;
		int res = 0;
		for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if (r & 1) res += nd[--r].get(l2, r2);
			if (l & 1) res += nd[l++].get(l2, r2);
		}
		return res;
	}
	int get_cell(int l, int r, int l2, int r2) {
		l = pos(l), r = pos(r + 1);
		if (l > r || l2 > r2) return 0;
		int res = 0;
		for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if (r & 1) res += nd[--r].get_cell(l2, r2);
			if (l & 1) res += nd[l++].get_cell(l2, r2);
		}
		return res;
	}
};
SegTree plane;

void set_pt(int x, int y) {
	if (used[0].count({x, y})) return;
	used[0][{x, y}] = 1;
	plane.pt.pb({x, y, 0});
}

void set_edge(int x, int y) {
	if (used[1].count({x, y})) return;
	used[1][{x, y}] = 1;
	plane.pt.pb({x, y, 1});
}

void set_cell(int x, int y) {
	if (used[2].count({x, y})) return;
	used[2][{x, y}] = 1;
	plane.pt.pb({x, y, 2});
}

void add(int x, int y) {
	x *= 2, y *= 2;
	set_cell(x - 1, y - 1);
	set_pt(x, y), set_pt(x - 2, y), set_pt(x, y - 2), set_pt(x - 2, y - 2);
	set_edge(x, y - 1), set_edge(x - 1, y), set_edge(x - 1, y - 2), set_edge(x - 2, y - 1);
}

void init(int R, int C, int sr, int sc, int M, char *S) {
	r = R, c = C;
	int x = sr, y = sc;
	add(x, y);
	for (int i = 0; i < M; i++) {
		if (S[i] == 'N') x--;
		if (S[i] == 'S') x++;
		if (S[i] == 'W') y--;
		if (S[i] == 'E') y++;
		add(x, y);
	}
	plane.build();
}

int colour(int ar, int ac, int br, int bc) {
	ar--, ac--;
	ar *= 2, ac *= 2, br *= 2, bc *= 2;
	int cells = plane.get_cell(ar, br, ac, bc);
	int nb_cells = plane.get_cell(ar + 2, br - 2, ac + 2, bc - 2);
	int C = 2;
	if (cells > nb_cells || cells == 0) C = 1;
	int ve = plane.get(ar + 1, br - 1, ac + 1, bc - 1);
	int res = C + plane.get(ar + 1, br - 1, ac + 1, bc - 1) - cells;
    return res;
}

Compilation message

rainbow.cpp: In function 'int colour(int, int, int, int)':
rainbow.cpp:147:6: warning: unused variable 've' [-Wunused-variable]
  147 |  int ve = plane.get(ar + 1, br - 1, ac + 1, bc - 1);
      |      ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 640 KB Output is correct
2 Correct 9 ms 1536 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 5 ms 768 KB Output is correct
5 Correct 11 ms 1792 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 5 ms 768 KB Output is correct
12 Correct 7 ms 1280 KB Output is correct
13 Correct 12 ms 2304 KB Output is correct
14 Correct 14 ms 2432 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 520 ms 60484 KB Output is correct
4 Correct 829 ms 97672 KB Output is correct
5 Correct 810 ms 95924 KB Output is correct
6 Correct 664 ms 74136 KB Output is correct
7 Correct 690 ms 72984 KB Output is correct
8 Correct 97 ms 4120 KB Output is correct
9 Correct 805 ms 96828 KB Output is correct
10 Correct 804 ms 95812 KB Output is correct
11 Correct 644 ms 74520 KB Output is correct
12 Correct 656 ms 89608 KB Output is correct
13 Correct 678 ms 96904 KB Output is correct
14 Correct 682 ms 95924 KB Output is correct
15 Correct 586 ms 74648 KB Output is correct
16 Correct 634 ms 69400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 610 ms 84328 KB Output is correct
3 Correct 1261 ms 301128 KB Output is correct
4 Correct 1430 ms 262108 KB Output is correct
5 Correct 1124 ms 206240 KB Output is correct
6 Correct 288 ms 31148 KB Output is correct
7 Correct 476 ms 56896 KB Output is correct
8 Correct 768 ms 91084 KB Output is correct
9 Correct 814 ms 84456 KB Output is correct
10 Correct 253 ms 22920 KB Output is correct
11 Correct 605 ms 73496 KB Output is correct
12 Correct 616 ms 84364 KB Output is correct
13 Correct 1311 ms 301192 KB Output is correct
14 Correct 1438 ms 262176 KB Output is correct
15 Correct 1092 ms 206124 KB Output is correct
16 Correct 229 ms 23724 KB Output is correct
17 Correct 458 ms 62240 KB Output is correct
18 Correct 990 ms 157100 KB Output is correct
19 Correct 1524 ms 267668 KB Output is correct
20 Correct 1542 ms 267616 KB Output is correct
21 Correct 815 ms 91148 KB Output is correct
22 Correct 789 ms 84464 KB Output is correct
23 Correct 237 ms 22952 KB Output is correct
24 Correct 718 ms 73532 KB Output is correct
25 Correct 629 ms 84280 KB Output is correct
26 Correct 1316 ms 301164 KB Output is correct
27 Correct 1484 ms 262148 KB Output is correct
28 Correct 1112 ms 206172 KB Output is correct
29 Correct 239 ms 23724 KB Output is correct
30 Correct 606 ms 62244 KB Output is correct
31 Correct 1185 ms 157096 KB Output is correct
32 Correct 1477 ms 267720 KB Output is correct
33 Correct 1437 ms 267644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 640 KB Output is correct
2 Correct 9 ms 1536 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 5 ms 768 KB Output is correct
5 Correct 11 ms 1792 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 5 ms 768 KB Output is correct
12 Correct 7 ms 1280 KB Output is correct
13 Correct 12 ms 2304 KB Output is correct
14 Correct 14 ms 2432 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1197 ms 94020 KB Output is correct
19 Correct 191 ms 7032 KB Output is correct
20 Correct 158 ms 4472 KB Output is correct
21 Correct 173 ms 5112 KB Output is correct
22 Correct 198 ms 6136 KB Output is correct
23 Correct 176 ms 6904 KB Output is correct
24 Correct 214 ms 4936 KB Output is correct
25 Correct 210 ms 5632 KB Output is correct
26 Correct 203 ms 6392 KB Output is correct
27 Correct 697 ms 64292 KB Output is correct
28 Correct 579 ms 33460 KB Output is correct
29 Correct 730 ms 60708 KB Output is correct
30 Correct 1361 ms 160540 KB Output is correct
31 Correct 6 ms 512 KB Output is correct
32 Correct 912 ms 64544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 640 KB Output is correct
2 Correct 9 ms 1536 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 5 ms 768 KB Output is correct
5 Correct 11 ms 1792 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 0 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 5 ms 768 KB Output is correct
12 Correct 7 ms 1280 KB Output is correct
13 Correct 12 ms 2304 KB Output is correct
14 Correct 14 ms 2432 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1197 ms 94020 KB Output is correct
19 Correct 191 ms 7032 KB Output is correct
20 Correct 158 ms 4472 KB Output is correct
21 Correct 173 ms 5112 KB Output is correct
22 Correct 198 ms 6136 KB Output is correct
23 Correct 176 ms 6904 KB Output is correct
24 Correct 214 ms 4936 KB Output is correct
25 Correct 210 ms 5632 KB Output is correct
26 Correct 203 ms 6392 KB Output is correct
27 Correct 697 ms 64292 KB Output is correct
28 Correct 579 ms 33460 KB Output is correct
29 Correct 730 ms 60708 KB Output is correct
30 Correct 1361 ms 160540 KB Output is correct
31 Correct 6 ms 512 KB Output is correct
32 Correct 912 ms 64544 KB Output is correct
33 Correct 610 ms 84328 KB Output is correct
34 Correct 1261 ms 301128 KB Output is correct
35 Correct 1430 ms 262108 KB Output is correct
36 Correct 1124 ms 206240 KB Output is correct
37 Correct 288 ms 31148 KB Output is correct
38 Correct 476 ms 56896 KB Output is correct
39 Correct 768 ms 91084 KB Output is correct
40 Correct 814 ms 84456 KB Output is correct
41 Correct 253 ms 22920 KB Output is correct
42 Correct 605 ms 73496 KB Output is correct
43 Correct 616 ms 84364 KB Output is correct
44 Correct 1311 ms 301192 KB Output is correct
45 Correct 1438 ms 262176 KB Output is correct
46 Correct 1092 ms 206124 KB Output is correct
47 Correct 229 ms 23724 KB Output is correct
48 Correct 458 ms 62240 KB Output is correct
49 Correct 990 ms 157100 KB Output is correct
50 Correct 1524 ms 267668 KB Output is correct
51 Correct 1542 ms 267616 KB Output is correct
52 Correct 815 ms 91148 KB Output is correct
53 Correct 789 ms 84464 KB Output is correct
54 Correct 237 ms 22952 KB Output is correct
55 Correct 718 ms 73532 KB Output is correct
56 Correct 629 ms 84280 KB Output is correct
57 Correct 1316 ms 301164 KB Output is correct
58 Correct 1484 ms 262148 KB Output is correct
59 Correct 1112 ms 206172 KB Output is correct
60 Correct 239 ms 23724 KB Output is correct
61 Correct 606 ms 62244 KB Output is correct
62 Correct 1185 ms 157096 KB Output is correct
63 Correct 1477 ms 267720 KB Output is correct
64 Correct 1437 ms 267644 KB Output is correct
65 Correct 1102 ms 94608 KB Output is correct
66 Correct 1149 ms 87908 KB Output is correct
67 Correct 432 ms 26408 KB Output is correct
68 Correct 827 ms 77244 KB Output is correct
69 Correct 688 ms 87872 KB Output is correct
70 Correct 1523 ms 304704 KB Output is correct
71 Correct 1596 ms 265716 KB Output is correct
72 Correct 1261 ms 209660 KB Output is correct
73 Correct 324 ms 27304 KB Output is correct
74 Correct 569 ms 65784 KB Output is correct
75 Correct 1064 ms 160668 KB Output is correct
76 Correct 1691 ms 271376 KB Output is correct
77 Correct 1659 ms 271416 KB Output is correct
78 Correct 520 ms 60484 KB Output is correct
79 Correct 829 ms 97672 KB Output is correct
80 Correct 810 ms 95924 KB Output is correct
81 Correct 664 ms 74136 KB Output is correct
82 Correct 690 ms 72984 KB Output is correct
83 Correct 97 ms 4120 KB Output is correct
84 Correct 805 ms 96828 KB Output is correct
85 Correct 804 ms 95812 KB Output is correct
86 Correct 644 ms 74520 KB Output is correct
87 Correct 656 ms 89608 KB Output is correct
88 Correct 678 ms 96904 KB Output is correct
89 Correct 682 ms 95924 KB Output is correct
90 Correct 586 ms 74648 KB Output is correct
91 Correct 634 ms 69400 KB Output is correct