http://codeforces.com/gym/102059/problem/J
C++版本一
题解:单调栈+二分
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
#define PL pair<ll,ll>
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=300000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int n;
ll a[N],w[N],ans[N];
int s[N];
ll l,r;
priority_queue<PL,vector<PL>,greater<PL> >pq;
struct Node{
ll l,r;
}p[N];
void ddz(){
int tot=0;
a[0]=a[n+1]=0;
s[++tot]=0;
for (int j=1;j<=n+1;++j){
ll wid=0;
while (a[s[tot]]>a[j]){
p[s[tot]].l=w[s[tot]];
p[s[tot]].r=wid+1;
wid+=w[s[tot]];
--tot;
}
s[++tot]=j;
w[j]=wid+1;
}
}
ll solve(ll mid){
ll rk=0;
for (int j=1;j<=n;++j){
ll ww=mid/a[j];
//printf("%I64d %I64d %I64d %I64d %I64d\n",ww,mid,a[s[tot]],wid,w[s[tot]]);
ll minn=min(p[j].l,p[j].r);
ll maxn=max(p[j].l,p[j].r);
if (ww<=minn){
rk+=ww*(ww+1)/2;
}else{
rk+=minn*(minn+1)/2;
if (ww<=maxn){
rk+=minn*(ww-minn);
}else{
rk+=minn*(maxn-minn);
ll sum=p[j].l+p[j].r;
ww=min(ww,sum);
ll ah=sum-maxn-1;
ll wh=sum-ww;
rk+=(ww-maxn)*(ah+wh)/2;
}
}
}
return rk;
}
void fid(ll pos){
ll ls=1;
ll rs=3e14;
ll rk=0;
while (ls<=rs){
ll mid=(ls+rs)>>1;
rk=solve(mid);
if (rk<pos)ls=mid+1;
else rs=mid-1;
}
if (rk<pos)rk=solve(ls);
for (ll j=pos;j<=r&&j<=rk;++j)ans[j-l]=ls;
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//scanf("%d",&t);
//while(t--){
scanf("%d",&n);
for (int i=1;i<=n;++i)scanf("%I64d",&a[i]);
scanf("%I64d%I64d",&l,&r);
ddz();
fid(l);
for (int i=1;i<=n;++i){
ll lim=ans[0]/a[i];
pq.push(MP((lim+1)*a[i],i));
}
int pos=0;
for (int i=1;i<=r-l;++i)if(!ans[i]){pos=i-1;break;}
while (!pq.empty()&&!ans[r-l]){
PL fp=pq.top();
pq.pop();
int j=fp.SE;
ll ww=fp.FI/a[j];
ll ns=fp.FI;
ll minn=min(p[j].l,p[j].r);
ll maxn=max(p[j].l,p[j].r);
ll rk=0;
if (ww<=minn){
rk=ww;
}else{
if (ww<=maxn){
rk=minn;
}else{
ll sum=p[j].l+p[j].r-1;
rk=max(sum-ww+1,0ll);
}
}
for (int i=pos+1;i<=r-l&&i<=pos+rk;++i)ans[i]=ns;
pos+=rk;
cout<<pos<<endl;
if (rk!=0) pq.push(MP(fp.FI+a[fp.SE],fp.SE));for (int i=0;i<=r-l;++i)printf("%3I64d%c",ans[i]," \n"[i==r-l]);
}
//}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}