Submission #987270

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


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

int32_t up[MAXN][12][MAXN], down[MAXN][12][MAXN];
int32_t Left[MAXN][12][MAXN], Right[MAXN][12][MAXN];

int n,m;

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 left_query(int i, int l, int r) {
  int k = 32 - __builtin_clz(r - l + 1) - 1;
  return max(Left[i][k][l], Left[i][k][r - (1<<k) + 1]);
}

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

int count_rectangles(std::vector<std::vector<int32_t> > a) {
	
  n = a.size(), m = a[0].size();

  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;
        }
      }
    }
  }
  for(int i=0; i<n; i++) {
    for(int j=0; j<m; j++) {
      Left[j][0][i] = leftBad[i][j];
      Right[j][0][i] = rightBad[i][j];
      up[i][0][j] = upBad[i][j];
      down[i][0][j] = downBad[i][j];
    }
  }

  for(int k=1; k<12; 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))]);
      }
    }
    for(int i=0; i<m; i++) {
      for(int j=0; j+(1<<k)-1<n; j++) {
        Left[i][k][j] = max(Left[i][k-1][j], Left[i][k-1][j+(1<<(k-1))]);
        Right[i][k][j] = min(Right[i][k-1][j], Right[i][k-1][j+(1<<(k-1))]);
      }
    }
  }
  
  int ans = 0;
  for(int len=1; len<=n-2; len++) {
    for(int i=1; i+len-1<=n-2; i++) {
      int sto[m];
      for(int x=0; x<m; x++) sto[x] = -1;
      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_query(x - 1, u, d) - 1;
        sto[l] = r;
        if(u <= d && l <= r && l > 0 && r + 1 < m) {
          alive = 1;
          alive &= (right_query(l - 1, u, d) > r);
          alive &= (left_query(r + 1, u, d) < l);
          alive &= (down_query(u - 1, l, r) > d);
          alive &= (up_query(d + 1, l, r) < u);
          ans += alive;
          //if(alive) cout << "u = " << u << ", d = " << d << ", l = " << l << ", r = "<<r<<"\n";
        }
        // as right
        l = left_query(x + 1, u, d) + 1, r = x;
        
        if(u <= d && l <= r && sto[l] != r && l > 0 && r + 1 < m) {
          alive = 1;
          alive &= (right_query(l - 1, u, d) > r);
          alive &= (left_query(r + 1, u, d) < l);
          alive &= (down_query(u - 1, l, r) > d);
          alive &= (up_query(d + 1, l, r) < u);
          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 2648 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 3420 KB Output is correct
6 Correct 2 ms 3420 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2140 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3416 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Correct 2 ms 3416 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 2 ms 3420 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 3420 KB Output is correct
6 Correct 2 ms 3420 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2140 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3416 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Correct 2 ms 3416 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 2 ms 3420 KB Output is correct
20 Correct 1 ms 2392 KB Output is correct
21 Correct 1 ms 860 KB Output is correct
22 Correct 9 ms 11604 KB Output is correct
23 Correct 11 ms 11512 KB Output is correct
24 Correct 8 ms 11356 KB Output is correct
25 Correct 7 ms 11352 KB Output is correct
26 Correct 7 ms 11356 KB Output is correct
27 Correct 8 ms 11204 KB Output is correct
28 Correct 7 ms 13400 KB Output is correct
29 Correct 5 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 3420 KB Output is correct
6 Correct 2 ms 3420 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2140 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3416 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Correct 2 ms 3416 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 9 ms 11604 KB Output is correct
18 Correct 11 ms 11512 KB Output is correct
19 Correct 8 ms 11356 KB Output is correct
20 Correct 7 ms 11352 KB Output is correct
21 Correct 7 ms 11356 KB Output is correct
22 Correct 8 ms 11204 KB Output is correct
23 Correct 7 ms 13400 KB Output is correct
24 Correct 5 ms 9820 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 2 ms 3420 KB Output is correct
28 Correct 1 ms 2392 KB Output is correct
29 Correct 1 ms 860 KB Output is correct
30 Correct 85 ms 35908 KB Output is correct
31 Correct 85 ms 35664 KB Output is correct
32 Correct 73 ms 35984 KB Output is correct
33 Correct 33 ms 35776 KB Output is correct
34 Correct 33 ms 35672 KB Output is correct
35 Correct 39 ms 36432 KB Output is correct
36 Correct 40 ms 35932 KB Output is correct
37 Correct 33 ms 35672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 3420 KB Output is correct
6 Correct 2 ms 3420 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2140 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3416 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Correct 2 ms 3416 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 9 ms 11604 KB Output is correct
18 Correct 11 ms 11512 KB Output is correct
19 Correct 8 ms 11356 KB Output is correct
20 Correct 7 ms 11352 KB Output is correct
21 Correct 7 ms 11356 KB Output is correct
22 Correct 8 ms 11204 KB Output is correct
23 Correct 7 ms 13400 KB Output is correct
24 Correct 5 ms 9820 KB Output is correct
25 Correct 85 ms 35908 KB Output is correct
26 Correct 85 ms 35664 KB Output is correct
27 Correct 73 ms 35984 KB Output is correct
28 Correct 33 ms 35776 KB Output is correct
29 Correct 33 ms 35672 KB Output is correct
30 Correct 39 ms 36432 KB Output is correct
31 Correct 40 ms 35932 KB Output is correct
32 Correct 33 ms 35672 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 2 ms 3420 KB Output is correct
36 Correct 1 ms 2392 KB Output is correct
37 Correct 1 ms 860 KB Output is correct
38 Correct 2055 ms 205116 KB Output is correct
39 Correct 1991 ms 205196 KB Output is correct
40 Correct 2072 ms 205120 KB Output is correct
41 Correct 2013 ms 205116 KB Output is correct
42 Correct 3602 ms 205224 KB Output is correct
43 Correct 3545 ms 205044 KB Output is correct
44 Correct 3614 ms 205776 KB Output is correct
45 Correct 2888 ms 195732 KB Output is correct
46 Correct 975 ms 202936 KB Output is correct
47 Correct 1022 ms 202944 KB Output is correct
48 Correct 1018 ms 203840 KB Output is correct
49 Correct 1012 ms 205652 KB Output is correct
50 Correct 384 ms 129860 KB Output is correct
51 Correct 292 ms 123732 KB Output is correct
52 Correct 1200 ms 204748 KB Output is correct
53 Correct 1090 ms 205704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 44368 KB Output is correct
2 Correct 22 ms 38236 KB Output is correct
3 Correct 22 ms 44380 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 21 ms 42328 KB Output is correct
6 Correct 21 ms 42328 KB Output is correct
7 Correct 24 ms 42332 KB Output is correct
8 Correct 22 ms 42332 KB Output is correct
9 Correct 21 ms 42332 KB Output is correct
10 Correct 11 ms 21852 KB Output is correct
11 Correct 20 ms 42076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 1 ms 1116 KB Output is correct
7 Execution timed out 5119 ms 761684 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2648 KB Output is correct
2 Correct 2 ms 5212 KB Output is correct
3 Correct 2 ms 3420 KB Output is correct
4 Correct 2 ms 5208 KB Output is correct
5 Correct 2 ms 3420 KB Output is correct
6 Correct 2 ms 3420 KB Output is correct
7 Correct 2 ms 2396 KB Output is correct
8 Correct 1 ms 2140 KB Output is correct
9 Correct 2 ms 3420 KB Output is correct
10 Correct 2 ms 3416 KB Output is correct
11 Correct 2 ms 3420 KB Output is correct
12 Correct 2 ms 3416 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 1116 KB Output is correct
16 Correct 1 ms 604 KB Output is correct
17 Correct 9 ms 11604 KB Output is correct
18 Correct 11 ms 11512 KB Output is correct
19 Correct 8 ms 11356 KB Output is correct
20 Correct 7 ms 11352 KB Output is correct
21 Correct 7 ms 11356 KB Output is correct
22 Correct 8 ms 11204 KB Output is correct
23 Correct 7 ms 13400 KB Output is correct
24 Correct 5 ms 9820 KB Output is correct
25 Correct 85 ms 35908 KB Output is correct
26 Correct 85 ms 35664 KB Output is correct
27 Correct 73 ms 35984 KB Output is correct
28 Correct 33 ms 35776 KB Output is correct
29 Correct 33 ms 35672 KB Output is correct
30 Correct 39 ms 36432 KB Output is correct
31 Correct 40 ms 35932 KB Output is correct
32 Correct 33 ms 35672 KB Output is correct
33 Correct 2055 ms 205116 KB Output is correct
34 Correct 1991 ms 205196 KB Output is correct
35 Correct 2072 ms 205120 KB Output is correct
36 Correct 2013 ms 205116 KB Output is correct
37 Correct 3602 ms 205224 KB Output is correct
38 Correct 3545 ms 205044 KB Output is correct
39 Correct 3614 ms 205776 KB Output is correct
40 Correct 2888 ms 195732 KB Output is correct
41 Correct 975 ms 202936 KB Output is correct
42 Correct 1022 ms 202944 KB Output is correct
43 Correct 1018 ms 203840 KB Output is correct
44 Correct 1012 ms 205652 KB Output is correct
45 Correct 384 ms 129860 KB Output is correct
46 Correct 292 ms 123732 KB Output is correct
47 Correct 1200 ms 204748 KB Output is correct
48 Correct 1090 ms 205704 KB Output is correct
49 Correct 28 ms 44368 KB Output is correct
50 Correct 22 ms 38236 KB Output is correct
51 Correct 22 ms 44380 KB Output is correct
52 Correct 1 ms 2396 KB Output is correct
53 Correct 21 ms 42328 KB Output is correct
54 Correct 21 ms 42328 KB Output is correct
55 Correct 24 ms 42332 KB Output is correct
56 Correct 22 ms 42332 KB Output is correct
57 Correct 21 ms 42332 KB Output is correct
58 Correct 11 ms 21852 KB Output is correct
59 Correct 20 ms 42076 KB Output is correct
60 Correct 1 ms 1116 KB Output is correct
61 Execution timed out 5119 ms 761684 KB Time limit exceeded
62 Halted 0 ms 0 KB -