#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 ;
}
void qryTogether(int pos1, int pos2, int l, int r, int beg, int en, int &z, int &x )
{
if( l > en || r < beg ) return ;
if( l >= beg && r <= en )
{
z += _sum[pos1] ;
x += _sum[pos2] ;
return ;
}
qryTogether( lef[pos1], lef[pos2] , l , m(l,r) , beg, en , z, x ) ;
qryTogether( rig[pos1], rig[pos2], m(l,r)+1, r, beg, en, z, x ) ;
}
void subtraction(int pos1, int pos2, int l, int r ,int beg, int en, int &y )
{
if( l > en || r < beg ) return ;
if( l >= beg && r <= en )
{
y += _sum[pos1] - _sum[pos2] ;
return ;
}
subtraction(lef[pos1], lef[pos2], l, m(l,r), beg, en, y ) ;
subtraction(rig[pos1], rig[pos2], m(l,r)+1, r, beg, en, y ) ;
}
} ;
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 = 0 , x = 0 , y = 0 ;
vertices[0].qryTogether(vertices[0].roots[bc], vertices[0].roots[ac-1], 1, R, ar, br , z, x ) ;
vertices[1].subtraction( vertices[1].roots[bc] , vertices[1].roots[ac-1], 1 , R , ar , br , y ) ;
y = (y - x - z )/2 ;
int qtdVert = x + y + z ;
//Count of edges
br-- ;
z = x = y = 0 ;
edges[0].qryTogether(edges[0].roots[bc], edges[0].roots[ac-1] , 1 , R , ar , br , z, x ) ;
edges[1].subtraction( edges[1].roots[bc], edges[1].roots[ac-1] , 1 , R , ar , br, y ) ;
y = (y-x-z)/2 ;
int qtdEdges = x + y + z ;
return qtdVert - qtdEdges + toSum ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
876 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
876 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
584 KB |
Output is correct |
13 |
Correct |
4 ms |
1132 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
77 ms |
4452 KB |
Output is correct |
4 |
Correct |
78 ms |
5088 KB |
Output is correct |
5 |
Correct |
94 ms |
6496 KB |
Output is correct |
6 |
Correct |
88 ms |
6644 KB |
Output is correct |
7 |
Correct |
98 ms |
9184 KB |
Output is correct |
8 |
Correct |
82 ms |
5088 KB |
Output is correct |
9 |
Correct |
80 ms |
5216 KB |
Output is correct |
10 |
Correct |
91 ms |
6368 KB |
Output is correct |
11 |
Correct |
94 ms |
6752 KB |
Output is correct |
12 |
Correct |
63 ms |
4996 KB |
Output is correct |
13 |
Correct |
65 ms |
5088 KB |
Output is correct |
14 |
Correct |
67 ms |
6516 KB |
Output is correct |
15 |
Correct |
69 ms |
6880 KB |
Output is correct |
16 |
Correct |
86 ms |
4916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1174 ms |
387280 KB |
Output is correct |
3 |
Correct |
1956 ms |
590592 KB |
Output is correct |
4 |
Correct |
1632 ms |
485032 KB |
Output is correct |
5 |
Correct |
1655 ms |
485124 KB |
Output is correct |
6 |
Correct |
1169 ms |
391640 KB |
Output is correct |
7 |
Correct |
1176 ms |
392944 KB |
Output is correct |
8 |
Correct |
15 ms |
7264 KB |
Output is correct |
9 |
Correct |
30 ms |
13792 KB |
Output is correct |
10 |
Correct |
561 ms |
195864 KB |
Output is correct |
11 |
Correct |
774 ms |
236444 KB |
Output is correct |
12 |
Correct |
1149 ms |
387364 KB |
Output is correct |
13 |
Correct |
1931 ms |
590412 KB |
Output is correct |
14 |
Correct |
1605 ms |
484912 KB |
Output is correct |
15 |
Correct |
1656 ms |
485228 KB |
Output is correct |
16 |
Correct |
1178 ms |
390912 KB |
Output is correct |
17 |
Correct |
1175 ms |
393200 KB |
Output is correct |
18 |
Correct |
1572 ms |
483752 KB |
Output is correct |
19 |
Correct |
1142 ms |
396292 KB |
Output is correct |
20 |
Correct |
1158 ms |
396168 KB |
Output is correct |
21 |
Correct |
15 ms |
7264 KB |
Output is correct |
22 |
Correct |
31 ms |
13792 KB |
Output is correct |
23 |
Correct |
565 ms |
195736 KB |
Output is correct |
24 |
Correct |
784 ms |
236616 KB |
Output is correct |
25 |
Correct |
1158 ms |
387228 KB |
Output is correct |
26 |
Correct |
1916 ms |
590532 KB |
Output is correct |
27 |
Correct |
1612 ms |
485128 KB |
Output is correct |
28 |
Correct |
1659 ms |
484980 KB |
Output is correct |
29 |
Correct |
1160 ms |
390892 KB |
Output is correct |
30 |
Correct |
1181 ms |
393064 KB |
Output is correct |
31 |
Correct |
1574 ms |
483708 KB |
Output is correct |
32 |
Correct |
1176 ms |
396420 KB |
Output is correct |
33 |
Correct |
1152 ms |
396168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
876 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
876 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
584 KB |
Output is correct |
13 |
Correct |
4 ms |
1132 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
628 ms |
52240 KB |
Output is correct |
19 |
Correct |
119 ms |
1516 KB |
Output is correct |
20 |
Correct |
117 ms |
1516 KB |
Output is correct |
21 |
Correct |
139 ms |
1900 KB |
Output is correct |
22 |
Correct |
143 ms |
2540 KB |
Output is correct |
23 |
Correct |
117 ms |
1516 KB |
Output is correct |
24 |
Correct |
118 ms |
1516 KB |
Output is correct |
25 |
Correct |
141 ms |
2028 KB |
Output is correct |
26 |
Correct |
143 ms |
2668 KB |
Output is correct |
27 |
Correct |
302 ms |
27612 KB |
Output is correct |
28 |
Correct |
226 ms |
8672 KB |
Output is correct |
29 |
Correct |
258 ms |
12396 KB |
Output is correct |
30 |
Correct |
460 ms |
60656 KB |
Output is correct |
31 |
Correct |
3 ms |
364 KB |
Output is correct |
32 |
Correct |
242 ms |
13268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
876 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
3 ms |
876 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
584 KB |
Output is correct |
13 |
Correct |
4 ms |
1132 KB |
Output is correct |
14 |
Correct |
3 ms |
876 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
628 ms |
52240 KB |
Output is correct |
19 |
Correct |
119 ms |
1516 KB |
Output is correct |
20 |
Correct |
117 ms |
1516 KB |
Output is correct |
21 |
Correct |
139 ms |
1900 KB |
Output is correct |
22 |
Correct |
143 ms |
2540 KB |
Output is correct |
23 |
Correct |
117 ms |
1516 KB |
Output is correct |
24 |
Correct |
118 ms |
1516 KB |
Output is correct |
25 |
Correct |
141 ms |
2028 KB |
Output is correct |
26 |
Correct |
143 ms |
2668 KB |
Output is correct |
27 |
Correct |
302 ms |
27612 KB |
Output is correct |
28 |
Correct |
226 ms |
8672 KB |
Output is correct |
29 |
Correct |
258 ms |
12396 KB |
Output is correct |
30 |
Correct |
460 ms |
60656 KB |
Output is correct |
31 |
Correct |
3 ms |
364 KB |
Output is correct |
32 |
Correct |
242 ms |
13268 KB |
Output is correct |
33 |
Correct |
1174 ms |
387280 KB |
Output is correct |
34 |
Correct |
1956 ms |
590592 KB |
Output is correct |
35 |
Correct |
1632 ms |
485032 KB |
Output is correct |
36 |
Correct |
1655 ms |
485124 KB |
Output is correct |
37 |
Correct |
1169 ms |
391640 KB |
Output is correct |
38 |
Correct |
1176 ms |
392944 KB |
Output is correct |
39 |
Correct |
15 ms |
7264 KB |
Output is correct |
40 |
Correct |
30 ms |
13792 KB |
Output is correct |
41 |
Correct |
561 ms |
195864 KB |
Output is correct |
42 |
Correct |
774 ms |
236444 KB |
Output is correct |
43 |
Correct |
1149 ms |
387364 KB |
Output is correct |
44 |
Correct |
1931 ms |
590412 KB |
Output is correct |
45 |
Correct |
1605 ms |
484912 KB |
Output is correct |
46 |
Correct |
1656 ms |
485228 KB |
Output is correct |
47 |
Correct |
1178 ms |
390912 KB |
Output is correct |
48 |
Correct |
1175 ms |
393200 KB |
Output is correct |
49 |
Correct |
1572 ms |
483752 KB |
Output is correct |
50 |
Correct |
1142 ms |
396292 KB |
Output is correct |
51 |
Correct |
1158 ms |
396168 KB |
Output is correct |
52 |
Correct |
15 ms |
7264 KB |
Output is correct |
53 |
Correct |
31 ms |
13792 KB |
Output is correct |
54 |
Correct |
565 ms |
195736 KB |
Output is correct |
55 |
Correct |
784 ms |
236616 KB |
Output is correct |
56 |
Correct |
1158 ms |
387228 KB |
Output is correct |
57 |
Correct |
1916 ms |
590532 KB |
Output is correct |
58 |
Correct |
1612 ms |
485128 KB |
Output is correct |
59 |
Correct |
1659 ms |
484980 KB |
Output is correct |
60 |
Correct |
1160 ms |
390892 KB |
Output is correct |
61 |
Correct |
1181 ms |
393064 KB |
Output is correct |
62 |
Correct |
1574 ms |
483708 KB |
Output is correct |
63 |
Correct |
1176 ms |
396420 KB |
Output is correct |
64 |
Correct |
1152 ms |
396168 KB |
Output is correct |
65 |
Correct |
109 ms |
7264 KB |
Output is correct |
66 |
Correct |
180 ms |
13792 KB |
Output is correct |
67 |
Correct |
806 ms |
195780 KB |
Output is correct |
68 |
Correct |
1106 ms |
236560 KB |
Output is correct |
69 |
Correct |
1997 ms |
387340 KB |
Output is correct |
70 |
Correct |
2825 ms |
590520 KB |
Output is correct |
71 |
Correct |
2507 ms |
485080 KB |
Output is correct |
72 |
Correct |
2551 ms |
485156 KB |
Output is correct |
73 |
Correct |
1910 ms |
391236 KB |
Output is correct |
74 |
Correct |
2017 ms |
393320 KB |
Output is correct |
75 |
Correct |
2444 ms |
483952 KB |
Output is correct |
76 |
Correct |
1973 ms |
396420 KB |
Output is correct |
77 |
Correct |
1726 ms |
396680 KB |
Output is correct |
78 |
Correct |
77 ms |
4452 KB |
Output is correct |
79 |
Correct |
78 ms |
5088 KB |
Output is correct |
80 |
Correct |
94 ms |
6496 KB |
Output is correct |
81 |
Correct |
88 ms |
6644 KB |
Output is correct |
82 |
Correct |
98 ms |
9184 KB |
Output is correct |
83 |
Correct |
82 ms |
5088 KB |
Output is correct |
84 |
Correct |
80 ms |
5216 KB |
Output is correct |
85 |
Correct |
91 ms |
6368 KB |
Output is correct |
86 |
Correct |
94 ms |
6752 KB |
Output is correct |
87 |
Correct |
63 ms |
4996 KB |
Output is correct |
88 |
Correct |
65 ms |
5088 KB |
Output is correct |
89 |
Correct |
67 ms |
6516 KB |
Output is correct |
90 |
Correct |
69 ms |
6880 KB |
Output is correct |
91 |
Correct |
86 ms |
4916 KB |
Output is correct |