Submission #1009579

# Submission time Handle Problem Language Result Execution time Memory
1009579 2024-06-27T16:13:19 Z boris_mihov Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 974008 KB
#include "rect.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <stack>
 
typedef long long llong;
const short MAXLOG = 6;
const short MAXN = 2500 + 10;
const int INF  = 1e8;
 
short n, m;
short getLOG[MAXN];
struct SparseMIN
{
    short sparse[MAXN][MAXLOG];
    short len;
 
    void build(short a[], short length)
    {
        len = length;
        for (short i = 1 ; i <= len ; ++i)
        {
            sparse[i][0] = a[i]; 
        }
 
        for (short lg = 1 ; (1 << (lg << 1)) <= len ; ++lg)
        {
            for (short i = 1 ; i + (1 << (lg << 1)) - 1 <= len ; ++i)
            {
                sparse[i][lg] = sparse[i][lg - 1];
                if (sparse[i][lg] > sparse[i + (1 << ((lg - 1) << 1))][lg - 1]) sparse[i][lg] = sparse[i + (1 << ((lg - 1) << 1))][lg - 1];
                if (sparse[i][lg] > sparse[i + 2 * (1 << ((lg - 1) << 1))][lg - 1]) sparse[i][lg] = sparse[i + 2 * (1 << ((lg - 1) << 1))][lg - 1];
                if (sparse[i][lg] > sparse[i + 3 * (1 << ((lg - 1) << 1))][lg - 1]) sparse[i][lg] = sparse[i + 3 * (1 << ((lg - 1) << 1))][lg - 1];
            }
        }
    }
 
    inline short findMIN(short &l, short &r, short &val)
    {
        short lg = getLOG[r - l + 1];
        if (sparse[l][lg] < val) return false;
        if (sparse[r - (1 << (lg << 1)) + 1][lg] < val) return false;
        if (r - 2 * (1 << (lg << 1)) + 1 >= l && sparse[r - 2 * (1 << (lg << 1)) + 1][lg] < val) return false;
        if (r - 3 * (1 << (lg << 1)) + 1 >= l && sparse[r - 3 * (1 << (lg << 1)) + 1][lg] < val) return false;
        return true;
    }
};
 
struct SparseMAX
{
    short sparse[MAXN][MAXLOG];
    short len;
 
    void build(short a[], short length)
    {
        len = length;
        for (short i = 1 ; i <= len ; ++i)
        {
            sparse[i][0] = a[i]; 
        }
 
        for (short lg = 1 ; (1 << (lg << 1)) <= len ; ++lg)
        {
            for (short i = 1 ; i + (1 << (lg << 1)) - 1 <= len ; ++i)
            {
                sparse[i][lg] = sparse[i][lg - 1];
                if (sparse[i][lg] < sparse[i + (1 << ((lg - 1) << 1))][lg - 1]) sparse[i][lg] = sparse[i + (1 << ((lg - 1) << 1))][lg - 1];
                if (sparse[i][lg] < sparse[i + 2 * (1 << ((lg - 1) << 1))][lg - 1]) sparse[i][lg] = sparse[i + 2 * (1 << ((lg - 1) << 1))][lg - 1];
                if (sparse[i][lg] < sparse[i + 3 * (1 << ((lg - 1) << 1))][lg - 1]) sparse[i][lg] = sparse[i + 3 * (1 << ((lg - 1) << 1))][lg - 1];
            }
        }
    }
 
    inline short findMAX(short &l, short &r, short &val)
    {
        short lg = getLOG[r - l + 1];
        if (sparse[l][lg] > val) return false;
        if (sparse[r - (1 << (lg << 1)) + 1][lg] > val) return false;
        if (r - 2 * (1 << (lg << 1)) + 1 >= l && sparse[r - 2 * (1 << (lg << 1)) + 1][lg] > val) return false;
        if (r - 3 * (1 << (lg << 1)) + 1 >= l && sparse[r - 3 * (1 << (lg << 1)) + 1][lg] > val) return false;
        return true;
    }
};
 
short a[MAXN][MAXN]; // >
short b[MAXN][MAXN]; // v
short c[MAXN][MAXN]; // <
short d[MAXN][MAXN]; // ^
int t[MAXN][MAXN];
short cpy[MAXN];
 
SparseMIN sparseRD[MAXN];
SparseMAX sparseRU[MAXN];
SparseMIN sparseCL[MAXN];
SparseMAX sparseCR[MAXN];
std::vector <short> possibleColE[MAXN][MAXN];
std::vector <short> possibleRowE[MAXN][MAXN];
 
int answer = 0;
void isOK(short &rowB, short &colB, short &rowE, short &colE)
{
    bool result = true;
    colB++; colE--;
    if (result && !sparseRD[rowB].findMIN(colB, colE, rowE)) result = false;
    if (result && !sparseRU[rowE].findMAX(colB, colE, rowB)) result = false;
    colB--; colE++;
    rowE--; rowB++;
    if (result && !sparseCL[colB].findMIN(rowB, rowE, colE)) result = false;
    if (result && !sparseCR[colE].findMAX(rowB, rowE, colB)) result = false;
    rowE++; rowB--;
    answer += result;
}
 
llong count_rectangles(std::vector <std::vector<int>> _a)
{
    n = _a.size(); m = _a[0].size();
    for (short i = 0 ; i < n ; ++i)
    {
        for (short j = 0 ; j < m ; ++j)
        {
            // max = std::max(max, a[i][j]);
            t[i + 1][j + 1] = _a[i][j];
        }
    }
 
    for (short i = 0 ; i <= n + 1 ; ++i)
    {
        t[i][0] = t[i][m + 1] = INF;
    }
 
    for (short i = 0 ; i <= m + 1 ; ++i)
    {
        t[0][i] = t[n + 1][i] = INF;
    }
 
    std::stack <short> st;
    for (short i = 1 ; i <= n ; ++i)
    {
        while (st.size()) st.pop(); st.push(m + 1);
        for (short j = m ; j >= 1 ; --j)
        {
            while (t[i][j] > t[i][st.top()])
            {
                st.pop();
            }
 
            a[i][j] = st.top();
            st.push(j);
        }
 
        while (st.size()) st.pop(); st.push(0);
        for (short j = 1 ; j <= m ; ++j)
        {
            while (t[i][j] > t[i][st.top()])
            {
                st.pop();
            }
 
            c[i][j] = st.top();
            st.push(j);
        }
    }
 
    for (short i = 1 ; i <= m ; ++i)
    {
        while (st.size()) st.pop(); st.push(n + 1);
        for (short j = n ; j >= 1 ; --j)
        {
            while (t[j][i] > t[st.top()][i])
            {
                st.pop();
            }
 
            b[j][i] = st.top();
            st.push(j);
        }
 
        while (st.size()) st.pop(); st.push(0);
        for (short j = 1 ; j <= n ; ++j)
        {
            while (t[j][i] > t[st.top()][i])
            {
                st.pop();
            }
 
            d[j][i] = st.top();
            st.push(j);
        }
    }
 
    for (short i = 1 ; i <= std::max(n, m) ; ++i)
    {
        getLOG[i] = getLOG[i - 1];
        if ((1 << ((getLOG[i] + 1) << 1)) < i) getLOG[i]++;
    }
 
    for (short i = 1 ; i <= n ; ++i)
    {
        sparseRD[i].build(b[i], m);
        sparseRU[i].build(d[i], m);
    }
 
    for (short i = 1 ; i <= m ; ++i)
    {
        for (short j = 1 ; j <= n ; ++j)
        {
            cpy[j] = a[j][i];
        }
 
        sparseCL[i].build(cpy, n);
 
        for (short j = 1 ; j <= n ; ++j)
        {
            cpy[j] = c[j][i];
        }
 
        sparseCR[i].build(cpy, n);
    }
 
    for (short i = 2 ; i <= n ; ++i)
    {
        for (short j = 2 ; j <= m ; ++j)
        {
            if (d[i][j] < i - 1) possibleRowE[d[i][j]][j - 1].push_back(i);
            if (c[i][j] < j - 1) possibleColE[i - 1][c[i][j]].push_back(j);
        }
    }
 
    for (short i = 2 ; i <= n ; ++i)
    {
        for (short j = 2 ; j <= m ; ++j)
        {
            if (i < b[i - 1][j] && b[i - 1][j] != n + 1 && (possibleRowE[i - 1][j - 1].empty() || possibleRowE[i - 1][j - 1].back() != b[i - 1][j])) possibleRowE[i - 1][j - 1].push_back(b[i - 1][j]);
            if (j < a[i][j - 1] && a[i][j - 1] != m + 1 && (possibleColE[i - 1][j - 1].empty() || possibleColE[i - 1][j - 1].back() != a[i][j - 1])) possibleColE[i - 1][j - 1].push_back(a[i][j - 1]);
        }
    }
    
    for (short rowB = 1 ; rowB + 1 < n ; ++rowB)
    {
        for (short colB = 1 ; colB + 1 < m ; ++colB)
        {
            for (short &rowE : possibleRowE[rowB][colB])
            {
                for (short &colE : possibleColE[rowB][colB])
                {
                    isOK(rowB, colB, rowE, colE);
                }
            }
        }
    }
 
    return answer;
}

Compilation message

rect.cpp: In function 'llong count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:142:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  142 |         while (st.size()) st.pop(); st.push(m + 1);
      |         ^~~~~
rect.cpp:142:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  142 |         while (st.size()) st.pop(); st.push(m + 1);
      |                                     ^~
rect.cpp:154:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  154 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:154:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  154 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
rect.cpp:169:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  169 |         while (st.size()) st.pop(); st.push(n + 1);
      |         ^~~~~
rect.cpp:169:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  169 |         while (st.size()) st.pop(); st.push(n + 1);
      |                                     ^~
rect.cpp:181:9: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
  181 |         while (st.size()) st.pop(); st.push(0);
      |         ^~~~~
rect.cpp:181:37: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
  181 |         while (st.size()) st.pop(); st.push(0);
      |                                     ^~
# Verdict Execution time Memory Grader output
1 Correct 98 ms 296528 KB Output is correct
2 Correct 99 ms 297492 KB Output is correct
3 Correct 94 ms 297556 KB Output is correct
4 Correct 99 ms 297552 KB Output is correct
5 Correct 98 ms 297300 KB Output is correct
6 Correct 91 ms 297556 KB Output is correct
7 Correct 93 ms 297300 KB Output is correct
8 Correct 93 ms 296960 KB Output is correct
9 Correct 107 ms 297300 KB Output is correct
10 Correct 97 ms 297496 KB Output is correct
11 Correct 91 ms 297556 KB Output is correct
12 Correct 98 ms 297552 KB Output is correct
13 Correct 95 ms 296276 KB Output is correct
14 Correct 99 ms 296432 KB Output is correct
15 Correct 95 ms 296532 KB Output is correct
16 Correct 99 ms 296356 KB Output is correct
17 Correct 105 ms 296272 KB Output is correct
18 Correct 95 ms 296276 KB Output is correct
19 Correct 93 ms 297452 KB Output is correct
20 Correct 116 ms 297540 KB Output is correct
21 Correct 98 ms 296532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 296528 KB Output is correct
2 Correct 99 ms 297492 KB Output is correct
3 Correct 94 ms 297556 KB Output is correct
4 Correct 99 ms 297552 KB Output is correct
5 Correct 98 ms 297300 KB Output is correct
6 Correct 91 ms 297556 KB Output is correct
7 Correct 93 ms 297300 KB Output is correct
8 Correct 93 ms 296960 KB Output is correct
9 Correct 107 ms 297300 KB Output is correct
10 Correct 97 ms 297496 KB Output is correct
11 Correct 91 ms 297556 KB Output is correct
12 Correct 98 ms 297552 KB Output is correct
13 Correct 95 ms 296276 KB Output is correct
14 Correct 99 ms 296432 KB Output is correct
15 Correct 95 ms 296532 KB Output is correct
16 Correct 99 ms 296356 KB Output is correct
17 Correct 105 ms 296272 KB Output is correct
18 Correct 95 ms 296276 KB Output is correct
19 Correct 93 ms 297452 KB Output is correct
20 Correct 116 ms 297540 KB Output is correct
21 Correct 98 ms 296532 KB Output is correct
22 Correct 102 ms 299904 KB Output is correct
23 Correct 95 ms 299856 KB Output is correct
24 Correct 96 ms 299860 KB Output is correct
25 Correct 95 ms 299600 KB Output is correct
26 Correct 106 ms 299860 KB Output is correct
27 Correct 96 ms 299860 KB Output is correct
28 Correct 101 ms 299700 KB Output is correct
29 Correct 93 ms 299092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 296528 KB Output is correct
2 Correct 99 ms 297492 KB Output is correct
3 Correct 94 ms 297556 KB Output is correct
4 Correct 99 ms 297552 KB Output is correct
5 Correct 98 ms 297300 KB Output is correct
6 Correct 91 ms 297556 KB Output is correct
7 Correct 93 ms 297300 KB Output is correct
8 Correct 93 ms 296960 KB Output is correct
9 Correct 107 ms 297300 KB Output is correct
10 Correct 97 ms 297496 KB Output is correct
11 Correct 91 ms 297556 KB Output is correct
12 Correct 98 ms 297552 KB Output is correct
13 Correct 95 ms 296276 KB Output is correct
14 Correct 99 ms 296432 KB Output is correct
15 Correct 95 ms 296532 KB Output is correct
16 Correct 99 ms 296356 KB Output is correct
17 Correct 102 ms 299904 KB Output is correct
18 Correct 95 ms 299856 KB Output is correct
19 Correct 96 ms 299860 KB Output is correct
20 Correct 95 ms 299600 KB Output is correct
21 Correct 106 ms 299860 KB Output is correct
22 Correct 96 ms 299860 KB Output is correct
23 Correct 101 ms 299700 KB Output is correct
24 Correct 93 ms 299092 KB Output is correct
25 Correct 105 ms 296272 KB Output is correct
26 Correct 95 ms 296276 KB Output is correct
27 Correct 93 ms 297452 KB Output is correct
28 Correct 116 ms 297540 KB Output is correct
29 Correct 98 ms 296532 KB Output is correct
30 Correct 111 ms 308560 KB Output is correct
31 Correct 107 ms 308572 KB Output is correct
32 Correct 100 ms 308564 KB Output is correct
33 Correct 106 ms 306916 KB Output is correct
34 Correct 109 ms 307540 KB Output is correct
35 Correct 115 ms 307540 KB Output is correct
36 Correct 102 ms 307280 KB Output is correct
37 Correct 110 ms 307284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 296528 KB Output is correct
2 Correct 99 ms 297492 KB Output is correct
3 Correct 94 ms 297556 KB Output is correct
4 Correct 99 ms 297552 KB Output is correct
5 Correct 98 ms 297300 KB Output is correct
6 Correct 91 ms 297556 KB Output is correct
7 Correct 93 ms 297300 KB Output is correct
8 Correct 93 ms 296960 KB Output is correct
9 Correct 107 ms 297300 KB Output is correct
10 Correct 97 ms 297496 KB Output is correct
11 Correct 91 ms 297556 KB Output is correct
12 Correct 98 ms 297552 KB Output is correct
13 Correct 95 ms 296276 KB Output is correct
14 Correct 99 ms 296432 KB Output is correct
15 Correct 95 ms 296532 KB Output is correct
16 Correct 99 ms 296356 KB Output is correct
17 Correct 102 ms 299904 KB Output is correct
18 Correct 95 ms 299856 KB Output is correct
19 Correct 96 ms 299860 KB Output is correct
20 Correct 95 ms 299600 KB Output is correct
21 Correct 106 ms 299860 KB Output is correct
22 Correct 96 ms 299860 KB Output is correct
23 Correct 101 ms 299700 KB Output is correct
24 Correct 93 ms 299092 KB Output is correct
25 Correct 111 ms 308560 KB Output is correct
26 Correct 107 ms 308572 KB Output is correct
27 Correct 100 ms 308564 KB Output is correct
28 Correct 106 ms 306916 KB Output is correct
29 Correct 109 ms 307540 KB Output is correct
30 Correct 115 ms 307540 KB Output is correct
31 Correct 102 ms 307280 KB Output is correct
32 Correct 110 ms 307284 KB Output is correct
33 Correct 105 ms 296272 KB Output is correct
34 Correct 95 ms 296276 KB Output is correct
35 Correct 93 ms 297452 KB Output is correct
36 Correct 116 ms 297540 KB Output is correct
37 Correct 98 ms 296532 KB Output is correct
38 Correct 175 ms 369616 KB Output is correct
39 Correct 191 ms 362072 KB Output is correct
40 Correct 171 ms 361988 KB Output is correct
41 Correct 1244 ms 354392 KB Output is correct
42 Correct 194 ms 383572 KB Output is correct
43 Correct 196 ms 383468 KB Output is correct
44 Correct 201 ms 383568 KB Output is correct
45 Correct 196 ms 378060 KB Output is correct
46 Correct 184 ms 360528 KB Output is correct
47 Correct 203 ms 363260 KB Output is correct
48 Correct 272 ms 368212 KB Output is correct
49 Correct 256 ms 368296 KB Output is correct
50 Correct 183 ms 342848 KB Output is correct
51 Correct 186 ms 334992 KB Output is correct
52 Correct 239 ms 367000 KB Output is correct
53 Correct 235 ms 367188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 106 ms 317268 KB Output is correct
2 Correct 99 ms 314188 KB Output is correct
3 Correct 101 ms 317012 KB Output is correct
4 Correct 96 ms 296276 KB Output is correct
5 Correct 106 ms 317272 KB Output is correct
6 Correct 104 ms 317264 KB Output is correct
7 Correct 105 ms 317256 KB Output is correct
8 Correct 107 ms 317300 KB Output is correct
9 Correct 108 ms 317264 KB Output is correct
10 Correct 99 ms 316756 KB Output is correct
11 Correct 101 ms 316892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 105 ms 296272 KB Output is correct
2 Correct 95 ms 296276 KB Output is correct
3 Correct 93 ms 297452 KB Output is correct
4 Correct 116 ms 297540 KB Output is correct
5 Correct 98 ms 296532 KB Output is correct
6 Correct 99 ms 296528 KB Output is correct
7 Correct 593 ms 558616 KB Output is correct
8 Correct 1179 ms 821844 KB Output is correct
9 Correct 1182 ms 823636 KB Output is correct
10 Correct 1304 ms 823636 KB Output is correct
11 Correct 416 ms 528452 KB Output is correct
12 Correct 759 ms 713044 KB Output is correct
13 Correct 796 ms 726192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 98 ms 296528 KB Output is correct
2 Correct 99 ms 297492 KB Output is correct
3 Correct 94 ms 297556 KB Output is correct
4 Correct 99 ms 297552 KB Output is correct
5 Correct 98 ms 297300 KB Output is correct
6 Correct 91 ms 297556 KB Output is correct
7 Correct 93 ms 297300 KB Output is correct
8 Correct 93 ms 296960 KB Output is correct
9 Correct 107 ms 297300 KB Output is correct
10 Correct 97 ms 297496 KB Output is correct
11 Correct 91 ms 297556 KB Output is correct
12 Correct 98 ms 297552 KB Output is correct
13 Correct 95 ms 296276 KB Output is correct
14 Correct 99 ms 296432 KB Output is correct
15 Correct 95 ms 296532 KB Output is correct
16 Correct 99 ms 296356 KB Output is correct
17 Correct 102 ms 299904 KB Output is correct
18 Correct 95 ms 299856 KB Output is correct
19 Correct 96 ms 299860 KB Output is correct
20 Correct 95 ms 299600 KB Output is correct
21 Correct 106 ms 299860 KB Output is correct
22 Correct 96 ms 299860 KB Output is correct
23 Correct 101 ms 299700 KB Output is correct
24 Correct 93 ms 299092 KB Output is correct
25 Correct 111 ms 308560 KB Output is correct
26 Correct 107 ms 308572 KB Output is correct
27 Correct 100 ms 308564 KB Output is correct
28 Correct 106 ms 306916 KB Output is correct
29 Correct 109 ms 307540 KB Output is correct
30 Correct 115 ms 307540 KB Output is correct
31 Correct 102 ms 307280 KB Output is correct
32 Correct 110 ms 307284 KB Output is correct
33 Correct 175 ms 369616 KB Output is correct
34 Correct 191 ms 362072 KB Output is correct
35 Correct 171 ms 361988 KB Output is correct
36 Correct 1244 ms 354392 KB Output is correct
37 Correct 194 ms 383572 KB Output is correct
38 Correct 196 ms 383468 KB Output is correct
39 Correct 201 ms 383568 KB Output is correct
40 Correct 196 ms 378060 KB Output is correct
41 Correct 184 ms 360528 KB Output is correct
42 Correct 203 ms 363260 KB Output is correct
43 Correct 272 ms 368212 KB Output is correct
44 Correct 256 ms 368296 KB Output is correct
45 Correct 183 ms 342848 KB Output is correct
46 Correct 186 ms 334992 KB Output is correct
47 Correct 239 ms 367000 KB Output is correct
48 Correct 235 ms 367188 KB Output is correct
49 Correct 106 ms 317268 KB Output is correct
50 Correct 99 ms 314188 KB Output is correct
51 Correct 101 ms 317012 KB Output is correct
52 Correct 96 ms 296276 KB Output is correct
53 Correct 106 ms 317272 KB Output is correct
54 Correct 104 ms 317264 KB Output is correct
55 Correct 105 ms 317256 KB Output is correct
56 Correct 107 ms 317300 KB Output is correct
57 Correct 108 ms 317264 KB Output is correct
58 Correct 99 ms 316756 KB Output is correct
59 Correct 101 ms 316892 KB Output is correct
60 Correct 99 ms 296528 KB Output is correct
61 Correct 593 ms 558616 KB Output is correct
62 Correct 1179 ms 821844 KB Output is correct
63 Correct 1182 ms 823636 KB Output is correct
64 Correct 1304 ms 823636 KB Output is correct
65 Correct 416 ms 528452 KB Output is correct
66 Correct 759 ms 713044 KB Output is correct
67 Correct 796 ms 726192 KB Output is correct
68 Correct 105 ms 296272 KB Output is correct
69 Correct 95 ms 296276 KB Output is correct
70 Correct 93 ms 297452 KB Output is correct
71 Correct 116 ms 297540 KB Output is correct
72 Correct 98 ms 296532 KB Output is correct
73 Correct 1233 ms 974008 KB Output is correct
74 Correct 1092 ms 876440 KB Output is correct
75 Correct 1086 ms 876388 KB Output is correct
76 Execution timed out 5109 ms 778836 KB Time limit exceeded
77 Halted 0 ms 0 KB -