#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef struct item * pitem; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int INF=1e9+7; const int LIM=5e4+7; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); struct item { int key, prior, iles, miw; pitem l, r; item (int key) : key(key), prior(rng()), iles(1), miw(key), l(nullptr), r(nullptr) { } }; pitem V[70][70], arr[LIM]; int ANS[LIM]; vector<int>D[70]; int iles(pitem t) { if(!t) return 0; return t->iles; } int miw(pitem t) { if(!t) return INF; return t->miw; } void upd(pitem t) { if(!t) return; t->iles=1+iles(t->l)+iles(t->r); t->miw=min(t->key, min(miw(t->l), miw(t->r))); } void spl(pitem t, int key, pitem &l, pitem &r) { if(!t) l=r=nullptr; else if(t->key<=key) { spl(t->r, key, t->r, r); l=t; } else { spl(t->l, key, l, t->l); r=t; } upd(t); } void merge(pitem &t, pitem l, pitem r) { if(!l || !r) t=l?l:r; else if(l->prior>r->prior) { merge(l->r, l->r, r); t=l; } else { merge(r->l, l, r->l); t=r; } upd(t); } int cnt(pitem t, int x) { pitem a; spl(t, x, a, t); int wyn=iles(a); merge(t, a, t); return wyn; } pitem uni(pitem l, pitem r) { while(l && r) { int a=miw(l), b=miw(r); pitem t; if(a<b) { spl(l, b, t, l); merge(r, t, r); } else { spl(r, a, t, r); merge(l, t, l); } } if(!l) return r; return l; } void DFS(pitem v, int w) { if(!v) return; ANS[v->key]=w; DFS(v->l, w); DFS(v->r, w); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); for(int i=1; i<=65; ++i) { for(int j=i; j<=65; j+=i) D[j].pb(i); } int n, m; cin >> n >> m; rep(i, n) arr[i]=new item(i); vector<int>T(n), czy(n); rep(i, n) cin >> T[i]; reverse(all(T)); int ile=0; vector<pair<int,int>>P; rep(i, n) { int k=(i-2*ile)/2, sum=0; pair<int,int>mi={65, n}; if(k<=0) mi={-1, -1}; for(int j=1; j<=65; ++j) { if(k<=0) { mi=min(mi, {j-1, n}); for(auto l : D[j]) { V[l][69]=uni(V[l][69], V[l][j/l-1]); V[l][j/l-1]=nullptr; } continue; } int aktk=0, aktsum=0; for(auto l : D[j]) { aktk+=iles(V[l][j/l-1]); aktsum+=iles(V[l][j/l-1])*j; } for(auto l : P) if(l.st+T[l.nd]==j) { ++aktk; aktsum+=j; } if(aktk<k) { sum+=aktsum; if(sum>m) break; k-=aktk; continue; } int po=0, ko=n; while(po<ko) { int sr=(po+ko)/2; int aktile=0; for(auto l : D[j]) aktile+=cnt(V[l][j/l-1], sr); for(auto l : P) if(l.st+T[l.nd]==j && l.nd<=sr) ++aktile; if(aktile<k) po=sr+1; else ko=sr; } sum+=k*j; if(sum>m) break; k=0; mi={j, po}; for(auto l : D[j]) { pitem a; spl(V[l][j/l-1], po, V[l][j/l-1], a); V[l][69]=uni(V[l][69], a); } } if(sum>m) { czy[i]=1; ++ile; continue; } rep(j, ile) V[T[i-j-1]][69]=uni(V[T[i-j-1]][69], arr[i-j-1]); ile=0; for(int j=(int)P.size()-1; j>=0; --j) { P[j].st+=T[P[j].nd]; if(P[j]>mi) { swap(P[j], P[(int)P.size()-1]); V[T[P.back().nd]][69]=uni(V[T[P.back().nd]][69], arr[P.back().nd]); P.pop_back(); } } P.pb({m-sum, i}); for(int j=1; j<=65; ++j) { V[j][69]=uni(V[j][69], V[j][65/j]); V[j][65/j]=nullptr; for(int l=65/j; l>=0; --l) V[j][l+1]=V[j][l]; V[j][0]=V[j][69]; V[j][69]=nullptr; } } rep(i, n) ANS[i]=-1; rep(i, P.size()) ANS[P[i].nd]=P[i].st; for(int j=1; j<=65; ++j) for(int i=0; i<=65; ++i) DFS(V[j][i], i*j); reverse(ANS, ANS+n); rep(i, n) cout << ANS[i] << " "; cout << '\n'; }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef struct item * pitem; #define rep(a, b) for(int a = 0; a < (b); ++a) #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const int INF=1e9+7; const int LIM=5e4+7; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); struct item { int key, prior, iles, miw; pitem l, r; item (int key) : key(key), prior(rng()), iles(1), miw(key), l(nullptr), r(nullptr) { } }; pitem V[70][70], arr[LIM]; int ANS[LIM]; vector<int>D[70]; int iles(pitem t) { if(!t) return 0; return t->iles; } int miw(pitem t) { if(!t) return INF; return t->miw; } void upd(pitem t) { if(!t) return; t->iles=1+iles(t->l)+iles(t->r); t->miw=min(t->key, min(miw(t->l), miw(t->r))); } void spl(pitem t, int key, pitem &l, pitem &r) { if(!t) l=r=nullptr; else if(t->key<=key) { spl(t->r, key, t->r, r); l=t; } else { spl(t->l, key, l, t->l); r=t; } upd(t); } void merge(pitem &t, pitem l, pitem r) { if(!l || !r) t=l?l:r; else if(l->prior>r->prior) { merge(l->r, l->r, r); t=l; } else { merge(r->l, l, r->l); t=r; } upd(t); } int cnt(pitem t, int x) { pitem a; spl(t, x, a, t); int wyn=iles(a); merge(t, a, t); return wyn; } pitem uni(pitem l, pitem r) { while(l && r) { int a=miw(l), b=miw(r); pitem t; if(a<b) { spl(l, b, t, l); merge(r, t, r); } else { spl(r, a, t, r); merge(l, t, l); } } if(!l) return r; return l; } void DFS(pitem v, int w) { if(!v) return; ANS[v->key]=w; DFS(v->l, w); DFS(v->r, w); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); for(int i=1; i<=65; ++i) { for(int j=i; j<=65; j+=i) D[j].pb(i); } int n, m; cin >> n >> m; rep(i, n) arr[i]=new item(i); vector<int>T(n), czy(n); rep(i, n) cin >> T[i]; reverse(all(T)); int ile=0; vector<pair<int,int>>P; rep(i, n) { int k=(i-2*ile)/2, sum=0; pair<int,int>mi={65, n}; if(k<=0) mi={-1, -1}; for(int j=1; j<=65; ++j) { if(k<=0) { mi=min(mi, {j-1, n}); for(auto l : D[j]) { V[l][69]=uni(V[l][69], V[l][j/l-1]); V[l][j/l-1]=nullptr; } continue; } int aktk=0, aktsum=0; for(auto l : D[j]) { aktk+=iles(V[l][j/l-1]); aktsum+=iles(V[l][j/l-1])*j; } for(auto l : P) if(l.st+T[l.nd]==j) { ++aktk; aktsum+=j; } if(aktk<k) { sum+=aktsum; if(sum>m) break; k-=aktk; continue; } int po=0, ko=n; while(po<ko) { int sr=(po+ko)/2; int aktile=0; for(auto l : D[j]) aktile+=cnt(V[l][j/l-1], sr); for(auto l : P) if(l.st+T[l.nd]==j && l.nd<=sr) ++aktile; if(aktile<k) po=sr+1; else ko=sr; } sum+=k*j; if(sum>m) break; k=0; mi={j, po}; for(auto l : D[j]) { pitem a; spl(V[l][j/l-1], po, V[l][j/l-1], a); V[l][69]=uni(V[l][69], a); } } if(sum>m) { czy[i]=1; ++ile; continue; } rep(j, ile) V[T[i-j-1]][69]=uni(V[T[i-j-1]][69], arr[i-j-1]); ile=0; for(int j=(int)P.size()-1; j>=0; --j) { P[j].st+=T[P[j].nd]; if(P[j]>mi) { swap(P[j], P[(int)P.size()-1]); V[T[P.back().nd]][69]=uni(V[T[P.back().nd]][69], arr[P.back().nd]); P.pop_back(); } } P.pb({m-sum, i}); for(int j=1; j<=65; ++j) { V[j][69]=uni(V[j][69], V[j][65/j]); V[j][65/j]=nullptr; for(int l=65/j; l>=0; --l) V[j][l+1]=V[j][l]; V[j][0]=V[j][69]; V[j][69]=nullptr; } } rep(i, n) ANS[i]=-1; rep(i, P.size()) ANS[P[i].nd]=P[i].st; for(int j=1; j<=65; ++j) for(int i=0; i<=65; ++i) DFS(V[j][i], i*j); reverse(ANS, ANS+n); rep(i, n) cout << ANS[i] << " "; cout << '\n'; } |