답안 #281601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
281601 2020-08-23T09:02:35 Z stoyan_malinin Rectangles (IOI19_rect) C++14
72 / 100
5000 ms 982924 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 = 15;

struct FenwickTree
{
    vector <int> tree;

    FenwickTree(){}
    FenwickTree(int 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;
    }
};

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];
            }
        }
    }
}

map <pair <int, int>, vector <int>> pairRows;
void init()
{
    //if(n<m) rotate90();

    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]);
    }
}

vector <int> toRemove[MAXN];
long long evalRowSeq(int lRow, int rRow, int c1, int c2)
{
    if(lRow>rRow) return 0;

    FenwickTree T(n);
    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, +1);
        toRemove[getVal(row+1, c1+1, c2-1, upBad, 1)].push_back(row);

        for(int x: toRemove[row]) T.update(x, -1);

        ans += T.query(min(maxRow-1, rRow));
        //if(c1==0 && c2==2) cout << "add " << row << " until " << getVal(row+1, c1+1, c2-1, upBad, 1) << '\n';
    }

    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;
}

short int downOne[MAXN][MAXN][20];
int leftZero[MAXN][MAXN], rightZero[MAXN][MAXN], downZero[MAXN][MAXN];

void init01()
{
    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++)
    {
        int zero = -1;
        for(int j = 0;j<m;j++)
        {
            if(a[i][j]==0) zero = j;
            leftZero[i][j] = zero;
        }

        zero = m;
        for(int j = m-1;j>=0;j--)
        {
            if(a[i][j]==0) zero = j;
            rightZero[i][j] = zero;
        }
    }

    for(int j = 0;j<m;j++)
    {
        int one = n, zero = n;
        for(int i = n-1;i>=0;i--)
        {
            if(a[i][j]==1) one = i;
            if(a[i][j]==0) zero = i;

            downOne[i][j][0] = one;
            downZero[i][j] = zero;
        }
    }

    for(int row = 0;row<n;row++)
        initSparse(downOne, row, 0);
}

long long eval01()
{
    init01();

    function <bool(int, int, int)> eval = [&](int l, int r, int row)
    {
        if(l+1>=r) return false;

        int oneRow = getVal(row+1, l+1, r-1, downOne, 0);
        if(oneRow>=n) return false;

        //cout << oneRow << '\n';
        if(downZero[row+1][l]<oneRow) return false;
        if(downZero[row+1][r]<oneRow) return false;

        //cout << "ok" << '\n';
        if(rightZero[row][l+1]<=r-1) return false;
        if(rightZero[oneRow][l+1]<=r-1) return false;

        return true;
    };

    int answer = 0;
    for(int i = 0;i<n-2;i++)
    {
        int last = -1;
        for(int j = 0;j<m;j++)
        {
            if(a[i+1][j]==1)
            {
                if(last==-1)
                {
                    last = j;
                    continue;
                }

                //cout << i << " -> " << last << " " << j << '\n';

                answer += eval(last, j, i);
                last = j;
            }
        }
    }

    return answer;
}

int recogniseSubtask()
{
    int maxVal = 0;
    for(int i = 0;i<n;i++)
        for(int j = 0;j<m;j++)
            maxVal = max(maxVal, a[i][j]);

    if(maxVal<=1) return 6;
    return 1;
}

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();

    //
    if(recogniseSubtask()==6) return eval01();
    return evalN3log();

    /*
    cout << '\n';
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<m;j++) cout << a[i][j] << " ";
        cout << '\n';
    }
    cout << '\n';

    cout << '\n';
    for(int i = 0;i<n;i++)
    {
        for(int j = 0;j<m;j++) cout << downBad[i][j][0] << " ";
        cout << '\n';
    }
    cout << '\n';
    */


}
/*
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:30:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   30 |         while(ind<tree.size())
      |               ~~~^~~~~~~~~~~~
rect.cpp: In function 'long long int evalN3log()':
rect.cpp:224:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  224 |             for(int i = 0;i<v.size();)
      |                           ~^~~~~~~~~
rect.cpp:227:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  227 |                 for(;i<v.size();i++)
      |                      ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 2 ms 1152 KB Output is correct
5 Correct 2 ms 1664 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1024 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 2 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 2 ms 1152 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 1664 KB Output is correct
20 Correct 2 ms 1536 KB Output is correct
21 Correct 1 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 2 ms 1152 KB Output is correct
5 Correct 2 ms 1664 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1024 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 2 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 2 ms 1152 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 4 ms 3072 KB Output is correct
18 Correct 4 ms 3072 KB Output is correct
19 Correct 4 ms 3072 KB Output is correct
20 Correct 4 ms 2944 KB Output is correct
21 Correct 6 ms 3072 KB Output is correct
22 Correct 5 ms 3072 KB Output is correct
23 Correct 6 ms 3072 KB Output is correct
24 Correct 3 ms 2432 KB Output is correct
25 Correct 0 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 1664 KB Output is correct
28 Correct 2 ms 1536 KB Output is correct
29 Correct 1 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 2 ms 1152 KB Output is correct
5 Correct 2 ms 1664 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1024 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 2 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 2 ms 1152 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 4 ms 3072 KB Output is correct
18 Correct 4 ms 3072 KB Output is correct
19 Correct 4 ms 3072 KB Output is correct
20 Correct 4 ms 2944 KB Output is correct
21 Correct 6 ms 3072 KB Output is correct
22 Correct 5 ms 3072 KB Output is correct
23 Correct 6 ms 3072 KB Output is correct
24 Correct 3 ms 2432 KB Output is correct
25 Correct 20 ms 10680 KB Output is correct
26 Correct 20 ms 10624 KB Output is correct
27 Correct 20 ms 10744 KB Output is correct
28 Correct 24 ms 10496 KB Output is correct
29 Correct 35 ms 10616 KB Output is correct
30 Correct 38 ms 10872 KB Output is correct
31 Correct 32 ms 10744 KB Output is correct
32 Correct 31 ms 10616 KB Output is correct
33 Correct 0 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 1664 KB Output is correct
36 Correct 2 ms 1536 KB Output is correct
37 Correct 1 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 2 ms 1152 KB Output is correct
5 Correct 2 ms 1664 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1024 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 2 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 2 ms 1152 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 4 ms 3072 KB Output is correct
18 Correct 4 ms 3072 KB Output is correct
19 Correct 4 ms 3072 KB Output is correct
20 Correct 4 ms 2944 KB Output is correct
21 Correct 6 ms 3072 KB Output is correct
22 Correct 5 ms 3072 KB Output is correct
23 Correct 6 ms 3072 KB Output is correct
24 Correct 3 ms 2432 KB Output is correct
25 Correct 20 ms 10680 KB Output is correct
26 Correct 20 ms 10624 KB Output is correct
27 Correct 20 ms 10744 KB Output is correct
28 Correct 24 ms 10496 KB Output is correct
29 Correct 35 ms 10616 KB Output is correct
30 Correct 38 ms 10872 KB Output is correct
31 Correct 32 ms 10744 KB Output is correct
32 Correct 31 ms 10616 KB Output is correct
33 Correct 504 ms 93800 KB Output is correct
34 Correct 504 ms 93836 KB Output is correct
35 Correct 398 ms 92664 KB Output is correct
36 Correct 433 ms 92600 KB Output is correct
37 Correct 260 ms 87856 KB Output is correct
38 Correct 252 ms 87804 KB Output is correct
39 Correct 252 ms 87800 KB Output is correct
40 Correct 241 ms 83832 KB Output is correct
41 Correct 206 ms 103288 KB Output is correct
42 Correct 346 ms 86192 KB Output is correct
43 Correct 576 ms 88240 KB Output is correct
44 Correct 581 ms 88312 KB Output is correct
45 Correct 261 ms 46840 KB Output is correct
46 Correct 276 ms 54392 KB Output is correct
47 Correct 504 ms 87948 KB Output is correct
48 Correct 528 ms 88056 KB Output is correct
49 Correct 0 ms 384 KB Output is correct
50 Correct 1 ms 384 KB Output is correct
51 Correct 1 ms 1664 KB Output is correct
52 Correct 2 ms 1536 KB Output is correct
53 Correct 1 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 826 ms 245976 KB Output is correct
2 Correct 534 ms 177656 KB Output is correct
3 Correct 3 ms 1792 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 748 ms 246008 KB Output is correct
6 Correct 742 ms 246008 KB Output is correct
7 Correct 737 ms 245972 KB Output is correct
8 Correct 739 ms 245880 KB Output is correct
9 Correct 734 ms 246008 KB Output is correct
10 Correct 1136 ms 245008 KB Output is correct
11 Correct 1057 ms 245280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 768 KB Output is correct
2 Correct 1285 ms 472448 KB Output is correct
3 Correct 2909 ms 977044 KB Output is correct
4 Correct 2952 ms 982924 KB Output is correct
5 Correct 3066 ms 982736 KB Output is correct
6 Correct 1038 ms 481784 KB Output is correct
7 Correct 1977 ms 947888 KB Output is correct
8 Correct 2117 ms 970616 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 1664 KB Output is correct
12 Correct 2 ms 1536 KB Output is correct
13 Correct 1 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 1 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 2 ms 1152 KB Output is correct
5 Correct 2 ms 1664 KB Output is correct
6 Correct 1 ms 1152 KB Output is correct
7 Correct 1 ms 1024 KB Output is correct
8 Correct 2 ms 768 KB Output is correct
9 Correct 1 ms 1152 KB Output is correct
10 Correct 2 ms 1152 KB Output is correct
11 Correct 1 ms 1152 KB Output is correct
12 Correct 2 ms 1152 KB Output is correct
13 Correct 0 ms 384 KB Output is correct
14 Correct 1 ms 640 KB Output is correct
15 Correct 1 ms 768 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 4 ms 3072 KB Output is correct
18 Correct 4 ms 3072 KB Output is correct
19 Correct 4 ms 3072 KB Output is correct
20 Correct 4 ms 2944 KB Output is correct
21 Correct 6 ms 3072 KB Output is correct
22 Correct 5 ms 3072 KB Output is correct
23 Correct 6 ms 3072 KB Output is correct
24 Correct 3 ms 2432 KB Output is correct
25 Correct 20 ms 10680 KB Output is correct
26 Correct 20 ms 10624 KB Output is correct
27 Correct 20 ms 10744 KB Output is correct
28 Correct 24 ms 10496 KB Output is correct
29 Correct 35 ms 10616 KB Output is correct
30 Correct 38 ms 10872 KB Output is correct
31 Correct 32 ms 10744 KB Output is correct
32 Correct 31 ms 10616 KB Output is correct
33 Correct 504 ms 93800 KB Output is correct
34 Correct 504 ms 93836 KB Output is correct
35 Correct 398 ms 92664 KB Output is correct
36 Correct 433 ms 92600 KB Output is correct
37 Correct 260 ms 87856 KB Output is correct
38 Correct 252 ms 87804 KB Output is correct
39 Correct 252 ms 87800 KB Output is correct
40 Correct 241 ms 83832 KB Output is correct
41 Correct 206 ms 103288 KB Output is correct
42 Correct 346 ms 86192 KB Output is correct
43 Correct 576 ms 88240 KB Output is correct
44 Correct 581 ms 88312 KB Output is correct
45 Correct 261 ms 46840 KB Output is correct
46 Correct 276 ms 54392 KB Output is correct
47 Correct 504 ms 87948 KB Output is correct
48 Correct 528 ms 88056 KB Output is correct
49 Correct 826 ms 245976 KB Output is correct
50 Correct 534 ms 177656 KB Output is correct
51 Correct 3 ms 1792 KB Output is correct
52 Correct 1 ms 512 KB Output is correct
53 Correct 748 ms 246008 KB Output is correct
54 Correct 742 ms 246008 KB Output is correct
55 Correct 737 ms 245972 KB Output is correct
56 Correct 739 ms 245880 KB Output is correct
57 Correct 734 ms 246008 KB Output is correct
58 Correct 1136 ms 245008 KB Output is correct
59 Correct 1057 ms 245280 KB Output is correct
60 Correct 1 ms 768 KB Output is correct
61 Correct 1285 ms 472448 KB Output is correct
62 Correct 2909 ms 977044 KB Output is correct
63 Correct 2952 ms 982924 KB Output is correct
64 Correct 3066 ms 982736 KB Output is correct
65 Correct 1038 ms 481784 KB Output is correct
66 Correct 1977 ms 947888 KB Output is correct
67 Correct 2117 ms 970616 KB Output is correct
68 Execution timed out 5129 ms 828096 KB Time limit exceeded
69 Halted 0 ms 0 KB -