답안 #792203

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
792203 2023-07-24T17:30:01 Z cig32 Dijamant (COCI22_dijamant) C++17
70 / 70
388 ms 116572 KB
#include "bits/stdc++.h"
using namespace std;
const int MAXN = 4e6 + 10;
const int MOD = 1e9 + 7;
#define int int
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) { // bigmod
    if(p==0) return 1;
    int r = bm(b, p/2);
    if(p&1) return (((r*r) % MOD) * b) % MOD;
    return (r*r) % MOD;
}
int inv(int b) { // modinv
    return bm(b, MOD-2);
}
int f[MAXN];
int nCr(int n, int r) { // nCr main function, requires precomputation function
    int ans = f[n]; ans *= inv(f[r]); ans %= MOD;
    ans *= inv(f[n-r]); ans %= MOD; return ans;
}
void precomp() { // factorials for nCr, function MUST BE CALLED
    f[0] = 1;
    for(int i=1; i<MAXN; i++) f[i] = (f[i-1] * i) % MOD;
}
int dsu[MAXN];
int set_of(int u) {
  if(dsu[u] == u) return u;
  return dsu[u] = set_of(dsu[u]);
}
void union_(int u, int v) {
  dsu[set_of(u)] = set_of(v);
}
void solve(int tc) {
  int n, m;
  cin >> n >> m;
  for(int i=1; i<=n*m; i++) dsu[i] = i;
  char a[n+1][m+1];
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      cin >> a[i][j];
    }
  }
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      if(i < n && a[i][j] == '.' && a[i+1][j] == '.' && set_of((i-1) * m + j) != set_of(i * m + j)) union_((i-1) * m + j, i * m + j);
      if(j < m && a[i][j] == '.' && a[i][j+1] == '.' && set_of((i-1) * m + j) != set_of((i-1) * m + j + 1)) union_((i-1) * m + j, (i-1) * m + j + 1); 
    }
  }
  int sz[n * m + 1];
  for(int i=1; i<=n * m; i++) sz[i] = 0;
  for(int i=1; i<=n * m; i++) sz[set_of(i)]++;
  int plus[n+2][m+2], minus[n+2][m+2];
  int hor[n+2][m+2], ver[n+2][m+2];
  for(int i=0; i<=n+1; i++) {
    for(int j=0; j<=m+1; j++) {
      plus[i][j] = minus[i][j] = hor[i][j] = ver[i][j] = 0;
    }
  }
  for(int i=1; i<=n; i++) {
    for(int j=1; j<=m; j++) {
      plus[i][j] = plus[i-1][j+1] + (a[i][j] == '#');
      minus[i][j] = minus[i-1][j-1] + (a[i][j] == '#');
      hor[i][j] = hor[i][j-1] + (a[i][j] == '#');
      ver[i][j] = ver[i-1][j] + (a[i][j] == '#');
    }
  }
  int ans = 0;
  for(int i=2; i<n; i++) {
    for(int j=2; j<m; j++) {
      if(a[i][j] == '#') continue;
      int lb = 0, rb = j;
      int len;
      while(lb < rb) {
        int mid = (lb + rb + 1) >> 1;
        if(hor[i][j] - hor[i][mid] == 0) rb = mid - 1;
        else lb = mid;
      }
      if(hor[i][j] - hor[i][lb] == 1) {
        len = j - lb - 1; // dist between corner and centre
      }
      if(j + len > m) continue;
      if(i + len > n) continue;
      if(i - len < 1) continue;
      if(hor[i][j + len] - hor[i][j] == 1 && a[i][j + len] == '#') {
        if(ver[i + len][j] - ver[i][j] == 1 && a[i + len][j] == '#') {
          if(ver[i - 1][j] - ver[i - len - 1][j] == 1 && a[i - len][j] == '#') {
            if(a[i][j - len] == '#') {
              if(minus[i][j + len] - minus[i - len][j] == len) {
                if(plus[i + len][j] - plus[i][j + len] == len) {
                  if(minus[i + len][j] - minus[i][j - len] == len) {
                    if(plus[i][j - len] - plus[i - len][j] == len) {
                      if(sz[set_of((i-1) * m + j)] == len*len + (len-1) * (len-1)) {
                        ans++;
                      } 
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  cout << ans << "\n";
}
int32_t main(){
  precomp();
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}  

Compilation message

Main.cpp: In function 'void solve(int)':
Main.cpp:93:19: warning: 'len' may be used uninitialized in this function [-Wmaybe-uninitialized]
   93 |                   if(minus[i + len][j] - minus[i][j - len] == len) {
      |                   ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 16212 KB Output is correct
2 Correct 22 ms 16152 KB Output is correct
3 Correct 22 ms 16168 KB Output is correct
4 Correct 24 ms 16196 KB Output is correct
5 Correct 22 ms 16232 KB Output is correct
6 Correct 22 ms 16168 KB Output is correct
7 Correct 22 ms 16228 KB Output is correct
8 Correct 22 ms 16104 KB Output is correct
9 Correct 23 ms 16120 KB Output is correct
10 Correct 23 ms 16164 KB Output is correct
11 Correct 22 ms 16116 KB Output is correct
12 Correct 24 ms 16168 KB Output is correct
13 Correct 24 ms 16196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 16212 KB Output is correct
2 Correct 22 ms 16152 KB Output is correct
3 Correct 22 ms 16168 KB Output is correct
4 Correct 24 ms 16196 KB Output is correct
5 Correct 22 ms 16232 KB Output is correct
6 Correct 22 ms 16168 KB Output is correct
7 Correct 22 ms 16228 KB Output is correct
8 Correct 22 ms 16104 KB Output is correct
9 Correct 23 ms 16120 KB Output is correct
10 Correct 23 ms 16164 KB Output is correct
11 Correct 22 ms 16116 KB Output is correct
12 Correct 24 ms 16168 KB Output is correct
13 Correct 24 ms 16196 KB Output is correct
14 Correct 284 ms 109336 KB Output is correct
15 Correct 112 ms 111864 KB Output is correct
16 Correct 225 ms 104204 KB Output is correct
17 Correct 331 ms 104224 KB Output is correct
18 Correct 141 ms 116572 KB Output is correct
19 Correct 341 ms 101040 KB Output is correct
20 Correct 388 ms 116280 KB Output is correct
21 Correct 329 ms 101196 KB Output is correct
22 Correct 313 ms 106628 KB Output is correct
23 Correct 314 ms 101956 KB Output is correct
24 Correct 341 ms 108600 KB Output is correct
25 Correct 260 ms 100776 KB Output is correct
26 Correct 145 ms 104024 KB Output is correct