제출 #145021

#제출 시각아이디문제언어결과실행 시간메모리
145021TadijaSebezRectangles (IOI19_rect)C++14
72 / 100
5047 ms598100 KiB
#include "rect.h" #include <bits/stdc++.h> using namespace std; #define ll long long #define mt make_tuple #define pb push_back const int N=2505; const int M=2*N; const int inf=1e9+7; int a[N][N],n,m,U1[N][N],D1[N][N],L1[N][N],R1[N][N],U2[N][N],D2[N][N],L2[N][N],R2[N][N],S[N],c; struct SegmentTreeMax { int mx[M]; SegmentTreeMax(){ for(int i=0;i<M;i++) mx[i]=-inf;} void Set(int i, int f){ for(i+=N;i;i>>=1) mx[i]=max(mx[i],f);} int Get(int l, int r) { int ans=-inf; for(l+=N,r+=N;l<=r;l>>=1,r>>=1) { if(l%2==1) ans=max(ans,mx[l++]); if(r%2==0) ans=max(ans,mx[r--]); } return ans; } } RMX[N],CMX[N]; struct SegmentTreeMin { int mn[M]; SegmentTreeMin(){ for(int i=0;i<M;i++) mn[i]=inf;} void Set(int i, int f){ for(i+=N;i;i>>=1) mn[i]=min(mn[i],f);} int Get(int l, int r) { int ans=inf; for(l+=N,r+=N;l<=r;l>>=1,r>>=1) { if(l%2==1) ans=min(ans,mn[l++]); if(r%2==0) ans=min(ans,mn[r--]); } return ans; } } RMN[N],CMN[N]; void Build() { for(int i=1;i<=n;i++) { S[c=0]=0; for(int j=1;j<=m;j++) { while(c && a[i][S[c]]<=a[i][j]) c--; L1[i][j]=S[c];S[++c]=j; } S[c=0]=m+1; for(int j=m;j>=1;j--) { while(c && a[i][S[c]]<=a[i][j]) c--; R1[i][j]=S[c];S[++c]=j; } S[c=0]=0; for(int j=1;j<=m;j++) { while(c && a[i][S[c]]<a[i][j]) c--; L2[i][j]=S[c];S[++c]=j; CMX[j].Set(i,L2[i][j]); } S[c=0]=m+1; for(int j=m;j>=1;j--) { while(c && a[i][S[c]]<a[i][j]) c--; R2[i][j]=S[c];S[++c]=j; CMN[j].Set(i,R2[i][j]); } } for(int j=1;j<=m;j++) { S[c=0]=0; for(int i=1;i<=n;i++) { while(c && a[S[c]][j]<=a[i][j]) c--; U1[i][j]=S[c];S[++c]=i; } S[c=0]=n+1; for(int i=n;i>=1;i--) { while(c && a[S[c]][j]<=a[i][j]) c--; D1[i][j]=S[c];S[++c]=i; } S[c=0]=0; for(int i=1;i<=n;i++) { while(c && a[S[c]][j]<a[i][j]) c--; U2[i][j]=S[c];S[++c]=i; RMX[i].Set(j,U2[i][j]); } S[c=0]=n+1; for(int i=n;i>=1;i--) { while(c && a[S[c]][j]<a[i][j]) c--; D2[i][j]=S[c];S[++c]=i; RMN[i].Set(j,D2[i][j]); } } } struct Rectangle { int x1,y1,x2,y2; Rectangle(){} Rectangle(int a, int b, int c, int d):x1(a),y1(b),x2(c),y2(d){} bool operator == (Rectangle b){ return mt(x1,y1,x2,y2)==mt(b.x1,b.y1,b.x2,b.y2);} bool operator < (Rectangle b){ return mt(x1,y1,x2,y2)<mt(b.x1,b.y1,b.x2,b.y2);} }; vector<Rectangle> work; void Try(int x, int y) { if(L1[x][y]!=0 && R1[x][y]!=m+1 && U1[x][y]!=0 && D1[x][y]!=n+1) { int x1=U1[x][y]; int y1=L1[x][y]; int x2=D1[x][y]; int y2=R1[x][y]; work.pb(Rectangle(x1,y1,x2,y2)); } } ll Solve() { Build(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) Try(i,j); sort(work.begin(),work.end()); work.resize(unique(work.begin(),work.end())-work.begin()); ll ans=0; for(Rectangle r:work) { int x1,y1,x2,y2; tie(x1,y1,x2,y2)=mt(r.x1,r.y1,r.x2,r.y2); int ok=0; if(RMN[x1].Get(y1+1,y2-1)>=x2) ok++; if(RMX[x2].Get(y1+1,y2-1)<=x1) ok++; if(CMN[y1].Get(x1+1,x2-1)>=y2) ok++; if(CMX[y2].Get(x1+1,x2-1)<=y1) ok++; if(ok==4) ans++; } return ans; } ll count_rectangles(vector<vector<int> > a) { n=a.size();m=a[0].size(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) ::a[i][j]=a[i-1][j-1]; return Solve(); }
#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...