Submission #251356

# Submission time Handle Problem Language Result Execution time Memory
251356 2020-07-21T01:07:06 Z Lawliet Land of the Rainbow Gold (APIO17_rainbow) C++17
100 / 100
2650 ms 278244 KB
#include "rainbow.h"
#include <bits/stdc++.h>
 
using namespace std;
typedef pair<int,int> pii;
typedef long long int lli;
 
const int MAXN = 200010;
 
int dx[] = { -1 , 1 , 0 , 0 };
int dy[] = { 0 , 0 , -1 , 1 };
 
char dir[] = { 'N' , 'S' , 'W' , 'E' };
 
int R, C;

class SparseSegmentTree
{
	public:
 
		void create()
		{
			e.push_back( 0 );
			d.push_back( 0 );
			sum.push_back( 0 );
		}
 
		int getLeft(int node)
		{
			if( e[node] == 0 )
				e[node] = ++cnt, create();
 
			return e[node];
		}
 
		int getRight(int node)
		{
			if( d[node] == 0 )
				d[node] = ++cnt, create();
 
			return d[node];
		}
 
		void update(int node, int l, int r, int i, int v)
		{
			if( i < l || r < i ) return;
 
			if( l == r )
			{
				sum[node] += v;
				return;
			}
 
			int m = ( l + r )/2;
 
			if( i <= m ) update( getLeft(node) , l , m , i , v );
			else update( getRight(node) , m + 1 , r , i , v );
 
			sum[node] = sum[ e[node] ] + sum[ d[node] ];
		}
 
		int query(int node, int l, int r, int i, int j)
		{
			if( node == 0 ) return 0;
			if( j < l || r < i ) return 0;
			if( i <= l && r <= j ) return sum[node];
 
			int m = ( l + r )/2;
 
			int sumLeft = query( e[node] , l , m , i , j );
			int sumRight = query( d[node] , m + 1 , r , i , j );
 
			return sumLeft + sumRight;
		}
 
		SparseSegmentTree() : cnt(1) { create(); create(); }
 
	protected:
 
		int cnt;
 
		vector<int> sum;
		vector<int> e, d;
};
 
class FenwickTree
{
	public:
 
		void update(int x, int y, int v)
		{
			for( ; x != 0 && x <= R ; x += x & -x)
				SEG[x].update( 1 , 1 , C , y , v );
		}
 
		int query(int x, int y1, int y2)
		{
			int ans = 0;
 
			for( ; x > 0 ; x -= x & -x)
				ans += SEG[x].query( 1 , 1 , C , y1 , y2 );
 
			return ans;
		}
 
		int query(int x1, int y1, int x2, int y2)
		{
			int ans = query( x2 , y1 , y2 );
			ans -= query( x1 - 1 , y1 , y2 );
 
			return ans;
		}
 
	protected:
 
		SparseSegmentTree SEG[MAXN];
};
 
int minY, maxY;
int minX, maxX;
 
FenwickTree faces;
FenwickTree vertices;
FenwickTree edgesVert, edgesHor;
 
set<pii> river;
 
int conv(char a)
{
	for(int i = 0 ; i < 4 ; i++)
		if( a == dir[i] ) return i;
}
 
bool inside(int x, int y) { return ( river.find( { x , y } ) ) != river.end(); }
 
void init(int r, int c, int sr, int sc, int M, char *S) 
{
	R = r; C = c;
	river.insert( { sr , sc } );
 
	for(int i = 0 ; i < M ; i++)
	{
		int d = conv( S[i] );
		sr += dx[d]; sc += dy[d];
 
		river.insert( { sr , sc } );
	}
 
	maxX = -MAXN; minX = MAXN;
	maxY = -MAXN; minY = MAXN;
 
	for(auto it = river.begin() ; it != river.end() ; it++)
	{
		int x = it->first;
		int y = it->second;
 
		maxX = max( maxX , x ); minX = min( minX , x );
		maxY = max( maxY , y ); minY = min( minY , y );
 
		vertices.update( x , y , 1 );
 
		edgesHor.update( x , y , 1 );
		edgesVert.update( x , y , 1 );
 
		if( !inside( x - 1 , y ) )
			edgesVert.update( x - 1 , y , 1 );
 
		if( !inside( x , y - 1 ) )
			edgesHor.update( x , y - 1 , 1 );
 
		faces.update( x , y , 1 );
 
		if( !inside( x - 1 , y ) )
			faces.update( x - 1 , y , 1 );
 
		if( !inside( x , y - 1 ) && !inside( x + 1 , y - 1 ) )
			faces.update( x , y - 1 , 1 );
 
		if( !inside( x , y - 1 ) && !inside( x - 1 , y ) && !inside( x - 1 , y - 1 ) )
			faces.update( x - 1 , y - 1 , 1 );
	}
}
 
int colour(int ar, int ac, int br, int bc) 
{
	lli N = br - ar + 1;
	lli M = bc - ac + 1;
 
	lli qtdVertices = N*M - vertices.query( ar , ac , br , bc );
 
	lli qtdEdges = 2*N*M - N - M;
	qtdEdges -= edgesHor.query( ar , ac , br , bc - 1 );
	qtdEdges -= edgesVert.query( ar , ac , br - 1 , bc );
 
	lli qtdFaces = (N - 1)*(M - 1);
	qtdFaces -= faces.query( ar , ac , br - 1 , bc - 1 );
 
	lli ans = qtdVertices - qtdEdges + qtdFaces;
 
	if( ar < minX && maxX < br && ac < minY && maxY < bc )
		ans++;
 
    return ans;
}

Compilation message

rainbow.cpp: In function 'int conv(char)':
rainbow.cpp:132:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 290 ms 138200 KB Output is correct
2 Correct 309 ms 138488 KB Output is correct
3 Correct 303 ms 138364 KB Output is correct
4 Correct 285 ms 138232 KB Output is correct
5 Correct 314 ms 138420 KB Output is correct
6 Correct 296 ms 138104 KB Output is correct
7 Correct 303 ms 138104 KB Output is correct
8 Correct 284 ms 138108 KB Output is correct
9 Correct 283 ms 138060 KB Output is correct
10 Correct 284 ms 138104 KB Output is correct
11 Correct 302 ms 138256 KB Output is correct
12 Correct 330 ms 138232 KB Output is correct
13 Correct 329 ms 138488 KB Output is correct
14 Correct 330 ms 138488 KB Output is correct
15 Correct 289 ms 138232 KB Output is correct
16 Correct 319 ms 138204 KB Output is correct
17 Correct 289 ms 138104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 319 ms 138204 KB Output is correct
2 Correct 289 ms 138104 KB Output is correct
3 Correct 621 ms 151652 KB Output is correct
4 Correct 842 ms 163580 KB Output is correct
5 Correct 814 ms 162100 KB Output is correct
6 Correct 636 ms 155628 KB Output is correct
7 Correct 668 ms 155300 KB Output is correct
8 Correct 387 ms 141816 KB Output is correct
9 Correct 825 ms 163588 KB Output is correct
10 Correct 796 ms 162420 KB Output is correct
11 Correct 635 ms 155696 KB Output is correct
12 Correct 578 ms 162472 KB Output is correct
13 Correct 595 ms 163384 KB Output is correct
14 Correct 596 ms 162064 KB Output is correct
15 Correct 592 ms 155632 KB Output is correct
16 Correct 635 ms 157588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 289 ms 138232 KB Output is correct
2 Correct 1593 ms 225348 KB Output is correct
3 Correct 1609 ms 274504 KB Output is correct
4 Correct 1714 ms 270220 KB Output is correct
5 Correct 1298 ms 231576 KB Output is correct
6 Correct 505 ms 144760 KB Output is correct
7 Correct 642 ms 150520 KB Output is correct
8 Correct 544 ms 164776 KB Output is correct
9 Correct 569 ms 163020 KB Output is correct
10 Correct 371 ms 143608 KB Output is correct
11 Correct 488 ms 162676 KB Output is correct
12 Correct 1605 ms 225428 KB Output is correct
13 Correct 1579 ms 274840 KB Output is correct
14 Correct 1864 ms 270224 KB Output is correct
15 Correct 1294 ms 231572 KB Output is correct
16 Correct 470 ms 143736 KB Output is correct
17 Correct 727 ms 150648 KB Output is correct
18 Correct 1637 ms 164216 KB Output is correct
19 Correct 1580 ms 256168 KB Output is correct
20 Correct 1770 ms 266152 KB Output is correct
21 Correct 728 ms 164844 KB Output is correct
22 Correct 558 ms 163152 KB Output is correct
23 Correct 367 ms 143648 KB Output is correct
24 Correct 488 ms 162804 KB Output is correct
25 Correct 1619 ms 225412 KB Output is correct
26 Correct 1557 ms 274588 KB Output is correct
27 Correct 1703 ms 270192 KB Output is correct
28 Correct 1341 ms 231572 KB Output is correct
29 Correct 474 ms 143608 KB Output is correct
30 Correct 726 ms 150648 KB Output is correct
31 Correct 1642 ms 164132 KB Output is correct
32 Correct 1561 ms 256196 KB Output is correct
33 Correct 1777 ms 266176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 290 ms 138200 KB Output is correct
2 Correct 309 ms 138488 KB Output is correct
3 Correct 303 ms 138364 KB Output is correct
4 Correct 285 ms 138232 KB Output is correct
5 Correct 314 ms 138420 KB Output is correct
6 Correct 296 ms 138104 KB Output is correct
7 Correct 303 ms 138104 KB Output is correct
8 Correct 284 ms 138108 KB Output is correct
9 Correct 283 ms 138060 KB Output is correct
10 Correct 284 ms 138104 KB Output is correct
11 Correct 302 ms 138256 KB Output is correct
12 Correct 330 ms 138232 KB Output is correct
13 Correct 329 ms 138488 KB Output is correct
14 Correct 330 ms 138488 KB Output is correct
15 Correct 289 ms 138232 KB Output is correct
16 Correct 319 ms 138204 KB Output is correct
17 Correct 289 ms 138104 KB Output is correct
18 Correct 1919 ms 166380 KB Output is correct
19 Correct 660 ms 140024 KB Output is correct
20 Correct 547 ms 139128 KB Output is correct
21 Correct 690 ms 139384 KB Output is correct
22 Correct 634 ms 139896 KB Output is correct
23 Correct 649 ms 140024 KB Output is correct
24 Correct 554 ms 139196 KB Output is correct
25 Correct 629 ms 139408 KB Output is correct
26 Correct 647 ms 140024 KB Output is correct
27 Correct 737 ms 151800 KB Output is correct
28 Correct 632 ms 145656 KB Output is correct
29 Correct 719 ms 150392 KB Output is correct
30 Correct 1199 ms 164472 KB Output is correct
31 Correct 286 ms 138176 KB Output is correct
32 Correct 1192 ms 153308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 290 ms 138200 KB Output is correct
2 Correct 309 ms 138488 KB Output is correct
3 Correct 303 ms 138364 KB Output is correct
4 Correct 285 ms 138232 KB Output is correct
5 Correct 314 ms 138420 KB Output is correct
6 Correct 296 ms 138104 KB Output is correct
7 Correct 303 ms 138104 KB Output is correct
8 Correct 284 ms 138108 KB Output is correct
9 Correct 283 ms 138060 KB Output is correct
10 Correct 284 ms 138104 KB Output is correct
11 Correct 302 ms 138256 KB Output is correct
12 Correct 330 ms 138232 KB Output is correct
13 Correct 329 ms 138488 KB Output is correct
14 Correct 330 ms 138488 KB Output is correct
15 Correct 289 ms 138232 KB Output is correct
16 Correct 319 ms 138204 KB Output is correct
17 Correct 289 ms 138104 KB Output is correct
18 Correct 1919 ms 166380 KB Output is correct
19 Correct 660 ms 140024 KB Output is correct
20 Correct 547 ms 139128 KB Output is correct
21 Correct 690 ms 139384 KB Output is correct
22 Correct 634 ms 139896 KB Output is correct
23 Correct 649 ms 140024 KB Output is correct
24 Correct 554 ms 139196 KB Output is correct
25 Correct 629 ms 139408 KB Output is correct
26 Correct 647 ms 140024 KB Output is correct
27 Correct 737 ms 151800 KB Output is correct
28 Correct 632 ms 145656 KB Output is correct
29 Correct 719 ms 150392 KB Output is correct
30 Correct 1199 ms 164472 KB Output is correct
31 Correct 286 ms 138176 KB Output is correct
32 Correct 1192 ms 153308 KB Output is correct
33 Correct 1593 ms 225348 KB Output is correct
34 Correct 1609 ms 274504 KB Output is correct
35 Correct 1714 ms 270220 KB Output is correct
36 Correct 1298 ms 231576 KB Output is correct
37 Correct 505 ms 144760 KB Output is correct
38 Correct 642 ms 150520 KB Output is correct
39 Correct 544 ms 164776 KB Output is correct
40 Correct 569 ms 163020 KB Output is correct
41 Correct 371 ms 143608 KB Output is correct
42 Correct 488 ms 162676 KB Output is correct
43 Correct 1605 ms 225428 KB Output is correct
44 Correct 1579 ms 274840 KB Output is correct
45 Correct 1864 ms 270224 KB Output is correct
46 Correct 1294 ms 231572 KB Output is correct
47 Correct 470 ms 143736 KB Output is correct
48 Correct 727 ms 150648 KB Output is correct
49 Correct 1637 ms 164216 KB Output is correct
50 Correct 1580 ms 256168 KB Output is correct
51 Correct 1770 ms 266152 KB Output is correct
52 Correct 728 ms 164844 KB Output is correct
53 Correct 558 ms 163152 KB Output is correct
54 Correct 367 ms 143648 KB Output is correct
55 Correct 488 ms 162804 KB Output is correct
56 Correct 1619 ms 225412 KB Output is correct
57 Correct 1557 ms 274588 KB Output is correct
58 Correct 1703 ms 270192 KB Output is correct
59 Correct 1341 ms 231572 KB Output is correct
60 Correct 474 ms 143608 KB Output is correct
61 Correct 726 ms 150648 KB Output is correct
62 Correct 1642 ms 164132 KB Output is correct
63 Correct 1561 ms 256196 KB Output is correct
64 Correct 1777 ms 266176 KB Output is correct
65 Correct 1493 ms 168384 KB Output is correct
66 Correct 1550 ms 166420 KB Output is correct
67 Correct 783 ms 147084 KB Output is correct
68 Correct 1018 ms 166128 KB Output is correct
69 Correct 2477 ms 229144 KB Output is correct
70 Correct 2092 ms 278244 KB Output is correct
71 Correct 2571 ms 273576 KB Output is correct
72 Correct 2016 ms 235116 KB Output is correct
73 Correct 1091 ms 147192 KB Output is correct
74 Correct 1266 ms 154316 KB Output is correct
75 Correct 2419 ms 167856 KB Output is correct
76 Correct 2650 ms 259640 KB Output is correct
77 Correct 2515 ms 269668 KB Output is correct
78 Correct 621 ms 151652 KB Output is correct
79 Correct 842 ms 163580 KB Output is correct
80 Correct 814 ms 162100 KB Output is correct
81 Correct 636 ms 155628 KB Output is correct
82 Correct 668 ms 155300 KB Output is correct
83 Correct 387 ms 141816 KB Output is correct
84 Correct 825 ms 163588 KB Output is correct
85 Correct 796 ms 162420 KB Output is correct
86 Correct 635 ms 155696 KB Output is correct
87 Correct 578 ms 162472 KB Output is correct
88 Correct 595 ms 163384 KB Output is correct
89 Correct 596 ms 162064 KB Output is correct
90 Correct 592 ms 155632 KB Output is correct
91 Correct 635 ms 157588 KB Output is correct