Submission #281667

# Submission time Handle Problem Language Result Execution time Memory
281667 2020-08-23T09:48:57 Z stoyan_malinin Rectangles (IOI19_rect) C++14
100 / 100
3676 ms 917280 KB
#include "rect.h"
//#include "grader.cpp"

#include <map>
#include <stack>
#include <deque>
#include <vector>
#include <assert.h>
#include <iostream>
#include <functional>

using namespace std;

const int MAXN = 2505;
const int MAXLog = 12;

struct FenwickTree
{
    int n;
    vector <int> tree;

    FenwickTree(){}
    FenwickTree(int n)
    {
        this->n = n;
        this->tree.assign(n+5, 0);
    }

    void update(int ind, int val)
    {
        ind++;
        while(ind<tree.size())
        {
            tree[ind] += val;
            ind += ind&(-ind);
        }
    }

    int query(int ind)
    {
        ind++;
        int sum = 0;

        while(ind>0)
        {
            sum += tree[ind];
            ind -= ind&(-ind);
        }

        return sum;
    }

    void reset(int newN)
    {
        n = newN;
        for(int i = 0;i<=newN;i++) tree[i] = 0;
    }
};

int n, m;
int a[MAXN][MAXN];

int helpRotate[MAXN][MAXN];
void rotate90()
{
    for(int j = 0;j<m;j++)
        for(int i = n-1;i>=0;i--)
            helpRotate[j][n - 1 - i] = a[i][j];

    swap(n, m);
    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
        a[i][j] = helpRotate[i][j];
}

int rightBad[MAXN][MAXN], leftBad[MAXN][MAXN];
short int downBad[MAXN][MAXN][20], upBad[MAXN][MAXN][20];

int logVal[MAXN];

void initSparse(short int sparse[MAXN][MAXN][20], int row, int mode)
{
    for(int step = 1;step<=MAXLog;step++)
    {
        for(int j = 0;j<m;j++)
        {
            if(j+(1<<(step-1))<m)
            {
                if(mode==0)
                    sparse[row][j][step] = min(sparse[row][j][step-1], sparse[row][j+(1<<(step-1))][step-1]);
                else
                    sparse[row][j][step] = max(sparse[row][j][step-1], sparse[row][j+(1<<(step-1))][step-1]);
            }
            else
            {
                sparse[row][j][step] = sparse[row][j][step-1];
            }
        }
    }
}

vector <int> pairRows[MAXN][MAXN];

FenwickTree T;
vector <int> toRemove[MAXN];

void init()
{
    if(n<m) rotate90();

    T = FenwickTree(n);

    logVal[0] = logVal[1] = 0;
    for(int i = 2;i<=max(n, m)+2;i++)
    {
        logVal[i] = logVal[i/2] + 1;
    }

    for(int i = 0;i<n;i++)
    {
        stack <int> st;
        for(int j = 0;j<m;j++)
        {
            while(st.empty()==false && a[i][st.top()]<a[i][j]) st.pop();
            leftBad[i][j] = ((st.empty()==true)?-1:st.top());

            st.push(j);
        }

        while(st.empty()==false) st.pop();
        for(int j = m-1;j>=0;j--)
        {
            while(st.empty()==false && a[i][st.top()]<a[i][j]) st.pop();
            rightBad[i][j] = ((st.empty()==true)?m-1:st.top());

            st.push(j);
        }
    }

    for(int j = 0;j<m;j++)
    {
        stack <int> st;
        for(int i = 0;i<n;i++)
        {
            while(st.empty()==false && a[st.top()][j]<a[i][j]) st.pop();
            upBad[i][j][0] = ((st.empty()==true)?-1:st.top());

            st.push(i);
        }

        while(st.empty()==false) st.pop();
        for(int i = n-1;i>=0;i--)
        {
            while(st.empty()==false && a[st.top()][j]<a[i][j]) st.pop();
            downBad[i][j][0] = ((st.empty()==true)?n-1:st.top());

            st.push(i);
        }
    }

    for(int row = 0;row<n;row++)
    {
        initSparse(downBad, row, 0);
        initSparse(upBad, row, 1);
    }

    for(int i = 1;i<n-1;i++)
    {
        vector <int> v;
        for(int j = 0;j<m;j++)
        {
            for(int p = v.size()-1;p>=0;p--)
            {
                if(v[p]<leftBad[i][j]) break;
                pairRows[ v[p] ][j].push_back(i);
            }

            while(v.empty()==false && a[i][v.back()]<=a[i][j]) v.pop_back();
            v.push_back(j);
        }
    }
}

int getVal(int row, int l, int r, short int sparse[MAXN][MAXN][20], int mode)
{
    int log2 = logVal[r-l+1];

    if(mode==0)
    {
        if(l>r) return n;
        return min(sparse[row][l][log2], sparse[row][r-(1<<log2)+1][log2]);
    }
    else
    {
        if(l>r) return -1;
        return max(sparse[row][l][log2], sparse[row][r-(1<<log2)+1][log2]);
    }
}

long long evalRowSeq(int lRow, int rRow, int c1, int c2)
{
    if(lRow>rRow) return 0;

    T.reset(rRow-lRow+1);
    for(int row = rRow;row>=lRow;row--)
    {
        toRemove[row].clear();
    }

    int ans = 0;
    for(int row = rRow;row>=lRow;row--)
    {
        int maxRow = getVal(row-1, c1+1, c2-1, downBad, 0);

        T.update(row-lRow, +1);
        toRemove[getVal(row+1, c1+1, c2-1, upBad, 1)].push_back(row);

        for(int x: toRemove[row]) T.update(x-lRow, -1);
        ans += T.query(min(maxRow-1, rRow)-lRow);
    }

    return ans;
}

long long evalN3log()
{
    long long answer = 0;
    for(int c1 = 0;c1<m;c1++)
    {
        for(int c2 = c1+2;c2<m;c2++)
        {
            vector <int> v = pairRows[c1][c2];
            if(v.empty()==true) continue;

            for(int i = 0;i<v.size();)
            {
                int startInd = i;
                for(;i<v.size();i++)
                {
                    if(i-startInd!=v[i]-v[startInd]) break;
                }

                answer += evalRowSeq(v[startInd], v[i-1], c1, c2);
            }
        }
    }

    return answer;
}

long long count_rectangles(vector<vector<int>> _a)
{
    n = _a.size();
    m = _a[0].size();
    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            a[i][j] = _a[i][j];

    init();
    return evalN3log();
}
/*
6 5
4 8 7 5 6
7 4 10 3 5
9 7 20 14 2
9 14 7 3 6
5 7 5 2 7
4 5 13 5 6

4 4
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1

4 5
1 1 1 0 1
1 0 1 1 1
1 1 1 0 1
1 1 1 1 1

2 2
1 1
1 1

3 3
1 1 1
1 1 1
1 0 1

5 5
1 1 1 1 1
1 1 1 1 1
1 1 0 1 1
1 1 1 1 1
1 1 1 1 1
*/

Compilation message

rect.cpp: In member function 'void FenwickTree::update(int, int)':
rect.cpp:32:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |         while(ind<tree.size())
      |               ~~~^~~~~~~~~~~~
rect.cpp: In function 'long long int evalN3log()':
rect.cpp:235:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  235 |             for(int i = 0;i<v.size();)
      |                           ~^~~~~~~~~
rect.cpp:238:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  238 |                 for(;i<v.size();i++)
      |                      ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 93 ms 147832 KB Output is correct
2 Correct 96 ms 148472 KB Output is correct
3 Correct 95 ms 148472 KB Output is correct
4 Correct 95 ms 148472 KB Output is correct
5 Correct 95 ms 148476 KB Output is correct
6 Correct 95 ms 148472 KB Output is correct
7 Correct 97 ms 148344 KB Output is correct
8 Correct 96 ms 148600 KB Output is correct
9 Correct 99 ms 148472 KB Output is correct
10 Correct 95 ms 148472 KB Output is correct
11 Correct 94 ms 148500 KB Output is correct
12 Correct 94 ms 148472 KB Output is correct
13 Correct 95 ms 147704 KB Output is correct
14 Correct 96 ms 147960 KB Output is correct
15 Correct 94 ms 147960 KB Output is correct
16 Correct 94 ms 147828 KB Output is correct
17 Correct 95 ms 147704 KB Output is correct
18 Correct 95 ms 147832 KB Output is correct
19 Correct 95 ms 148472 KB Output is correct
20 Correct 95 ms 148472 KB Output is correct
21 Correct 95 ms 147960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 147832 KB Output is correct
2 Correct 96 ms 148472 KB Output is correct
3 Correct 95 ms 148472 KB Output is correct
4 Correct 95 ms 148472 KB Output is correct
5 Correct 95 ms 148476 KB Output is correct
6 Correct 95 ms 148472 KB Output is correct
7 Correct 97 ms 148344 KB Output is correct
8 Correct 96 ms 148600 KB Output is correct
9 Correct 99 ms 148472 KB Output is correct
10 Correct 95 ms 148472 KB Output is correct
11 Correct 94 ms 148500 KB Output is correct
12 Correct 94 ms 148472 KB Output is correct
13 Correct 95 ms 147704 KB Output is correct
14 Correct 96 ms 147960 KB Output is correct
15 Correct 94 ms 147960 KB Output is correct
16 Correct 94 ms 147828 KB Output is correct
17 Correct 99 ms 150136 KB Output is correct
18 Correct 99 ms 150136 KB Output is correct
19 Correct 99 ms 150136 KB Output is correct
20 Correct 99 ms 150008 KB Output is correct
21 Correct 99 ms 150392 KB Output is correct
22 Correct 98 ms 150136 KB Output is correct
23 Correct 96 ms 150136 KB Output is correct
24 Correct 99 ms 149736 KB Output is correct
25 Correct 95 ms 147704 KB Output is correct
26 Correct 95 ms 147832 KB Output is correct
27 Correct 95 ms 148472 KB Output is correct
28 Correct 95 ms 148472 KB Output is correct
29 Correct 95 ms 147960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 147832 KB Output is correct
2 Correct 96 ms 148472 KB Output is correct
3 Correct 95 ms 148472 KB Output is correct
4 Correct 95 ms 148472 KB Output is correct
5 Correct 95 ms 148476 KB Output is correct
6 Correct 95 ms 148472 KB Output is correct
7 Correct 97 ms 148344 KB Output is correct
8 Correct 96 ms 148600 KB Output is correct
9 Correct 99 ms 148472 KB Output is correct
10 Correct 95 ms 148472 KB Output is correct
11 Correct 94 ms 148500 KB Output is correct
12 Correct 94 ms 148472 KB Output is correct
13 Correct 95 ms 147704 KB Output is correct
14 Correct 96 ms 147960 KB Output is correct
15 Correct 94 ms 147960 KB Output is correct
16 Correct 94 ms 147828 KB Output is correct
17 Correct 99 ms 150136 KB Output is correct
18 Correct 99 ms 150136 KB Output is correct
19 Correct 99 ms 150136 KB Output is correct
20 Correct 99 ms 150008 KB Output is correct
21 Correct 99 ms 150392 KB Output is correct
22 Correct 98 ms 150136 KB Output is correct
23 Correct 96 ms 150136 KB Output is correct
24 Correct 99 ms 149736 KB Output is correct
25 Correct 106 ms 156348 KB Output is correct
26 Correct 107 ms 156408 KB Output is correct
27 Correct 109 ms 156408 KB Output is correct
28 Correct 105 ms 156152 KB Output is correct
29 Correct 111 ms 156408 KB Output is correct
30 Correct 111 ms 156540 KB Output is correct
31 Correct 110 ms 156408 KB Output is correct
32 Correct 121 ms 156408 KB Output is correct
33 Correct 95 ms 147704 KB Output is correct
34 Correct 95 ms 147832 KB Output is correct
35 Correct 95 ms 148472 KB Output is correct
36 Correct 95 ms 148472 KB Output is correct
37 Correct 95 ms 147960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 147832 KB Output is correct
2 Correct 96 ms 148472 KB Output is correct
3 Correct 95 ms 148472 KB Output is correct
4 Correct 95 ms 148472 KB Output is correct
5 Correct 95 ms 148476 KB Output is correct
6 Correct 95 ms 148472 KB Output is correct
7 Correct 97 ms 148344 KB Output is correct
8 Correct 96 ms 148600 KB Output is correct
9 Correct 99 ms 148472 KB Output is correct
10 Correct 95 ms 148472 KB Output is correct
11 Correct 94 ms 148500 KB Output is correct
12 Correct 94 ms 148472 KB Output is correct
13 Correct 95 ms 147704 KB Output is correct
14 Correct 96 ms 147960 KB Output is correct
15 Correct 94 ms 147960 KB Output is correct
16 Correct 94 ms 147828 KB Output is correct
17 Correct 99 ms 150136 KB Output is correct
18 Correct 99 ms 150136 KB Output is correct
19 Correct 99 ms 150136 KB Output is correct
20 Correct 99 ms 150008 KB Output is correct
21 Correct 99 ms 150392 KB Output is correct
22 Correct 98 ms 150136 KB Output is correct
23 Correct 96 ms 150136 KB Output is correct
24 Correct 99 ms 149736 KB Output is correct
25 Correct 106 ms 156348 KB Output is correct
26 Correct 107 ms 156408 KB Output is correct
27 Correct 109 ms 156408 KB Output is correct
28 Correct 105 ms 156152 KB Output is correct
29 Correct 111 ms 156408 KB Output is correct
30 Correct 111 ms 156540 KB Output is correct
31 Correct 110 ms 156408 KB Output is correct
32 Correct 121 ms 156408 KB Output is correct
33 Correct 312 ms 224808 KB Output is correct
34 Correct 258 ms 224628 KB Output is correct
35 Correct 313 ms 223608 KB Output is correct
36 Correct 259 ms 223608 KB Output is correct
37 Correct 292 ms 218832 KB Output is correct
38 Correct 286 ms 218744 KB Output is correct
39 Correct 288 ms 219128 KB Output is correct
40 Correct 266 ms 220308 KB Output is correct
41 Correct 243 ms 214520 KB Output is correct
42 Correct 266 ms 215032 KB Output is correct
43 Correct 327 ms 217848 KB Output is correct
44 Correct 342 ms 219768 KB Output is correct
45 Correct 203 ms 190840 KB Output is correct
46 Correct 205 ms 195260 KB Output is correct
47 Correct 303 ms 218488 KB Output is correct
48 Correct 321 ms 219492 KB Output is correct
49 Correct 95 ms 147704 KB Output is correct
50 Correct 95 ms 147832 KB Output is correct
51 Correct 95 ms 148472 KB Output is correct
52 Correct 95 ms 148472 KB Output is correct
53 Correct 95 ms 147960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 124 ms 209784 KB Output is correct
2 Correct 120 ms 200440 KB Output is correct
3 Correct 123 ms 209656 KB Output is correct
4 Correct 95 ms 147832 KB Output is correct
5 Correct 123 ms 209644 KB Output is correct
6 Correct 123 ms 209620 KB Output is correct
7 Correct 126 ms 209656 KB Output is correct
8 Correct 124 ms 209656 KB Output is correct
9 Correct 125 ms 209620 KB Output is correct
10 Correct 121 ms 209144 KB Output is correct
11 Correct 122 ms 209400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 147960 KB Output is correct
2 Correct 1082 ms 524408 KB Output is correct
3 Correct 2273 ms 835348 KB Output is correct
4 Correct 2206 ms 811640 KB Output is correct
5 Correct 2242 ms 811320 KB Output is correct
6 Correct 840 ms 546216 KB Output is correct
7 Correct 1513 ms 784304 KB Output is correct
8 Correct 1597 ms 801032 KB Output is correct
9 Correct 95 ms 147704 KB Output is correct
10 Correct 95 ms 147832 KB Output is correct
11 Correct 95 ms 148472 KB Output is correct
12 Correct 95 ms 148472 KB Output is correct
13 Correct 95 ms 147960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 147832 KB Output is correct
2 Correct 96 ms 148472 KB Output is correct
3 Correct 95 ms 148472 KB Output is correct
4 Correct 95 ms 148472 KB Output is correct
5 Correct 95 ms 148476 KB Output is correct
6 Correct 95 ms 148472 KB Output is correct
7 Correct 97 ms 148344 KB Output is correct
8 Correct 96 ms 148600 KB Output is correct
9 Correct 99 ms 148472 KB Output is correct
10 Correct 95 ms 148472 KB Output is correct
11 Correct 94 ms 148500 KB Output is correct
12 Correct 94 ms 148472 KB Output is correct
13 Correct 95 ms 147704 KB Output is correct
14 Correct 96 ms 147960 KB Output is correct
15 Correct 94 ms 147960 KB Output is correct
16 Correct 94 ms 147828 KB Output is correct
17 Correct 99 ms 150136 KB Output is correct
18 Correct 99 ms 150136 KB Output is correct
19 Correct 99 ms 150136 KB Output is correct
20 Correct 99 ms 150008 KB Output is correct
21 Correct 99 ms 150392 KB Output is correct
22 Correct 98 ms 150136 KB Output is correct
23 Correct 96 ms 150136 KB Output is correct
24 Correct 99 ms 149736 KB Output is correct
25 Correct 106 ms 156348 KB Output is correct
26 Correct 107 ms 156408 KB Output is correct
27 Correct 109 ms 156408 KB Output is correct
28 Correct 105 ms 156152 KB Output is correct
29 Correct 111 ms 156408 KB Output is correct
30 Correct 111 ms 156540 KB Output is correct
31 Correct 110 ms 156408 KB Output is correct
32 Correct 121 ms 156408 KB Output is correct
33 Correct 312 ms 224808 KB Output is correct
34 Correct 258 ms 224628 KB Output is correct
35 Correct 313 ms 223608 KB Output is correct
36 Correct 259 ms 223608 KB Output is correct
37 Correct 292 ms 218832 KB Output is correct
38 Correct 286 ms 218744 KB Output is correct
39 Correct 288 ms 219128 KB Output is correct
40 Correct 266 ms 220308 KB Output is correct
41 Correct 243 ms 214520 KB Output is correct
42 Correct 266 ms 215032 KB Output is correct
43 Correct 327 ms 217848 KB Output is correct
44 Correct 342 ms 219768 KB Output is correct
45 Correct 203 ms 190840 KB Output is correct
46 Correct 205 ms 195260 KB Output is correct
47 Correct 303 ms 218488 KB Output is correct
48 Correct 321 ms 219492 KB Output is correct
49 Correct 124 ms 209784 KB Output is correct
50 Correct 120 ms 200440 KB Output is correct
51 Correct 123 ms 209656 KB Output is correct
52 Correct 95 ms 147832 KB Output is correct
53 Correct 123 ms 209644 KB Output is correct
54 Correct 123 ms 209620 KB Output is correct
55 Correct 126 ms 209656 KB Output is correct
56 Correct 124 ms 209656 KB Output is correct
57 Correct 125 ms 209620 KB Output is correct
58 Correct 121 ms 209144 KB Output is correct
59 Correct 122 ms 209400 KB Output is correct
60 Correct 93 ms 147960 KB Output is correct
61 Correct 1082 ms 524408 KB Output is correct
62 Correct 2273 ms 835348 KB Output is correct
63 Correct 2206 ms 811640 KB Output is correct
64 Correct 2242 ms 811320 KB Output is correct
65 Correct 840 ms 546216 KB Output is correct
66 Correct 1513 ms 784304 KB Output is correct
67 Correct 1597 ms 801032 KB Output is correct
68 Correct 2997 ms 917280 KB Output is correct
69 Correct 2355 ms 914964 KB Output is correct
70 Correct 3159 ms 898572 KB Output is correct
71 Correct 2363 ms 898364 KB Output is correct
72 Correct 2866 ms 883220 KB Output is correct
73 Correct 2102 ms 647500 KB Output is correct
74 Correct 2175 ms 651308 KB Output is correct
75 Correct 3285 ms 858296 KB Output is correct
76 Correct 2081 ms 663296 KB Output is correct
77 Correct 2716 ms 771060 KB Output is correct
78 Correct 3676 ms 884376 KB Output is correct
79 Correct 2062 ms 654596 KB Output is correct
80 Correct 3456 ms 879560 KB Output is correct
81 Correct 3571 ms 901708 KB Output is correct
82 Correct 1942 ms 638296 KB Output is correct
83 Correct 2991 ms 882904 KB Output is correct
84 Correct 2875 ms 883344 KB Output is correct
85 Correct 2910 ms 883312 KB Output is correct
86 Correct 95 ms 147704 KB Output is correct
87 Correct 95 ms 147832 KB Output is correct
88 Correct 95 ms 148472 KB Output is correct
89 Correct 95 ms 148472 KB Output is correct
90 Correct 95 ms 147960 KB Output is correct