#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
map<char,int> adjr = {{'N', -1}, {'S', 1}};
map<char,int> adjc = {{'E', 1}, {'W', -1}};
set<pair<int,int>> Snake;
int R, C;
pair<int,int> Snakex, Snakey;
struct Segment2D{
vector<int> fen[maxn], fex[maxn];
vector<pair<int,int>> Q;
int get(int X, int Y){
int ret = 0;
for (int x = X; x; x -= x & -x){
int y = upper_bound(fex[x].begin(),fex[x].end(),Y)-fex[x].begin();
for (; y; y -= y & -y)
ret += fen[x][y-1];
}
return ret;
}
int get(int x1, int x2, int y1, int y2){
if (x2 < x1 or y2 < y1)
return 0;
return get(x2, y2) - get(x2, y1-1) - get(x1-1, y2) + get(x1-1,y1-1);
}
void addreal(int X, int Y){
for (int x = X; x < maxn; x += x & -x){
int y = lower_bound(fex[x].begin(),fex[x].end(),Y)-fex[x].begin()+1;
for (; y <= fen[x].size(); y += y & -y)
fen[x][y-1] ++;
}
}
void add(int X, int Y){
for (int x = X; x < maxn; x += x & -x)
fex[x].push_back(Y);
Q.push_back({X,Y});
}
void build(){
for (int i = 1; i < maxn; i++){
sort(fex[i].begin(),fex[i].end());
fex[i].resize(unique(fex[i].begin(),fex[i].end())-fex[i].begin());
fen[i].resize(fex[i].size());
}
sort(Q.begin(),Q.end());
Q.resize(unique(Q.begin(),Q.end())-Q.begin());
for (auto it : Q)
addreal(it.first, it.second);
}
} seg[6];
void init(int r, int c, int sr, int sc, int M, char *S){
R = r, C = c;
Snake.insert({sr,sc});
Snakex = {sr,sr};
Snakey = {sc,sc};
seg[0].add(sr,sc);
for (int i = 0; i < M; i++){
sr += adjr[S[i]], sc += adjc[S[i]];
Snake.insert({sr,sc});
seg[0].add(sr,sc);
Snakex.first = min(Snakex.first,sr), Snakex.second = max(Snakex.second,sr);
Snakey.first = min(Snakey.first,sc), Snakey.second = max(Snakey.second,sc);
}
for (auto it : Snake){
int x = it.first, y = it.second;
if (Snake.count({x-1,y}))
seg[1].add(x,y);
if (Snake.count({x,y-1}))
seg[2].add(x,y);
if (Snake.count({x,y-1}) and Snake.count({x-1,y}) and Snake.count({x-1,y-1}))
seg[3].add(x,y);
if (Snake.count({x-1,y-1}) and !Snake.count({x-1,y}) and !Snake.count({x,y-1}))
seg[4].add(x,y);
if (Snake.count({x-1,y+1}) and !Snake.count({x-1,y}) and !Snake.count({x,y+1}))
seg[5].add(x,y+1);
}
for (int i = 0; i < 6; i++)
seg[i].build();
}
int colour(int x1, int y1, int x2, int y2){
int v = seg[0].get(x1, x2, y1, y2) + 2*(x2-x1+y2-y1+4);
int e = seg[1].get(x1+1, x2, y1, y2) + seg[2].get(x1,x2, y1+1,y2)
+ seg[0].get(x1, x1, y1, y2) + seg[0].get(x2, x2, y1, y2) + seg[0].get(x1, x2, y1, y1) + seg[0].get(x1, x2, y2, y2)
+ 2*(x2-x1+y2-y1+4) + seg[4].get(x1+1, x2, y1+1, y2) + seg[5].get(x1+1, x2, y1+1, y2);
int c = 1 + (Snakex.first > x1 and Snakex.second < x2 and Snakey.first > y1 and Snakey.second < y2);
int f = seg[3].get(x1+1, x2, y1+1, y2) + seg[1].get(x1+1, x2, y1, y1) + seg[1].get(x1+1, x2, y2, y2)
+ seg[2].get(x1, x1, y1+1, y2) + seg[2].get(x2, x2, y1+1, y2) + Snake.count({x1,y1}) + Snake.count({x1,y2})
+ Snake.count({x2,y1}) + Snake.count({x2,y2});
return c + e - v - f;
}
Compilation message
rainbow.cpp: In member function 'void Segment2D::addreal(int, int)':
rainbow.cpp:31:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (; y <= fen[x].size(); y += y & -y)
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
56824 KB |
Output is correct |
2 |
Correct |
54 ms |
57208 KB |
Output is correct |
3 |
Correct |
47 ms |
56824 KB |
Output is correct |
4 |
Correct |
50 ms |
56824 KB |
Output is correct |
5 |
Correct |
54 ms |
57336 KB |
Output is correct |
6 |
Correct |
43 ms |
56696 KB |
Output is correct |
7 |
Correct |
42 ms |
56696 KB |
Output is correct |
8 |
Correct |
43 ms |
56704 KB |
Output is correct |
9 |
Correct |
42 ms |
56696 KB |
Output is correct |
10 |
Correct |
44 ms |
56704 KB |
Output is correct |
11 |
Correct |
51 ms |
57080 KB |
Output is correct |
12 |
Correct |
52 ms |
57080 KB |
Output is correct |
13 |
Correct |
53 ms |
57208 KB |
Output is correct |
14 |
Correct |
55 ms |
57464 KB |
Output is correct |
15 |
Correct |
42 ms |
56704 KB |
Output is correct |
16 |
Correct |
42 ms |
56704 KB |
Output is correct |
17 |
Correct |
43 ms |
56704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
56704 KB |
Output is correct |
2 |
Correct |
43 ms |
56704 KB |
Output is correct |
3 |
Correct |
603 ms |
80840 KB |
Output is correct |
4 |
Correct |
983 ms |
95508 KB |
Output is correct |
5 |
Correct |
980 ms |
95496 KB |
Output is correct |
6 |
Correct |
859 ms |
96520 KB |
Output is correct |
7 |
Correct |
773 ms |
87308 KB |
Output is correct |
8 |
Correct |
175 ms |
68756 KB |
Output is correct |
9 |
Correct |
956 ms |
95440 KB |
Output is correct |
10 |
Correct |
977 ms |
95380 KB |
Output is correct |
11 |
Correct |
898 ms |
96328 KB |
Output is correct |
12 |
Correct |
646 ms |
93268 KB |
Output is correct |
13 |
Correct |
632 ms |
95376 KB |
Output is correct |
14 |
Correct |
666 ms |
95276 KB |
Output is correct |
15 |
Correct |
696 ms |
96372 KB |
Output is correct |
16 |
Correct |
690 ms |
86796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
56704 KB |
Output is correct |
2 |
Correct |
258 ms |
76348 KB |
Output is correct |
3 |
Correct |
184 ms |
81380 KB |
Output is correct |
4 |
Correct |
341 ms |
84320 KB |
Output is correct |
5 |
Correct |
186 ms |
75088 KB |
Output is correct |
6 |
Correct |
178 ms |
67400 KB |
Output is correct |
7 |
Correct |
253 ms |
70104 KB |
Output is correct |
8 |
Correct |
525 ms |
83868 KB |
Output is correct |
9 |
Correct |
455 ms |
80156 KB |
Output is correct |
10 |
Correct |
153 ms |
63452 KB |
Output is correct |
11 |
Correct |
203 ms |
67852 KB |
Output is correct |
12 |
Correct |
250 ms |
76344 KB |
Output is correct |
13 |
Correct |
188 ms |
81380 KB |
Output is correct |
14 |
Correct |
337 ms |
84364 KB |
Output is correct |
15 |
Correct |
188 ms |
75084 KB |
Output is correct |
16 |
Correct |
175 ms |
66896 KB |
Output is correct |
17 |
Correct |
284 ms |
73800 KB |
Output is correct |
18 |
Correct |
323 ms |
76520 KB |
Output is correct |
19 |
Correct |
237 ms |
80272 KB |
Output is correct |
20 |
Correct |
246 ms |
81868 KB |
Output is correct |
21 |
Correct |
522 ms |
83868 KB |
Output is correct |
22 |
Correct |
455 ms |
80156 KB |
Output is correct |
23 |
Correct |
154 ms |
63452 KB |
Output is correct |
24 |
Correct |
202 ms |
67904 KB |
Output is correct |
25 |
Correct |
251 ms |
76348 KB |
Output is correct |
26 |
Correct |
183 ms |
81380 KB |
Output is correct |
27 |
Correct |
344 ms |
84560 KB |
Output is correct |
28 |
Correct |
190 ms |
75092 KB |
Output is correct |
29 |
Correct |
165 ms |
66944 KB |
Output is correct |
30 |
Correct |
287 ms |
73928 KB |
Output is correct |
31 |
Correct |
330 ms |
76396 KB |
Output is correct |
32 |
Correct |
235 ms |
80204 KB |
Output is correct |
33 |
Correct |
247 ms |
81868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
56824 KB |
Output is correct |
2 |
Correct |
54 ms |
57208 KB |
Output is correct |
3 |
Correct |
47 ms |
56824 KB |
Output is correct |
4 |
Correct |
50 ms |
56824 KB |
Output is correct |
5 |
Correct |
54 ms |
57336 KB |
Output is correct |
6 |
Correct |
43 ms |
56696 KB |
Output is correct |
7 |
Correct |
42 ms |
56696 KB |
Output is correct |
8 |
Correct |
43 ms |
56704 KB |
Output is correct |
9 |
Correct |
42 ms |
56696 KB |
Output is correct |
10 |
Correct |
44 ms |
56704 KB |
Output is correct |
11 |
Correct |
51 ms |
57080 KB |
Output is correct |
12 |
Correct |
52 ms |
57080 KB |
Output is correct |
13 |
Correct |
53 ms |
57208 KB |
Output is correct |
14 |
Correct |
55 ms |
57464 KB |
Output is correct |
15 |
Correct |
42 ms |
56704 KB |
Output is correct |
16 |
Correct |
42 ms |
56704 KB |
Output is correct |
17 |
Correct |
43 ms |
56704 KB |
Output is correct |
18 |
Correct |
1777 ms |
73660 KB |
Output is correct |
19 |
Correct |
718 ms |
60920 KB |
Output is correct |
20 |
Correct |
496 ms |
60280 KB |
Output is correct |
21 |
Correct |
554 ms |
60536 KB |
Output is correct |
22 |
Correct |
602 ms |
60664 KB |
Output is correct |
23 |
Correct |
619 ms |
60920 KB |
Output is correct |
24 |
Correct |
727 ms |
60792 KB |
Output is correct |
25 |
Correct |
699 ms |
60792 KB |
Output is correct |
26 |
Correct |
709 ms |
60792 KB |
Output is correct |
27 |
Correct |
997 ms |
76224 KB |
Output is correct |
28 |
Correct |
1037 ms |
73316 KB |
Output is correct |
29 |
Correct |
1054 ms |
78272 KB |
Output is correct |
30 |
Correct |
1257 ms |
80044 KB |
Output is correct |
31 |
Correct |
48 ms |
56960 KB |
Output is correct |
32 |
Correct |
1440 ms |
73788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
56824 KB |
Output is correct |
2 |
Correct |
54 ms |
57208 KB |
Output is correct |
3 |
Correct |
47 ms |
56824 KB |
Output is correct |
4 |
Correct |
50 ms |
56824 KB |
Output is correct |
5 |
Correct |
54 ms |
57336 KB |
Output is correct |
6 |
Correct |
43 ms |
56696 KB |
Output is correct |
7 |
Correct |
42 ms |
56696 KB |
Output is correct |
8 |
Correct |
43 ms |
56704 KB |
Output is correct |
9 |
Correct |
42 ms |
56696 KB |
Output is correct |
10 |
Correct |
44 ms |
56704 KB |
Output is correct |
11 |
Correct |
51 ms |
57080 KB |
Output is correct |
12 |
Correct |
52 ms |
57080 KB |
Output is correct |
13 |
Correct |
53 ms |
57208 KB |
Output is correct |
14 |
Correct |
55 ms |
57464 KB |
Output is correct |
15 |
Correct |
42 ms |
56704 KB |
Output is correct |
16 |
Correct |
42 ms |
56704 KB |
Output is correct |
17 |
Correct |
43 ms |
56704 KB |
Output is correct |
18 |
Correct |
1777 ms |
73660 KB |
Output is correct |
19 |
Correct |
718 ms |
60920 KB |
Output is correct |
20 |
Correct |
496 ms |
60280 KB |
Output is correct |
21 |
Correct |
554 ms |
60536 KB |
Output is correct |
22 |
Correct |
602 ms |
60664 KB |
Output is correct |
23 |
Correct |
619 ms |
60920 KB |
Output is correct |
24 |
Correct |
727 ms |
60792 KB |
Output is correct |
25 |
Correct |
699 ms |
60792 KB |
Output is correct |
26 |
Correct |
709 ms |
60792 KB |
Output is correct |
27 |
Correct |
997 ms |
76224 KB |
Output is correct |
28 |
Correct |
1037 ms |
73316 KB |
Output is correct |
29 |
Correct |
1054 ms |
78272 KB |
Output is correct |
30 |
Correct |
1257 ms |
80044 KB |
Output is correct |
31 |
Correct |
48 ms |
56960 KB |
Output is correct |
32 |
Correct |
1440 ms |
73788 KB |
Output is correct |
33 |
Correct |
258 ms |
76348 KB |
Output is correct |
34 |
Correct |
184 ms |
81380 KB |
Output is correct |
35 |
Correct |
341 ms |
84320 KB |
Output is correct |
36 |
Correct |
186 ms |
75088 KB |
Output is correct |
37 |
Correct |
178 ms |
67400 KB |
Output is correct |
38 |
Correct |
253 ms |
70104 KB |
Output is correct |
39 |
Correct |
525 ms |
83868 KB |
Output is correct |
40 |
Correct |
455 ms |
80156 KB |
Output is correct |
41 |
Correct |
153 ms |
63452 KB |
Output is correct |
42 |
Correct |
203 ms |
67852 KB |
Output is correct |
43 |
Correct |
250 ms |
76344 KB |
Output is correct |
44 |
Correct |
188 ms |
81380 KB |
Output is correct |
45 |
Correct |
337 ms |
84364 KB |
Output is correct |
46 |
Correct |
188 ms |
75084 KB |
Output is correct |
47 |
Correct |
175 ms |
66896 KB |
Output is correct |
48 |
Correct |
284 ms |
73800 KB |
Output is correct |
49 |
Correct |
323 ms |
76520 KB |
Output is correct |
50 |
Correct |
237 ms |
80272 KB |
Output is correct |
51 |
Correct |
246 ms |
81868 KB |
Output is correct |
52 |
Correct |
522 ms |
83868 KB |
Output is correct |
53 |
Correct |
455 ms |
80156 KB |
Output is correct |
54 |
Correct |
154 ms |
63452 KB |
Output is correct |
55 |
Correct |
202 ms |
67904 KB |
Output is correct |
56 |
Correct |
251 ms |
76348 KB |
Output is correct |
57 |
Correct |
183 ms |
81380 KB |
Output is correct |
58 |
Correct |
344 ms |
84560 KB |
Output is correct |
59 |
Correct |
190 ms |
75092 KB |
Output is correct |
60 |
Correct |
165 ms |
66944 KB |
Output is correct |
61 |
Correct |
287 ms |
73928 KB |
Output is correct |
62 |
Correct |
330 ms |
76396 KB |
Output is correct |
63 |
Correct |
235 ms |
80204 KB |
Output is correct |
64 |
Correct |
247 ms |
81868 KB |
Output is correct |
65 |
Correct |
1691 ms |
87656 KB |
Output is correct |
66 |
Correct |
1806 ms |
83736 KB |
Output is correct |
67 |
Correct |
1081 ms |
66948 KB |
Output is correct |
68 |
Correct |
982 ms |
71572 KB |
Output is correct |
69 |
Correct |
1022 ms |
80216 KB |
Output is correct |
70 |
Correct |
900 ms |
84964 KB |
Output is correct |
71 |
Correct |
1382 ms |
87904 KB |
Output is correct |
72 |
Correct |
956 ms |
78672 KB |
Output is correct |
73 |
Correct |
740 ms |
70616 KB |
Output is correct |
74 |
Correct |
900 ms |
77636 KB |
Output is correct |
75 |
Correct |
901 ms |
80152 KB |
Output is correct |
76 |
Correct |
1096 ms |
83916 KB |
Output is correct |
77 |
Correct |
877 ms |
85452 KB |
Output is correct |
78 |
Correct |
603 ms |
80840 KB |
Output is correct |
79 |
Correct |
983 ms |
95508 KB |
Output is correct |
80 |
Correct |
980 ms |
95496 KB |
Output is correct |
81 |
Correct |
859 ms |
96520 KB |
Output is correct |
82 |
Correct |
773 ms |
87308 KB |
Output is correct |
83 |
Correct |
175 ms |
68756 KB |
Output is correct |
84 |
Correct |
956 ms |
95440 KB |
Output is correct |
85 |
Correct |
977 ms |
95380 KB |
Output is correct |
86 |
Correct |
898 ms |
96328 KB |
Output is correct |
87 |
Correct |
646 ms |
93268 KB |
Output is correct |
88 |
Correct |
632 ms |
95376 KB |
Output is correct |
89 |
Correct |
666 ms |
95276 KB |
Output is correct |
90 |
Correct |
696 ms |
96372 KB |
Output is correct |
91 |
Correct |
690 ms |
86796 KB |
Output is correct |