Submission #867597

#TimeUsernameProblemLanguageResultExecution timeMemory
867597JakobZorzRectangles (IOI19_rect)C++17
72 / 100
5070 ms218508 KiB
#include"rect.h"
#include<algorithm>
#include<unordered_set>
#include<set>
#include<iostream>
using namespace std;
typedef long long ll;

int gap_right[2500][2500];
int gap_down[2500][2500];
int gap_left[2500][2500];
int gap_up[2500][2500];
int arr[2500][2500];
int w,h;

void process_column(int x){
    vector<pair<int,int>>vals;
    for(int y=0;y<h;y++){
        while(!vals.empty()&&vals.back().first<arr[x][y])
            vals.pop_back();
        gap_up[x][y]=-1;
        if(!vals.empty())
            gap_up[x][y]=vals.back().second;
        vals.emplace_back(arr[x][y],y);
    }
    
    vals.clear();
    for(int y=h-1;y>=0;y--){
        while(!vals.empty()&&vals.back().first<arr[x][y])
            vals.pop_back();
        gap_down[x][y]=2500;
        if(!vals.empty())
            gap_down[x][y]=vals.back().second;
        vals.emplace_back(arr[x][y],y);
    }
}

void process_row(int y){
    vector<pair<int,int>>vals;
    for(int x=0;x<w;x++){
        while(!vals.empty()&&vals.back().first<arr[x][y])
            vals.pop_back();
        gap_left[x][y]=-1;
        if(!vals.empty())
            gap_left[x][y]=vals.back().second;
        vals.emplace_back(arr[x][y],x);
    }
    
    vals.clear();
    for(int x=w;x>=0;x--){
        while(!vals.empty()&&vals.back().first<arr[x][y])
            vals.pop_back();
        gap_right[x][y]=2500;
        if(!vals.empty())
            gap_right[x][y]=vals.back().second;
        vals.emplace_back(arr[x][y],x);
    }
}

int check1(int x1,int x2,int y){
    if(x1==-1||x2==2500||x1==x2-1)
        return 0;
    
    int y1=y-1;
    
    int y2=2500;
    for(int x=x1+1;x<x2;x++){
        y2=min(y2,gap_down[x][y1]);
    }
    
    if(y2>=h)
        return 0;
    
    if(y1==y2-1)
        return 0;
    
    for(int x=x1+1;x<x2;x++)
        if((gap_down[x][y1]!=y2&&gap_up[x][y2]!=y1)||gap_down[x][y1]<y2||gap_up[x][y2]>y1)
            return 0;
    
    for(int y=y1+1;y<y2;y++)
        if((gap_right[x1][y]!=x2&&gap_left[x2][y]!=x1)||gap_right[x1][y]<x2||gap_left[x2][y]>x1)
            return 0;
    
    //cout<<"("<<x1<<" "<<y1<<") ("<<x2<<" "<<y2<<")\n";
    
    return 1;
}

int check2(int x1,int x2,int y){
    if(x1==-1||x2==2500||x1==x2-1)
        return 0;
    
    int y2=y+1;
    
    int y1=-1;
    for(int x=x1+1;x<x2;x++){
        y1=max(y1,gap_up[x][y2]);
    }
    
    if(y1==-1)
        return 0;
    
    if(y1==y2-1)
        return 0;
    
    for(int x=x1+1;x<x2;x++)
        if((gap_down[x][y1]!=y2&&gap_up[x][y2]!=y1)||gap_down[x][y1]<y2||gap_up[x][y2]>y1||gap_down[x][y1]==y2)
            return 0;
    
    for(int y=y1+1;y<y2;y++)
        if((gap_right[x1][y]!=x2&&gap_left[x2][y]!=x1)||gap_right[x1][y]<x2||gap_left[x2][y]>x1)
            return 0;
    
    //cout<<"("<<x1<<" "<<y1<<") ("<<x2<<" "<<y2<<")\n";
    
    return 1;
}

int check(int x1,int x2,int y){
    return check1(x1,x2,y)+check2(x1,x2,y);
}

ll count_rectangles(vector<vector<int>>a_){
    h=(int)a_.size();
    w=(int)a_[0].size();
    for(int x=0;x<w;x++){
        for(int y=0;y<h;y++){
            arr[x][y]=a_[y][x];
        }
    }
    
    for(int x=0;x<w;x++)
        process_column(x);
    
    for(int y=0;y<h;y++)
        process_row(y);
    
    /*for(int y=0;y<h;y++){
        for(int x=0;x<w;x++)
            cout<<gap_left[x][y]<<" ";
        cout<<"\n";
    }*/
    
    ll res=0;
    for(int x=0;x<w;x++)
        for(int y=1;y<h-1;y++){
            res+=check(x,gap_right[x][y],y);
            //cout<<gap_left[x][y+1]<<" "<<x<<" "<<y<<"\n";
            if(arr[gap_left[x][y]][y]!=arr[x][y])
                res+=check(gap_left[x][y],x,y);
        }
    
	return res;
}

Compilation message (stderr)

rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:146:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
  146 |     for(int x=0;x<w;x++)
      |     ^~~
rect.cpp:154:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
  154 |  return res;
      |  ^~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...