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
| #include<bits/stdc++.h> typedef long long ll; using namespace std; struct query{int l,r;ll ans[2];}c[750005]; vector<int>v[3000005]; int now,n,f[2000005][20],lg[750005]; ll h[750005]; int merge(int x,int y) { if(h[x]!=h[y])return h[x]>h[y]?x:y; if(now)return x<y?x:y; return x>y?x:y; } int ask(int x,int y) { int t=lg[y-x+1]; return merge(f[x][t],f[y-(1<<t)+1][t]); } struct Segment_Tree { ll tree[3000005],cov[3000005],addk[3000005],addb[3000005]; void init() { memset(tree,0,sizeof(tree)); memset(cov,0x80,sizeof(cov)); memset(addk,0,sizeof(addk)); memset(addb,0,sizeof(addb)); } void pushdown(int l,int r,int x) { if(cov[x]!=-2139062144) { tree[x]=cov[x]; if(l!=r)cov[x*2]=cov[x],cov[x*2+1]=cov[x],addk[x*2]=addb[x*2]=addk[x*2+1]=addb[x*2+1]=0; cov[x]=-2139062144; } if(addk[x]||addb[x]) { tree[x]+=addb[x]+addk[x]*(r-l); if(l!=r) { int mid=(l+r)>>1; addk[x*2]+=addk[x],addb[x*2]+=addb[x]; addk[x*2+1]+=addk[x],addb[x*2+1]+=addb[x]+(mid+1-l)*addk[x]; } addk[x]=addb[x]=0; } } void add(int l,int r,int x,int a,int b,ll c,ll d) { pushdown(l,r,x); if(l>=a&&r<=b) { addk[x]+=c,addb[x]+=(l-a)*c+d; return; } int mid=(l+r)>>1; if(a<=mid)add(l,mid,x*2,a,b,c,d); if(b>mid)add(mid+1,r,x*2+1,a,b,c,d); pushdown(l,mid,x*2),pushdown(mid+1,r,x*2+1); tree[x]=tree[x*2+1]; } void modify(int l,int r,int x,int a,int b,ll c) { pushdown(l,r,x); if(l>=a&&r<=b) { cov[x]=c; addk[x]=addb[x]=0; return; } int mid=(l+r)>>1; if(a<=mid)modify(l,mid,x*2,a,b,c); if(b>mid)modify(mid+1,r,x*2+1,a,b,c); pushdown(l,mid,x*2),pushdown(mid+1,r,x*2+1); tree[x]=tree[x*2+1]; } ll query(int l,int r,int x,int a) { pushdown(l,r,x); if(l==r)return tree[x]; int mid=(l+r)>>1; if(a<=mid)return query(l,mid,x*2,a); else return query(mid+1,r,x*2+1,a); } int get(int l,int r,int x,int a,int b,ll c,ll d,int e) { pushdown(l,r,x); if(l==r)return l; int mid=(l+r)>>1; if(a<=mid&&b>mid) { pushdown(l,mid,x*2); if(c+(mid-(a-1)+1)*d>tree[x*2]+((a-1)-e+1)*d) return get(l,mid,x*2,a,b,c,d,e); return get(mid+1,r,x*2+1,a,b,c,d,e); } else if(a<=mid)return get(l,mid,x*2,a,b,c,d,e); return get(mid+1,r,x*2+1,a,b,c,d,e); } ll operator [](int x){return query(1,n,1,x);} }T; void dfs(int l,int r,bool s) { int mid=ask(l,r); if(l<mid) { dfs(l,mid-1,0); T.modify(1,n,1,mid,mid,T[mid-1]+h[mid]); } else T.modify(1,n,1,mid,mid,h[mid]); if(r>mid) { dfs(mid+1,r,1); int t; ll tmp=(l<mid)?T[mid-1]:0; if(T[r]+(mid-l+1)*h[mid]>=tmp+(r-mid+1)*h[mid]) t=r+1; else t=T.get(1,n,1,mid+1,r,tmp,h[mid],l); if(t!=mid+1) { T.modify(1,n,1,mid+1,t-1,tmp); T.add(1,n,1,mid+1,t-1,h[mid],2*h[mid]); } if(t!=r+1)T.add(1,n,1,t,r,0,(mid-l+1)*h[mid]); } if(s) { for(int i=0;i<v[l].size();i++) c[v[l][i]].ans[now]=T[c[v[l][i]].r]; } } int main() { int q; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++)scanf("%lld",&h[i]),f[i][0]=i; for(int i=1;i<=q;i++)scanf("%d%d",&c[i].l,&c[i].r),c[i].l++,c[i].r++; again: for(int i=1;i<=n;i++)v[i].clear(); T.init(); for(int i=2;i<=n;i++)lg[i]=lg[i>>1]+1; for(int k=1;k<=19;k++) for(int i=1;i<=n;i++) f[i][k]=merge(f[i][k-1],f[i+(1<<(k-1))][k-1]); for(int i=1;i<=q;i++) { int t=ask(c[i].l,c[i].r); if(t!=c[i].r)v[t+1].push_back(i); } dfs(1,n,1); if(!now) { now=1; reverse(h+1,h+n+1); for(int i=1;i<=q;i++) c[i].l=n-c[i].l+1,c[i].r=n-c[i].r+1,swap(c[i].l,c[i].r); goto again; } else for(int i=1;i<=q;i++) { int t=ask(c[i].l,c[i].r); printf("%lld\n",min(c[i].ans[0]+(c[i].r-t+1)*h[t],c[i].ans[1]+(t-c[i].l+1)*h[t])); } return 0; }
|