답안 #433507

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
433507 2021-06-20T00:30:00 Z rqi 무지개나라 (APIO17_rainbow) C++14
100 / 100
1827 ms 459240 KB
#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;

#define mp make_pair
#define f first
#define s second
#define ins insert
#define pb push_back

#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

void ckmin(int& a, int b){
	a = min(a, b);
}

void ckmax(int& a, int b){
	a = max(a, b);
}

vector<char> dchar = vector<char>{'S', 'E', 'N', 'W'};
vi dx = vi{1, 0, -1, 0};
vi dy = vi{0, 1, 0, -1};

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

struct hashFunc{
	size_t operator()(pi a) const {
		ll res = ll(a.f)*200005+ll(a.s);
		ll BIG_RAND = 102923014203249LL;
		return __builtin_bswap64((unsigned long long)(res)*BIG_RAND);
	}
};

const int mx = 200005;

struct node{
	ll val;
	node* c[2];

	node(){
		val = 0;
		c[0] = c[1] = NULL;
	}

	void pull(){
		val = 0;
		for(int i = 0; i < 2; i++){
			if(c[i]){
				val+=c[i]->val;
			}
		}
	}
};

const int SZ = 262144;

node* build(int L = 0, int R = SZ-1){
	node* a = new node();
	if(L == R){
		return a;
	}
	int M = (L+R)/2;
	a->c[0] = build(L, M);
	a->c[1] = build(M+1, R);
	return a;
}

node* upd(node* cur, int pos, int L = 0, int R = SZ-1){

	assert(cur);


	node* res = new node();

	*res = *cur;

	if(L == R){
		res->val++;
		return res;
	}


	assert(cur->c[0]);
	assert(cur->c[1]);

	int M = (L+R)/2;

	if(pos <= M){
		res->c[0] = upd(cur->c[0], pos, L, M);
	}
	else{
		res->c[1] = upd(cur->c[1], pos, M+1, R);
	}
	res->pull();
	return res;
}

ll query(node* cur, int l, int r, int L = 0, int R = SZ-1){
	if(r < L || R < l) return 0;

	if(l <= L && R <= r){
		return cur->val;
	}

	int M = (L+R)/2;

	return query(cur->c[0], l, r, L, M)+query(cur->c[1], l, r, M+1, R);
}

struct Off2DBIT{
	node* seg[mx];

	vpi all_updates;
	void update(int X, int Y){
		all_updates.pb(mp(X, Y));
	}


	void initBT(){
		// cout << "all_updates: " << "\n";
		// for(const auto& u: all_updates){
		// 	cout << u.f << " " << u.s << "\n";
		// }
		seg[0] = build();

		for(auto &u: all_updates){
			swap(u.s, u.f);
		}


		sort(all(all_updates));

		int cur_ind = 0;


		for(int i = 1; i < mx; i++){
			seg[i] = seg[i-1];
			while(cur_ind < sz(all_updates)){
				assert(all_updates[cur_ind].f >= i);
				if(all_updates[cur_ind].f > i) break;
				seg[i] = upd(seg[i], all_updates[cur_ind].s);
				cur_ind++; 
			}
		}
	}

	ll query(int xl, int yl, int xr, int yr){
		return ::query(seg[yr], xl, xr)-::query(seg[yl-1], xl, xr);
	}

	// ll query(int xl, int yl, int xr, int yr){
	// 	ll res = 0;
	// 	for(auto& u: all_updates){
	// 		if(xl <= u.f && u.f <= xr && yl <= u.s && u.s <= yr){
	// 			res++;
	// 		}
	// 	}
	// 	return res;
	// }
};

Off2DBIT bits[2][2];
gp_hash_table<pi, null_type, hashFunc> rects[4];

int min_sr, max_sr, min_sc, max_sc;

void init(int R, int C, int sr, int sc, int M, char *S) {
	min_sr = sr;
	min_sc = sc;
	max_sr = sr;
	max_sc = sc;
	for(int i = 0; i <= M; i++){
		for(int i = 0; i <= 1; i++){
			for(int j = 0; j <= 1; j++){
				for(int xl = sr-i; xl <= sr; xl++){
					for(int yl = sc-j; yl <= sc; yl++){
						if(1 <= xl && 1 <= yl){
							rects[2*i+j][mp(xl, yl)];
						}
					}
				}
			}
		}
		if(i < M){
			for(int d = 0; d < 4; d++){
				if(S[i] == dchar[d]){
					sr+=dx[d];
					sc+=dy[d];
					break;
				}
			}
		}

		ckmin(min_sr, sr);
		ckmin(min_sc, sc);
		ckmax(max_sr, sr);
		ckmax(max_sc, sc);
	}

	for(int i = 0; i <= 1; i++){
		for(int j = 0; j <= 1; j++){
			// cout << "i, j = " << i << ", " << j << "\n";
			for(auto u: rects[2*i+j]){
				// cout << "RECT " << i << " " << j << " " << u.f << " " << u.s << "\n";
				bits[i][j].update(u.f, u.s);
			}
		}
	}

	for(int i = 0; i <= 1; i++){
		for(int j = 0; j <= 1; j++){
			bits[i][j].initBT();
		}
	}

}

int colour(int ar, int ac, int br, int bc) {
	ll ans = 0;
	if(ar+1 <= min_sr && max_sr+1 <= br && ac+1 <= min_sc && max_sc+1 <= bc){
		ans++;
	}
	
	for(int i = 0; i <= 1; i++){
		for(int j = 0; j <= 1; j++){
			ll val = ll(br-i-ar+1)*ll(bc-j-ac+1);
			// cout << "val: " << val << "\n";
			// cout << "querying " << i << " " << j << "\n";
			val-=bits[i][j].query(ar, ac, br-i, bc-j);
			if((i+j) % 2 == 0){
				ans+=val;
			}
			else{
				ans-=val;
			}

			// cout << i << " " << j << " " << val << "\n";
		}
	}
    return (int)(ans);
}

# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 72756 KB Output is correct
2 Correct 112 ms 75104 KB Output is correct
3 Correct 106 ms 72920 KB Output is correct
4 Correct 117 ms 73152 KB Output is correct
5 Correct 106 ms 75792 KB Output is correct
6 Correct 97 ms 72132 KB Output is correct
7 Correct 97 ms 72112 KB Output is correct
8 Correct 101 ms 72172 KB Output is correct
9 Correct 107 ms 72144 KB Output is correct
10 Correct 102 ms 72132 KB Output is correct
11 Correct 104 ms 73284 KB Output is correct
12 Correct 104 ms 74504 KB Output is correct
13 Correct 108 ms 76996 KB Output is correct
14 Correct 108 ms 78180 KB Output is correct
15 Correct 96 ms 72112 KB Output is correct
16 Correct 95 ms 72108 KB Output is correct
17 Correct 102 ms 72124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 95 ms 72108 KB Output is correct
2 Correct 102 ms 72124 KB Output is correct
3 Correct 853 ms 267668 KB Output is correct
4 Correct 1196 ms 395484 KB Output is correct
5 Correct 1187 ms 390640 KB Output is correct
6 Correct 1012 ms 308952 KB Output is correct
7 Correct 1047 ms 297832 KB Output is correct
8 Correct 355 ms 72956 KB Output is correct
9 Correct 1190 ms 393688 KB Output is correct
10 Correct 1179 ms 389656 KB Output is correct
11 Correct 1045 ms 308940 KB Output is correct
12 Correct 897 ms 386620 KB Output is correct
13 Correct 973 ms 394972 KB Output is correct
14 Correct 922 ms 391100 KB Output is correct
15 Correct 842 ms 309536 KB Output is correct
16 Correct 930 ms 284236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 96 ms 72112 KB Output is correct
2 Correct 897 ms 456000 KB Output is correct
3 Correct 831 ms 455960 KB Output is correct
4 Correct 825 ms 458716 KB Output is correct
5 Correct 732 ms 362488 KB Output is correct
6 Correct 253 ms 143412 KB Output is correct
7 Correct 374 ms 214220 KB Output is correct
8 Correct 665 ms 365268 KB Output is correct
9 Correct 641 ms 336428 KB Output is correct
10 Correct 251 ms 149208 KB Output is correct
11 Correct 501 ms 261892 KB Output is correct
12 Correct 890 ms 456012 KB Output is correct
13 Correct 826 ms 455960 KB Output is correct
14 Correct 852 ms 458632 KB Output is correct
15 Correct 706 ms 362524 KB Output is correct
16 Correct 217 ms 129864 KB Output is correct
17 Correct 379 ms 214324 KB Output is correct
18 Correct 818 ms 457520 KB Output is correct
19 Correct 767 ms 395236 KB Output is correct
20 Correct 766 ms 395144 KB Output is correct
21 Correct 674 ms 365232 KB Output is correct
22 Correct 632 ms 336460 KB Output is correct
23 Correct 265 ms 149228 KB Output is correct
24 Correct 494 ms 261824 KB Output is correct
25 Correct 904 ms 456040 KB Output is correct
26 Correct 831 ms 455900 KB Output is correct
27 Correct 824 ms 458608 KB Output is correct
28 Correct 760 ms 362420 KB Output is correct
29 Correct 220 ms 129700 KB Output is correct
30 Correct 400 ms 214496 KB Output is correct
31 Correct 838 ms 457432 KB Output is correct
32 Correct 829 ms 395144 KB Output is correct
33 Correct 795 ms 394976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 72756 KB Output is correct
2 Correct 112 ms 75104 KB Output is correct
3 Correct 106 ms 72920 KB Output is correct
4 Correct 117 ms 73152 KB Output is correct
5 Correct 106 ms 75792 KB Output is correct
6 Correct 97 ms 72132 KB Output is correct
7 Correct 97 ms 72112 KB Output is correct
8 Correct 101 ms 72172 KB Output is correct
9 Correct 107 ms 72144 KB Output is correct
10 Correct 102 ms 72132 KB Output is correct
11 Correct 104 ms 73284 KB Output is correct
12 Correct 104 ms 74504 KB Output is correct
13 Correct 108 ms 76996 KB Output is correct
14 Correct 108 ms 78180 KB Output is correct
15 Correct 96 ms 72112 KB Output is correct
16 Correct 95 ms 72108 KB Output is correct
17 Correct 102 ms 72124 KB Output is correct
18 Correct 1512 ms 264144 KB Output is correct
19 Correct 411 ms 81792 KB Output is correct
20 Correct 316 ms 74308 KB Output is correct
21 Correct 349 ms 75948 KB Output is correct
22 Correct 374 ms 77652 KB Output is correct
23 Correct 428 ms 81512 KB Output is correct
24 Correct 328 ms 75564 KB Output is correct
25 Correct 353 ms 77252 KB Output is correct
26 Correct 391 ms 78604 KB Output is correct
27 Correct 773 ms 231048 KB Output is correct
28 Correct 578 ms 147300 KB Output is correct
29 Correct 809 ms 214564 KB Output is correct
30 Correct 1319 ms 458308 KB Output is correct
31 Correct 102 ms 72388 KB Output is correct
32 Correct 1358 ms 234648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 128 ms 72756 KB Output is correct
2 Correct 112 ms 75104 KB Output is correct
3 Correct 106 ms 72920 KB Output is correct
4 Correct 117 ms 73152 KB Output is correct
5 Correct 106 ms 75792 KB Output is correct
6 Correct 97 ms 72132 KB Output is correct
7 Correct 97 ms 72112 KB Output is correct
8 Correct 101 ms 72172 KB Output is correct
9 Correct 107 ms 72144 KB Output is correct
10 Correct 102 ms 72132 KB Output is correct
11 Correct 104 ms 73284 KB Output is correct
12 Correct 104 ms 74504 KB Output is correct
13 Correct 108 ms 76996 KB Output is correct
14 Correct 108 ms 78180 KB Output is correct
15 Correct 96 ms 72112 KB Output is correct
16 Correct 95 ms 72108 KB Output is correct
17 Correct 102 ms 72124 KB Output is correct
18 Correct 1512 ms 264144 KB Output is correct
19 Correct 411 ms 81792 KB Output is correct
20 Correct 316 ms 74308 KB Output is correct
21 Correct 349 ms 75948 KB Output is correct
22 Correct 374 ms 77652 KB Output is correct
23 Correct 428 ms 81512 KB Output is correct
24 Correct 328 ms 75564 KB Output is correct
25 Correct 353 ms 77252 KB Output is correct
26 Correct 391 ms 78604 KB Output is correct
27 Correct 773 ms 231048 KB Output is correct
28 Correct 578 ms 147300 KB Output is correct
29 Correct 809 ms 214564 KB Output is correct
30 Correct 1319 ms 458308 KB Output is correct
31 Correct 102 ms 72388 KB Output is correct
32 Correct 1358 ms 234648 KB Output is correct
33 Correct 897 ms 456000 KB Output is correct
34 Correct 831 ms 455960 KB Output is correct
35 Correct 825 ms 458716 KB Output is correct
36 Correct 732 ms 362488 KB Output is correct
37 Correct 253 ms 143412 KB Output is correct
38 Correct 374 ms 214220 KB Output is correct
39 Correct 665 ms 365268 KB Output is correct
40 Correct 641 ms 336428 KB Output is correct
41 Correct 251 ms 149208 KB Output is correct
42 Correct 501 ms 261892 KB Output is correct
43 Correct 890 ms 456012 KB Output is correct
44 Correct 826 ms 455960 KB Output is correct
45 Correct 852 ms 458632 KB Output is correct
46 Correct 706 ms 362524 KB Output is correct
47 Correct 217 ms 129864 KB Output is correct
48 Correct 379 ms 214324 KB Output is correct
49 Correct 818 ms 457520 KB Output is correct
50 Correct 767 ms 395236 KB Output is correct
51 Correct 766 ms 395144 KB Output is correct
52 Correct 674 ms 365232 KB Output is correct
53 Correct 632 ms 336460 KB Output is correct
54 Correct 265 ms 149228 KB Output is correct
55 Correct 494 ms 261824 KB Output is correct
56 Correct 904 ms 456040 KB Output is correct
57 Correct 831 ms 455900 KB Output is correct
58 Correct 824 ms 458608 KB Output is correct
59 Correct 760 ms 362420 KB Output is correct
60 Correct 220 ms 129700 KB Output is correct
61 Correct 400 ms 214496 KB Output is correct
62 Correct 838 ms 457432 KB Output is correct
63 Correct 829 ms 395144 KB Output is correct
64 Correct 795 ms 394976 KB Output is correct
65 Correct 853 ms 267668 KB Output is correct
66 Correct 1196 ms 395484 KB Output is correct
67 Correct 1187 ms 390640 KB Output is correct
68 Correct 1012 ms 308952 KB Output is correct
69 Correct 1047 ms 297832 KB Output is correct
70 Correct 355 ms 72956 KB Output is correct
71 Correct 1190 ms 393688 KB Output is correct
72 Correct 1179 ms 389656 KB Output is correct
73 Correct 1045 ms 308940 KB Output is correct
74 Correct 897 ms 386620 KB Output is correct
75 Correct 973 ms 394972 KB Output is correct
76 Correct 922 ms 391100 KB Output is correct
77 Correct 842 ms 309536 KB Output is correct
78 Correct 930 ms 284236 KB Output is correct
79 Correct 1442 ms 365864 KB Output is correct
80 Correct 1474 ms 337024 KB Output is correct
81 Correct 889 ms 149968 KB Output is correct
82 Correct 1168 ms 262648 KB Output is correct
83 Correct 1771 ms 456928 KB Output is correct
84 Correct 1827 ms 456740 KB Output is correct
85 Correct 1737 ms 459240 KB Output is correct
86 Correct 1642 ms 363220 KB Output is correct
87 Correct 1104 ms 130644 KB Output is correct
88 Correct 1210 ms 215204 KB Output is correct
89 Correct 1694 ms 458112 KB Output is correct
90 Correct 1676 ms 395828 KB Output is correct
91 Correct 1421 ms 395740 KB Output is correct