답안 #676923

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
676923 2023-01-01T15:31:01 Z kingfran1907 무지개나라 (APIO17_rainbow) C++14
100 / 100
1276 ms 414552 KB
#include "rainbow.h"
#include <bits/stdc++.h>
#define X first
#define Y second

using namespace std;
typedef long long llint;

const int maxn = 2e5+10;
const int logo = 19;
const int off = 1 << logo;
const int treesiz = off << 1;
const int inf = 0x3f3f3f3f;

struct node {
	node *l, *r;
	int val;
	node() {
		l = r = nullptr;
		val = 0;
	}
};
typedef node* pnode;

inline pnode L(pnode tren) {
	if (tren == nullptr) return nullptr;
	return tren->l;
}

inline pnode R(pnode tren) {
	if (tren == nullptr) return nullptr;
	return tren->r;
}

inline int value(pnode tren) {
	if (tren == nullptr) return 0;
	return tren->val;
}

void update(pnode tren, pnode prev, int x, int l, int r, int val) {
	if (l > x || r < x) return;
	if (x == l && x == r) {
		tren->val = value(prev) + val;
		return;
	}

	int mid = (l + r) / 2;
	if (x <= mid) {
		tren->l = new node();
		tren->r = R(prev);
		update(tren->l, L(prev), x, l, mid, val);
	} else {
		tren->r = new node();
		tren->l = L(prev);
		update(tren->r, R(prev), x, mid + 1, r, val);
	}
	tren->val = value(tren->l) + value(tren->r);
}

int query(pnode tren, int a, int b, int l, int r) {
	if (tren == nullptr) return 0;
	if (l > b || r < a) return 0;
	if (l >= a && r <= b) return tren->val;
	
	int mid = (l + r) / 2;
	return query(tren->l, a, b, l, mid) + query(tren->r, a, b, mid + 1, r);
}

struct tournament {
	pnode root[maxn];
	int kol;
	tournament() {
		kol = 0;
		for (int i = 0; i < maxn; i++) 
			root[i] = nullptr;
	}
	
	void add(vector< int > &v) {
		pnode ptr = root[kol];
		kol++;
		for (int tren : v) {
			pnode nw = new node();
			update(nw, ptr, tren, 0, off - 1, 1);
			ptr = nw;
		}
		root[kol] = ptr;
	}
	
	int query(int x1, int y1, int x2, int y2) {
		if (x1 > x2 || y1 > y2) return 0;
		return ::query(root[x2], y1, y2, 0, off - 1) - ::query(root[x1 - 1], y1, y2, 0, off - 1);
	}
} vert, faces, edgesx, edgesy;

void insert(set< pair<int, int> > &s, tournament &tour) {
	auto iter = s.begin();
	for (int i = 1; i < maxn; i++) {
		vector< int > v;
		while (iter != s.end() && iter->X == i) {
			v.push_back(iter->Y);
			iter++;
		}
		tour.add(v);
	}
}

int minx = inf, maxx = 0, miny = inf, maxy = 0;
void init(int R, int C, int sr, int sc, int M, char *S) {
	set< pair<int, int> > s;
	s.insert({sr, sc});
	minx = maxx = sr;
	miny = maxy = sc;
	
	for (int i = 0; i < M; i++) {
		if (S[i] == 'N') sr--;
		else if (S[i] == 'S') sr++;
		else if (S[i] == 'W') sc--;
		else sc++;
		s.insert({sr, sc});
		
		minx = min(minx, sr);
		maxx = max(maxx, sr);
		miny = min(miny, sc);
		maxy = max(maxy, sc);
	}
	
	auto iter = s.begin();
	set< pair<int, int> > ac;
	set< pair<int, int> > xv, yv;
	for (int i = 1; i < maxn; i++) {
		vector< int > v;
		while (iter != s.end() && iter->X == i) {
			for (int x = 0; x < 2; x++)
				for (int y = 0; y < 2; y++)
					ac.insert({iter->X + x, iter->Y + y});
			xv.insert(*iter); xv.insert({iter->X + 1, iter->Y});
			yv.insert(*iter); yv.insert({iter->X, iter->Y + 1});
			
			v.push_back(iter->Y);
			iter++;
		}
		vert.add(v);
	}
	insert(ac, faces);
	insert(xv, edgesx);
	insert(yv, edgesy);
}

int colour(int ar, int ac, int br, int bc) {
	llint v = (llint)(br - ar + 1) * (bc - ac + 1) - vert.query(ar, ac, br, bc);
	llint f = (llint)(br - ar) * (bc - ac) - faces.query(ar + 1, ac + 1, br, bc);
	llint e = (llint)(br - ar + 1) * (bc - ac) + (llint)(br - ar) * (bc - ac + 1);
	e -= edgesx.query(ar + 1, ac, br, bc);
	e -= edgesy.query(ar, ac + 1, br, bc);
	
	if (ar < minx && br > maxx && ac < miny && bc > maxy) f++;
	
	//printf("debug: %lld %lld %lld\n", v, e, f);
	assert(v + f - e >= 0);
   	return v + f - e;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7124 KB Output is correct
2 Correct 15 ms 9812 KB Output is correct
3 Correct 10 ms 7252 KB Output is correct
4 Correct 10 ms 7508 KB Output is correct
5 Correct 14 ms 10420 KB Output is correct
6 Correct 7 ms 6484 KB Output is correct
7 Correct 7 ms 6484 KB Output is correct
8 Correct 7 ms 6572 KB Output is correct
9 Correct 8 ms 6568 KB Output is correct
10 Correct 8 ms 6564 KB Output is correct
11 Correct 10 ms 7764 KB Output is correct
12 Correct 14 ms 9016 KB Output is correct
13 Correct 19 ms 11824 KB Output is correct
14 Correct 19 ms 12960 KB Output is correct
15 Correct 7 ms 6568 KB Output is correct
16 Correct 8 ms 6576 KB Output is correct
17 Correct 8 ms 6568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6576 KB Output is correct
2 Correct 8 ms 6568 KB Output is correct
3 Correct 548 ms 248460 KB Output is correct
4 Correct 875 ms 411476 KB Output is correct
5 Correct 897 ms 408044 KB Output is correct
6 Correct 661 ms 315852 KB Output is correct
7 Correct 772 ms 310420 KB Output is correct
8 Correct 68 ms 7360 KB Output is correct
9 Correct 981 ms 411312 KB Output is correct
10 Correct 884 ms 408100 KB Output is correct
11 Correct 720 ms 315780 KB Output is correct
12 Correct 660 ms 382848 KB Output is correct
13 Correct 703 ms 411176 KB Output is correct
14 Correct 782 ms 407980 KB Output is correct
15 Correct 562 ms 316308 KB Output is correct
16 Correct 620 ms 292620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 6568 KB Output is correct
2 Correct 668 ms 410896 KB Output is correct
3 Correct 763 ms 410716 KB Output is correct
4 Correct 732 ms 410468 KB Output is correct
5 Correct 559 ms 309488 KB Output is correct
6 Correct 143 ms 82120 KB Output is correct
7 Correct 247 ms 157320 KB Output is correct
8 Correct 597 ms 367316 KB Output is correct
9 Correct 541 ms 326676 KB Output is correct
10 Correct 130 ms 86480 KB Output is correct
11 Correct 318 ms 204732 KB Output is correct
12 Correct 757 ms 411120 KB Output is correct
13 Correct 771 ms 410676 KB Output is correct
14 Correct 716 ms 410664 KB Output is correct
15 Correct 528 ms 309668 KB Output is correct
16 Correct 118 ms 67020 KB Output is correct
17 Correct 264 ms 157584 KB Output is correct
18 Correct 614 ms 410572 KB Output is correct
19 Correct 681 ms 410800 KB Output is correct
20 Correct 702 ms 410524 KB Output is correct
21 Correct 594 ms 367312 KB Output is correct
22 Correct 546 ms 326632 KB Output is correct
23 Correct 134 ms 86468 KB Output is correct
24 Correct 323 ms 204820 KB Output is correct
25 Correct 679 ms 411052 KB Output is correct
26 Correct 743 ms 410668 KB Output is correct
27 Correct 729 ms 410656 KB Output is correct
28 Correct 523 ms 309620 KB Output is correct
29 Correct 132 ms 67020 KB Output is correct
30 Correct 250 ms 157700 KB Output is correct
31 Correct 605 ms 410592 KB Output is correct
32 Correct 664 ms 410772 KB Output is correct
33 Correct 671 ms 410508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7124 KB Output is correct
2 Correct 15 ms 9812 KB Output is correct
3 Correct 10 ms 7252 KB Output is correct
4 Correct 10 ms 7508 KB Output is correct
5 Correct 14 ms 10420 KB Output is correct
6 Correct 7 ms 6484 KB Output is correct
7 Correct 7 ms 6484 KB Output is correct
8 Correct 7 ms 6572 KB Output is correct
9 Correct 8 ms 6568 KB Output is correct
10 Correct 8 ms 6564 KB Output is correct
11 Correct 10 ms 7764 KB Output is correct
12 Correct 14 ms 9016 KB Output is correct
13 Correct 19 ms 11824 KB Output is correct
14 Correct 19 ms 12960 KB Output is correct
15 Correct 7 ms 6568 KB Output is correct
16 Correct 8 ms 6576 KB Output is correct
17 Correct 8 ms 6568 KB Output is correct
18 Correct 1041 ms 214164 KB Output is correct
19 Correct 250 ms 19132 KB Output is correct
20 Correct 173 ms 11508 KB Output is correct
21 Correct 234 ms 13048 KB Output is correct
22 Correct 210 ms 14988 KB Output is correct
23 Correct 224 ms 18748 KB Output is correct
24 Correct 179 ms 12704 KB Output is correct
25 Correct 230 ms 14500 KB Output is correct
26 Correct 250 ms 15800 KB Output is correct
27 Correct 424 ms 174852 KB Output is correct
28 Correct 289 ms 89188 KB Output is correct
29 Correct 379 ms 160492 KB Output is correct
30 Correct 930 ms 414064 KB Output is correct
31 Correct 11 ms 6740 KB Output is correct
32 Correct 713 ms 176832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 7124 KB Output is correct
2 Correct 15 ms 9812 KB Output is correct
3 Correct 10 ms 7252 KB Output is correct
4 Correct 10 ms 7508 KB Output is correct
5 Correct 14 ms 10420 KB Output is correct
6 Correct 7 ms 6484 KB Output is correct
7 Correct 7 ms 6484 KB Output is correct
8 Correct 7 ms 6572 KB Output is correct
9 Correct 8 ms 6568 KB Output is correct
10 Correct 8 ms 6564 KB Output is correct
11 Correct 10 ms 7764 KB Output is correct
12 Correct 14 ms 9016 KB Output is correct
13 Correct 19 ms 11824 KB Output is correct
14 Correct 19 ms 12960 KB Output is correct
15 Correct 7 ms 6568 KB Output is correct
16 Correct 8 ms 6576 KB Output is correct
17 Correct 8 ms 6568 KB Output is correct
18 Correct 1041 ms 214164 KB Output is correct
19 Correct 250 ms 19132 KB Output is correct
20 Correct 173 ms 11508 KB Output is correct
21 Correct 234 ms 13048 KB Output is correct
22 Correct 210 ms 14988 KB Output is correct
23 Correct 224 ms 18748 KB Output is correct
24 Correct 179 ms 12704 KB Output is correct
25 Correct 230 ms 14500 KB Output is correct
26 Correct 250 ms 15800 KB Output is correct
27 Correct 424 ms 174852 KB Output is correct
28 Correct 289 ms 89188 KB Output is correct
29 Correct 379 ms 160492 KB Output is correct
30 Correct 930 ms 414064 KB Output is correct
31 Correct 11 ms 6740 KB Output is correct
32 Correct 713 ms 176832 KB Output is correct
33 Correct 668 ms 410896 KB Output is correct
34 Correct 763 ms 410716 KB Output is correct
35 Correct 732 ms 410468 KB Output is correct
36 Correct 559 ms 309488 KB Output is correct
37 Correct 143 ms 82120 KB Output is correct
38 Correct 247 ms 157320 KB Output is correct
39 Correct 597 ms 367316 KB Output is correct
40 Correct 541 ms 326676 KB Output is correct
41 Correct 130 ms 86480 KB Output is correct
42 Correct 318 ms 204732 KB Output is correct
43 Correct 757 ms 411120 KB Output is correct
44 Correct 771 ms 410676 KB Output is correct
45 Correct 716 ms 410664 KB Output is correct
46 Correct 528 ms 309668 KB Output is correct
47 Correct 118 ms 67020 KB Output is correct
48 Correct 264 ms 157584 KB Output is correct
49 Correct 614 ms 410572 KB Output is correct
50 Correct 681 ms 410800 KB Output is correct
51 Correct 702 ms 410524 KB Output is correct
52 Correct 594 ms 367312 KB Output is correct
53 Correct 546 ms 326632 KB Output is correct
54 Correct 134 ms 86468 KB Output is correct
55 Correct 323 ms 204820 KB Output is correct
56 Correct 679 ms 411052 KB Output is correct
57 Correct 743 ms 410668 KB Output is correct
58 Correct 729 ms 410656 KB Output is correct
59 Correct 523 ms 309620 KB Output is correct
60 Correct 132 ms 67020 KB Output is correct
61 Correct 250 ms 157700 KB Output is correct
62 Correct 605 ms 410592 KB Output is correct
63 Correct 664 ms 410772 KB Output is correct
64 Correct 671 ms 410508 KB Output is correct
65 Correct 548 ms 248460 KB Output is correct
66 Correct 875 ms 411476 KB Output is correct
67 Correct 897 ms 408044 KB Output is correct
68 Correct 661 ms 315852 KB Output is correct
69 Correct 772 ms 310420 KB Output is correct
70 Correct 68 ms 7360 KB Output is correct
71 Correct 981 ms 411312 KB Output is correct
72 Correct 884 ms 408100 KB Output is correct
73 Correct 720 ms 315780 KB Output is correct
74 Correct 660 ms 382848 KB Output is correct
75 Correct 703 ms 411176 KB Output is correct
76 Correct 782 ms 407980 KB Output is correct
77 Correct 562 ms 316308 KB Output is correct
78 Correct 620 ms 292620 KB Output is correct
79 Correct 1267 ms 370688 KB Output is correct
80 Correct 1276 ms 329996 KB Output is correct
81 Correct 363 ms 89872 KB Output is correct
82 Correct 549 ms 208200 KB Output is correct
83 Correct 980 ms 414552 KB Output is correct
84 Correct 868 ms 414120 KB Output is correct
85 Correct 945 ms 414256 KB Output is correct
86 Correct 687 ms 313052 KB Output is correct
87 Correct 200 ms 70580 KB Output is correct
88 Correct 364 ms 161160 KB Output is correct
89 Correct 732 ms 414192 KB Output is correct
90 Correct 1051 ms 414368 KB Output is correct
91 Correct 948 ms 414064 KB Output is correct