Advertisement
Naxocist

mini02_BALLS

Apr 10th, 2022
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using pi = pair<int, int>;
  5.  
  6. const int N = 5005;
  7. int table[N][N], visited[N][N], dsu[N][N], n, parent[N*N], sz[N*N], index=1;
  8. int tx[] = {1, -1, 0, 0};
  9. int ty[] = {0, 0, -1, 1};
  10.  
  11.  
  12. int root(int u){
  13.     if(u == parent[u]) return u;
  14.     return parent[u] = root(parent[u]);
  15. }
  16.  
  17.  
  18. void un(int u, int v){
  19.     int x = root(u), y = root(v);
  20.     if(x == y) return ;
  21.     parent[y] = x;
  22. }
  23.  
  24.  
  25. int func(int r, int c){
  26.     if(table[r][c] == 1) return -1;
  27.     int p = root(dsu[r][c]);
  28.     return sz[p];
  29. }
  30.  
  31.  
  32. void setting(int r, int c){
  33.     vector<pi> v;
  34.     queue<pi> q;
  35.  
  36.     int cnt = 1;
  37.     q.push({r, c});
  38.     v.push_back({r, c});
  39.     visited[r][c] = true;
  40.  
  41.     while(!q.empty()){
  42.         int x = q.front().first, y = q.front().second;
  43.         q.pop();
  44.  
  45.         for(int i=0; i<4; ++i){
  46.             int h = x + tx[i], k = y + ty[i];
  47.             if(h < 1 || k < 1 || h > n || k > n) continue;
  48.  
  49.             if(!visited[h][k] && table[h][k] == 0){
  50.                 visited[h][k] = true;
  51.                 q.push({h, k});
  52.                 v.push_back({h, k});
  53.                 cnt++;
  54.             }
  55.         }
  56.     }
  57.  
  58.     for(auto x : v) dsu[x.first][x.second] = index;
  59.    
  60.     parent[index] = index;
  61.     sz[index] = cnt;
  62.     index += 1;
  63. }
  64.  
  65. void update(int r, int c){
  66.     if(table[r][c] == 0) return ;
  67.     table[r][c] = 0;
  68.  
  69.     dsu[r][c] = index;
  70.     parent[index] = index;
  71.    
  72.     unordered_set<int> us;
  73.     for(int i=0; i<4; ++i){ // check changes
  74.         int h = r + tx[i], k = c + ty[i];
  75.         if(h < 1 || k < 1 || h > n || k > n) continue;
  76.         if(dsu[h][k] != 0){
  77.             int ch = root(dsu[h][k]);
  78.             un(parent[dsu[r][c]], ch);
  79.             us.insert(ch);
  80.         }
  81.     }
  82.  
  83.     int V = 1;
  84.     for(auto x : us) V += sz[x];
  85.     sz[index] = V;
  86.  
  87.     index += 1;
  88. }
  89.  
  90.  
  91. int main(){
  92.  
  93.     scanf("%d", &n);
  94.    
  95.     for(int i=1; i<=n; ++i){
  96.         string s; cin >> s;
  97.         for(int j=1; j<=n; ++j){
  98.             table[i][j] = s[j-1] - '0';
  99.         }
  100.     }
  101.  
  102.     for(int i=1; i<=n; ++i){
  103.         for(int j=1; j<=n; ++j){
  104.             if(visited[i][j] || table[i][j] == 1) continue;
  105.             setting(i, j);
  106.         }
  107.     }
  108.  
  109.     int q; scanf("%d", &q);
  110.     while(q--){
  111.         char k; scanf(" %c", &k);
  112.         int r, c; scanf("%d %d", &r, &c);
  113.  
  114.         if(k == 'F') printf("%d\n", func(r, c));
  115.        
  116.         if(k == 'D') update(r, c);
  117.        
  118.     }
  119.     return 0;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement