#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 PersistentSegmentTree
{
public:
void copy(int node)
{
e.push_back( e[node] );
d.push_back( d[node] );
sum.push_back( sum[node] );
}
int update(int node, int l, int r, int i)
{
int newNode = ++cnt;
copy( node );
if( l == r )
{
sum[newNode]++;
return newNode;
}
int m = ( l + r )/2;
if( i <= m )
{
int aux = update( e[newNode] , l , m , i );
e[newNode] = aux;
}
else
{
int aux = update( d[newNode] , m + 1 , r , i );
d[newNode] = aux;
}
sum[newNode] = sum[ e[newNode] ] + sum[ d[newNode] ];
return newNode;
}
void updateRoot() { root.push_back( root.back() ); }
void update(int y)
{
if( y < 1 || C < y ) return;
int r = update( root.back() , 1 , C , y );
root.back() = r;
}
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;
}
int query(int x1, int y1, int x2, int y2)
{
int ans = query( root[x2] , 1 , C , y1 , y2 );
ans -= query( root[x1 - 1] , 1 , C , y1 , y2 );
return ans;
}
PersistentSegmentTree() : cnt(1)
{
e.resize( 2 , 0 ); d.resize( 2 , 0 );
sum.resize( 2 , 0 ); root.resize( 1 , 1 );
}
protected:
int cnt;
vector<int> e, d;
vector<int> sum, root;
};
int minY, maxY;
int minX, maxX;
vector<int> sweep[4][MAXN];
PersistentSegmentTree SEG[4];
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 );
sweep[0][x].push_back( y );
sweep[1][x].push_back( y );//Hor
sweep[2][x].push_back( y );
if( !inside( x , y - 1 ) )
sweep[1][x].push_back( y - 1 );
if( !inside( x - 1 , y ) )
sweep[2][x - 1].push_back( y );
sweep[3][x].push_back( y );
if( !inside( x - 1 , y ) )
sweep[3][x - 1].push_back( y );
if( !inside( x , y - 1 ) && !inside( x + 1 , y - 1 ) )
sweep[3][x].push_back( y - 1 );
if( !inside( x , y - 1 ) && !inside( x - 1 , y ) && !inside( x - 1 , y - 1 ) )
sweep[3][x - 1].push_back( y - 1 );
}
for(int i = 1 ; i <= R ; i++)
{
for(int k = 0 ; k < 4 ; k++)
{
SEG[k].updateRoot();
while( !sweep[k][i].empty() )
{
SEG[k].update( sweep[k][i].back() );
sweep[k][i].pop_back();
}
}
}
}
int colour(int ar, int ac, int br, int bc)
{
lli N = br - ar + 1;
lli M = bc - ac + 1;
lli qtdVertices = N*M - SEG[0].query( ar , ac , br , bc );
lli qtdEdges = 2*N*M - N - M;
qtdEdges -= SEG[1].query( ar , ac , br , bc - 1 );
qtdEdges -= SEG[2].query( ar , ac , br - 1 , bc );
lli qtdFaces = (N - 1)*(M - 1);
qtdFaces -= SEG[3].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:116:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
19200 KB |
Output is correct |
2 |
Correct |
23 ms |
19712 KB |
Output is correct |
3 |
Correct |
13 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19328 KB |
Output is correct |
5 |
Correct |
14 ms |
19840 KB |
Output is correct |
6 |
Correct |
13 ms |
19072 KB |
Output is correct |
7 |
Correct |
12 ms |
19072 KB |
Output is correct |
8 |
Correct |
12 ms |
19072 KB |
Output is correct |
9 |
Correct |
13 ms |
19072 KB |
Output is correct |
10 |
Correct |
13 ms |
19072 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
14 ms |
19584 KB |
Output is correct |
13 |
Correct |
15 ms |
20224 KB |
Output is correct |
14 |
Correct |
16 ms |
20480 KB |
Output is correct |
15 |
Correct |
12 ms |
19072 KB |
Output is correct |
16 |
Correct |
12 ms |
19072 KB |
Output is correct |
17 |
Correct |
14 ms |
19072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19072 KB |
Output is correct |
2 |
Correct |
14 ms |
19072 KB |
Output is correct |
3 |
Correct |
465 ms |
103004 KB |
Output is correct |
4 |
Correct |
634 ms |
149540 KB |
Output is correct |
5 |
Correct |
637 ms |
148776 KB |
Output is correct |
6 |
Correct |
443 ms |
111016 KB |
Output is correct |
7 |
Correct |
543 ms |
106472 KB |
Output is correct |
8 |
Correct |
92 ms |
19888 KB |
Output is correct |
9 |
Correct |
640 ms |
149176 KB |
Output is correct |
10 |
Correct |
631 ms |
148704 KB |
Output is correct |
11 |
Correct |
491 ms |
111064 KB |
Output is correct |
12 |
Correct |
358 ms |
145720 KB |
Output is correct |
13 |
Correct |
366 ms |
149436 KB |
Output is correct |
14 |
Correct |
371 ms |
148804 KB |
Output is correct |
15 |
Correct |
319 ms |
111272 KB |
Output is correct |
16 |
Correct |
499 ms |
110936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19072 KB |
Output is correct |
2 |
Correct |
371 ms |
168452 KB |
Output is correct |
3 |
Correct |
444 ms |
183852 KB |
Output is correct |
4 |
Correct |
453 ms |
167480 KB |
Output is correct |
5 |
Correct |
339 ms |
136636 KB |
Output is correct |
6 |
Correct |
108 ms |
50032 KB |
Output is correct |
7 |
Correct |
187 ms |
78780 KB |
Output is correct |
8 |
Correct |
292 ms |
124464 KB |
Output is correct |
9 |
Correct |
255 ms |
102868 KB |
Output is correct |
10 |
Correct |
73 ms |
40872 KB |
Output is correct |
11 |
Correct |
186 ms |
85404 KB |
Output is correct |
12 |
Correct |
372 ms |
168452 KB |
Output is correct |
13 |
Correct |
426 ms |
183788 KB |
Output is correct |
14 |
Correct |
437 ms |
167368 KB |
Output is correct |
15 |
Correct |
331 ms |
136640 KB |
Output is correct |
16 |
Correct |
89 ms |
43540 KB |
Output is correct |
17 |
Correct |
201 ms |
77652 KB |
Output is correct |
18 |
Correct |
393 ms |
163840 KB |
Output is correct |
19 |
Correct |
373 ms |
147276 KB |
Output is correct |
20 |
Correct |
379 ms |
147408 KB |
Output is correct |
21 |
Correct |
288 ms |
124428 KB |
Output is correct |
22 |
Correct |
257 ms |
102884 KB |
Output is correct |
23 |
Correct |
75 ms |
40880 KB |
Output is correct |
24 |
Correct |
189 ms |
85396 KB |
Output is correct |
25 |
Correct |
393 ms |
168448 KB |
Output is correct |
26 |
Correct |
415 ms |
183908 KB |
Output is correct |
27 |
Correct |
465 ms |
167368 KB |
Output is correct |
28 |
Correct |
330 ms |
136640 KB |
Output is correct |
29 |
Correct |
91 ms |
43548 KB |
Output is correct |
30 |
Correct |
197 ms |
77556 KB |
Output is correct |
31 |
Correct |
398 ms |
163840 KB |
Output is correct |
32 |
Correct |
374 ms |
147288 KB |
Output is correct |
33 |
Correct |
384 ms |
147304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
19200 KB |
Output is correct |
2 |
Correct |
23 ms |
19712 KB |
Output is correct |
3 |
Correct |
13 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19328 KB |
Output is correct |
5 |
Correct |
14 ms |
19840 KB |
Output is correct |
6 |
Correct |
13 ms |
19072 KB |
Output is correct |
7 |
Correct |
12 ms |
19072 KB |
Output is correct |
8 |
Correct |
12 ms |
19072 KB |
Output is correct |
9 |
Correct |
13 ms |
19072 KB |
Output is correct |
10 |
Correct |
13 ms |
19072 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
14 ms |
19584 KB |
Output is correct |
13 |
Correct |
15 ms |
20224 KB |
Output is correct |
14 |
Correct |
16 ms |
20480 KB |
Output is correct |
15 |
Correct |
12 ms |
19072 KB |
Output is correct |
16 |
Correct |
12 ms |
19072 KB |
Output is correct |
17 |
Correct |
14 ms |
19072 KB |
Output is correct |
18 |
Correct |
748 ms |
65036 KB |
Output is correct |
19 |
Correct |
245 ms |
22360 KB |
Output is correct |
20 |
Correct |
163 ms |
20216 KB |
Output is correct |
21 |
Correct |
188 ms |
20600 KB |
Output is correct |
22 |
Correct |
202 ms |
21240 KB |
Output is correct |
23 |
Correct |
234 ms |
22124 KB |
Output is correct |
24 |
Correct |
168 ms |
20344 KB |
Output is correct |
25 |
Correct |
190 ms |
20984 KB |
Output is correct |
26 |
Correct |
215 ms |
21368 KB |
Output is correct |
27 |
Correct |
274 ms |
55536 KB |
Output is correct |
28 |
Correct |
207 ms |
37504 KB |
Output is correct |
29 |
Correct |
272 ms |
52620 KB |
Output is correct |
30 |
Correct |
563 ms |
109516 KB |
Output is correct |
31 |
Correct |
16 ms |
19200 KB |
Output is correct |
32 |
Correct |
748 ms |
56012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
19200 KB |
Output is correct |
2 |
Correct |
23 ms |
19712 KB |
Output is correct |
3 |
Correct |
13 ms |
19200 KB |
Output is correct |
4 |
Correct |
15 ms |
19328 KB |
Output is correct |
5 |
Correct |
14 ms |
19840 KB |
Output is correct |
6 |
Correct |
13 ms |
19072 KB |
Output is correct |
7 |
Correct |
12 ms |
19072 KB |
Output is correct |
8 |
Correct |
12 ms |
19072 KB |
Output is correct |
9 |
Correct |
13 ms |
19072 KB |
Output is correct |
10 |
Correct |
13 ms |
19072 KB |
Output is correct |
11 |
Correct |
16 ms |
19328 KB |
Output is correct |
12 |
Correct |
14 ms |
19584 KB |
Output is correct |
13 |
Correct |
15 ms |
20224 KB |
Output is correct |
14 |
Correct |
16 ms |
20480 KB |
Output is correct |
15 |
Correct |
12 ms |
19072 KB |
Output is correct |
16 |
Correct |
12 ms |
19072 KB |
Output is correct |
17 |
Correct |
14 ms |
19072 KB |
Output is correct |
18 |
Correct |
748 ms |
65036 KB |
Output is correct |
19 |
Correct |
245 ms |
22360 KB |
Output is correct |
20 |
Correct |
163 ms |
20216 KB |
Output is correct |
21 |
Correct |
188 ms |
20600 KB |
Output is correct |
22 |
Correct |
202 ms |
21240 KB |
Output is correct |
23 |
Correct |
234 ms |
22124 KB |
Output is correct |
24 |
Correct |
168 ms |
20344 KB |
Output is correct |
25 |
Correct |
190 ms |
20984 KB |
Output is correct |
26 |
Correct |
215 ms |
21368 KB |
Output is correct |
27 |
Correct |
274 ms |
55536 KB |
Output is correct |
28 |
Correct |
207 ms |
37504 KB |
Output is correct |
29 |
Correct |
272 ms |
52620 KB |
Output is correct |
30 |
Correct |
563 ms |
109516 KB |
Output is correct |
31 |
Correct |
16 ms |
19200 KB |
Output is correct |
32 |
Correct |
748 ms |
56012 KB |
Output is correct |
33 |
Correct |
371 ms |
168452 KB |
Output is correct |
34 |
Correct |
444 ms |
183852 KB |
Output is correct |
35 |
Correct |
453 ms |
167480 KB |
Output is correct |
36 |
Correct |
339 ms |
136636 KB |
Output is correct |
37 |
Correct |
108 ms |
50032 KB |
Output is correct |
38 |
Correct |
187 ms |
78780 KB |
Output is correct |
39 |
Correct |
292 ms |
124464 KB |
Output is correct |
40 |
Correct |
255 ms |
102868 KB |
Output is correct |
41 |
Correct |
73 ms |
40872 KB |
Output is correct |
42 |
Correct |
186 ms |
85404 KB |
Output is correct |
43 |
Correct |
372 ms |
168452 KB |
Output is correct |
44 |
Correct |
426 ms |
183788 KB |
Output is correct |
45 |
Correct |
437 ms |
167368 KB |
Output is correct |
46 |
Correct |
331 ms |
136640 KB |
Output is correct |
47 |
Correct |
89 ms |
43540 KB |
Output is correct |
48 |
Correct |
201 ms |
77652 KB |
Output is correct |
49 |
Correct |
393 ms |
163840 KB |
Output is correct |
50 |
Correct |
373 ms |
147276 KB |
Output is correct |
51 |
Correct |
379 ms |
147408 KB |
Output is correct |
52 |
Correct |
288 ms |
124428 KB |
Output is correct |
53 |
Correct |
257 ms |
102884 KB |
Output is correct |
54 |
Correct |
75 ms |
40880 KB |
Output is correct |
55 |
Correct |
189 ms |
85396 KB |
Output is correct |
56 |
Correct |
393 ms |
168448 KB |
Output is correct |
57 |
Correct |
415 ms |
183908 KB |
Output is correct |
58 |
Correct |
465 ms |
167368 KB |
Output is correct |
59 |
Correct |
330 ms |
136640 KB |
Output is correct |
60 |
Correct |
91 ms |
43548 KB |
Output is correct |
61 |
Correct |
197 ms |
77556 KB |
Output is correct |
62 |
Correct |
398 ms |
163840 KB |
Output is correct |
63 |
Correct |
374 ms |
147288 KB |
Output is correct |
64 |
Correct |
384 ms |
147304 KB |
Output is correct |
65 |
Correct |
1195 ms |
124452 KB |
Output is correct |
66 |
Correct |
1255 ms |
103764 KB |
Output is correct |
67 |
Correct |
488 ms |
41776 KB |
Output is correct |
68 |
Correct |
425 ms |
85720 KB |
Output is correct |
69 |
Correct |
702 ms |
168440 KB |
Output is correct |
70 |
Correct |
568 ms |
184492 KB |
Output is correct |
71 |
Correct |
720 ms |
168140 KB |
Output is correct |
72 |
Correct |
515 ms |
137040 KB |
Output is correct |
73 |
Correct |
161 ms |
44172 KB |
Output is correct |
74 |
Correct |
267 ms |
78244 KB |
Output is correct |
75 |
Correct |
477 ms |
164488 KB |
Output is correct |
76 |
Correct |
702 ms |
148152 KB |
Output is correct |
77 |
Correct |
642 ms |
148040 KB |
Output is correct |
78 |
Correct |
465 ms |
103004 KB |
Output is correct |
79 |
Correct |
634 ms |
149540 KB |
Output is correct |
80 |
Correct |
637 ms |
148776 KB |
Output is correct |
81 |
Correct |
443 ms |
111016 KB |
Output is correct |
82 |
Correct |
543 ms |
106472 KB |
Output is correct |
83 |
Correct |
92 ms |
19888 KB |
Output is correct |
84 |
Correct |
640 ms |
149176 KB |
Output is correct |
85 |
Correct |
631 ms |
148704 KB |
Output is correct |
86 |
Correct |
491 ms |
111064 KB |
Output is correct |
87 |
Correct |
358 ms |
145720 KB |
Output is correct |
88 |
Correct |
366 ms |
149436 KB |
Output is correct |
89 |
Correct |
371 ms |
148804 KB |
Output is correct |
90 |
Correct |
319 ms |
111272 KB |
Output is correct |
91 |
Correct |
499 ms |
110936 KB |
Output is correct |