Submission #987135

# Submission time Handle Problem Language Result Execution time Memory
987135 2024-05-22T04:33:14 Z cig32 Rectangles (IOI19_rect) C++17
18 / 100
5000 ms 22680 KB
#include "rect.h"
#include "bits/stdc++.h"
using namespace std;
#define int long long


const int MAXN = 30;
int32_t upBad[MAXN][MAXN];
int32_t downBad[MAXN][MAXN];
int32_t leftBad[MAXN][MAXN];
int32_t rightBad[MAXN][MAXN];

int32_t up[MAXN][10][MAXN], down[MAXN][10][MAXN];

int up_query(int i, int l, int r) {
  int k = 32 - __builtin_clz(r - l + 1) - 1;
  return max(up[i][k][l], up[i][k][r - (1<<k) + 1]);
}

int down_query(int i, int l, int r) {
  int k = 32 - __builtin_clz(r - l + 1) - 1;
  return min(down[i][k][l], down[i][k][r - (1<<k) + 1]);
}

int count_rectangles(std::vector<std::vector<int32_t> > a) {
	
  int n = a.size(), m = a[0].size();
  
  int buh = 0;
  for(int u=1; u+1<n; u++) {
    for(int d=u; d+1<n; d++) {
      for(int l=1; l+1<m; l++) {
        for(int r=l; r+1<m; r++) {
          bool alive = 1;
          for(int i=u; i<=d; i++) {
            for(int j=l; j<=r; j++) {
              alive &= (a[i][j] < a[u-1][j]);
              alive &= (a[i][j] < a[d+1][j]);
              alive &= (a[i][j] < a[i][l-1]);
              alive &= (a[i][j] < a[i][r+1]);
            }
          }
          buh += alive;
          //if(alive) cout << u << ' ' << d << ' ' << l << ' ' <<r << '\n';
        }
      }
    }
  }
  return buh;

  for(int i=0; i<n; i++) {
    for(int j=0; j<m; j++) {
      rightBad[i][j] = m;
      downBad[i][j] = n;
      upBad[i][j] = -1;
      leftBad[i][j] = -1;
      for(int k=j+1; k<m; k++) {
        if(a[i][k] >= a[i][j]) {
          rightBad[i][j] = k; break;
        }
      }
      for(int k=j-1; k>=0; k--) {
        if(a[i][k] >= a[i][j]) {
          leftBad[i][j] = k; break;
        }
      }
      for(int k=i+1; k<n; k++) {
        if(a[k][j] >= a[i][j]) {
          downBad[i][j] = k; break;
        }
      }
      for(int k=i-1; k>=0; k--) {
        if(a[k][j] >= a[i][j]) {
          upBad[i][j] = k; break;
        }
      }
    }
  }
  int left[n][m], right[n][m];
  for(int i=0; i<n; i++) {
    for(int j=0; j<m; j++) {
      left[i][j] = leftBad[i][j];
      right[i][j] = rightBad[i][j];
      up[i][0][j] = upBad[i][j];
      down[i][0][j] = downBad[i][j];
    }
  }

  for(int k=1; k<10; k++) {
    for(int i=0; i<n; i++) {
      for(int j=0; j+(1<<k)-1<m; j++) {
        up[i][k][j] = max(up[i][k-1][j], up[i][k-1][j+(1<<(k-1))]);
        down[i][k][j] = min(down[i][k-1][j], down[i][k-1][j+(1<<(k-1))]);
      }
    }
  }
  //return 0;
  int ans = 0;
  for(int len=1; len<=n-2; len++) {
    if(len > 1) {
      for(int x=0; x+len-1<m; x++) {
        for(int i=0; i+len-1<n; i++) {
          right[i][x] = min(right[i][x], right[i+1][x]);
          left[i][x] = max(left[i][x], left[i+1][x]);
        }
      }
    }
    for(int i=1; i+len-1<=n-2; i++) {
      for(int x=1; x+1<m; x++) {
        int u, d, l, r;
        bool alive;
        u = i, d = i + len - 1;
        // as left
        l = x, r = right[i][x - 1] - 1;
        if(u <= d && l <= r) {
          alive = 1;
          alive &= (right[u][l - 1] > r);
          alive &= (left[u][r + 1] < l);
          alive &= (down_query(u - 1, l, r) > d);
          alive &= (up_query(d + 1, l, r) < u);
          for(int i=u; i<=d; i++) {
            for(int j=l; j<=r; j++) {
              alive &= (a[i][j] < a[u-1][j]);
              alive &= (a[i][j] < a[d+1][j]);
              alive &= (a[i][j] < a[i][l-1]);
              alive &= (a[i][j] < a[i][r+1]);
            }
          }
          ans += alive;
          //if(alive) cout << "u = " << u << ", d = " << d << ", l = " << l << ", r = "<<r<<"\n";
        }
        // as right
        l = left[i][x + 1] + 1, r = x;
        
        if(u <= d && l <= r && !(right[u][l-1] == r+1 && left[u][r+1] == l-1)) {
          alive = 1;
          alive &= (right[u][l - 1] > r);
          alive &= (left[u][r + 1] < l);
          alive &= (down_query(u - 1, l, r) > d);
          alive &= (up_query(d + 1, l, r) < u);
          for(int i=u; i<=d; i++) {
            for(int j=l; j<=r; j++) {
              alive &= (a[i][j] < a[u-1][j]);
              alive &= (a[i][j] < a[d+1][j]);
              alive &= (a[i][j] < a[i][l-1]);
              alive &= (a[i][j] < a[i][r+1]);
            }
          }
          ans += alive;
          //if(alive) cout << "u = " << u << ", d = " << d << ", l = " << l << ", r = "<<r<<"\n";
        }
      }
    }
  }

  return ans;
}

/*
g++ -std=c++17 -O2 -o rect grader.cpp rect.cpp
./rect < input.txt

*/
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 24 ms 348 KB Output is correct
3 Correct 24 ms 348 KB Output is correct
4 Correct 26 ms 432 KB Output is correct
5 Correct 24 ms 344 KB Output is correct
6 Correct 25 ms 596 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 24 ms 344 KB Output is correct
10 Correct 25 ms 348 KB Output is correct
11 Correct 25 ms 348 KB Output is correct
12 Correct 24 ms 452 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 24 ms 600 KB Output is correct
20 Correct 4 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 24 ms 348 KB Output is correct
3 Correct 24 ms 348 KB Output is correct
4 Correct 26 ms 432 KB Output is correct
5 Correct 24 ms 344 KB Output is correct
6 Correct 25 ms 596 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 24 ms 344 KB Output is correct
10 Correct 25 ms 348 KB Output is correct
11 Correct 25 ms 348 KB Output is correct
12 Correct 24 ms 452 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 24 ms 600 KB Output is correct
20 Correct 4 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Execution timed out 5091 ms 444 KB Time limit exceeded
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 24 ms 348 KB Output is correct
3 Correct 24 ms 348 KB Output is correct
4 Correct 26 ms 432 KB Output is correct
5 Correct 24 ms 344 KB Output is correct
6 Correct 25 ms 596 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 24 ms 344 KB Output is correct
10 Correct 25 ms 348 KB Output is correct
11 Correct 25 ms 348 KB Output is correct
12 Correct 24 ms 452 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Execution timed out 5091 ms 444 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 24 ms 348 KB Output is correct
3 Correct 24 ms 348 KB Output is correct
4 Correct 26 ms 432 KB Output is correct
5 Correct 24 ms 344 KB Output is correct
6 Correct 25 ms 596 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 24 ms 344 KB Output is correct
10 Correct 25 ms 348 KB Output is correct
11 Correct 25 ms 348 KB Output is correct
12 Correct 24 ms 452 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Execution timed out 5091 ms 444 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3398 ms 592 KB Output is correct
2 Correct 2108 ms 468 KB Output is correct
3 Correct 3389 ms 476 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 3415 ms 472 KB Output is correct
6 Correct 3397 ms 596 KB Output is correct
7 Correct 3436 ms 472 KB Output is correct
8 Correct 3471 ms 496 KB Output is correct
9 Correct 3430 ms 472 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 24 ms 600 KB Output is correct
4 Correct 4 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Execution timed out 5076 ms 22680 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 24 ms 348 KB Output is correct
3 Correct 24 ms 348 KB Output is correct
4 Correct 26 ms 432 KB Output is correct
5 Correct 24 ms 344 KB Output is correct
6 Correct 25 ms 596 KB Output is correct
7 Correct 4 ms 344 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 24 ms 344 KB Output is correct
10 Correct 25 ms 348 KB Output is correct
11 Correct 25 ms 348 KB Output is correct
12 Correct 24 ms 452 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Execution timed out 5091 ms 444 KB Time limit exceeded
18 Halted 0 ms 0 KB -