#include "rainbow.h"
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
/*
V - E + F - C = 1
V = land cells
E = pairs of adjacent land cells
F = 2x2 grids of land cells
C = what we want
Use merge sort trees
* Blue vertices
* Blue horizontal edges (at top endpoint)
* Blue vertical edges (at left endpoint)
* 2x2 grids of white cells (at TL corner)
Also, check if a set of cells surrounding entire snake is captured in the query
1 2 3 4
1 X X X
2 X X X
3 X X X
4 X X X
5 X X
6
*/
struct segtree
{
int l;
int r;
vector<int> v;
segtree* left = NULL;
segtree* right = NULL;
segtree()
{
;
}
segtree(int L, int R)
{
l = L;
r = R;
if(l == r) return;
int m = (l+r)/2;
left = new segtree(l, m);
right = new segtree(m+1, r);
}
void insert(int I, int V)
{
if(r < I || I < l) return;
v.push_back(V);
if(left != NULL)
{
left->insert(I, V);
right->insert(I, V);
}
}
void segtree_sort()
{
sort(v.begin(), v.end());
if(left != NULL)
{
left->segtree_sort();
right->segtree_sort();
}
}
long long query(int L, int R, int V1, int V2) //count the numbers with value in [V1, V2] in range [L, R].
{
if(R < l || r < L) return 0;
else if(L <= l && r <= R)
{
int e1 = -1, e2 = -1;
//smallest index >= v1, largest index <= v2
for(int bit = 19; bit >= 0; bit--)
{
if(e1 + (1 << bit) >= v.size()) continue;
if(v[e1 + (1 << bit)] < V1)
e1 += (1 << bit);
}
e1++;
for(int bit = 19; bit >= 0; bit--)
{
if(e2 + (1 << bit) >= v.size()) continue;
if(v[e2 + (1 << bit)] <= V2)
e2 += (1 << bit);
}
if(e1 == v.size() || e1 == -1 || e2 == -1 || e1 > e2)
return 0;
else
return e2-e1+1;
}
else
{
return left->query(L, R, V1, V2) + right->query(L, R, V1, V2);
}
}
};
segtree cells(0, 200001);
segtree horiz(0, 200001);
segtree vert(0, 200001);
segtree faces(0, 200001);
struct pos
{
int r;
int c;
};
bool operator < (pos A, pos B)
{
if(A.r == B.r) return A.c < B.c;
return A.r < B.r;
};
set<pos> water;
int tp = 1e9, lf = 1e9, dn = -1, rg = 0;
void init(int R, int C, int sr, int sc, int M, char *S)
{
water.insert(pos{sr, sc});
int curr_r = sr;
int curr_c = sc;
for(int i = 0; i < M; i++)
{
// cerr << S[i] << '\n';
if(S[i] == 'N') curr_r--;
else if(S[i] == 'S') curr_r++;
else if(S[i] == 'W') curr_c--;
else if(S[i] == 'E') curr_c++;
// cerr << curr_r << ' ' << curr_c << '\n';
water.insert(pos{curr_r, curr_c});
// cerr << "water: ";
// for(pos w:water) cerr << w.r << ' ' << w.c << " | ";
// cerr << '\n';
}
for(pos w: water)
{
cells.insert(w.r, w.c);
horiz.insert(w.r, w.c);
if(water.find(pos{w.r, w.c-1}) == water.end())
horiz.insert(w.r, w.c-1);
vert.insert(w.r, w.c);
if(water.find(pos{w.r-1, w.c}) == water.end())
vert.insert(w.r-1, w.c);
/*
Preference order: 1 2
3 4
*/
//Face DR
faces.insert(w.r, w.c);
// cerr << "insert " << w.r << ' ' << w.c << '\n';
//Face DL
if(water.find(pos{w.r, w.c-1}) == water.end())
{
faces.insert(w.r, w.c-1);
// cerr << "insert " << w.r << ' ' << w.c-1 << '\n';
}
//Face TR
if(water.find(pos{w.r-1, w.c}) == water.end())
if(water.find(pos{w.r-1, w.c+1}) == water.end())
{
faces.insert(w.r-1, w.c);
// cerr << "insert " << w.r-1 << ' ' << w.c << '\n';
}
//Face TL
if(water.find(pos{w.r-1, w.c}) == water.end())
if(water.find(pos{w.r, w.c-1}) == water.end())
if(water.find(pos{w.r-1, w.c-1}) == water.end())
{
faces.insert(w.r-1, w.c-1);
// cerr << "insert " << w.r-1 << ' ' << w.c-1 << '\n';
}
tp = min(tp, w.r);
dn = max(dn, w.r);
lf = min(lf, w.c);
rg = max(rg, w.c);
}
cells.segtree_sort();
horiz.segtree_sort();
vert.segtree_sort();
faces.segtree_sort();
// cerr << tp << ' ' << dn << ' ' << lf << ' ' << rg << '\n';
}
int colour(int ar, int ac, int br, int bc)
{
long long h = br-ar+1;
long long w = bc-ac+1;
/*
V - E + F - C = 1
C = V-E+F-1
*/
// cerr << "check\n";
long long V = h*w - cells.query(ar, br, ac, bc);
long long E = h*(w-1) + (h-1)*w - vert.query(ar, br-1, ac, bc) - horiz.query(ar, br, ac, bc-1);
long long F = (h-1)*(w-1) - faces.query(ar, br-1, ac, bc-1) + 1;
// cerr << (tp<ar) << (br<dn) << (lf<ac) << (bc<rg) << '\n';
if(ar < tp && dn < br && ac < lf && rg < bc)
F++;
long long C = V - E + F - 1;
// cerr << V << ' ' << E << ' ' << F << ' ' << C << '\n';
return int(C);
}
Compilation message
rainbow.cpp: In member function 'long long int segtree::query(int, int, int, int)':
rainbow.cpp:90:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | if(e1 + (1 << bit) >= v.size()) continue;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
rainbow.cpp:98:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
98 | if(e2 + (1 << bit) >= v.size()) continue;
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~
rainbow.cpp:103:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | if(e1 == v.size() || e1 == -1 || e2 == -1 || e1 > e2)
| ~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
100568 KB |
Output is correct |
2 |
Correct |
99 ms |
100916 KB |
Output is correct |
3 |
Correct |
109 ms |
100716 KB |
Output is correct |
4 |
Correct |
98 ms |
100640 KB |
Output is correct |
5 |
Correct |
101 ms |
101088 KB |
Output is correct |
6 |
Correct |
94 ms |
100376 KB |
Output is correct |
7 |
Correct |
93 ms |
100420 KB |
Output is correct |
8 |
Correct |
95 ms |
100404 KB |
Output is correct |
9 |
Correct |
94 ms |
100440 KB |
Output is correct |
10 |
Correct |
92 ms |
100496 KB |
Output is correct |
11 |
Correct |
100 ms |
100732 KB |
Output is correct |
12 |
Correct |
99 ms |
100864 KB |
Output is correct |
13 |
Correct |
108 ms |
101384 KB |
Output is correct |
14 |
Correct |
104 ms |
101592 KB |
Output is correct |
15 |
Correct |
94 ms |
100408 KB |
Output is correct |
16 |
Correct |
107 ms |
100404 KB |
Output is correct |
17 |
Correct |
96 ms |
100460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
100404 KB |
Output is correct |
2 |
Correct |
96 ms |
100460 KB |
Output is correct |
3 |
Correct |
923 ms |
135480 KB |
Output is correct |
4 |
Correct |
1300 ms |
156300 KB |
Output is correct |
5 |
Correct |
1180 ms |
155920 KB |
Output is correct |
6 |
Correct |
940 ms |
145976 KB |
Output is correct |
7 |
Correct |
953 ms |
144880 KB |
Output is correct |
8 |
Correct |
229 ms |
104108 KB |
Output is correct |
9 |
Correct |
1051 ms |
156360 KB |
Output is correct |
10 |
Correct |
1146 ms |
155924 KB |
Output is correct |
11 |
Correct |
951 ms |
146072 KB |
Output is correct |
12 |
Correct |
1548 ms |
153240 KB |
Output is correct |
13 |
Correct |
1080 ms |
156316 KB |
Output is correct |
14 |
Correct |
1096 ms |
156012 KB |
Output is correct |
15 |
Correct |
918 ms |
146500 KB |
Output is correct |
16 |
Correct |
948 ms |
143764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
100408 KB |
Output is correct |
2 |
Correct |
615 ms |
152132 KB |
Output is correct |
3 |
Correct |
565 ms |
175368 KB |
Output is correct |
4 |
Correct |
578 ms |
169536 KB |
Output is correct |
5 |
Correct |
450 ms |
151000 KB |
Output is correct |
6 |
Correct |
218 ms |
111940 KB |
Output is correct |
7 |
Correct |
345 ms |
122272 KB |
Output is correct |
8 |
Correct |
787 ms |
148216 KB |
Output is correct |
9 |
Correct |
698 ms |
142600 KB |
Output is correct |
10 |
Correct |
236 ms |
112644 KB |
Output is correct |
11 |
Correct |
461 ms |
128628 KB |
Output is correct |
12 |
Correct |
621 ms |
152092 KB |
Output is correct |
13 |
Correct |
561 ms |
175360 KB |
Output is correct |
14 |
Correct |
589 ms |
169364 KB |
Output is correct |
15 |
Correct |
450 ms |
151056 KB |
Output is correct |
16 |
Correct |
199 ms |
109892 KB |
Output is correct |
17 |
Correct |
334 ms |
122820 KB |
Output is correct |
18 |
Correct |
717 ms |
156208 KB |
Output is correct |
19 |
Correct |
572 ms |
166288 KB |
Output is correct |
20 |
Correct |
569 ms |
166304 KB |
Output is correct |
21 |
Correct |
772 ms |
148208 KB |
Output is correct |
22 |
Correct |
693 ms |
142608 KB |
Output is correct |
23 |
Correct |
241 ms |
112772 KB |
Output is correct |
24 |
Correct |
460 ms |
128708 KB |
Output is correct |
25 |
Correct |
610 ms |
152164 KB |
Output is correct |
26 |
Correct |
569 ms |
175488 KB |
Output is correct |
27 |
Correct |
575 ms |
169336 KB |
Output is correct |
28 |
Correct |
448 ms |
151136 KB |
Output is correct |
29 |
Correct |
197 ms |
109940 KB |
Output is correct |
30 |
Correct |
335 ms |
122828 KB |
Output is correct |
31 |
Correct |
707 ms |
156092 KB |
Output is correct |
32 |
Correct |
570 ms |
166104 KB |
Output is correct |
33 |
Correct |
574 ms |
166212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
100568 KB |
Output is correct |
2 |
Correct |
99 ms |
100916 KB |
Output is correct |
3 |
Correct |
109 ms |
100716 KB |
Output is correct |
4 |
Correct |
98 ms |
100640 KB |
Output is correct |
5 |
Correct |
101 ms |
101088 KB |
Output is correct |
6 |
Correct |
94 ms |
100376 KB |
Output is correct |
7 |
Correct |
93 ms |
100420 KB |
Output is correct |
8 |
Correct |
95 ms |
100404 KB |
Output is correct |
9 |
Correct |
94 ms |
100440 KB |
Output is correct |
10 |
Correct |
92 ms |
100496 KB |
Output is correct |
11 |
Correct |
100 ms |
100732 KB |
Output is correct |
12 |
Correct |
99 ms |
100864 KB |
Output is correct |
13 |
Correct |
108 ms |
101384 KB |
Output is correct |
14 |
Correct |
104 ms |
101592 KB |
Output is correct |
15 |
Correct |
94 ms |
100408 KB |
Output is correct |
16 |
Correct |
107 ms |
100404 KB |
Output is correct |
17 |
Correct |
96 ms |
100460 KB |
Output is correct |
18 |
Correct |
1796 ms |
134296 KB |
Output is correct |
19 |
Correct |
442 ms |
105560 KB |
Output is correct |
20 |
Correct |
429 ms |
104256 KB |
Output is correct |
21 |
Correct |
475 ms |
104500 KB |
Output is correct |
22 |
Correct |
509 ms |
104708 KB |
Output is correct |
23 |
Correct |
430 ms |
105388 KB |
Output is correct |
24 |
Correct |
457 ms |
104504 KB |
Output is correct |
25 |
Correct |
500 ms |
104680 KB |
Output is correct |
26 |
Correct |
524 ms |
105156 KB |
Output is correct |
27 |
Correct |
1107 ms |
128004 KB |
Output is correct |
28 |
Correct |
921 ms |
115568 KB |
Output is correct |
29 |
Correct |
1052 ms |
125556 KB |
Output is correct |
30 |
Correct |
2005 ms |
159568 KB |
Output is correct |
31 |
Correct |
101 ms |
100548 KB |
Output is correct |
32 |
Correct |
1384 ms |
128556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
100568 KB |
Output is correct |
2 |
Correct |
99 ms |
100916 KB |
Output is correct |
3 |
Correct |
109 ms |
100716 KB |
Output is correct |
4 |
Correct |
98 ms |
100640 KB |
Output is correct |
5 |
Correct |
101 ms |
101088 KB |
Output is correct |
6 |
Correct |
94 ms |
100376 KB |
Output is correct |
7 |
Correct |
93 ms |
100420 KB |
Output is correct |
8 |
Correct |
95 ms |
100404 KB |
Output is correct |
9 |
Correct |
94 ms |
100440 KB |
Output is correct |
10 |
Correct |
92 ms |
100496 KB |
Output is correct |
11 |
Correct |
100 ms |
100732 KB |
Output is correct |
12 |
Correct |
99 ms |
100864 KB |
Output is correct |
13 |
Correct |
108 ms |
101384 KB |
Output is correct |
14 |
Correct |
104 ms |
101592 KB |
Output is correct |
15 |
Correct |
94 ms |
100408 KB |
Output is correct |
16 |
Correct |
107 ms |
100404 KB |
Output is correct |
17 |
Correct |
96 ms |
100460 KB |
Output is correct |
18 |
Correct |
1796 ms |
134296 KB |
Output is correct |
19 |
Correct |
442 ms |
105560 KB |
Output is correct |
20 |
Correct |
429 ms |
104256 KB |
Output is correct |
21 |
Correct |
475 ms |
104500 KB |
Output is correct |
22 |
Correct |
509 ms |
104708 KB |
Output is correct |
23 |
Correct |
430 ms |
105388 KB |
Output is correct |
24 |
Correct |
457 ms |
104504 KB |
Output is correct |
25 |
Correct |
500 ms |
104680 KB |
Output is correct |
26 |
Correct |
524 ms |
105156 KB |
Output is correct |
27 |
Correct |
1107 ms |
128004 KB |
Output is correct |
28 |
Correct |
921 ms |
115568 KB |
Output is correct |
29 |
Correct |
1052 ms |
125556 KB |
Output is correct |
30 |
Correct |
2005 ms |
159568 KB |
Output is correct |
31 |
Correct |
101 ms |
100548 KB |
Output is correct |
32 |
Correct |
1384 ms |
128556 KB |
Output is correct |
33 |
Correct |
615 ms |
152132 KB |
Output is correct |
34 |
Correct |
565 ms |
175368 KB |
Output is correct |
35 |
Correct |
578 ms |
169536 KB |
Output is correct |
36 |
Correct |
450 ms |
151000 KB |
Output is correct |
37 |
Correct |
218 ms |
111940 KB |
Output is correct |
38 |
Correct |
345 ms |
122272 KB |
Output is correct |
39 |
Correct |
787 ms |
148216 KB |
Output is correct |
40 |
Correct |
698 ms |
142600 KB |
Output is correct |
41 |
Correct |
236 ms |
112644 KB |
Output is correct |
42 |
Correct |
461 ms |
128628 KB |
Output is correct |
43 |
Correct |
621 ms |
152092 KB |
Output is correct |
44 |
Correct |
561 ms |
175360 KB |
Output is correct |
45 |
Correct |
589 ms |
169364 KB |
Output is correct |
46 |
Correct |
450 ms |
151056 KB |
Output is correct |
47 |
Correct |
199 ms |
109892 KB |
Output is correct |
48 |
Correct |
334 ms |
122820 KB |
Output is correct |
49 |
Correct |
717 ms |
156208 KB |
Output is correct |
50 |
Correct |
572 ms |
166288 KB |
Output is correct |
51 |
Correct |
569 ms |
166304 KB |
Output is correct |
52 |
Correct |
772 ms |
148208 KB |
Output is correct |
53 |
Correct |
693 ms |
142608 KB |
Output is correct |
54 |
Correct |
241 ms |
112772 KB |
Output is correct |
55 |
Correct |
460 ms |
128708 KB |
Output is correct |
56 |
Correct |
610 ms |
152164 KB |
Output is correct |
57 |
Correct |
569 ms |
175488 KB |
Output is correct |
58 |
Correct |
575 ms |
169336 KB |
Output is correct |
59 |
Correct |
448 ms |
151136 KB |
Output is correct |
60 |
Correct |
197 ms |
109940 KB |
Output is correct |
61 |
Correct |
335 ms |
122828 KB |
Output is correct |
62 |
Correct |
707 ms |
156092 KB |
Output is correct |
63 |
Correct |
570 ms |
166104 KB |
Output is correct |
64 |
Correct |
574 ms |
166212 KB |
Output is correct |
65 |
Correct |
923 ms |
135480 KB |
Output is correct |
66 |
Correct |
1300 ms |
156300 KB |
Output is correct |
67 |
Correct |
1180 ms |
155920 KB |
Output is correct |
68 |
Correct |
940 ms |
145976 KB |
Output is correct |
69 |
Correct |
953 ms |
144880 KB |
Output is correct |
70 |
Correct |
229 ms |
104108 KB |
Output is correct |
71 |
Correct |
1051 ms |
156360 KB |
Output is correct |
72 |
Correct |
1146 ms |
155924 KB |
Output is correct |
73 |
Correct |
951 ms |
146072 KB |
Output is correct |
74 |
Correct |
1548 ms |
153240 KB |
Output is correct |
75 |
Correct |
1080 ms |
156316 KB |
Output is correct |
76 |
Correct |
1096 ms |
156012 KB |
Output is correct |
77 |
Correct |
918 ms |
146500 KB |
Output is correct |
78 |
Correct |
948 ms |
143764 KB |
Output is correct |
79 |
Correct |
1069 ms |
151616 KB |
Output is correct |
80 |
Correct |
1012 ms |
146052 KB |
Output is correct |
81 |
Correct |
609 ms |
116164 KB |
Output is correct |
82 |
Correct |
1139 ms |
132048 KB |
Output is correct |
83 |
Correct |
1888 ms |
155704 KB |
Output is correct |
84 |
Correct |
2498 ms |
178872 KB |
Output is correct |
85 |
Correct |
1975 ms |
172856 KB |
Output is correct |
86 |
Correct |
1962 ms |
154556 KB |
Output is correct |
87 |
Correct |
1457 ms |
113672 KB |
Output is correct |
88 |
Correct |
1573 ms |
126364 KB |
Output is correct |
89 |
Correct |
1924 ms |
159672 KB |
Output is correct |
90 |
Correct |
2164 ms |
169708 KB |
Output is correct |
91 |
Correct |
1975 ms |
169792 KB |
Output is correct |