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
| #include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define db double #define ll long long #define inf 0x3f3f3f3f using namespace std; const int N=2e5+5; int n,m,Q; int cnt,s[N]; ll c[N],res[N],sm[N],p[N]; vector<pair<int,int>>q[N]; ll calc(pair<ll,ll>x,pair<ll,ll>y){ return x.fi*y.se-x.se*y.fi; } signed main(){ ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); cin>>n>>m;for(int i=1;i<=m;i++)cin>>c[i]; cin>>Q; for(int i=1;i<=Q;i++){ int x,y;cin>>x>>y; q[y].pb({x,i}); }p[1]=1; s[0]=1; for(int i=1;i<=m;i++){ while(cnt>1&&calc({i-s[cnt],c[i]-c[s[cnt]]},{s[cnt]-s[cnt-1],c[s[cnt]]-c[s[cnt-1]]})>0)cnt--; s[++cnt]=i; if(cnt>1){ p[cnt]=(1.0*(c[s[cnt]]-c[s[cnt-1]])/(s[cnt]-s[cnt-1])+1)/2; p[cnt]=max(p[cnt],1ll); sm[cnt]=sm[cnt-1]+c[s[cnt-1]]*(p[cnt]-p[cnt-1])+p[cnt-1]*p[cnt-1]*(s[cnt-1]-s[cnt-2]); } for(auto e:q[i]){ ll x=e.fi;int id=e.se; if(cnt==1)res[id]=(x-1)*c[1]; else{ int l=1,r=cnt,tmp=0; while(l<=r){ int mid=l+r>>1; if(p[mid]<=x)tmp=mid,l=mid+1; else r=mid-1; } res[id]=sm[tmp]+c[s[tmp]]*(x-p[tmp])+p[tmp]*p[tmp]*(s[tmp]-s[tmp-1])+(i-s[tmp])*x*x; } } } for(int i=1;i<=Q;i++)cout<<res[i]<<"\n"; }
|