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
| #include<algorithm> #include<cstring> #include<cstdio> #include<vector> using namespace std; using pi=pair<int,int>; using ll=long long; inline int read() { int s=0,w=1;char ch; while((ch=getchar())>'9'||ch<'0') if(ch=='-') w=-1; while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar(); return s*w; } vector<pi>q[750001]; int st[750001][21]; ll tagk[3000001]; ll tagb[3000001]; ll tagf[3000001]; ll num[3000001]; int mid[750001]; ll ans[750001]; int l[750001]; int r[750001]; int h[750001]; int p[750001]; bool flag; int root; int n,m; inline int get_max(int l,int r) { int num=p[r-l],val1=st[l][num],val2=st[r-(1<<num)+1][num]; if(!flag) return h[val1]>h[val2]?val1:val2; return h[val1]>=h[val2]?val1:val2; } void run_st() { for(int i=n;i;i--) for(int j=1;i+(1<<j)-1<=n;j++) st[i][j]=get_max(i,i+(1<<j)-1); } void push_down(int p,int pl,int pr) { int mid=(pl+pr)>>1; if(~tagf[p]) { tagk[p*2]=tagk[p*2|1]=tagb[p*2]=tagb[p*2|1]=0; tagf[p*2]=tagf[p*2|1]=num[p*2]=num[p*2|1]=tagf[p]; tagf[p]=-1; } num[p*2]+=mid*tagk[p]+tagb[p]; num[p*2|1]+=pr*tagk[p]+tagb[p]; tagk[p*2]+=tagk[p],tagk[p*2|1]+=tagk[p],tagk[p]=0; tagb[p*2]+=tagb[p],tagb[p*2|1]+=tagb[p],tagb[p]=0; } void push_up(int p) { num[p]=num[p*2|1]; } void cover(int p,int pl,int pr,int l,int r,ll y) { if(l<=pl&&pr<=r){ tagk[p]=tagb[p]=0,tagf[p]=num[p]=y;return ;} push_down(p,pl,pr); int mid=(pl+pr)>>1; if(l<=mid) cover(p*2,pl,mid,l,r,y); if(mid<r) cover(p*2|1,mid+1,pr,l,r,y); push_up(p); } ll get(int p,int pl,int pr,int x) { if(pl==pr) return tagf[p]+tagk[p]*pl+tagb[p]; push_down(p,pl,pr); int mid=(pl+pr)>>1; if(x<=mid) return get(p*2,pl,mid,x); else return get(p*2|1,mid+1,pr,x); } int cal(int p,int pl,int pr,int l,int r,int len,ll dp) { if(pl==pr) { if(dp+(ll)(pl-l+1)*h[l-1]<=num[p]+(ll)len*h[l-1]) return pl; return pl-1; } push_down(p,pl,pr); int mid=(pl+pr)>>1; if(l>mid) return cal(p*2|1,mid+1,pr,l,r,len,dp); if(mid>=r) return cal(p*2,pl,mid,l,r,len,dp); if(dp+(ll)(mid-l+1)*h[l-1]<=num[p*2]+(ll)len*h[l-1]) return cal(p*2|1,mid+1,pr,l,r,len,dp); return cal(p*2,pl,mid,l,r,len,dp); } void add(int p,int pl,int pr,int l,int r,ll k,ll b) { if(l<=pl&&pr<=r) { tagk[p]+=k,tagb[p]+=b; num[p]+=k*pr+b; return ; } push_down(p,pl,pr); int mid=(pl+pr)>>1; if(l<=mid) add(p*2,pl,mid,l,r,k,b); if(mid<r) add(p*2|1,mid+1,pr,l,r,k,b); push_up(p); } void dfs(int nl,int nr) { int num=get_max(nl,nr),nb=-1;ll dp; if(num!=nl) { dfs(nl,num-1); dp=get(1,1,n,num-1)+h[num]; } else dp=h[num]; cover(1,1,n,num,num,dp); if(num!=nr) { dfs(num+1,nr); int mi=nb=cal(1,1,n,num+1,nr,num-nl+1,dp); if(mi>num) { cover(1,1,n,num+1,mi,0); add(1,1,n,num+1,mi,h[num],dp-(ll)num*h[num]); } if(mi<nr) add(1,1,n,mi+1,nr,0,(ll)(num-nl+1)*h[num]); } for(auto i:q[num]) { int id=i.first,qr=i.second; ans[id]=min(ans[id],get(1,1,n,qr)+(ll)(mid[id]-l[id]+1)*h[mid[id]]); } } void run() { for(int i=1;i<=m;i++) if(mid[i]!=r[i]) q[get_max(mid[i]+1,r[i])].push_back(pi(i,r[i])); dfs(1,n); } void reverse() { flag=1; cover(1,1,n,1,n,0),reverse(h+1,h+n+1),run_st(); for(int i=1;i<=m;i++) { l[i]=n-l[i]+1,r[i]=n-r[i]+1; swap(l[i],r[i]),mid[i]=get_max(l[i],r[i]); } for(int i=1;i<=n;i++) q[i].clear(); } int main() { n=read(),m=read(); for(int i=1;i<=n;i++) h[i]=read(),st[i][0]=i; for(int i=0;(1<<i)<=n;i++) p[1<<i]=i; for(int i=1;i<=n;i++) if(!p[i]) p[i]=p[i-1]; run_st(); for(int i=1;i<=m;i++) { l[i]=read()+1,r[i]=read()+1; mid[i]=get_max(l[i],r[i]); ans[i]=(ll)(r[i]-l[i]+1)*h[mid[i]]; } run(),reverse(),run(); for(int i=1;i<=m;i++) printf("%lld\n",ans[i]); return 0; }
|