#include "rainbow.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int INF = 1e9+100;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int dx8[9] = {-1, -1, -1, 0, 1, 1, 1, 0, -1}, dy8[9] = {-1, 0, 1, 1, 1, 0, -1, -1, -1};
set<pair<int, int>> st;
vector<int> S[3], E[3];
int n, r;
void init(int R, int C, int sr, int sc, int M, char *S) {
st.emplace(sr, sc);
for (int i=0;i<M;i++){
int k;
if (S[i]=='N') k = 0;
if (S[i]=='E') k = 1;
if (S[i]=='S') k = 2;
if (S[i]=='W') k = 3;
sr += dx[k], sc += dy[k];
st.emplace(sr, sc);
}
auto iter = st.begin();
for (int i=1;i<=2;i++){
int prv = 1;
while(iter!=st.end() && iter->first==i){
if (iter->second > prv){
::S[i].push_back(prv);
E[i].push_back(iter->second-1);
//ran.emplace_back(prv, iter->second-1);
//printf("ran: %d %d\n", prv, iter->second-1);
}
prv = iter->second+1;
++iter;
}
if (prv<=C){
::S[i].push_back(prv);
E[i].push_back(C);
//printf("ran: %d %d\n", prv, C);
}
}
n = C;
r = R;
}
bool valid(int x, int y, int ar, int ac, int br, int bc){
if (st.find(make_pair(x, y))!=st.end()) return 0;
return ar <= x && x <= br && ac <= y && y <= bc;
}
void add_edge(int x1, int y1, int x2, int y2, set<pair<int, int>> &V, map<pair<int, int>, vector<pair<int, int>>> &E, int ar, int ac, int br, int bc){
if (valid(x1, y1, ar, ac, br, bc)) V.emplace(x1, y1);
if (valid(x2, y2, ar, ac, br, bc)) V.emplace(x2, y2);
if (!valid(x1, y1, ar, ac, br, bc)) return;
if (!valid(x2, y2, ar, ac, br, bc)) return;
auto p1 = make_pair(x1, y1);
auto p2 = make_pair(x2, y2);
E[p1].push_back(p2);
E[p2].push_back(p1);
//printf("add_edge: %d %d %d %d\n", x1, y1, x2, y2);
}
void dfs(int x, int y, map<pair<int, int>, vector<pair<int, int>>> &E, set<pair<int, int>> &visited){
visited.emplace(x, y);
auto p = make_pair(x, y);
for (auto np:E[p]) if (visited.find(np)==visited.end()){
dfs(np.first, np.second, E, visited);
}
}
int colour(int ar, int ac, int br, int bc) {
if (r==2){
int cnt[3];
for (int i=1;i<=2;i++){
cnt[i] = (upper_bound(S[i].begin(), S[i].end(), bc) - S[i].begin()) - (lower_bound(E[i].begin(), E[i].end(), ac) - E[i].begin());
cnt[i] = max(cnt[i], 0);
}
if (ar==br) return cnt[ar];
int ans = cnt[1] + cnt[2];
if (!S[1].empty() && !S[2].empty()){
if (S[1][0]==1 && S[2][0]==1 && E[1][0] >= ac && E[2][0] >= ac) ans--;
if (E[1].back()==n && E[2].back()==n && S[1].back() <= bc && S[2].back() <= bc) ans--;
}
return ans;
}
if (br <= st.begin()->first-1) return 1;
ar = max(ar, st.begin()->first-1);
set<pair<int, int>> V, visited;
map<pair<int, int>, vector<pair<int, int>>> E;
for (int i=ar;i<=br;i++){
add_edge(i, ac, i+1, ac, V, E, ar, ac, br, bc);
add_edge(i, bc, i+1, bc, V, E, ar, ac, br, bc);
}
for (int j=ac;j<=bc;j++){
add_edge(ar, j, ar, j+1, V, E, ar, ac, br, bc);
add_edge(br, j, br, j+1, V, E, ar, ac, br, bc);
}
for (auto [x, y]:st){
for (int k=0;k<8;k++){
int nx1 = x + dx8[k], ny1 = y + dy8[k];
int nx2 = x + dx8[k+1], ny2 = y + dy8[k+1];
add_edge(nx1, ny1, nx2, ny2, V, E, ar, ac, br, bc);
}
}
int ret = 0;
for (auto &[x, y]:V) if (visited.find(make_pair(x, y))==visited.end()) {dfs(x, y, E, visited); ret++;}
return ret;
}
Compilation message
rainbow.cpp: In function 'void init(int, int, int, int, int, char*)':
rainbow.cpp:23:32: warning: 'k' may be used uninitialized in this function [-Wmaybe-uninitialized]
23 | sr += dx[k], sc += dy[k];
| ~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
348 KB |
Output is correct |
2 |
Correct |
989 ms |
644 KB |
Output is correct |
3 |
Correct |
250 ms |
420 KB |
Output is correct |
4 |
Correct |
329 ms |
428 KB |
Output is correct |
5 |
Correct |
1293 ms |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
512 ms |
428 KB |
Output is correct |
12 |
Correct |
919 ms |
444 KB |
Output is correct |
13 |
Correct |
1847 ms |
752 KB |
Output is correct |
14 |
Correct |
2009 ms |
500 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
56 ms |
3732 KB |
Output is correct |
4 |
Correct |
69 ms |
5708 KB |
Output is correct |
5 |
Correct |
76 ms |
5796 KB |
Output is correct |
6 |
Correct |
71 ms |
5196 KB |
Output is correct |
7 |
Correct |
73 ms |
4812 KB |
Output is correct |
8 |
Correct |
52 ms |
984 KB |
Output is correct |
9 |
Correct |
73 ms |
5684 KB |
Output is correct |
10 |
Correct |
76 ms |
5772 KB |
Output is correct |
11 |
Correct |
72 ms |
5156 KB |
Output is correct |
12 |
Correct |
61 ms |
5340 KB |
Output is correct |
13 |
Correct |
61 ms |
5712 KB |
Output is correct |
14 |
Correct |
62 ms |
5804 KB |
Output is correct |
15 |
Correct |
60 ms |
5172 KB |
Output is correct |
16 |
Correct |
61 ms |
4272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
872 ms |
82496 KB |
Output is correct |
3 |
Correct |
989 ms |
110244 KB |
Output is correct |
4 |
Correct |
966 ms |
108436 KB |
Output is correct |
5 |
Correct |
428 ms |
40568 KB |
Output is correct |
6 |
Correct |
616 ms |
95536 KB |
Output is correct |
7 |
Correct |
1158 ms |
166144 KB |
Output is correct |
8 |
Correct |
156 ms |
10312 KB |
Output is correct |
9 |
Correct |
116 ms |
6504 KB |
Output is correct |
10 |
Correct |
51 ms |
3388 KB |
Output is correct |
11 |
Correct |
13 ms |
2644 KB |
Output is correct |
12 |
Correct |
421 ms |
39236 KB |
Output is correct |
13 |
Correct |
389 ms |
39296 KB |
Output is correct |
14 |
Correct |
379 ms |
39340 KB |
Output is correct |
15 |
Correct |
346 ms |
38056 KB |
Output is correct |
16 |
Correct |
500 ms |
84472 KB |
Output is correct |
17 |
Correct |
741 ms |
115788 KB |
Output is correct |
18 |
Correct |
1124 ms |
159096 KB |
Output is correct |
19 |
Correct |
1310 ms |
173016 KB |
Output is correct |
20 |
Correct |
1081 ms |
132532 KB |
Output is correct |
21 |
Correct |
309 ms |
23700 KB |
Output is correct |
22 |
Correct |
307 ms |
23040 KB |
Output is correct |
23 |
Correct |
77 ms |
5532 KB |
Output is correct |
24 |
Correct |
353 ms |
41804 KB |
Output is correct |
25 |
Correct |
2175 ms |
228304 KB |
Output is correct |
26 |
Correct |
2320 ms |
278996 KB |
Output is correct |
27 |
Correct |
2496 ms |
318424 KB |
Output is correct |
28 |
Correct |
2121 ms |
260704 KB |
Output is correct |
29 |
Correct |
1300 ms |
198996 KB |
Output is correct |
30 |
Correct |
1707 ms |
253320 KB |
Output is correct |
31 |
Correct |
2171 ms |
280628 KB |
Output is correct |
32 |
Correct |
2307 ms |
289428 KB |
Output is correct |
33 |
Correct |
2258 ms |
289400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
348 KB |
Output is correct |
2 |
Correct |
989 ms |
644 KB |
Output is correct |
3 |
Correct |
250 ms |
420 KB |
Output is correct |
4 |
Correct |
329 ms |
428 KB |
Output is correct |
5 |
Correct |
1293 ms |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
512 ms |
428 KB |
Output is correct |
12 |
Correct |
919 ms |
444 KB |
Output is correct |
13 |
Correct |
1847 ms |
752 KB |
Output is correct |
14 |
Correct |
2009 ms |
500 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Execution timed out |
3058 ms |
15656 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
348 KB |
Output is correct |
2 |
Correct |
989 ms |
644 KB |
Output is correct |
3 |
Correct |
250 ms |
420 KB |
Output is correct |
4 |
Correct |
329 ms |
428 KB |
Output is correct |
5 |
Correct |
1293 ms |
612 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
512 ms |
428 KB |
Output is correct |
12 |
Correct |
919 ms |
444 KB |
Output is correct |
13 |
Correct |
1847 ms |
752 KB |
Output is correct |
14 |
Correct |
2009 ms |
500 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Execution timed out |
3058 ms |
15656 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |