답안 #328199

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
328199 2020-11-15T17:17:29 Z CaroLinda 무지개나라 (APIO17_rainbow) C++14
74 / 100
3000 ms 590932 KB
#include "rainbow.h"
#include <bits/stdc++.h>

#define mk make_pair
#define pii pair<int,int>
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define sz(x) (int)(x.size() )
#define ll long long

const int MAX = 2e5+10 ;

using namespace std ;

/*
I need two types of persistent segment trees:

* +1 in the opening and -1 in the closure
* +1 in everything
 
*/

struct persistentSeg
{

	//don't forget to create the dummy node

	vector<int> lef, rig , _sum ;
	int roots[MAX] ;
	
	int create()
	{
		lef.push_back(0) ;
		rig.push_back(0) ;
		_sum.push_back(0) ;

		return sz(lef) - 1 ;
	}

	int createAndCopy(int pos )
	{
		lef.push_back( lef[pos] ) ;
		rig.push_back( rig[pos] ) ;
		_sum.push_back( _sum[pos] ) ;

		return sz(lef) - 1 ;
	}

	int m(int l, int r ) { return (l+r)>>1 ; }

	int upd(int pos, int l, int r, int idx, int val )
	{
		int newPos = createAndCopy(pos) ;

		_sum[newPos] += val ;

		if( l== r ) return newPos ;

		if( idx <= m(l,r) ) 
		{
			int novo = upd(lef[newPos] , l , m(l,r) , idx, val ) ; 
			lef[newPos] = novo ;
		}
		else 
		{
			int novo = upd(rig[newPos] , m(l,r) + 1 , r , idx , val ) ; 
			rig[newPos] = novo ;			
		}

		return newPos ;

	}

	int qry(int pos, int l, int r, int beg, int en )
	{
		if( l > en || r < beg || !pos ) return 0 ;
		if( l >= beg && r <= en ) return _sum[pos] ;

		int al = qry(lef[pos] , l , m(l,r), beg, en  ) ;
		int ar = qry(rig[pos], m(l,r)+1, r, beg, en ) ;

		return al + ar ;
	}

} ;

struct Event
{

	//Type
	// 0 = opening
	// 1 = closure

	int r , c , type ;

	Event(int r = 0 , int c = 0 , int type = 0 ) : r(r)  ,c(c) , type(type) {} 
	
	bool operator < ( Event other ) const 
	{
		if( c != other.c ) return c < other.c ;
		return type < other.type ;
	}

	void print() { printf("%d %d %d\n", r, c , type ) ; }

} ;

int R, C ;
int maxR, minR, maxC, minC ;
persistentSeg vertices[2] , edges[2] ;

void init(int _R, int _C, int sr, int sc, int M, char *S) 
{
	
	R = _R ;
	C = _C ;

	vector< pii > serpentPath(1, make_pair(sr, sc) ) ;

	maxR = minR = sr ;
	maxC = minC = sc ;

	for(int i = 0 ; i < M ; i++ ) 
	{
		if( S[i] == 'N' ) sr-- ;
		if( S[i] == 'E' ) sc++ ;
		if(S[i] == 'S' ) sr++ ;
		if(S[i] == 'W' ) sc-- ;

		serpentPath.push_back(make_pair(sr, sc) ) ;

		maxR = max(maxR, sr ) ;
		minR = min(minR, sr) ;
		maxC = max(maxC, sc ) ;
		minC = min(minC, sc) ;

	}
	
	vector<int> freq[R+1] ;
	for(auto p : serpentPath ) freq[ p.first ].push_back( p.second ) ;

	vector<Event> sweep ;

	for(int i = 1 ; i <= R ; i++ )
	{
		if( sz(freq[i] ) == 0 )
		{
			sweep.push_back( Event(i , 1 , 0 ) ) ;
			sweep.push_back( Event(i, C , 1 ) ) ;	
			continue ;
		}
		
		sort(all(freq[i] ) ) ;
		freq[i].push_back(C+1) ;

		int formerColumn = 0 ;

		for(auto e : freq[i] )
		{
			if( formerColumn+1 <= e-1 )
			{
				sweep.push_back(Event(i, formerColumn+1, 0 ) ) ;
				sweep.push_back( Event(i, e-1, 1 ) ) ;
			}
				
			formerColumn =  e ;
		}

	}


	sort(all(sweep ) ) ;

	set<int> currentRows ;

	vertices[0].create() ; vertices[0].roots[0] = 0 ;
	vertices[1].create() ; vertices[1].roots[0] = 0 ;
	edges[0].create() ; edges[0].roots[0] = 0 ;
	edges[1].create() ; edges[1].roots[0] = 0 ;

	for(int i = 1 , ptr=0 ; i <= C ; i++ )
	{
		vertices[0].roots[i] = vertices[0].roots[i-1] ;
		vertices[1].roots[i] = vertices[1].roots[i-1] ;
		edges[0].roots[i] = edges[0].roots[i-1] ;
		edges[1].roots[i] = edges[1].roots[i-1] ;

		while( ptr < sz(sweep ) && sweep[ptr].c == i )
		{

			vertices[1].roots[i] = vertices[1].upd( vertices[1].roots[i] , 1 , R , sweep[ptr].r , 1 ) ;

			if(sweep[ptr].type == 0 )
			{
				vertices[0].roots[i] = vertices[0].upd( vertices[0].roots[i] , 1 , R , sweep[ptr].r , 1 ) ; 
				currentRows.insert( sweep[ptr].r ) ;

				auto it = currentRows.find( sweep[ptr].r ) ;

				if( it != currentRows.begin() ) 
				{
					it-- ;

					if( *it == sweep[ptr].r-1)
					{
						edges[0].roots[i] = edges[0].upd( edges[0].roots[i] , 1 , R  , *it , 1 ) ;
						edges[1].roots[i] = edges[1].upd( edges[1].roots[i] , 1 , R  , *it , 1 ) ;
					}
					it++ ;
				}
				it++ ;
				if(it != currentRows.end() && *it == sweep[ptr].r+1 )
				{
					edges[0].roots[i] = edges[0].upd( edges[0].roots[i] , 1 , R  , sweep[ptr].r , 1 ) ;
					edges[1].roots[i] = edges[1].upd( edges[1].roots[i] , 1 , R  , sweep[ptr].r , 1 ) ;					
				}

			}
			else
			{
				vertices[0].roots[i] = vertices[0].upd( vertices[0].roots[i] , 1 , R , sweep[ptr].r , -1 ) ;

				auto it = currentRows.find( sweep[ptr].r ) ;

				if( it != currentRows.begin() ) 
				{
					it-- ;
					if(*it == sweep[ptr].r-1 )
					{
						edges[0].roots[i] = edges[0].upd( edges[0].roots[i] , 1 , R  , *it , -1 ) ;
						edges[1].roots[i] = edges[1].upd( edges[1].roots[i] , 1 , R  , *it , 1 ) ;
					}
					it++ ;
				}
				it++ ;
				if(it != currentRows.end() && *it == sweep[ptr].r+1 )
				{
					edges[0].roots[i] = edges[0].upd( edges[0].roots[i] , 1 , R  , sweep[ptr].r , -1 ) ;
					edges[1].roots[i] = edges[1].upd( edges[1].roots[i] , 1 , R  , sweep[ptr].r , 1 ) ;					
				}
				it-- ;

				currentRows.erase(it) ;

			}                  

			ptr++ ;
		} 

	}

}

int colour(int ar, int ac, int br, int bc) 
{
	 
	int toSum = 0 ;

	if( ar < minR && br > maxR && ac < minC && bc > maxC )
	{
		ar = minR-1 ;
		br = maxR+1 ;
		ac = minC-1 ;
		bc = maxC+1 ;
		toSum = 1 ;	
	}

	//Count of vertices
	int z = vertices[0].qry(vertices[0].roots[bc], 1, R, ar, br ) ;
	int x = vertices[0].qry( vertices[0].roots[ac-1], 1 , R , ar , br ) ;
	int y = vertices[1].qry( vertices[1].roots[bc] , 1 , R , ar , br ) - vertices[1].qry( vertices[1].roots[ac-1] , 1 , R , ar , br ) ;

	y = (y - x - z )/2 ;

	int qtdVert = x + y + z ;

	//Count of edges
	br-- ;
	z = edges[0].qry(edges[0].roots[bc] , 1 , R , ar , br ) ;
	x = edges[0].qry( edges[0].roots[ac-1] , 1 , R , ar , br ) ;
	y = edges[1].qry( edges[1].roots[bc] , 1 , R , ar , br ) - edges[1].qry( edges[1].roots[ac-1] , 1 , R , ar , br ) ;
	y = (y-x-z)/2 ;

	int qtdEdges = x + y + z ;

	return qtdVert - qtdEdges + toSum ;

}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 3 ms 896 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 4 ms 876 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 396 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 2 ms 620 KB Output is correct
13 Correct 3 ms 1132 KB Output is correct
14 Correct 3 ms 876 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 82 ms 4452 KB Output is correct
4 Correct 87 ms 5108 KB Output is correct
5 Correct 100 ms 6496 KB Output is correct
6 Correct 97 ms 6624 KB Output is correct
7 Correct 102 ms 9184 KB Output is correct
8 Correct 86 ms 5088 KB Output is correct
9 Correct 83 ms 5216 KB Output is correct
10 Correct 101 ms 6368 KB Output is correct
11 Correct 95 ms 6752 KB Output is correct
12 Correct 62 ms 5012 KB Output is correct
13 Correct 67 ms 5088 KB Output is correct
14 Correct 70 ms 6496 KB Output is correct
15 Correct 67 ms 6880 KB Output is correct
16 Correct 85 ms 4788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1154 ms 387192 KB Output is correct
3 Correct 1932 ms 590600 KB Output is correct
4 Correct 1593 ms 484824 KB Output is correct
5 Correct 1666 ms 484944 KB Output is correct
6 Correct 1156 ms 391788 KB Output is correct
7 Correct 1169 ms 392984 KB Output is correct
8 Correct 15 ms 7392 KB Output is correct
9 Correct 30 ms 13920 KB Output is correct
10 Correct 567 ms 195992 KB Output is correct
11 Correct 772 ms 236832 KB Output is correct
12 Correct 1152 ms 387432 KB Output is correct
13 Correct 1936 ms 590848 KB Output is correct
14 Correct 1616 ms 485236 KB Output is correct
15 Correct 1665 ms 485096 KB Output is correct
16 Correct 1165 ms 391184 KB Output is correct
17 Correct 1165 ms 393228 KB Output is correct
18 Correct 1575 ms 483708 KB Output is correct
19 Correct 1181 ms 396444 KB Output is correct
20 Correct 1150 ms 396336 KB Output is correct
21 Correct 15 ms 7392 KB Output is correct
22 Correct 30 ms 13920 KB Output is correct
23 Correct 568 ms 195992 KB Output is correct
24 Correct 771 ms 236572 KB Output is correct
25 Correct 1148 ms 387828 KB Output is correct
26 Correct 1929 ms 590572 KB Output is correct
27 Correct 1628 ms 484952 KB Output is correct
28 Correct 1642 ms 485268 KB Output is correct
29 Correct 1168 ms 391260 KB Output is correct
30 Correct 1175 ms 393356 KB Output is correct
31 Correct 1563 ms 483856 KB Output is correct
32 Correct 1140 ms 396292 KB Output is correct
33 Correct 1142 ms 396588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 3 ms 896 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 4 ms 876 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 396 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 2 ms 620 KB Output is correct
13 Correct 3 ms 1132 KB Output is correct
14 Correct 3 ms 876 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 821 ms 54052 KB Output is correct
19 Correct 154 ms 4332 KB Output is correct
20 Correct 149 ms 4204 KB Output is correct
21 Correct 169 ms 4716 KB Output is correct
22 Correct 187 ms 5356 KB Output is correct
23 Correct 154 ms 4332 KB Output is correct
24 Correct 151 ms 4204 KB Output is correct
25 Correct 173 ms 4716 KB Output is correct
26 Correct 183 ms 5356 KB Output is correct
27 Correct 363 ms 29896 KB Output is correct
28 Correct 289 ms 11596 KB Output is correct
29 Correct 317 ms 14928 KB Output is correct
30 Correct 536 ms 60968 KB Output is correct
31 Correct 3 ms 492 KB Output is correct
32 Correct 314 ms 15664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 492 KB Output is correct
2 Correct 3 ms 896 KB Output is correct
3 Correct 2 ms 492 KB Output is correct
4 Correct 2 ms 620 KB Output is correct
5 Correct 4 ms 876 KB Output is correct
6 Correct 1 ms 364 KB Output is correct
7 Correct 1 ms 364 KB Output is correct
8 Correct 1 ms 396 KB Output is correct
9 Correct 1 ms 364 KB Output is correct
10 Correct 1 ms 364 KB Output is correct
11 Correct 3 ms 620 KB Output is correct
12 Correct 2 ms 620 KB Output is correct
13 Correct 3 ms 1132 KB Output is correct
14 Correct 3 ms 876 KB Output is correct
15 Correct 1 ms 364 KB Output is correct
16 Correct 0 ms 364 KB Output is correct
17 Correct 1 ms 364 KB Output is correct
18 Correct 821 ms 54052 KB Output is correct
19 Correct 154 ms 4332 KB Output is correct
20 Correct 149 ms 4204 KB Output is correct
21 Correct 169 ms 4716 KB Output is correct
22 Correct 187 ms 5356 KB Output is correct
23 Correct 154 ms 4332 KB Output is correct
24 Correct 151 ms 4204 KB Output is correct
25 Correct 173 ms 4716 KB Output is correct
26 Correct 183 ms 5356 KB Output is correct
27 Correct 363 ms 29896 KB Output is correct
28 Correct 289 ms 11596 KB Output is correct
29 Correct 317 ms 14928 KB Output is correct
30 Correct 536 ms 60968 KB Output is correct
31 Correct 3 ms 492 KB Output is correct
32 Correct 314 ms 15664 KB Output is correct
33 Correct 1154 ms 387192 KB Output is correct
34 Correct 1932 ms 590600 KB Output is correct
35 Correct 1593 ms 484824 KB Output is correct
36 Correct 1666 ms 484944 KB Output is correct
37 Correct 1156 ms 391788 KB Output is correct
38 Correct 1169 ms 392984 KB Output is correct
39 Correct 15 ms 7392 KB Output is correct
40 Correct 30 ms 13920 KB Output is correct
41 Correct 567 ms 195992 KB Output is correct
42 Correct 772 ms 236832 KB Output is correct
43 Correct 1152 ms 387432 KB Output is correct
44 Correct 1936 ms 590848 KB Output is correct
45 Correct 1616 ms 485236 KB Output is correct
46 Correct 1665 ms 485096 KB Output is correct
47 Correct 1165 ms 391184 KB Output is correct
48 Correct 1165 ms 393228 KB Output is correct
49 Correct 1575 ms 483708 KB Output is correct
50 Correct 1181 ms 396444 KB Output is correct
51 Correct 1150 ms 396336 KB Output is correct
52 Correct 15 ms 7392 KB Output is correct
53 Correct 30 ms 13920 KB Output is correct
54 Correct 568 ms 195992 KB Output is correct
55 Correct 771 ms 236572 KB Output is correct
56 Correct 1148 ms 387828 KB Output is correct
57 Correct 1929 ms 590572 KB Output is correct
58 Correct 1628 ms 484952 KB Output is correct
59 Correct 1642 ms 485268 KB Output is correct
60 Correct 1168 ms 391260 KB Output is correct
61 Correct 1175 ms 393356 KB Output is correct
62 Correct 1563 ms 483856 KB Output is correct
63 Correct 1140 ms 396292 KB Output is correct
64 Correct 1142 ms 396588 KB Output is correct
65 Correct 128 ms 10316 KB Output is correct
66 Correct 209 ms 15516 KB Output is correct
67 Correct 905 ms 196500 KB Output is correct
68 Correct 1207 ms 236816 KB Output is correct
69 Correct 2108 ms 387700 KB Output is correct
70 Execution timed out 3075 ms 590932 KB Time limit exceeded
71 Halted 0 ms 0 KB -