답안 #80308

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
80308 2018-10-20T00:10:04 Z qkxwsm 무지개나라 (APIO17_rainbow) C++14
100 / 100
2414 ms 382068 KB
#include "rainbow.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

random_device(rd);
mt19937 rng(rd());
const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();

struct custom_hash
{
	template<class T>
    unsigned long long operator()(T v) const
	{
		unsigned long long x = v;
		x += FIXED_RANDOM; x += 11400714819323198485ull;
        x = (x ^ (x >> 30)) * 13787848793156543929ull;
        x = (x ^ (x >> 27)) * 10723151780598845931ull;
        return x ^ (x >> 31);
    }
};

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>;

template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
long long expo(long long a, long long e, long long mod)
{
	return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod));
}
template<class T, class U>
T nmod(T &x, U mod)
{
	if (x >= mod) x -= mod;
}
template<class T>
T gcd(T a, T b)
{
	return (b ? gcd(b, a % b) : a);
}
template<class T>
T randomize(T mod)
{
	return (uniform_int_distribution<T>(0, mod - 1))(rng);
}

#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define fi first
#define se second
#define debug(x) cerr << #x << " = " << x << endl;
#define sz(x) ((int) (x.size()))

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-9;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 400013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

int N;

struct pseg
{
	struct node
	{
		node *ch[2];
		ll stor;
		node()
		{
			ch[0] = ch[1] = nullptr;
			stor = 0;
		}
	};
	int T;
	node *root[MAXN];
	int ft[MAXN];
	vector<pair<ll, int> > updates[MAXN];
	ll DNE = 0;
	ll comb(ll a, ll b)
	{
		return a + b;
	}
	node* build(int L, int R)
	{
		node* t = new node();
		if (L == R) return t;
		int mid = (L + R) >> 1;
		t -> ch[0] = build(L, mid);
		t -> ch[1] = build(mid + 1, R);
		return t;
	}
	node *update(node *w, int L, int R, int a, ll v)
	{
		if (a < L || R < a)
		{
			return w;
		}
		node *t = new node();
		if (L == R)
		{
			t -> stor = w -> stor + v;
			return t;
		}
		int mid = (L + R) >> 1;
		t -> ch[0] = update(w -> ch[0], L, mid, a, v);
		t -> ch[1] = update(w -> ch[1], mid + 1, R, a, v);
		t -> stor = comb(t -> ch[0] -> stor, t -> ch[1] -> stor);
		return t;
	}
	ll query(node *w, int L, int R, int a, int b)
	{
		if (b < L || R < a)
		{
			return DNE;
		}
		if (a <= L && R <= b)
		{
			return w -> stor;
		}
		int mid = (L + R) >> 1;
		return query(w -> ch[0], L, mid, a, b) + query(w -> ch[1], mid + 1, R, a, b);
	}
	void process()
	{
		root[0] = build(0, N);
		ft[0] = 0;
		for (int i = 1; i <= N; i++)
		{
			for (auto p : updates[i])
			{
				ll v = p.fi; int idx = p.se;
				T++; root[T] = update(root[T - 1], 0, N, idx, v);
			}
			ft[i] = T;
		}
	}
	ll sum(int t, int L, int R)
	{
		return query(root[ft[t]], 0, N, L, R);
	}
	ll rect(int x0, int x1, int y0, int y1)
	{
		if (x0 > x1 || y0 > y1) return 0;
		return query(root[ft[x1]], 0, N, y0, y1) - query(root[ft[x0 - 1]], 0, N, y0, y1);
	}
};

pseg tseg, fseg, hseg, vseg, dseg, useg;
set<pii> s;
vector<pii> pts, face, hor, vert, dn, up;

void prep()
{
	for (pii p : s)
	{
		int x = p.fi, y = p.se;
		bool go[2][2];
		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				go[i][j] = true;
				if (s.find({x + i, y + j}) == s.end()) go[i][j] = false;
			}
		}
		pts.PB({x, y});
		if (go[0][1] && go[1][0] && go[1][1]) face.PB({x, y});
		if (go[1][0]) vert.PB({x, y});
		if (go[0][1]) hor.PB({x, y});
		if (go[1][1] && !go[0][1] && !go[1][0]) dn.PB({x, y});
		if (s.find({x - 1, y + 1}) != s.end() && s.find({x - 1, y}) == s.end() && !go[0][1]) up.PB({x, y});
 	}
	for (pii p : pts) tseg.updates[p.fi].PB({1, p.se}); tseg.process();
	for (pii p : face) fseg.updates[p.fi].PB({1, p.se}); fseg.process();
	for (pii p : hor) hseg.updates[p.fi].PB({1, p.se}); hseg.process();
	for (pii p : vert) vseg.updates[p.fi].PB({1, p.se}); vseg.process();
	for (pii p : dn) dseg.updates[p.fi].PB({1, p.se}); dseg.process();
	for (pii p : up) useg.updates[p.fi].PB({1, p.se}); useg.process();
}
void init(int R, int C, int x, int y, int K, char *S)
{
	N = max(R, C) + 2;
	s.insert({x, y});
	for (int i = 0; i < K; i++)
	{
		if (S[i] == 'N') x--;
		if (S[i] == 'S') x++;
		if (S[i] == 'E') y++;
		if (S[i] == 'W') y--;
		s.insert({x, y});
	}
	prep();
}

ll x0, x1, y0, y1;

ll edges()
{
	ll res = 0;
	res += vseg.rect(x0, x1 - 1, y0, y1) + (x1 - x0 + 2) * 2 + tseg.rect(x0, x0, y0, y1) + tseg.rect(x1, x1, y0, y1);
	// cerr << "alive\n";
	res += hseg.rect(x0, x1, y0, y1 - 1) + (y1 - y0 + 2) * 2 + tseg.rect(x0, x1, y0, y0) + tseg.rect(x0, x1, y1, y1);
	res += dseg.rect(x0, x1 - 1, y0, y1 - 1) + useg.rect(x0 + 1, x1, y0, y1 - 1);
	// cerr << "edges " << res << endl;
	return res;
}
ll vertices()
{
	ll res = 0;
	res = tseg.rect(x0, x1, y0, y1) + (x1 - x0 + 2) * 2 + (y1 - y0 + 2) * 2;
	// cerr << "vertices " << res << endl;
	return res;
}
ll faces()
{
	ll res = 0;
	res = fseg.rect(x0, x1 - 1, y0, y1 - 1);
	res += vseg.rect(x0, x1 - 1, y0, y0);
	res += vseg.rect(x0, x1 - 1, y1, y1);
	// for (int i = x0; i < x1; i++)
	// {
	// 	if (s.find({i, y0}) != s.end() && s.find({i + 1, y0}) != s.end()) res++;
	// 	if (s.find({i, y1}) != s.end() && s.find({i + 1, y1}) != s.end()) res++;
	// }
	// for (int i = y0; i < y1; i++)
	// {
	// 	if (s.find({x0, i}) != s.end() && s.find({x0, i + 1}) != s.end()) res++;
	// 	if (s.find({x1, i}) != s.end() && s.find({x1, i + 1}) != s.end()) res++;
	// }
	res += hseg.rect(x0, x0, y0, y1 - 1);
	res += hseg.rect(x1, x1, y0, y1 - 1);
	if (s.find({x0, y0}) != s.end()) res++;
	if (s.find({x1, y0}) != s.end()) res++;
	if (s.find({x0, y1}) != s.end()) res++;
	if (s.find({x1, y1}) != s.end()) res++;
	// cerr << "faces " << res << endl;
	return res;
}
int colour(int _x0, int _y0, int _x1, int _y1)
{
	x0 = _x0; x1 = _x1; y0 = _y0; y1 = _y1;
	if (tseg.rect(x0, x1, y0, y1) == 0) return 1;
	ll ans = tseg.rect(x0, x1, y0, y1) - tseg.rect(x0 + 1, x1 - 1, y0 + 1, y1 - 1);
	ans = (ans ? 1 : 0);
	// cerr << "bads " << ans << endl;
	// debug(edges()); debug(vertices()); debug(faces()); debug(ans);
	// cerr << "ans = " << ans << endl;
	ans = edges() - vertices() - faces() + 2 - ans;
    return ans;
}

Compilation message

rainbow.cpp: In function 'void prep()':
rainbow.cpp:204:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (pii p : pts) tseg.updates[p.fi].PB({1, p.se}); tseg.process();
  ^~~
rainbow.cpp:204:54: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (pii p : pts) tseg.updates[p.fi].PB({1, p.se}); tseg.process();
                                                      ^~~~
rainbow.cpp:205:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (pii p : face) fseg.updates[p.fi].PB({1, p.se}); fseg.process();
  ^~~
rainbow.cpp:205:55: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (pii p : face) fseg.updates[p.fi].PB({1, p.se}); fseg.process();
                                                       ^~~~
rainbow.cpp:206:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (pii p : hor) hseg.updates[p.fi].PB({1, p.se}); hseg.process();
  ^~~
rainbow.cpp:206:54: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (pii p : hor) hseg.updates[p.fi].PB({1, p.se}); hseg.process();
                                                      ^~~~
rainbow.cpp:207:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (pii p : vert) vseg.updates[p.fi].PB({1, p.se}); vseg.process();
  ^~~
rainbow.cpp:207:55: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (pii p : vert) vseg.updates[p.fi].PB({1, p.se}); vseg.process();
                                                       ^~~~
rainbow.cpp:208:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (pii p : dn) dseg.updates[p.fi].PB({1, p.se}); dseg.process();
  ^~~
rainbow.cpp:208:53: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (pii p : dn) dseg.updates[p.fi].PB({1, p.se}); dseg.process();
                                                     ^~~~
rainbow.cpp:209:2: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  for (pii p : up) useg.updates[p.fi].PB({1, p.se}); useg.process();
  ^~~
rainbow.cpp:209:53: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  for (pii p : up) useg.updates[p.fi].PB({1, p.se}); useg.process();
                                                     ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 56956 KB Output is correct
2 Correct 62 ms 57472 KB Output is correct
3 Correct 57 ms 57472 KB Output is correct
4 Correct 55 ms 57472 KB Output is correct
5 Correct 61 ms 57872 KB Output is correct
6 Correct 63 ms 57872 KB Output is correct
7 Correct 59 ms 57872 KB Output is correct
8 Correct 66 ms 57872 KB Output is correct
9 Correct 53 ms 57872 KB Output is correct
10 Correct 103 ms 57872 KB Output is correct
11 Correct 58 ms 57872 KB Output is correct
12 Correct 56 ms 57872 KB Output is correct
13 Correct 68 ms 57872 KB Output is correct
14 Correct 66 ms 58256 KB Output is correct
15 Correct 53 ms 58256 KB Output is correct
16 Correct 58 ms 58256 KB Output is correct
17 Correct 51 ms 58256 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 58 ms 58256 KB Output is correct
2 Correct 51 ms 58256 KB Output is correct
3 Correct 1419 ms 216664 KB Output is correct
4 Correct 1905 ms 271352 KB Output is correct
5 Correct 1888 ms 274996 KB Output is correct
6 Correct 1394 ms 285384 KB Output is correct
7 Correct 1698 ms 285384 KB Output is correct
8 Correct 942 ms 285384 KB Output is correct
9 Correct 1937 ms 285752 KB Output is correct
10 Correct 2001 ms 289100 KB Output is correct
11 Correct 1721 ms 299488 KB Output is correct
12 Correct 969 ms 299488 KB Output is correct
13 Correct 1036 ms 299488 KB Output is correct
14 Correct 1000 ms 300728 KB Output is correct
15 Correct 1067 ms 311088 KB Output is correct
16 Correct 1524 ms 311088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 53 ms 58256 KB Output is correct
2 Correct 527 ms 311088 KB Output is correct
3 Correct 554 ms 311088 KB Output is correct
4 Correct 529 ms 311088 KB Output is correct
5 Correct 428 ms 311088 KB Output is correct
6 Correct 303 ms 311088 KB Output is correct
7 Correct 446 ms 311088 KB Output is correct
8 Correct 345 ms 311088 KB Output is correct
9 Correct 293 ms 311088 KB Output is correct
10 Correct 228 ms 311088 KB Output is correct
11 Correct 383 ms 311088 KB Output is correct
12 Correct 520 ms 311088 KB Output is correct
13 Correct 517 ms 311088 KB Output is correct
14 Correct 556 ms 311088 KB Output is correct
15 Correct 432 ms 311088 KB Output is correct
16 Correct 293 ms 311088 KB Output is correct
17 Correct 426 ms 311088 KB Output is correct
18 Correct 500 ms 311088 KB Output is correct
19 Correct 504 ms 311088 KB Output is correct
20 Correct 503 ms 311088 KB Output is correct
21 Correct 325 ms 311088 KB Output is correct
22 Correct 342 ms 311088 KB Output is correct
23 Correct 223 ms 311088 KB Output is correct
24 Correct 266 ms 311088 KB Output is correct
25 Correct 553 ms 311088 KB Output is correct
26 Correct 549 ms 311088 KB Output is correct
27 Correct 516 ms 311088 KB Output is correct
28 Correct 434 ms 311088 KB Output is correct
29 Correct 286 ms 311088 KB Output is correct
30 Correct 427 ms 311088 KB Output is correct
31 Correct 477 ms 311088 KB Output is correct
32 Correct 501 ms 311088 KB Output is correct
33 Correct 518 ms 311088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 56956 KB Output is correct
2 Correct 62 ms 57472 KB Output is correct
3 Correct 57 ms 57472 KB Output is correct
4 Correct 55 ms 57472 KB Output is correct
5 Correct 61 ms 57872 KB Output is correct
6 Correct 63 ms 57872 KB Output is correct
7 Correct 59 ms 57872 KB Output is correct
8 Correct 66 ms 57872 KB Output is correct
9 Correct 53 ms 57872 KB Output is correct
10 Correct 103 ms 57872 KB Output is correct
11 Correct 58 ms 57872 KB Output is correct
12 Correct 56 ms 57872 KB Output is correct
13 Correct 68 ms 57872 KB Output is correct
14 Correct 66 ms 58256 KB Output is correct
15 Correct 53 ms 58256 KB Output is correct
16 Correct 58 ms 58256 KB Output is correct
17 Correct 51 ms 58256 KB Output is correct
18 Correct 1483 ms 311088 KB Output is correct
19 Correct 324 ms 311088 KB Output is correct
20 Correct 301 ms 311088 KB Output is correct
21 Correct 239 ms 311088 KB Output is correct
22 Correct 247 ms 311088 KB Output is correct
23 Correct 310 ms 311088 KB Output is correct
24 Correct 419 ms 311088 KB Output is correct
25 Correct 324 ms 311088 KB Output is correct
26 Correct 265 ms 311088 KB Output is correct
27 Correct 679 ms 311088 KB Output is correct
28 Correct 747 ms 311088 KB Output is correct
29 Correct 818 ms 311088 KB Output is correct
30 Correct 997 ms 311088 KB Output is correct
31 Correct 56 ms 311088 KB Output is correct
32 Correct 1188 ms 311088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 67 ms 56956 KB Output is correct
2 Correct 62 ms 57472 KB Output is correct
3 Correct 57 ms 57472 KB Output is correct
4 Correct 55 ms 57472 KB Output is correct
5 Correct 61 ms 57872 KB Output is correct
6 Correct 63 ms 57872 KB Output is correct
7 Correct 59 ms 57872 KB Output is correct
8 Correct 66 ms 57872 KB Output is correct
9 Correct 53 ms 57872 KB Output is correct
10 Correct 103 ms 57872 KB Output is correct
11 Correct 58 ms 57872 KB Output is correct
12 Correct 56 ms 57872 KB Output is correct
13 Correct 68 ms 57872 KB Output is correct
14 Correct 66 ms 58256 KB Output is correct
15 Correct 53 ms 58256 KB Output is correct
16 Correct 58 ms 58256 KB Output is correct
17 Correct 51 ms 58256 KB Output is correct
18 Correct 1483 ms 311088 KB Output is correct
19 Correct 324 ms 311088 KB Output is correct
20 Correct 301 ms 311088 KB Output is correct
21 Correct 239 ms 311088 KB Output is correct
22 Correct 247 ms 311088 KB Output is correct
23 Correct 310 ms 311088 KB Output is correct
24 Correct 419 ms 311088 KB Output is correct
25 Correct 324 ms 311088 KB Output is correct
26 Correct 265 ms 311088 KB Output is correct
27 Correct 679 ms 311088 KB Output is correct
28 Correct 747 ms 311088 KB Output is correct
29 Correct 818 ms 311088 KB Output is correct
30 Correct 997 ms 311088 KB Output is correct
31 Correct 56 ms 311088 KB Output is correct
32 Correct 1188 ms 311088 KB Output is correct
33 Correct 527 ms 311088 KB Output is correct
34 Correct 554 ms 311088 KB Output is correct
35 Correct 529 ms 311088 KB Output is correct
36 Correct 428 ms 311088 KB Output is correct
37 Correct 303 ms 311088 KB Output is correct
38 Correct 446 ms 311088 KB Output is correct
39 Correct 345 ms 311088 KB Output is correct
40 Correct 293 ms 311088 KB Output is correct
41 Correct 228 ms 311088 KB Output is correct
42 Correct 383 ms 311088 KB Output is correct
43 Correct 520 ms 311088 KB Output is correct
44 Correct 517 ms 311088 KB Output is correct
45 Correct 556 ms 311088 KB Output is correct
46 Correct 432 ms 311088 KB Output is correct
47 Correct 293 ms 311088 KB Output is correct
48 Correct 426 ms 311088 KB Output is correct
49 Correct 500 ms 311088 KB Output is correct
50 Correct 504 ms 311088 KB Output is correct
51 Correct 503 ms 311088 KB Output is correct
52 Correct 325 ms 311088 KB Output is correct
53 Correct 342 ms 311088 KB Output is correct
54 Correct 223 ms 311088 KB Output is correct
55 Correct 266 ms 311088 KB Output is correct
56 Correct 553 ms 311088 KB Output is correct
57 Correct 549 ms 311088 KB Output is correct
58 Correct 516 ms 311088 KB Output is correct
59 Correct 434 ms 311088 KB Output is correct
60 Correct 286 ms 311088 KB Output is correct
61 Correct 427 ms 311088 KB Output is correct
62 Correct 477 ms 311088 KB Output is correct
63 Correct 501 ms 311088 KB Output is correct
64 Correct 518 ms 311088 KB Output is correct
65 Correct 2350 ms 311088 KB Output is correct
66 Correct 2414 ms 311088 KB Output is correct
67 Correct 1183 ms 311088 KB Output is correct
68 Correct 1235 ms 311088 KB Output is correct
69 Correct 1258 ms 357268 KB Output is correct
70 Correct 1309 ms 364980 KB Output is correct
71 Correct 909 ms 365360 KB Output is correct
72 Correct 989 ms 365360 KB Output is correct
73 Correct 861 ms 365360 KB Output is correct
74 Correct 803 ms 365360 KB Output is correct
75 Correct 713 ms 375604 KB Output is correct
76 Correct 1448 ms 379652 KB Output is correct
77 Correct 2261 ms 382068 KB Output is correct
78 Correct 1419 ms 216664 KB Output is correct
79 Correct 1905 ms 271352 KB Output is correct
80 Correct 1888 ms 274996 KB Output is correct
81 Correct 1394 ms 285384 KB Output is correct
82 Correct 1698 ms 285384 KB Output is correct
83 Correct 942 ms 285384 KB Output is correct
84 Correct 1937 ms 285752 KB Output is correct
85 Correct 2001 ms 289100 KB Output is correct
86 Correct 1721 ms 299488 KB Output is correct
87 Correct 969 ms 299488 KB Output is correct
88 Correct 1036 ms 299488 KB Output is correct
89 Correct 1000 ms 300728 KB Output is correct
90 Correct 1067 ms 311088 KB Output is correct
91 Correct 1524 ms 311088 KB Output is correct