Submission #771351

#TimeUsernameProblemLanguageResultExecution timeMemory
771351TimDeeRectangles (IOI19_rect)C++17
37 / 100
5053 ms143948 KiB
#include "rect.h" #include <bits/stdc++.h> using namespace std; #define forn(i,n) for(int i=0; i<n; ++i) #define pi pair<int,int> #define f first #define s second #define ll long long #define pb push_back #define all(x) x.begin(),x.end() struct BIT { int n,m; vector<vector<int>> t; BIT(int n, int m) { this->n=n, this->m=m; t.assign(n,vector<int>(m,0)); } void addi(int i, int j, int x) { for(;j<m;j=j|(j+1)) t[i][j]+=x; } void add(int i, int j, int x) { for(;i<n;i=i|(i+1)) addi(i,j,x); } int sumi(int i, int j) { int q=0; for(;j>=0;j=(j&(j+1))-1) q+=t[i][j]; return q; } int sum(int a, int b) { int q=0; for(;a>=0;a=(a&(a+1))-1) q+=sumi(a,b); return q; } int sum(int a, int b, int c, int d) { return sum(c,d)-sum(a-1,d)-sum(c,b-1)+sum(a-1,b-1); } }; const int N=700; struct dsu { vector<int> p,sz,l,r; dsu(int n) { forn(i,n) p.pb(i), sz.pb(1); l=r=p; } int get(int u) { return p[u]==u?u:get(p[u]); } void uni(int u, int v) { u=get(u),v=get(v); if (u==v) return; if (sz[u]<sz[v]) swap(u,v); p[v]=u; sz[u]+=sz[v]; l[u]=min(l[u],l[v]); r[u]=max(r[u],r[v]); } }; vector<dsu> dsu1(N,dsu(N)), dsu2(N,dsu(N)); int ok[N][N]; int a[N][N]; BIT ft(N,N); bitset<N> dp1[N][N], dp2[N][N]; int ite=0; int check(int l, int u, int r, int d) { int ok=1; for (int i=l; i<=r; ++i) { //int x=query1(i,u,d); //ok&=a[i][u-1]>x && x<a[i][d+1]; ok&=dp1[i][u][d]; } for (int i=u; i<=d; ++i) { //int x=query2(i,l,r); //ok&=a[l-1][i]>x && x<a[r+1][i]; ok&=dp2[i][l][r]; } return ok; } ll count_rectangles(vector<vector<int>> b) { int n=b.size(), m=b[0].size(); if (min(n,m)<3) return 0; if (n==3) { ll ans=0; for (int i=0; i<m; ++i) { int mx=0; for (int j=i+1; j<m-1; ++j) { if (b[0][j]<=b[1][j] || b[1][j]>=b[2][j]) break; mx=max(mx,b[1][j]); ans += b[1][i]>mx && mx<b[1][j+1]; } } return ans; } if (max(n,m)>N) exit(0); forn(i,n) forn(j,m) a[i][j]=b[i][j]; forn(i,n) { for (int l=0; l<m; ++l) { int mx=0; for (int r=l+1; r+1<m; ++r) { mx=max(mx,a[i][r]); dp1[i][l+1][r]=a[i][l]>mx && mx<a[i][r+1]; } } } forn(i,m) { for (int l=0; l<n; ++l) { int mx=0; for (int r=l+1; r+1<n; ++r) { mx=max(mx,a[r][i]); dp2[i][l+1][r]=a[l][i]>mx && mx<a[r+1][i]; } } } vector<pi> v; forn(i,n) forn(j,m) v.pb({a[i][j],i*m+j}); sort(all(v)); ll ans=0; int N=v.size(); map<ll,ll> was; for(int i=0; i<N; ) { vector<int> z; int x=v[i].f; while (i<N && v[i].f==x) { z.pb(v[i].s); ++i; } for(auto&it:z) { int x=it/m, y=it%m; if (x) if (ok[x-1][y]) dsu1[y].uni(x-1,x); if (x<n-1) if (ok[x+1][y]) dsu1[y].uni(x+1,x); if (y) if (ok[x][y-1]) dsu2[x].uni(y-1,y); if (y<m-1) if (ok[x][y+1]) dsu2[x].uni(y+1,y); ok[x][y]=1; ft.add(x,y,1); } for(auto&it:z) { int x=it/m, y=it%m; int u=dsu1[y].get(x); int a=dsu1[y].l[u], c=dsu1[y].r[u]; int v=dsu2[x].get(y); int b=dsu2[x].l[v], d=dsu2[x].r[v]; if (a==0 || b==0 || c==n-1 || d==m-1) continue; int s=ft.sum(a,b,c,d); if (s < (c-a+1)*(d-b+1)) continue; if (was[1ll*a*m+b+1ll*n*m*(1ll*c*m+d)]) continue; was[1ll*a*m+b+1ll*n*m*(1ll*c*m+d)]=1; //if (ite<=3e8) { int q = check(a,b,c,d); ans+=q; // ite+=c-a+d-b+2; //} else exit(0); } } 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...