#include<bits/stdc++.h> #define int long long using namespace std; inline int read() { int n=0,f=1,ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { n=n*10+ch-'0'; ch=getchar(); } return n*f; } struct tree { int l,r,laz; int minn,tsl[11]; }a[400004]; void build(int l,int r,int k) { a[k].l=l; a[k].r=r; a[k].minn=0; a[k].laz=0; a[k].tsl[0]=r-l+1; for(int j=1;j<=10;j++)a[k].tsl[j]=0; if(l==r)return; build(l,(l+r)>>1,k<<1); build(((l+r)>>1)+1,r,k<<1|1); } void pushup(int k) { a[k].minn=min(a[k<<1].minn,a[k<<1|1].minn); for(int j=0;j<=10;j++) { a[k].tsl[j]=0; if(a[k<<1].minn<=a[k].minn+j)a[k].tsl[j]+=a[k<<1].tsl[a[k].minn+j-a[k<<1].minn]; if(a[k<<1|1].minn<=a[k].minn+j)a[k].tsl[j]+=a[k<<1|1].tsl[a[k].minn+j-a[k<<1|1].minn]; } } void change(int l,int r,int k,int x) { if(a[k].l>r||a[k].r<l)return; if(a[k].l>=l&&a[k].r<=r) { a[k].minn+=x; a[k].laz+=x; return; } if(a[k].laz!=0) { a[k<<1].minn+=a[k].laz; a[k<<1].laz+=a[k].laz; a[k<<1|1].minn+=a[k].laz; a[k<<1|1].laz+=a[k].laz; a[k].laz=0; } change(l,r,k<<1,x); change(l,r,k<<1|1,x); pushup(k); } int query(int x,int k) { if(a[k].l==a[k].r)return a[k].minn; if(a[k].laz!=0) { a[k<<1].minn+=a[k].laz; a[k<<1].laz+=a[k].laz; a[k<<1|1].minn+=a[k].laz; a[k<<1|1].laz+=a[k].laz; a[k].laz=0; } if(x<=a[k<<1].r)return query(x,k<<1); else return query(x,k<<1|1); } int qz[100005][2],pl[200005]; int pos[5],cnt; bool bi(int x,int y) { return pl[x]<pl[y]; } int n; void add(int x,int now,int q) { cnt=0; if(x>0&&pl[2*x-1]<=now)pos[++cnt]=2*x-1; if(x>0&&pl[2*x]<=now)pos[++cnt]=2*x; if(x==0&&pl[2*n-1]<=now)pos[++cnt]=2*n-1; if(x==0&&pl[2*n]<=now)pos[++cnt]=2*n; if(pl[2*x+1]<=now)pos[++cnt]=2*x+1; if(pl[2*x+2]<=now)pos[++cnt]=2*x+2; if(cnt==0)return; sort(pos+1,pos+cnt+1,bi); int tl=0,tr=0; for(int i=cnt;i>=1;i--) { if(x>0&&pos[i]==2*x-1)tl|=1; if(x>0&&pos[i]==2*x)tl|=2; if(x==0&&pos[i]==2*n-1)tl|=1; if(x==0&&pos[i]==2*n)tl|=2; if(pos[i]==2*x+1)tr|=1; if(pos[i]==2*x+2)tr|=2; if(tr!=0&&(tl&tr)==0) { int nowl; if(i==1)nowl=1; else nowl=pl[pos[i-1]]+1; change(nowl,pl[pos[i]],1,q); } } } int posx[200005]; int ans[11]; signed main() { int k; n=read(); k=read(); for(int i=1;i<=n;i++) { qz[i][0]=read(); posx[qz[i][0]]=i; pl[2*i-1]=qz[i][0]; } for(int i=1;i<=n;i++) { qz[i][1]=read(); posx[qz[i][1]]=i; pl[2*i]=qz[i][1]; } build(1,2*n,1); for(int i=1;i<=2*n;i++) { if(posx[i]<n) { add(posx[i],i-1,-1); add(posx[i],i,1); } else { add(0,i-1,-1); add(0,i,1); } add(posx[i]-1,i-1,-1); add(posx[i]-1,i,1); for(int j=1;j<=k;j++) { if(j>=a[1].minn)ans[j]+=a[1].tsl[j-a[1].minn]; } if(a[1].minn==0)ans[1]+=a[1].tsl[0]-(2*n-i); } for(int j=1;j<=k;j++)printf("%lld ",ans[j]); printf("\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 | #include<bits/stdc++.h> #define int long long using namespace std; inline int read() { int n=0,f=1,ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { n=n*10+ch-'0'; ch=getchar(); } return n*f; } struct tree { int l,r,laz; int minn,tsl[11]; }a[400004]; void build(int l,int r,int k) { a[k].l=l; a[k].r=r; a[k].minn=0; a[k].laz=0; a[k].tsl[0]=r-l+1; for(int j=1;j<=10;j++)a[k].tsl[j]=0; if(l==r)return; build(l,(l+r)>>1,k<<1); build(((l+r)>>1)+1,r,k<<1|1); } void pushup(int k) { a[k].minn=min(a[k<<1].minn,a[k<<1|1].minn); for(int j=0;j<=10;j++) { a[k].tsl[j]=0; if(a[k<<1].minn<=a[k].minn+j)a[k].tsl[j]+=a[k<<1].tsl[a[k].minn+j-a[k<<1].minn]; if(a[k<<1|1].minn<=a[k].minn+j)a[k].tsl[j]+=a[k<<1|1].tsl[a[k].minn+j-a[k<<1|1].minn]; } } void change(int l,int r,int k,int x) { if(a[k].l>r||a[k].r<l)return; if(a[k].l>=l&&a[k].r<=r) { a[k].minn+=x; a[k].laz+=x; return; } if(a[k].laz!=0) { a[k<<1].minn+=a[k].laz; a[k<<1].laz+=a[k].laz; a[k<<1|1].minn+=a[k].laz; a[k<<1|1].laz+=a[k].laz; a[k].laz=0; } change(l,r,k<<1,x); change(l,r,k<<1|1,x); pushup(k); } int query(int x,int k) { if(a[k].l==a[k].r)return a[k].minn; if(a[k].laz!=0) { a[k<<1].minn+=a[k].laz; a[k<<1].laz+=a[k].laz; a[k<<1|1].minn+=a[k].laz; a[k<<1|1].laz+=a[k].laz; a[k].laz=0; } if(x<=a[k<<1].r)return query(x,k<<1); else return query(x,k<<1|1); } int qz[100005][2],pl[200005]; int pos[5],cnt; bool bi(int x,int y) { return pl[x]<pl[y]; } int n; void add(int x,int now,int q) { cnt=0; if(x>0&&pl[2*x-1]<=now)pos[++cnt]=2*x-1; if(x>0&&pl[2*x]<=now)pos[++cnt]=2*x; if(x==0&&pl[2*n-1]<=now)pos[++cnt]=2*n-1; if(x==0&&pl[2*n]<=now)pos[++cnt]=2*n; if(pl[2*x+1]<=now)pos[++cnt]=2*x+1; if(pl[2*x+2]<=now)pos[++cnt]=2*x+2; if(cnt==0)return; sort(pos+1,pos+cnt+1,bi); int tl=0,tr=0; for(int i=cnt;i>=1;i--) { if(x>0&&pos[i]==2*x-1)tl|=1; if(x>0&&pos[i]==2*x)tl|=2; if(x==0&&pos[i]==2*n-1)tl|=1; if(x==0&&pos[i]==2*n)tl|=2; if(pos[i]==2*x+1)tr|=1; if(pos[i]==2*x+2)tr|=2; if(tr!=0&&(tl&tr)==0) { int nowl; if(i==1)nowl=1; else nowl=pl[pos[i-1]]+1; change(nowl,pl[pos[i]],1,q); } } } int posx[200005]; int ans[11]; signed main() { int k; n=read(); k=read(); for(int i=1;i<=n;i++) { qz[i][0]=read(); posx[qz[i][0]]=i; pl[2*i-1]=qz[i][0]; } for(int i=1;i<=n;i++) { qz[i][1]=read(); posx[qz[i][1]]=i; pl[2*i]=qz[i][1]; } build(1,2*n,1); for(int i=1;i<=2*n;i++) { if(posx[i]<n) { add(posx[i],i-1,-1); add(posx[i],i,1); } else { add(0,i-1,-1); add(0,i,1); } add(posx[i]-1,i-1,-1); add(posx[i]-1,i,1); for(int j=1;j<=k;j++) { if(j>=a[1].minn)ans[j]+=a[1].tsl[j-a[1].minn]; } if(a[1].minn==0)ans[1]+=a[1].tsl[0]-(2*n-i); } for(int j=1;j<=k;j++)printf("%lld ",ans[j]); printf("\n"); return 0; } |