Submission #145028

#TimeUsernameProblemLanguageResultExecution timeMemory
145028TadijaSebezRectangles (IOI19_rect)C++14
100 / 100
4056 ms809504 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,tmp[N]; struct SegmentTreeMax { int mx[M]; SegmentTreeMax(){ for(int i=0;i<M;i++) mx[i]=-inf;} void Build(int a[]) { for(int i=0;i<N;i++) mx[i+N]=a[i]; for(int i=N-1;i;i--) mx[i]=max(mx[i<<1],mx[i<<1|1]); } 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 Build(int a[]) { for(int i=0;i<N;i++) mn[i+N]=a[i]; for(int i=N-1;i;i--) mn[i]=min(mn[i<<1],mn[i<<1|1]); } 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; } 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; } } 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; } 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; } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) tmp[j]=U2[i][j]; RMX[i].Build(tmp); for(int j=1;j<=m;j++) tmp[j]=D2[i][j]; RMN[i].Build(tmp); } for(int j=1;j<=m;j++) { for(int i=1;i<=n;i++) tmp[i]=L2[i][j]; CMX[j].Build(tmp); for(int i=1;i<=n;i++) tmp[i]=R2[i][j]; CMN[j].Build(tmp); } } /*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; vector<pair<int,int>> work[N][N]; 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)); work[x1][y1].pb({x2,y2}); } } bool was[N][N]; 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(int x1=1;x1<=n;x1++) { for(int y1=1;y1<=m;y1++) { for(auto p:work[x1][y1]) { int x2,y2; tie(x2,y2)=p; if(was[x2][y2]) continue; was[x2][y2]=1; 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++; } for(auto p:work[x1][y1]) { int x2,y2; tie(x2,y2)=p; was[x2][y2]=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...