# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1128377 | Zero_OP | Land of the Rainbow Gold (APIO17_rainbow) | C++20 | 0 ms | 0 KiB |
#include "bits/stdc++.h"
#ifndef LOCAL
#include "rainbow.h"
#endif
using namespace std;
const int MAX = 1e5 + 5;
const int dx[4] = {-1, +1, 0, 0};
const int dy[4] = {0, 0, +1, -1};
int R, C;
vector<vector<bool>> is_river, vis;
void init(int _R, int _C, int x, int y, int M, string S) {
R = _R;
C = _C;
is_river.resize(R + 1, vector<bool>(C + 1, false));
vis.resize(R + 1, vector<bool>(C + 1, false));
for(int i = 0; i <= M; ++i){
assert(1 <= x && x <= R && 1 <= y && y <= C);
is_river[x][y] = true;
if(i < M){
if(S[i] == 'N') --x;
if(S[i] == 'S') ++x;
if(S[i] == 'W') --y;
if(S[i] == 'E') ++y;
}
}
}
int colour(int ar, int ac, int br, int bc) {
for(int i = ar; i <= br; ++i){
for(int j = ac; j <= bc; ++j){
vis[i][j] = false;
}
}
int cc = 0;
queue<pair<int, int>> q;
for(int i = ar; i <= br; ++i){
for(int j = ac; j <= bc; ++j){
if(!vis[i][j] && !is_river[i][j]){
++cc;
q.push({i, j});
vis[i][j] = true;
while(!q.empty()){
int x, y;
tie(x, y) = q.front(); q.pop();
for(int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if(ar <= nx && nx <= br && ac <= ny && ny <= bc && !vis[nx][ny] && !is_river[nx][ny]){
vis[nx][ny] = true;
q.push({nx, ny});
}
}
}
}
}
}
return cc;
}
#ifdef LOCAL
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
int R, C, M, Q, sr, sc; string S;
cin >> R >> C >> M >> Q >> sr >> sc >> S;
init(R, C, sr, sc, M, S);
while(Q--){
int ar, ac, br, bc;
cin >> ar >> ac >> br >> bc;
cout << colour(ar, ac, br, bc) << '\n';
}
return 0;
}
#endif // LOCAL