Submission #383372

# Submission time Handle Problem Language Result Execution time Memory
383372 2021-03-29T18:28:11 Z AriaH Land of the Rainbow Gold (APIO17_rainbow) C++11
12 / 100
1555 ms 218464 KB
/** I can do this all day **/
#include <bits/stdc++.h>
#include "rainbow.h"

#pragma GCC optimize("O2")
using namespace std;

typedef long long                   ll;
typedef long double                 ld;
typedef pair<int,int>               pii;
typedef pair<ll,ll>                 pll;
#define all(x)                      (x).begin(),(x).end()
#define F                           first
#define S                           second
#define Mp                          make_pair
#define SZ(x)			    		(int)x.size()
#define fast_io                     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io                     freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);

const int N = 2e5 + 10;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 8e18;
const int LOG = 22;

ll pw(ll a , ll b, ll M)  { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); }

struct segment
{
	segment () {}
	vector < int > seg[N << 2], id[N << 2];
	void add(int p, int x, int v = 1, int tl = 0, int tr = N - 1)
	{
		if(p > tr || p < tl) return;
		if(tl == tr)
		{
			seg[v].push_back(x);
			return;
		}
		int mid = (tl + tr) >> 1;
		add(p, x, v << 1, tl, mid);
		add(p, x, v << 1 | 1, mid + 1, tr);
	}
	void Merge(int v)
	{
		int n = SZ(seg[v << 1]), m = SZ(seg[v << 1 | 1]), i = 0, j = 0, k = 0;
		id[v].resize(n + m + 1, 0);
		seg[v].resize(n + m);
		while(j < n || k < m)
		{
			if(j < n && (k == m || seg[v << 1][j] < seg[v << 1 | 1][k]))
			{
				seg[v][i] = seg[v << 1][j];
				id[v][i + 1] = id[v][i] + 1;
				j ++;
			}
			else
			{
				seg[v][i] = seg[v << 1 | 1][k];
				id[v][i + 1] = id[v][i];
				k ++;
			}
			i ++;
		}
	}
	void build(int v = 1, int tl = 0, int tr = N - 1)
	{
		if(tl == tr)
		{
			sort(all(seg[v]));
			seg[v].resize(unique(all(seg[v])) - seg[v].begin());
			return;
		}
		int mid = (tl + tr) >> 1;
		build(v << 1, tl, mid);
		build(v << 1 | 1, mid + 1, tr);
		Merge(v);
	}
	int get(int l, int r, int x, int v = 1, int tl = 0, int tr = N - 1)
	{
		if(l > tr || r < tl || l > r) return 0;
		if(l <= tl && tr <= r)
		{
			return x;
		}
		int mid = (tl + tr) >> 1;
		return get(l, r, id[v][x], v << 1, tl, mid) + get(l, r, x - id[v][x], v << 1 | 1, mid + 1, tr);
	}
	int ask(int l, int r, int L, int R)
	{
		if(l > r || L > R) return 0;
		int x = upper_bound(all(seg[1]), R) - seg[1].begin();
		int x2 = upper_bound(all(seg[1]), L - 1) - seg[1].begin();
		return get(l, r, x) - get(l, r, x2);
	}
} S1, S2, S3, S4; /// S1 -> bala chap 2 * 2
				  /// S2 -> bala chap 1 * 2
				  /// S3 -> bala chap 2 * 1
				  /// S4 -> bala chap 1 * 1

map < pii, int > mp;

int mnR, mnC, mxR, mxC;

void init(int R, int C, int sr, int sc, int M, char *S)
{
	char* s = S;
	int a = sr, b = sc, n = M;
	vector < pii > points;
	mnR = mxR = a;
	mnC = mxC = b;
	points.push_back(Mp(a, b));
	for(int i = 0; i < n; i ++)
	{
		if(s[i] == 'N')
		{
			a --;
		}
		if(s[i] == 'S')
		{
			a ++;
		}
		if(s[i] == 'E')
		{
			b ++;
		}
		if(s[i] == 'W')
		{
			b --;
		}
		points.push_back(Mp(a, b));
	}
	for(auto x : points)
	{
		mp[x] = 1;
		mnR = min(mnR, x.F);
		mxR = max(mxR, x.F);
		mnC = min(mnC, x.S);
		mxC = max(mxC, x.S);
	}
	for(auto x : points)
	{
		if(mp.count(Mp(x.F + 1, x.S)) == 1 && mp.count(Mp(x.F + 1, x.S + 1)) == 1 && mp.count(Mp(x.F, x.S + 1)) == 1)
		{
			S1.add(x.F, x.S);
		}
		if(mp.count(Mp(x.F + 1, x.S)) == 1)
		{
			S2.add(x.F, x.S);
		}
		if(mp.count(Mp(x.F, x.S + 1)) == 1)
		{
			S3.add(x.F, x.S);
		}
		S4.add(x.F, x.S);
	}
	S1.build();
	S2.build();
	S3.build();
	S4.build();
}

int colour(int ar, int ac, int br, int bc)				/// V + F - M = 1 + C -> C = tedad moalefe haye graph mosatah
{									/// M - V + C + 1 = F -> toye soale ma F - 1 ro mikhaim C = 2 age doresh tohi bashe
	int r1 = ar, c1 = ac, r2 = br, c2 = bc;
	int V = 0, M = 0;
	V += S4.ask(r1, r2, c1, c2);
	M += S3.ask(r1, r2, c1, c2 - 1);
	M += S2.ask(r1, r2 - 1, c1, c2);
	M += S4.ask(r1, r2, c1, c1);
	M += S4.ask(r1, r2, c2, c2);
	M += S4.ask(r1, r1, c1, c2);
	M += S4.ask(r2, r2, c1, c2);
	int tot = M - V + 1, ret = 0;
	ret += S1.ask(r1, r2 - 1, c1, c2 - 1);
	ret += S2.ask(r1, r2 - 1, c1, c1);
	ret += S2.ask(r1, r2 - 1, c2, c2);
	ret += S3.ask(r1, r1, c1, c2 - 1);
	ret += S3.ask(r2, r2, c1, c2 - 1);
	ret += mp.count(Mp(r1, c1));
	ret += mp.count(Mp(r1, c2));
	ret += mp.count(Mp(r2, c1));
	ret += mp.count(Mp(r2, c2));
	if(mnR - 1 > r1 && mxR + 1 < r2 && mnC - 1 > c1 && mxC + 1 < c2) tot ++;
	return tot - ret;
}

/*int main()
{
    return 0;    
}
*/
/** test corner cases(n = 1?) watch for overflow or minus indices **/

# Verdict Execution time Memory Grader output
1 Incorrect 190 ms 175724 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 178 ms 175724 KB Output is correct
2 Correct 180 ms 175716 KB Output is correct
3 Correct 1148 ms 200336 KB Output is correct
4 Correct 1444 ms 215116 KB Output is correct
5 Correct 1479 ms 215188 KB Output is correct
6 Correct 1236 ms 216288 KB Output is correct
7 Correct 1555 ms 206364 KB Output is correct
8 Correct 707 ms 180428 KB Output is correct
9 Correct 1485 ms 214932 KB Output is correct
10 Correct 1516 ms 215036 KB Output is correct
11 Correct 1427 ms 216100 KB Output is correct
12 Correct 764 ms 212484 KB Output is correct
13 Correct 766 ms 214860 KB Output is correct
14 Correct 766 ms 214988 KB Output is correct
15 Correct 766 ms 216020 KB Output is correct
16 Correct 1187 ms 204296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 173 ms 175724 KB Output is correct
2 Correct 305 ms 211976 KB Output is correct
3 Correct 386 ms 218464 KB Output is correct
4 Correct 360 ms 216160 KB Output is correct
5 Correct 320 ms 206432 KB Output is correct
6 Correct 311 ms 192352 KB Output is correct
7 Incorrect 337 ms 205536 KB Output isn't correct
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 190 ms 175724 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 190 ms 175724 KB Output isn't correct
2 Halted 0 ms 0 KB -