Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 1501;
- vector<pair<int,int>> v[N*N];
- vector<int> t[N*N];
- int knd[N*N];
- int a[N][N];
- int pos[N][N];
- int arj[N][N];
- int get(int x,int tl,int tr,int v,int l,int r){
- if(tl<=l&&r<=tr){
- return t[x][v];
- }
- else{
- int m=(l+r)/2;
- int cur=0;
- if(tl<=m)cur=max(cur,get(x,tl,tr,v+v,l,m));
- if(tr>m)cur=max(cur,get(x,tl,tr,v+v+1,m+1,r));
- return cur;
- }
- }
- void upd(int x,int pos,int l,int r,int v,int val){
- if(l==r){
- t[x][v]=max(t[x][v],val);
- }
- else{
- int m=(l+r)/2;
- if(pos<=m)upd(x,pos,l,m,v+v,val);
- else upd(x,pos,m+1,r,v+v+1,val);
- t[x][v]=max(t[x][v+v],t[x][v+v+1]);
- }
- }
- int ans[N*N];
- int hw[N*N];
- int main(){
- // freopen("input.txt","r",stdin);
- int n;
- cin>>n;
- for(int i=1;i<=n;++i)
- for(int j=1;j<=n;++j){
- scanf("%d",&a[i][j]);
- v[a[i][j]].push_back({j,i});
- ++hw[a[i][j]];
- }
- for(int i=1;i<=n*n;++i){
- if(hw[i]==1){
- ++ans[1];
- continue;
- }
- sort(v[i].begin(),v[i].end());
- if(hw[i]==2){
- if(v[i][1].second>=v[i][0].second&&v[i][1].first>=v[i][0].first){
- ans[2]+=2;
- }
- else {
- ans[1]+=2;
- }
- continue;
- }
- int cn=0;
- for(int j=0;j<v[i].size();++j){
- if(j==0||v[i][j].first!=v[i][j-1].first)
- pos[v[i][j].second][v[i][j].first]=cn++;
- else{
- pos[v[i][j].second][v[i][j].first]=cn-1;
- }
- }
- knd[i]=cn-1;
- t[i].resize((cn+1)*4,0);
- }
- for(int i=1;i<=n;++i){
- for(int j=1;j<=n;++j){
- if(hw[a[i][j]]<3)continue;
- int p=pos[i][j];
- int gn=a[i][j];
- int g=get(gn,0,p,1,0,knd[gn]);
- upd(gn,p,0,knd[gn],1,g+1);
- arj[i][j]=g+1;
- }
- }
- for(int i=1;i<=n*n;++i){
- if(hw[i]<3)continue;
- t[i].clear();
- t[i].resize((knd[i]+1)*4,0);
- }
- for(int i=n;i>=1;--i){
- for(int j=n;j>=1;--j){
- if(hw[a[i][j]]<3)continue;
- int p=pos[i][j];
- int gn=a[i][j];
- int g=get(gn,p,knd[gn],1,0,knd[gn]);
- arj[i][j]+=g;
- upd(gn,p,0,knd[gn],1,g+1);
- ++ans[arj[i][j]];
- }
- }
- for(int i=1;i<n+n;++i){
- printf("%d ",ans[i]);
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement