Submission #579226

#TimeUsernameProblemLanguageResultExecution timeMemory
579226ogibogi2004Rectangles (IOI19_rect)C++14
Compilation error
0 ms0 KiB
#include "rect.h"
#include<bits/stdc++.h>
const int MAXN=2502;
const int logN=12;
int bigger_up[MAXN][MAXN];
int bigger_down[MAXN][MAXN];
int bigger_left[MAXN][MAXN];
int bigger_right[MAXN][MAXN];
int n,m;
vector<vector<int> >b;
void precomp(int t)
{
    for(int i=0;i<n;i++)
    {
        stack<int>st;
        for(int j=0;j<m;j++)
        {
            while(!st.empty()&&b[i][st.top()]+t<=b[i][j])
            {
                st.pop();
            }
            if(!st.empty())
            {
                bigger_left[i][j]=st.top();
            }
            else
            {
                bigger_left[i][j]=-1;
            }
            st.push(j);
        }
    }
    for(int i=0;i<n;i++)
    {
        stack<int>st;
        for(int j=m-1;j>=0;j--)
        {
            while(!st.empty()&&b[i][st.top()]+t<=b[i][j])
            {
                st.pop();
            }
            if(!st.empty())
            {
                bigger_right[i][j]=st.top();
            }
            else
            {
                bigger_right[i][j]=2*MAXN;
            }
            st.push(j);
        }
    }

    for(int j=0;j<m;j++)
    {
        stack<int>st;
        for(int i=0;i<n;i++)
        {
            while(!st.empty()&&b[st.top()][j]+t<=b[i][j])
            {
                st.pop();
            }
            if(!st.empty())
            {
                bigger_up[i][j]=st.top();
            }
            else
            {
                bigger_up[i][j]=-1;
            }
            st.push(i);
        }
    }

    for(int j=0;j<m;j++)
    {
        stack<int>st;
        for(int i=n-1;i>=0;i--)
        {
            while(!st.empty()&&b[st.top()][j]+t<=b[i][j])
            {
                st.pop();
            }
            if(!st.empty())
            {
                bigger_down[i][j]=st.top();
            }
            else
            {
                bigger_down[i][j]=2*MAXN;
            }
            st.push(i);
        }
    }
}
struct rect
{
    int x1,x2,y1,y2;
    int idx;
    bool operator<(rect const& other)const
    {
        if(x1!=other.x1)return x1<other.x1;
        if(x2!=other.x2)return x2<other.x2;
        if(y1!=other.y1)return y1<other.y1;
        return y2<other.y2;
    }
};
vector<rect>check_row1[MAXN],check_col1[MAXN];
vector<rect>check_row2[MAXN],check_col2[MAXN];
int cnt[MAXN*MAXN];
int st[MAXN][logN];
int pw2[MAXN];
long long count_rectangles(vector<vector<int> > a) {
    b=a;
    n=a.size();
    m=a[0].size();
	precomp(0);
	vector<rect>to_check;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(bigger_down[i][j]==-1)continue;
            if(bigger_left[i][j]==2*MAXN)continue;
            if(bigger_up[i][j]==2*MAXN)continue;
            if(bigger_right[i][j]==-1)continue;
            int x1=bigger_up[i][j]+1;
            int x2=bigger_down[i][j]-1;
            int y1=bigger_left[i][j]+1;
            int y2=bigger_right[i][j]-1;
            if(x1<=0)continue;
            if(x2>=n-1)continue;
            if(y1<=0)continue;
            if(y2>=m-1)continue;
            to_check.push_back({x1,x2,y1,y2,0});
        }
    }
    sort(to_check.begin(),to_check.end());
    for(int i=0;i<to_check.size();i++)
    {
        to_check[i].idx=i;
        if(i!=0)
        {
            if(to_check[i].x1==to_check[i-1].x1&&to_check[i].x2==to_check[i-1].x2&&to_check[i].y1==to_check[i-1].y1&&to_check[i].y2==to_check[i-1].y2)continue;
        }
        check_row1[to_check[i].x1-1].push_back(to_check[i]);
        check_row2[to_check[i].x2+1].push_back(to_check[i]);
        check_col1[to_check[i].y1-1].push_back(to_check[i]);
        check_col2[to_check[i].y2+1].push_back(to_check[i]);
    }
    int pw=1,c=0;
    for(int j=1;j<MAXN;j++)
    {
        if(pw*2<=j){pw*=2;c++;}
        pw2[j]=c;
    }
    precomp(1);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)st[j][0]=bigger_down[i][j];
        for(int step=1;step<logN;step++)
        {
            for(int j=0;j<m;j++)
            {
                st[j][step]=min(st[j][step-1],st[min(m-1,j+(1<<(step-1)))][step-1]);
            }
        }
        for(int j=0;j<check_row1[i].size();j++)
        {
            int x1=check_row1[i][j].x1;
            int x2=check_row1[i][j].x2;
            int y1=check_row1[i][j].y1;
            int y2=check_row1[i][j].y2;
            int d=(y2-y1+1);
            d=pw2[d];
            c=d;
            int mxdown=min(st[y1][c],st[y2-(1<<c)+1][c]);
            //cout<<check_row1[i][j].idx<<" "<<x1<<" "<<x2<<" "<<y1<<" "<<y2<<" "<<mxdown<<endl;
            if(mxdown>x2)cnt[check_row1[i][j].idx]++;
        }
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)st[j][0]=bigger_up[i][j];
        for(int step=1;step<logN;step++)
        {
            for(int j=0;j<m;j++)
            {
                st[j][step]=max(st[j][step-1],st[min(m-1,j+(1<<(step-1)))][step-1]);
            }
        }
        for(int j=0;j<check_row2[i].size();j++)
        {
            int x1=check_row2[i][j].x1;
            int x2=check_row2[i][j].x2;
            int y1=check_row2[i][j].y1;
            int y2=check_row2[i][j].y2;
            int d=(y2-y1+1);
            d=pw2[d];
            c=d;
            int mxup=max(st[y1][c],st[y2-(1<<c)+1][c]);
            if(mxup<x1)cnt[check_row2[i][j].idx]++;
        }
    }

    for(int j=0;j<m;j++)
    {
        for(int i=0;i<n;i++)st[i][0]=bigger_right[i][j];
        for(int step=1;step<logN;step++)
        {
            for(int i=0;i<n;i++)
            {
                st[i][step]=min(st[i][step-1],st[min(n-1,i+(1<<(step-1)))][step-1]);
            }
        }
        for(int i=0;i<check_col1[j].size();i++)
        {
            int x1=check_col1[j][i].x1;
            int x2=check_col1[j][i].x2;
            int y1=check_col1[j][i].y1;
            int y2=check_col1[j][i].y2;
            int d=(x2-x1+1);
            d=pw2[d];
            c=d;
            int mxright=min(st[x1][c],st[x2-(1<<c)+1][c]);
            if(mxright>y2)cnt[check_col1[j][i].idx]++;
        }
    }

    for(int j=0;j<m;j++)
    {
        for(int i=0;i<n;i++)st[i][0]=bigger_left[i][j];
        for(int step=1;step<logN;step++)
        {
            for(int i=0;i<n;i++)
            {
                st[i][step]=max(st[i][step-1],st[min(n-1,i+(1<<(step-1)))][step-1]);
            }
        }
        for(int i=0;i<check_col2[j].size();i++)
        {
            int x1=check_col2[j][i].x1;
            int x2=check_col2[j][i].x2;
            int y1=check_col2[j][i].y1;
            int y2=check_col2[j][i].y2;
            int d=(x2-x1+1);
            d=pw2[d];
            c=d;
            int mxleft=max(st[x1][c],st[x2-(1<<c)+1][c]);
            if(mxleft<y1)cnt[check_col2[j][i].idx]++;
        }
    }
    int ans=0;
    //cout<<"??\n";
    for(int i=0;i<to_check.size();i++)
    {
        //printf("%d %d %d %d\n",to_check[i].x1,to_check[i].x2,to_check[i].y1,to_check[i].y2);
        //cerr<<to_check[i].x1<<" "<<to_check[i].x2<<" "<<to_check[i].y1<<" "<<to_check[i].y2<<" "<<cnt[to_check[i].idx]<<" \n";
        if(cnt[i]==4)ans++;
    }
    return ans;
}

Compilation message (stderr)

rect.cpp:10:1: error: 'vector' does not name a type
   10 | vector<vector<int> >b;
      | ^~~~~~
rect.cpp: In function 'void precomp(int)':
rect.cpp:15:9: error: 'stack' was not declared in this scope; did you mean 'std::stack'?
   15 |         stack<int>st;
      |         ^~~~~
      |         std::stack
In file included from /usr/include/c++/10/stack:61,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:89,
                 from rect.cpp:2:
/usr/include/c++/10/bits/stl_stack.h:99:11: note: 'std::stack' declared here
   99 |     class stack
      |           ^~~~~
rect.cpp:15:15: error: expected primary-expression before 'int'
   15 |         stack<int>st;
      |               ^~~
rect.cpp:18:20: error: 'st' was not declared in this scope; did you mean 't'?
   18 |             while(!st.empty()&&b[i][st.top()]+t<=b[i][j])
      |                    ^~
      |                    t
rect.cpp:18:32: error: 'b' was not declared in this scope
   18 |             while(!st.empty()&&b[i][st.top()]+t<=b[i][j])
      |                                ^
rect.cpp:22:17: error: 'st' was not declared in this scope; did you mean 't'?
   22 |             if(!st.empty())
      |                 ^~
      |                 t
rect.cpp:30:13: error: 'st' was not declared in this scope; did you mean 't'?
   30 |             st.push(j);
      |             ^~
      |             t
rect.cpp:35:9: error: 'stack' was not declared in this scope; did you mean 'std::stack'?
   35 |         stack<int>st;
      |         ^~~~~
      |         std::stack
In file included from /usr/include/c++/10/stack:61,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:89,
                 from rect.cpp:2:
/usr/include/c++/10/bits/stl_stack.h:99:11: note: 'std::stack' declared here
   99 |     class stack
      |           ^~~~~
rect.cpp:35:15: error: expected primary-expression before 'int'
   35 |         stack<int>st;
      |               ^~~
rect.cpp:38:20: error: 'st' was not declared in this scope; did you mean 't'?
   38 |             while(!st.empty()&&b[i][st.top()]+t<=b[i][j])
      |                    ^~
      |                    t
rect.cpp:38:32: error: 'b' was not declared in this scope
   38 |             while(!st.empty()&&b[i][st.top()]+t<=b[i][j])
      |                                ^
rect.cpp:42:17: error: 'st' was not declared in this scope; did you mean 't'?
   42 |             if(!st.empty())
      |                 ^~
      |                 t
rect.cpp:50:13: error: 'st' was not declared in this scope; did you mean 't'?
   50 |             st.push(j);
      |             ^~
      |             t
rect.cpp:56:9: error: 'stack' was not declared in this scope; did you mean 'std::stack'?
   56 |         stack<int>st;
      |         ^~~~~
      |         std::stack
In file included from /usr/include/c++/10/stack:61,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:89,
                 from rect.cpp:2:
/usr/include/c++/10/bits/stl_stack.h:99:11: note: 'std::stack' declared here
   99 |     class stack
      |           ^~~~~
rect.cpp:56:15: error: expected primary-expression before 'int'
   56 |         stack<int>st;
      |               ^~~
rect.cpp:59:20: error: 'st' was not declared in this scope; did you mean 't'?
   59 |             while(!st.empty()&&b[st.top()][j]+t<=b[i][j])
      |                    ^~
      |                    t
rect.cpp:59:32: error: 'b' was not declared in this scope
   59 |             while(!st.empty()&&b[st.top()][j]+t<=b[i][j])
      |                                ^
rect.cpp:63:17: error: 'st' was not declared in this scope; did you mean 't'?
   63 |             if(!st.empty())
      |                 ^~
      |                 t
rect.cpp:71:13: error: 'st' was not declared in this scope; did you mean 't'?
   71 |             st.push(i);
      |             ^~
      |             t
rect.cpp:77:9: error: 'stack' was not declared in this scope; did you mean 'std::stack'?
   77 |         stack<int>st;
      |         ^~~~~
      |         std::stack
In file included from /usr/include/c++/10/stack:61,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:89,
                 from rect.cpp:2:
/usr/include/c++/10/bits/stl_stack.h:99:11: note: 'std::stack' declared here
   99 |     class stack
      |           ^~~~~
rect.cpp:77:15: error: expected primary-expression before 'int'
   77 |         stack<int>st;
      |               ^~~
rect.cpp:80:20: error: 'st' was not declared in this scope; did you mean 't'?
   80 |             while(!st.empty()&&b[st.top()][j]+t<=b[i][j])
      |                    ^~
      |                    t
rect.cpp:80:32: error: 'b' was not declared in this scope
   80 |             while(!st.empty()&&b[st.top()][j]+t<=b[i][j])
      |                                ^
rect.cpp:84:17: error: 'st' was not declared in this scope; did you mean 't'?
   84 |             if(!st.empty())
      |                 ^~
      |                 t
rect.cpp:92:13: error: 'st' was not declared in this scope; did you mean 't'?
   92 |             st.push(i);
      |             ^~
      |             t
rect.cpp: At global scope:
rect.cpp:108:1: error: 'vector' does not name a type
  108 | vector<rect>check_row1[MAXN],check_col1[MAXN];
      | ^~~~~~
rect.cpp:109:1: error: 'vector' does not name a type
  109 | vector<rect>check_row2[MAXN],check_col2[MAXN];
      | ^~~~~~
rect.cpp:113:28: error: 'long long int count_rectangles' redeclared as different kind of entity
  113 | long long count_rectangles(vector<vector<int> > a) {
      |                            ^~~~~~
In file included from rect.cpp:1:
rect.h:7:11: note: previous declaration 'long long int count_rectangles(std::vector<std::vector<int> >)'
    7 | long long count_rectangles(std::vector<std::vector<int> > a);
      |           ^~~~~~~~~~~~~~~~
rect.cpp:113:28: error: 'vector' was not declared in this scope; did you mean 'std::vector'?
  113 | long long count_rectangles(vector<vector<int> > a) {
      |                            ^~~~~~
      |                            std::vector
In file included from /usr/include/c++/10/vector:67,
                 from rect.h:5,
                 from rect.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:389:11: note: 'std::vector' declared here
  389 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
rect.cpp:113:35: error: 'vector' was not declared in this scope; did you mean 'std::vector'?
  113 | long long count_rectangles(vector<vector<int> > a) {
      |                                   ^~~~~~
      |                                   std::vector
In file included from /usr/include/c++/10/vector:67,
                 from rect.h:5,
                 from rect.cpp:1:
/usr/include/c++/10/bits/stl_vector.h:389:11: note: 'std::vector' declared here
  389 |     class vector : protected _Vector_base<_Tp, _Alloc>
      |           ^~~~~~
rect.cpp:113:42: error: expected primary-expression before 'int'
  113 | long long count_rectangles(vector<vector<int> > a) {
      |                                          ^~~