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
| #include<bits/stdc++.h> using namespace std; #define int long long inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'|| ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back
#define N 1000010
int crx[N], f[N], rk[N]; int n, m, i, j, k, T; int ans[N], a[N], b[N], c[N], p; int e[N], d[N], s[N], l, r, len; map<int, int>mp;
inline void UP(int *a, int n) { int bbbb[N], i, j; for(i=1; i<=n; ++i) crx[i]=a[i]; sort(crx+1, crx+n+1); for(i=1; i<=n; ++i) a[i]=lower_bound(crx+1, crx+n+1, a[i])-crx; }
struct Tree_Zhuang_Number_Group { int cnt[N], i, ans, n; inline void clear() { for(i=0; i<=n; ++i) cnt[i]=0; } inline void add(int x, int y) {
while(x<=n) cnt[x]+=y, x+=x&-x; } inline int que(int x) { ans=0; while(x) ans+=cnt[x], x-=x&-x; return ans; } }S1, S2, S;
inline void calc(int op) { if(op==2) { for(i=1; i<=n; ++i) b[i]=a[i]-i; UP(b, n); for(i=1; i<=n; ++i) c[i]=a[i]+i; UP(c, n); S.clear(); } for(i=1; i<=(n+1)/2; ++i) { s[i]+=S.que(c[i]-1); S.add(c[i], 1); } S.clear(); for(i=1, l=1, r=0; i<=(n+1)/2; ++i) { while(r<2*i-1) S.add(b[++r], 1); while(l<=i) S.add(b[l++], -1); s[i]+=S.que(b[i]-1); } for(i=1, j=0; i<=n; ++i) f[++j]=e[i]=a[i]; for(i=1; i<=n; ++i) f[++j]=d[i]=a[i]+i-1; sort(f+1, f+2*n+1); len=unique(f+1,f+n*2+1)-f-1; for(i=1; i<=n; ++i) e[i]=lower_bound(f+1, f+len+1, e[i])-f; for(i=1; i<=n; ++i) d[i]=lower_bound(f+1, f+len+1, d[i])-f; S.n=len; S.clear(); for(i=(n+1)/2, r=n; i>=1; --i) { while(r>=2*i) S.add(e[r], 1), --r; s[i]+=S.que(d[i]-1); } S.n=n; }
inline void outp(int x){ if(x>9) outp(x/10); putchar(x%10+48); }
signed main() {
freopen("run.in", "r", stdin); freopen("run.out", "w", stdout);
n=read(); p=read(); for(i=1; i<=n; ++i) a[i]=read(); for(i=1; i<=n; ++i) b[i]=a[i]-i; UP(b, n); for(i=1; i<=n; ++i) c[i]=a[i]+i; UP(c, n); S1.n=S2.n=S.n=n; if(p==1) { for(i=1; i<=n; ++i) { ans[i]+=S.que(b[i]-1); S.add(b[i], 1); } S.clear(); for(i=n; i>=1; --i) { ans[i]+=S.que(c[i]-1); S.add(c[i], 1); } for(i=1; i<=n; ++i) printf("%lld\n", ans[i]+1); return 0; } for(i=1; i<=n; ++i) S1.add(b[i], 1); for(i=1; i<=n; ++i) ans[i]=max(ans[i], S1.que(b[i]-1)); for(i=1; i<=n; ++i) S2.add(c[i], 1); for(i=1; i<=n; ++i) ans[i]=max(ans[i], S2.que(c[i]-1)); calc(1); if(n&1) s[(n+1)>>1]=0; for(i=1, j=n; i<=j; ++i, --j) swap(a[i], a[j]), swap(s[i], s[j]); calc(2); for(i=1, j=n; i<=j; ++i, --j) swap(s[i], s[j]); for(i=1; i<=n; ++i) ans[i]=max(ans[i], s[i])+1; for(i=1; i<=n; ++i) outp(ans[i]), puts(""); return 0; }
|