Submission #726291

#TimeUsernameProblemLanguageResultExecution timeMemory
726291alvingogoRectangles (IOI19_rect)C++14
72 / 100
5099 ms493880 KiB
#include "rect.h" #include <bits/stdc++.h> #pragma GCC optimize("Ofast,unroll-loops,inline") #pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #define fs first #define sc second using namespace std; typedef pair<int,int> pii; namespace{ struct BIT{ int n; vector<int> bt; BIT(int x){ n=x; bt.resize(x+1); } BIT(){ n=0; } void update(int x,int y){ for(;x<=n;x+=(x&-x)){ bt[x]+=y; } } int query(int x){ int ans=0; for(;x>0;x-=(x&-x)){ ans+=bt[x]; } return ans; } }; } long long count_rectangles(vector<vector<int> > v) { int n=v.size(); int m=v[0].size(); vector<vector<int> > tmp(m,vector<int>(n)); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ tmp[j][i]=v[i][j]; } } if(n<=2 || m<=2){ return 0; } // swap(n,m); // v=tmp; vector<vector<pii> > az(n),bz(m); vector<pii> t1,t2; for(int i=1;i<n-1;i++){ vector<pair<int,int> > gg; vector<int> vis(m),up(m),down(m); for(int j=0;j<m;j++){ gg.push_back({v[i][j],j}); } sort(gg.begin(),gg.end()); for(auto h:gg){ int x=h.sc-1,y=h.sc+1; int l=h.sc,r=h.sc; if(x<0 || !vis[x]){ } else{ l=up[x]; } if(y>=m || !vis[y]){ } else{ r=down[y]; } vis[h.sc]=1; up[h.sc]=l; down[h.sc]=r; down[l]=r; up[r]=l; if(l!=0 && r!=m-1 && (v[i][r+1]>h.fs && v[i][l-1]>h.fs)){ az[i].push_back({l,r}); t1.push_back({l,r}); } } } for(int i=1;i<m-1;i++){ vector<pair<int,int> > gg; vector<int> vis(n),up(n),down(n); for(int j=0;j<n;j++){ gg.push_back({v[j][i],j}); } sort(gg.begin(),gg.end()); for(auto h:gg){ int x=h.sc-1,y=h.sc+1; int l=h.sc,r=h.sc; if(x<0 || !vis[x]){ } else{ l=up[x]; } if(y>=n || !vis[y]){ } else{ r=down[y]; } vis[h.sc]=1; up[h.sc]=l; down[h.sc]=r; down[l]=r; up[r]=l; if(l!=0 && r!=n-1 && (v[r+1][i]>h.fs && v[l-1][i]>h.fs)){ bz[i].push_back({l,r}); t2.push_back({l,r}); } } } sort(t1.begin(),t1.end()); t1.erase(unique(t1.begin(),t1.end()),t1.end()); sort(t2.begin(),t2.end()); t2.erase(unique(t2.begin(),t2.end()),t2.end()); auto lis1=[&](pii a){ return lower_bound(t1.begin(),t1.end(),a)-t1.begin(); }; auto lis2=[&](pii a){ return lower_bound(t2.begin(),t2.end(),a)-t2.begin(); }; vector<pair<int,pii> > ev[2505]; vector<vector<int> > ax(n),bx(m); for(int i=1;i<n-1;i++){ for(auto h:az[i]){ ax[i].push_back(lis1(h)); } } for(int i=1;i<m-1;i++){ for(auto h:bz[i]){ bx[i].push_back(lis2(h)); } } vector<int> y1(t1.size(),-1),y2(t2.size(),-1),p1(t1.size()),p2(t2.size()); for(int i=1;i<n-1;i++){ for(auto h:ax[i]){ p1[h]=1; if(y1[h]==-1){ y1[h]=i; } } if(i>1){ for(auto h:ax[i-1]){ if(p1[h]==0){ ev[t1[h].fs].push_back({-t1[h].sc,{y1[h],i-1}}); y1[h]=-1; } } } for(auto h:ax[i]){ p1[h]=0; } } for(int i=0;i<t1.size();i++){ if(y1[i]!=-1){ ev[t1[i].fs].push_back({-t1[i].sc,{y1[i],n-2}}); } } for(int i=1;i<m-1;i++){ for(auto h:bx[i]){ p2[h]=1; if(y2[h]==-1){ y2[h]=i; } } if(i>1){ for(auto h:bx[i-1]){ if(p2[h]==0){ for(int j=y2[h];j<=i-1;j++){ ev[j].push_back({-(i-1),{-t2[h].fs,-t2[h].sc}}); } y2[h]=-1; } } } for(auto h:bx[i]){ p2[h]=0; } } for(int i=0;i<y2.size();i++){ if(y2[i]==-1){ continue; } for(int j=y2[i];j<=m-2;j++){ ev[j].push_back({-(m-2),{-t2[i].fs,-t2[i].sc}}); } } // BIT vb[2502]; // for(int i=0;i<n;i++){ // vb[i]=BIT(n); // } vector<BIT> vb(n,BIT(n)); long long ans=0; // for(int i=1;i<m-1;i++){ // sort(ev[i].begin(),ev[i].end()); // } for(int i=1;i<m-1;i++){ sort(ev[i].begin(),ev[i].end()); for(auto h:ev[i]){ if(h.sc.sc>0){ for(int j=h.sc.fs;j<=h.sc.sc;j++){ ans+=vb[j].query(h.sc.sc); } } else{ vb[-h.sc.fs].update(-h.sc.sc,1); } } for(auto h:ev[i]){ if(h.sc.sc<0){ vb[-h.sc.fs].update(-h.sc.sc,-1); } } } return ans; }

Compilation message (stderr)

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:162:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  162 |  for(int i=0;i<t1.size();i++){
      |              ~^~~~~~~~~~
rect.cpp:188:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  188 |  for(int i=0;i<y2.size();i++){
      |              ~^~~~~~~~~~
#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...