#include<set>
#include<map>
#include<queue>
#include<vector>
#include<algorithm>
#include<bits/stdc++.h>
#define pr pair
#define f first
#define s second
#define ll long long
#define mp make_pair
#define pll pr<ll,ll>
#define pii pr<int,int>
#define piii pr<int,pii>
using namespace std;
struct node
{
int l,r;
int ls,rs;
int ts;
} nd[20000007];
int z=1;
int crt(int l,int r,int i)
{
int rt=z++;
nd[rt].l=l;
nd[rt].r=r;
nd[rt].ts=1;
if(l==r-1) return rt;
int m=l+r>>1;
if(i<m) nd[rt].ls=crt(l,m,i);
else nd[rt].rs=crt(m,r,i);
return rt;
}
void mg(int a,int b)
{
node&x=nd[a],&y=nd[b];
x.ts+=y.ts;
if(x.ls&&y.ls) mg(x.ls,y.ls);
else x.ls+=y.ls;
if(x.rs&&y.rs) mg(x.rs,y.rs);
else x.rs+=y.rs;
}
int fv(int x)
{
return nd[x].ts;
}
int hlr(int x,int y,int l,int r)
{
if(x+y==0) return 0;
int rt=z++;
nd[rt].l=l;
nd[rt].r=r;
nd[rt].ts=fv(x)+fv(y);
nd[rt].ls=x;
nd[rt].rs=y;
return rt;
}
pii spt(int i,int l)
{
node&x=nd[i];
if(l==x.l) return mp(0,i);
if(l==x.r) return mp(i,0);
int m=x.l+x.r>>1;
// cout<<"Spl "<<x.l<<' '<<x.r<<" "<<l<<endl;
if(l<m)
{
pii a=x.ls?spt(x.ls,l):mp(0,0);
return mp(hlr(a.f,0,x.l,x.r),hlr(a.s,x.rs,x.l,x.r));
}
else
{
pii a=x.rs?spt(x.rs,l):mp(0,0);
return mp(hlr(x.ls,a.f,x.l,x.r),hlr(0,a.s,x.l,x.r));
}
}
int fl(vector<int> i,int k)
{
int fd=0;
if(nd[i[0]].l==nd[i[0]].r-1) return nd[i[0]].r;
for(int j:i) fd+=fv(nd[j].ls);
vector<int> nw;
if(fd>=k)
{
for(int j:i) if(nd[j].ls) nw.push_back(nd[j].ls);
}
else
{
for(int j:i) if(nd[j].rs) nw.push_back(nd[j].rs);
k-=fd;
}
return fl(nw,k);
}
int ans[50004];
void dfs(int i,int vl)
{
node&x=nd[i];
if(x.l==x.r-1)
{
ans[x.l]=vl;
return;
}
if(x.ls) dfs(x.ls,vl);
if(x.rs) dfs(x.rs,vl);
}
ll cts;
set<pii> st[65];
set<pii>::iterator ls;
ll tov;
int dt;
void add(int i,int x,int y)
{
ls=st[i].lower_bound(mp(x,-1));
if(ls!=st[i].end()&&(*ls).f==x) mg((*ls).s,y);
else st[i].insert(mp(x,y));
}
void pop(int x)
{
// cout<<"Pop "<<x<<endl;
while(x)
{
int mx=-1;
for(int i=1;i<=64;i++)
{
if(st[i].empty()) continue;
mx=max(mx,(*--st[i].end()).f+dt*i);
}
vector<pii> hd;
for(int i=1;i<=64;i++)
{
if(st[i].empty()) continue;
if(mx==(*--st[i].end()).f+dt*i)
{
hd.push_back(mp((*--st[i].end()).s,i));
st[i].erase(*--st[i].end());
}
}
int al=0;
for(pii i:hd) al+=fv(i.f);
// cout<<"Fp "<<mx<<' '<<al<<endl;
// for(pii i:hd) cout<<i.f<<' '<<i.s<<" ";
// cout<<endl;
if(al<=x)
{
x-=al;
cts-=al*mx;
for(pii i:hd)
{
add(i.s,-dt*i.s,i.f);
}
continue;
}
vector<int> th;
for(pii i:hd) th.push_back(i.f);
int g=fl(th,x);
// cout<<"Spl "<<g<<endl;
cts-=x*mx;
for(pii i:hd)
{
pii gg=spt(i.f,g);
// cout<<"Splited "<<i.f<<' '<<fv(gg.f)<<'('<<gg.f<<')'<<' '<<fv(gg.s)<<'('<<gg.s<<')'<<endl;
if(gg.f) add(i.s,-dt*i.s,gg.f);
if(gg.s) add(i.s,mx-dt*i.s,gg.s);
}
break;
}
}
int a[50004];
int main()
{
ios_base::sync_with_stdio(0);
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
add(a[n-1],m,crt(0,n,n-1));
cts=m;
dt=1;
tov=a[n-1];
int ald=0;
int las=n-1;
for(int i=n-2;i>=0;i--)
{
int k=(n-i)/2;
// cout<<"\nBef "<<cts<<endl;
// for(int i=1;i<=64;i++)
// {
// for(pii j:st[i]) cout<<"F "<<i<<' '<<j.f<<' '<<fv(j.s)<<'('<<j.s<<')'<<endl;
// }
pop(k-ald);
ald=k;
// cout<<"Cr "<<cts<<' '<<dt<<' '<<tov<<endl;
if(cts+dt*tov>m) add(a[i],-dt*a[i],crt(0,n,i)),cts-=dt*a[i];
else
{
ll g=m-cts-dt*tov;
// cout<<"Get "<<g<<endl;
cts+=g-dt*a[i];
add(a[i],g-dt*a[i],crt(0,n,i));
dt++;
las=i;
ald=0;
}
tov+=a[i];
}
cerr<<las<<endl;
z=1;
for(int i=0;i<=20000000;i++)
{
nd[i].l=0;
nd[i].r=0;
nd[i].ls=0;
nd[i].rs=0;
nd[i].ts=0;
}
for(int i=1;i<=64;i++) st[i].clear();
add(a[n-1],m,crt(0,n,n-1));
cts=m;
dt=1;
tov=a[n-1];
ald=0;
cerr<<"Redo"<<endl;
for(int i=n-2;i>=las;i--)
{
int k=(n-i)/2;
// cout<<"\nBef "<<cts<<endl;
// for(int i=1;i<=64;i++)
// {
// for(pii j:st[i]) cout<<"F "<<i<<' '<<j.f<<' '<<fv(j.s)<<'('<<j.s<<')'<<endl;
// }
pop(k-ald);
ald=k;
// cout<<"Cr "<<cts<<' '<<dt<<' '<<tov<<endl;
if(cts+dt*tov>m) add(a[i],-dt*a[i],crt(0,n,i)),cts-=dt*a[i];
else
{
ll g=m-cts-dt*tov;
// cout<<"Get "<<g<<endl;
cts+=g-dt*a[i];
add(a[i],g-dt*a[i],crt(0,n,i));
dt++;
ald=0;
}
tov+=a[i];
}
for(int i=0;i<las;i++) ans[i]=-1;
for(int i=1;i<=64;i++)
{
for(pii j:st[i]) dfs(j.s,j.f+i*dt-i);
}
for(int i=0;i<n;i++) cout<<ans[i]<<' ';
cout<<endl;
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | #include<set> #include<map> #include<queue> #include<vector> #include<algorithm> #include<bits/stdc++.h> #define pr pair #define f first #define s second #define ll long long #define mp make_pair #define pll pr<ll,ll> #define pii pr<int,int> #define piii pr<int,pii> using namespace std; struct node { int l,r; int ls,rs; int ts; } nd[20000007]; int z=1; int crt(int l,int r,int i) { int rt=z++; nd[rt].l=l; nd[rt].r=r; nd[rt].ts=1; if(l==r-1) return rt; int m=l+r>>1; if(i<m) nd[rt].ls=crt(l,m,i); else nd[rt].rs=crt(m,r,i); return rt; } void mg(int a,int b) { node&x=nd[a],&y=nd[b]; x.ts+=y.ts; if(x.ls&&y.ls) mg(x.ls,y.ls); else x.ls+=y.ls; if(x.rs&&y.rs) mg(x.rs,y.rs); else x.rs+=y.rs; } int fv(int x) { return nd[x].ts; } int hlr(int x,int y,int l,int r) { if(x+y==0) return 0; int rt=z++; nd[rt].l=l; nd[rt].r=r; nd[rt].ts=fv(x)+fv(y); nd[rt].ls=x; nd[rt].rs=y; return rt; } pii spt(int i,int l) { node&x=nd[i]; if(l==x.l) return mp(0,i); if(l==x.r) return mp(i,0); int m=x.l+x.r>>1; // cout<<"Spl "<<x.l<<' '<<x.r<<" "<<l<<endl; if(l<m) { pii a=x.ls?spt(x.ls,l):mp(0,0); return mp(hlr(a.f,0,x.l,x.r),hlr(a.s,x.rs,x.l,x.r)); } else { pii a=x.rs?spt(x.rs,l):mp(0,0); return mp(hlr(x.ls,a.f,x.l,x.r),hlr(0,a.s,x.l,x.r)); } } int fl(vector<int> i,int k) { int fd=0; if(nd[i[0]].l==nd[i[0]].r-1) return nd[i[0]].r; for(int j:i) fd+=fv(nd[j].ls); vector<int> nw; if(fd>=k) { for(int j:i) if(nd[j].ls) nw.push_back(nd[j].ls); } else { for(int j:i) if(nd[j].rs) nw.push_back(nd[j].rs); k-=fd; } return fl(nw,k); } int ans[50004]; void dfs(int i,int vl) { node&x=nd[i]; if(x.l==x.r-1) { ans[x.l]=vl; return; } if(x.ls) dfs(x.ls,vl); if(x.rs) dfs(x.rs,vl); } ll cts; set<pii> st[65]; set<pii>::iterator ls; ll tov; int dt; void add(int i,int x,int y) { ls=st[i].lower_bound(mp(x,-1)); if(ls!=st[i].end()&&(*ls).f==x) mg((*ls).s,y); else st[i].insert(mp(x,y)); } void pop(int x) { // cout<<"Pop "<<x<<endl; while(x) { int mx=-1; for(int i=1;i<=64;i++) { if(st[i].empty()) continue; mx=max(mx,(*--st[i].end()).f+dt*i); } vector<pii> hd; for(int i=1;i<=64;i++) { if(st[i].empty()) continue; if(mx==(*--st[i].end()).f+dt*i) { hd.push_back(mp((*--st[i].end()).s,i)); st[i].erase(*--st[i].end()); } } int al=0; for(pii i:hd) al+=fv(i.f); // cout<<"Fp "<<mx<<' '<<al<<endl; // for(pii i:hd) cout<<i.f<<' '<<i.s<<" "; // cout<<endl; if(al<=x) { x-=al; cts-=al*mx; for(pii i:hd) { add(i.s,-dt*i.s,i.f); } continue; } vector<int> th; for(pii i:hd) th.push_back(i.f); int g=fl(th,x); // cout<<"Spl "<<g<<endl; cts-=x*mx; for(pii i:hd) { pii gg=spt(i.f,g); // cout<<"Splited "<<i.f<<' '<<fv(gg.f)<<'('<<gg.f<<')'<<' '<<fv(gg.s)<<'('<<gg.s<<')'<<endl; if(gg.f) add(i.s,-dt*i.s,gg.f); if(gg.s) add(i.s,mx-dt*i.s,gg.s); } break; } } int a[50004]; int main() { ios_base::sync_with_stdio(0); int n,m; cin>>n>>m; for(int i=0;i<n;i++) cin>>a[i]; add(a[n-1],m,crt(0,n,n-1)); cts=m; dt=1; tov=a[n-1]; int ald=0; int las=n-1; for(int i=n-2;i>=0;i--) { int k=(n-i)/2; // cout<<"\nBef "<<cts<<endl; // for(int i=1;i<=64;i++) // { // for(pii j:st[i]) cout<<"F "<<i<<' '<<j.f<<' '<<fv(j.s)<<'('<<j.s<<')'<<endl; // } pop(k-ald); ald=k; // cout<<"Cr "<<cts<<' '<<dt<<' '<<tov<<endl; if(cts+dt*tov>m) add(a[i],-dt*a[i],crt(0,n,i)),cts-=dt*a[i]; else { ll g=m-cts-dt*tov; // cout<<"Get "<<g<<endl; cts+=g-dt*a[i]; add(a[i],g-dt*a[i],crt(0,n,i)); dt++; las=i; ald=0; } tov+=a[i]; } cerr<<las<<endl; z=1; for(int i=0;i<=20000000;i++) { nd[i].l=0; nd[i].r=0; nd[i].ls=0; nd[i].rs=0; nd[i].ts=0; } for(int i=1;i<=64;i++) st[i].clear(); add(a[n-1],m,crt(0,n,n-1)); cts=m; dt=1; tov=a[n-1]; ald=0; cerr<<"Redo"<<endl; for(int i=n-2;i>=las;i--) { int k=(n-i)/2; // cout<<"\nBef "<<cts<<endl; // for(int i=1;i<=64;i++) // { // for(pii j:st[i]) cout<<"F "<<i<<' '<<j.f<<' '<<fv(j.s)<<'('<<j.s<<')'<<endl; // } pop(k-ald); ald=k; // cout<<"Cr "<<cts<<' '<<dt<<' '<<tov<<endl; if(cts+dt*tov>m) add(a[i],-dt*a[i],crt(0,n,i)),cts-=dt*a[i]; else { ll g=m-cts-dt*tov; // cout<<"Get "<<g<<endl; cts+=g-dt*a[i]; add(a[i],g-dt*a[i],crt(0,n,i)); dt++; ald=0; } tov+=a[i]; } for(int i=0;i<las;i++) ans[i]=-1; for(int i=1;i<=64;i++) { for(pii j:st[i]) dfs(j.s,j.f+i*dt-i); } for(int i=0;i<n;i++) cout<<ans[i]<<' '; cout<<endl; return 0; } |
English