제출 #402072

#제출 시각아이디문제언어결과실행 시간메모리
402072b00n0rpRectangles (IOI19_rect)C++17
72 / 100
5051 ms690180 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 int MX = 2505; int n,m; int gl[MX][MX],gr[MX][MX],gu[MX][MX],gd[MX][MX]; vector<pii> row_seg[MX][MX],col_seg[MX][MX]; set<pair<pii,pii> > rects; void check_rectangle(int x1,int y1,int x2,int y2){ auto it = lower_bound(all(row_seg[y1-1][y2+1]),make_pair(x1,MX)); if(it == row_seg[y1-1][y2+1].begin()) return; it--; if((*it).S < x2) return; it = lower_bound(all(col_seg[x1-1][x2+1]),make_pair(y1,MX)); if(it == col_seg[x1-1][x2+1].begin()) return; it--; if((*it).S < y2) return; rects.insert({{x1,x2},{y1,y2}}); } ll count_rectangles(vector<vi> a) { n = a.size(); m = a[0].size(); if(n <= 2 or m <= 2) return 0; REP(i,n){ 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); } set<pii> s; REP(j,m){ if(gr[i][j] != -1 and gr[i][j] != j+1) s.insert({j,gr[i][j]}); if(gl[i][j] != -1 and gl[i][j] != j-1) s.insert({gl[i][j],j}); } for(auto x:s){ if(row_seg[x.F][x.S].empty() or i-1 > row_seg[x.F][x.S].back().S){ row_seg[x.F][x.S].pb({i,i}); } else row_seg[x.F][x.S].back().S++; } } REP(j,m){ 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); } set<pii> s; REP(i,n){ if(gd[i][j] != -1 and gd[i][j] != i+1) s.insert({i,gd[i][j]}); if(gu[i][j] != -1 and gu[i][j] != i-1) s.insert({gu[i][j],i}); } for(auto x:s){ if(col_seg[x.F][x.S].empty() or j-1 > col_seg[x.F][x.S].back().S){ col_seg[x.F][x.S].pb({j,j}); } else col_seg[x.F][x.S].back().S++; } } FOR(i,1,n-1){ FOR(j,1,m-1){ // topleft if(gr[i][j-1] != -1 and gd[i-1][j] != -1){ check_rectangle(i,j,gd[i-1][j]-1,gr[i][j-1]-1); } // topright if(gl[i][j+1] != -1 and gd[i-1][j] != -1){ check_rectangle(i,gl[i][j+1]+1,gd[i-1][j]-1,j); } // bottomleft if(gr[i][j-1] != -1 and gu[i+1][j] != -1){ check_rectangle(gu[i+1][j]+1,j,i,gr[i][j-1]-1); } // bottomright if(gl[i][j+1] != -1 and gu[i+1][j] != -1){ check_rectangle(gu[i+1][j]+1,gl[i][j+1]+1,i,j); } // clockwise if(gr[i][j-1] != -1 and gd[i-1][gr[i][j-1]-1] != -1){ check_rectangle(i,j,gd[i-1][gr[i][j-1]-1]-1,gr[i][j-1]-1); } // anti-clockwise if(gl[i][j+1] != -1 and gd[i-1][gl[i][j+1]+1] != -1){ check_rectangle(i,gl[i][j+1]+1,gd[i-1][gl[i][j+1]+1]-1,j); } } } return SZ(rects); }
#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...