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
| #include<cstdio> #include<algorithm> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std;
typedef long long LL;
const int maxn=2e5+5;
struct QST{ int x,y,i; }; bool cmpQ(const QST &a,const QST &b) {return a.x>b.x;}
int n,q0,w[maxn],g[maxn],p[maxn]; QST q[maxn];
LL tr[4*maxn],sum[4*maxn],bz[4*maxn]; bool zero[4*maxn]; void update(int k,int t) { if (zero[k]) { tr[t]=tr[t+1]=sum[t]=sum[t+1]=0; zero[t]=zero[t+1]=1; zero[k]=0; } if (bz[k]) { bz[t]=bz[t+1]=bz[k]; sum[t]=tr[t]*bz[k]; sum[t+1]=tr[t+1]*bz[k]; } } void tr_xg(int ty,int k,int l,int r,int x,int y,LL z) { if (l==x && r==y) { if (ty) { sum[k]=tr[k]*z; bz[k]=z; } else { tr[k]=z; sum[k]=tr[k]*bz[k]; if (z==0) zero[k]=1; } return; } int t=k<<1, t1=(l+r)>>1; update(k,t); if (y<=t1) tr_xg(ty,t,l,t1,x,y,z); else if (x>t1) tr_xg(ty,t+1,t1+1,r,x,y,z); else tr_xg(ty,t,l,t1,x,t1,z), tr_xg(ty,t+1,t1+1,r,t1+1,y,z); tr[k]=tr[t]+tr[t+1]; sum[k]=sum[t]+sum[t+1]; } LL tr_cx(int k,int l,int r,int x,int y) { if (l==x && r==y) return sum[k]; int t=k<<1, t1=(l+r)>>1; update(k,t); if (y<=t1) return tr_cx(t,l,t1,x,y); else if (x>t1) return tr_cx(t+1,t1+1,r,x,y); else return tr_cx(t,l,t1,x,t1)+tr_cx(t+1,t1+1,r,t1+1,y); }
LL Ans[maxn],s[maxn]; int z[maxn][2],z0,fr[maxn]; // fr就是每个点不氪金最远走到哪里 int main() { freopen("G.in","r",stdin); freopen("G.out","w",stdout); scanf("%d %d",&n,&q0); fo(i,1,n-1) scanf("%d",&w[i]); fo(i,1,n) scanf("%d %d",&g[i],&p[i]); fo(i,1,q0) scanf("%d %d",&q[i].x,&q[i].y), q[i].i=i; sort(q+1,q+1+q0,cmpQ); w[n]=2139062143; fo(i,1,n) s[i]=s[i-1]+g[i]-w[i]; int wz=1; z[z0=1][0]=n; fd(i,n,1) { while (z0 && s[i-1]<=s[z[z0][0]]) z0--; fr[i]=z[z0][0]; z[++z0][0]=i-1; } z0=0; int j=1; fd(i,n,1) { if (g[i]-w[i]>=0) { int x=fr[i]; tr_xg(0,1,1,n,i,x,0); if (x+1<=n) tr_xg(0,1,1,n,x,x,s[i-1]-s[x]); } else { tr_xg(0,1,1,n,i,i,w[i]-g[i]); } int wz=i; while (z0 && p[i]<=z[z0][0]) wz=z[z0--][1]; tr_xg(1,1,1,n,i,wz,p[i]); z[++z0][0]=p[i], z[z0][1]=wz; for(; j<=q0 && q[j].x==i; j++) if (i<q[j].y) Ans[q[j].i]=tr_cx(1,1,n,i,q[j].y-1); } fo(i,1,q0) printf("%lld\n",Ans[i]); }
|