Submission #402143

#TimeUsernameProblemLanguageResultExecution timeMemory
402143b00n0rpRectangles (IOI19_rect)C++17
59 / 100
5118 ms907772 KiB
#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define vi vector<int>
#define pb push_back
#define REP(i,n) for(int i = 0; i < n; i++)
#define FOR(i,a,b) for(int i = a; i < b; i++)
#define FORD(i,a,b) for(int i = a; i >= b; i --)
#define pii pair<int,int>
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define SZ(v) (int)v.size()
 
const ll MX = 2505;
 
ll ans = 0;
int n,m; 
int gl[MX][MX],gr[MX][MX],gu[MX][MX],gd[MX][MX];
unordered_map<ll,int> row_map,col_map;
 
void check_rectangle(int x1,int y1,int x2,int y2){
    
    ll val1 = (y1-1)*MX*MX+(y2+1)*MX+x1;
    if(row_map.find(val1) == row_map.end()) return;
    if(row_map[val1] < x2) return;

    ll val2 = (x1-1)*MX*MX+(x2+1)*MX+y1;
    if(col_map.find(val2) == col_map.end()) return;
    if(col_map[val2] < y2) return;

    // cout << x1 << " " << y1 << " " << x2 << " " << y2 << "\n";

    ans++;
}
 
ll count_rectangles(vector<vi> a) {
	row_map.max_load_factor(0.25); 
	col_map.max_load_factor(0.25); 
	row_map.reserve(1048576); 
	col_map.reserve(1048576); 
    n = a.size();
    m = a[0].size();
    if(n <= 2 or m <= 2) return 0;
    FORD(i,n-1,0){
        stack<int> st;
        REP(j,m){
            gl[i][j] = gr[i][j] = gu[i][j] = gd[i][j] = -1;
        }
        REP(j,m){
            while(st.size() and a[i][st.top()] <= a[i][j]){
                gr[i][st.top()] = j;
                st.pop();
            }
            st.push(j);
        }
        while(!st.empty()) st.pop();
        FORD(j,m-1,0){
            while(st.size() and a[i][st.top()] <= a[i][j]){
                gl[i][st.top()] = j;
                st.pop();
            }
            st.push(j);
        }
        REP(j,m){
            if(gr[i][j] != -1 and gr[i][j] != j+1){
                ll val = j*MX*MX+gr[i][j]*MX+i;
                if(row_map.find(val+1) != row_map.end()){
                	row_map[val] = row_map[val+1];
                }
                else row_map[val] = i;
            }
            if(gl[i][j] != -1 and gl[i][j] != j-1 and gr[i][gl[i][j]] != j){
                ll val = gl[i][j]*MX*MX+j*MX+i;
                if(row_map.find(val+1) != row_map.end()){
                	row_map[val] = row_map[val+1];
                }
                else row_map[val] = i;
            }
        }
    }
    FORD(j,m-1,0){
        stack<int> st;
        REP(i,n){
            while(st.size() and a[st.top()][j] <= a[i][j]){
                gd[st.top()][j] = i;
                st.pop();
            }
            st.push(i);
        }
        while(!st.empty()) st.pop();
        FORD(i,n-1,0){
            while(st.size() and a[st.top()][j] <= a[i][j]){
                gu[st.top()][j] = i;
                st.pop();
            }
            st.push(i);
        }
        REP(i,n){
            if(gd[i][j] != -1 and gd[i][j] != i+1){
                ll val = i*MX*MX+gd[i][j]*MX+j;
                if(col_map.find(val+1) != col_map.end()){
                	col_map[val] = col_map[val+1];
                }
                else col_map[val] = j;
            }
            if(gu[i][j] != -1 and gu[i][j] != i-1 and gd[gu[i][j]][j] != i){
                ll val = gu[i][j]*MX*MX+i*MX+j;
                if(col_map.find(val+1) != col_map.end()){
                	col_map[val] = col_map[val+1];
                }
                else col_map[val] = j;
            }
        }
    }
    vector<ll> bruh;
    FOR(i,1,n-1){
        FOR(j,1,m-1){
            // topleft
            if(gr[i][j-1] != -1 and gd[i-1][j] != -1){
            	bruh.pb((i)*MX*MX*MX+(j)*MX*MX+(gd[i-1][j]-1)*MX+gr[i][j-1]-1);
            }
            // topright
            if(gl[i][j+1] != -1 and gd[i-1][j] != -1){
                bruh.pb((i)*MX*MX*MX+(gl[i][j+1]+1)*MX*MX+(gd[i-1][j]-1)*MX+j);
            }
            // bottomleft
            if(gr[i][j-1] != -1 and gu[i+1][j] != -1){
            	bruh.pb((gu[i+1][j]+1)*MX*MX*MX+(j)*MX*MX+(i)*MX+gr[i][j-1]-1);
            }
            // bottomright
            if(gl[i][j+1] != -1 and gu[i+1][j] != -1){
            	bruh.pb((gu[i+1][j]+1)*MX*MX*MX+(gl[i][j+1]+1)*MX*MX+(i)*MX+j);
            }
            // clockwise
            if(gr[i][j-1] != -1 and gd[i-1][gr[i][j-1]-1] != -1){
            	bruh.pb((i)*MX*MX*MX+(j)*MX*MX+(gd[i-1][gr[i][j-1]-1]-1)*MX+gr[i][j-1]-1);
            }
            // anti-clockwise
            if(gl[i][j+1] != -1 and gd[i-1][gl[i][j+1]+1] != -1){
            	bruh.pb((i)*MX*MX*MX+(gl[i][j+1]+1)*MX*MX+(gd[i-1][gl[i][j+1]+1]-1)*MX+j);
            }
        }
    }
    sort(all(bruh));
    REP(i,SZ(bruh)){
    	if(i == 0 or bruh[i] != bruh[i-1]){
    		// if((n <= 700 and m <= 700) or n <= 3)
    		check_rectangle(bruh[i]/(MX*MX*MX),(bruh[i]/(MX*MX))%MX,(bruh[i]/MX)%MX,bruh[i]%MX);
    	}
    }
    return ans;
}
#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...