#include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=1e5; const int P=1<<18; struct T { int mn; array<int,11> t; }; int k; int tab[N+10][2]; bool tch[3][2]; vector<tuple<int,int,int>> upd_val[2*N+10]; int cnt[N+10]; int col_num[2*N+10]; int pot; T tree[2*P+10]; int lazy[2*P+10]; long long ans[11]; int check_change() { if(!tch[1][1] && !tch[0][0] && !tch[2][0]) return 1; if(!tch[1][1] && tch[0][0] && tch[2][0]) return -1; if(!tch[1][1] && (tch[0][0] || tch[2][0])) return 0; if((tch[0][0] && !tch[0][1]) && (tch[2][0] && !tch[2][1])) return -2; if((tch[0][0] && !tch[0][1]) || (tch[2][0] && !tch[2][1])) return -1; return 0; } T merge(T a,T b) { if(b.mn<a.mn) swap(a,b); for(int i=0;i+b.mn-a.mn<=k;i++) a.t[i+b.mn-a.mn]+=b.t[i]; return a; } void push_down(int i) { tree[2*i].mn+=lazy[i]; tree[2*i+1].mn+=lazy[i]; lazy[2*i]+=lazy[i]; lazy[2*i+1]+=lazy[i]; lazy[i]=0; return; } void add(int i,int l,int r,int ls,int rs,int c) { if(r<ls || rs<l) return; if(ls<=l && r<=rs) { tree[i].mn+=c; lazy[i]+=c; return; } push_down(i); int mid=(l+r)/2; add(2*i,l,mid,ls,rs,c); add(2*i+1,mid+1,r,ls,rs,c); tree[i]=merge(tree[2*i],tree[2*i+1]); return; } int read(int x) { for(int i=1,l=1,r=pot;l<r;) { push_down(i); int mid=(l+r)/2; if(x<=mid) { r=mid; i=2*i; } else { l=mid+1; i=2*i+1; } } return tree[pot+x-1].mn; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n>>k; for(int j:{0,1}) { for(int i=0;i<n;i++) { cin>>tab[i][j]; col_num[tab[i][j]]=i; } } for(int i=0;i<n;i++) { for(int j:{0,1}) { //cerr<<tab[i][j]<<":::\n"; vector<tuple<int,int,bool>> tmp; for(int i2=-1;i2<=1;i2++) { for(int j2:{0,1}) { tmp.emplace_back(tab[(i+i2+n)%n][j^j2],i2,j2); //cerr<<tab[(i+i2+n)%n][j^j2]<<" "<<i2<<" "<<j2<<"\n"; } } sort(tmp.begin(),tmp.end()); reverse(tmp.begin(),tmp.end()); int lst=0,g=2*n; tch[0][0]=tch[0][1]=tch[1][0]=tch[1][1]=tch[2][0]=tch[2][1]=false; for(auto [c,i2,j2]:tmp) { if(!tch[1][0] && (i2!=0 || j2!=0)) continue; //cerr<<c<<"-\n"; tch[i2+1][j2]=true; upd_val[tab[i][j]].emplace_back(c+1,g,lst); lst=check_change(); //cerr<<tch[0][1]<<" "<<tch[1][1]<<" "<<tch[2][1]<<"\n"; //cerr<<tch[0][0]<<" "<<tch[1][0]<<" "<<tch[2][0]<<"\n"; //cerr<<lst<<"\n"; g=c; } upd_val[tab[i][j]].emplace_back(1,g,lst); } } for(pot=1;pot<2*n;pot*=2); for(int i=pot;i<pot+2*n;i++) { tree[i].mn=0; for(int j=0;j<=k;j++) tree[i].t[j]=0; tree[i].t[0]=1; } for(int i=pot-1;i>0;i--) { tree[i].mn=0; for(int j=0;j<=k;j++) tree[i].t[j]=0; tree[i].t[0]=tree[2*i].t[0]+tree[2*i+1].t[0]; } int bg=1,free_col=n; for(int i=1;i<=2*n;i++) { //cerr<<i<<":\n"; for(auto [l,r,c]:upd_val[i]) { add(1,1,pot,l,r,c); //cerr<<l<<" "<<r<<" "<<c<<"\n"; } if(cnt[col_num[i]]==0) free_col--; cnt[col_num[i]]++; while(free_col==0 && read(bg)==0) { add(1,1,pot,bg,bg,k+10); ans[1]+=2*n-i+1; cnt[col_num[bg]]--; if(cnt[col_num[bg]]==0) free_col++; bg++; //cerr<<"bg->"<<bg<<"\n"; } //cerr<<"free_col="<<free_col<<" read="<<read(bg)<<"\n"; //cerr<<"mn="<<tree[1].mn<<"\n"; for(int j=0;tree[1].mn+j<=k;j++) { ans[tree[1].mn+j]+=tree[1].t[j]; //cerr<<tree[1].t[j]<<"\n"; } } for(int i=1;i<=k;i++) cout<<ans[i]<<" "; cout<<"\n"; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | #include<bits/stdc++.h> #define fi first #define se second using namespace std; const int N=1e5; const int P=1<<18; struct T { int mn; array<int,11> t; }; int k; int tab[N+10][2]; bool tch[3][2]; vector<tuple<int,int,int>> upd_val[2*N+10]; int cnt[N+10]; int col_num[2*N+10]; int pot; T tree[2*P+10]; int lazy[2*P+10]; long long ans[11]; int check_change() { if(!tch[1][1] && !tch[0][0] && !tch[2][0]) return 1; if(!tch[1][1] && tch[0][0] && tch[2][0]) return -1; if(!tch[1][1] && (tch[0][0] || tch[2][0])) return 0; if((tch[0][0] && !tch[0][1]) && (tch[2][0] && !tch[2][1])) return -2; if((tch[0][0] && !tch[0][1]) || (tch[2][0] && !tch[2][1])) return -1; return 0; } T merge(T a,T b) { if(b.mn<a.mn) swap(a,b); for(int i=0;i+b.mn-a.mn<=k;i++) a.t[i+b.mn-a.mn]+=b.t[i]; return a; } void push_down(int i) { tree[2*i].mn+=lazy[i]; tree[2*i+1].mn+=lazy[i]; lazy[2*i]+=lazy[i]; lazy[2*i+1]+=lazy[i]; lazy[i]=0; return; } void add(int i,int l,int r,int ls,int rs,int c) { if(r<ls || rs<l) return; if(ls<=l && r<=rs) { tree[i].mn+=c; lazy[i]+=c; return; } push_down(i); int mid=(l+r)/2; add(2*i,l,mid,ls,rs,c); add(2*i+1,mid+1,r,ls,rs,c); tree[i]=merge(tree[2*i],tree[2*i+1]); return; } int read(int x) { for(int i=1,l=1,r=pot;l<r;) { push_down(i); int mid=(l+r)/2; if(x<=mid) { r=mid; i=2*i; } else { l=mid+1; i=2*i+1; } } return tree[pot+x-1].mn; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin>>n>>k; for(int j:{0,1}) { for(int i=0;i<n;i++) { cin>>tab[i][j]; col_num[tab[i][j]]=i; } } for(int i=0;i<n;i++) { for(int j:{0,1}) { //cerr<<tab[i][j]<<":::\n"; vector<tuple<int,int,bool>> tmp; for(int i2=-1;i2<=1;i2++) { for(int j2:{0,1}) { tmp.emplace_back(tab[(i+i2+n)%n][j^j2],i2,j2); //cerr<<tab[(i+i2+n)%n][j^j2]<<" "<<i2<<" "<<j2<<"\n"; } } sort(tmp.begin(),tmp.end()); reverse(tmp.begin(),tmp.end()); int lst=0,g=2*n; tch[0][0]=tch[0][1]=tch[1][0]=tch[1][1]=tch[2][0]=tch[2][1]=false; for(auto [c,i2,j2]:tmp) { if(!tch[1][0] && (i2!=0 || j2!=0)) continue; //cerr<<c<<"-\n"; tch[i2+1][j2]=true; upd_val[tab[i][j]].emplace_back(c+1,g,lst); lst=check_change(); //cerr<<tch[0][1]<<" "<<tch[1][1]<<" "<<tch[2][1]<<"\n"; //cerr<<tch[0][0]<<" "<<tch[1][0]<<" "<<tch[2][0]<<"\n"; //cerr<<lst<<"\n"; g=c; } upd_val[tab[i][j]].emplace_back(1,g,lst); } } for(pot=1;pot<2*n;pot*=2); for(int i=pot;i<pot+2*n;i++) { tree[i].mn=0; for(int j=0;j<=k;j++) tree[i].t[j]=0; tree[i].t[0]=1; } for(int i=pot-1;i>0;i--) { tree[i].mn=0; for(int j=0;j<=k;j++) tree[i].t[j]=0; tree[i].t[0]=tree[2*i].t[0]+tree[2*i+1].t[0]; } int bg=1,free_col=n; for(int i=1;i<=2*n;i++) { //cerr<<i<<":\n"; for(auto [l,r,c]:upd_val[i]) { add(1,1,pot,l,r,c); //cerr<<l<<" "<<r<<" "<<c<<"\n"; } if(cnt[col_num[i]]==0) free_col--; cnt[col_num[i]]++; while(free_col==0 && read(bg)==0) { add(1,1,pot,bg,bg,k+10); ans[1]+=2*n-i+1; cnt[col_num[bg]]--; if(cnt[col_num[bg]]==0) free_col++; bg++; //cerr<<"bg->"<<bg<<"\n"; } //cerr<<"free_col="<<free_col<<" read="<<read(bg)<<"\n"; //cerr<<"mn="<<tree[1].mn<<"\n"; for(int j=0;tree[1].mn+j<=k;j++) { ans[tree[1].mn+j]+=tree[1].t[j]; //cerr<<tree[1].t[j]<<"\n"; } } for(int i=1;i<=k;i++) cout<<ans[i]<<" "; cout<<"\n"; return 0; } |