#include<iostream> #include<cstdlib> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<algorithm> #define kl (k<<1) #define kr ((k<<1)+1) #define mid ((l+r)>>1) #define lowbit(x) ((x)&-(x)) using namespace std; const int maxn=2*1e5 + 10, INF=0x7f7f7f7f; typedef long long lint; int n, m, a[maxn], ql[maxn], qr[maxn]; lint s[maxn]; vector<int> vct[maxn]; struct node { int x[2]; node(int l=0, int r=0, int c=0){ x[0]=l, x[1]=r; } }tr[maxn<<2]; void Build(int k=1, int l=1, int r=n) { tr[k].x[0]=tr[k].x[1]=-INF; if(l==r) return ; Build(kl, l, mid); Build(kr, mid+1, r); return ; } void Push_down(int k, int f) { tr[kl].x[f]=max(tr[kl].x[f], tr[k].x[f]); tr[kr].x[f]=max(tr[kr].x[f], tr[k].x[f]); return ; } void Insert(int ll, int rr, int f, int val, int k=1, int l=1, int r=n) { if(rr <l||r< ll) return ; if(ll<=l&&r<=rr) { tr[k].x[f]=val; return ; } Push_down(k, f); Insert(ll, rr, f, val, kl, l, mid); Insert(ll, rr, f, val, kr, mid+1, r); return ; } void Add(int x, int val) { for(int i=x; i<=m; i+=lowbit(i)) s[i]+=val; return ; } lint Get(int x, lint sum=0) { for(int i=x; i; i-=lowbit(i)) sum+=s[i]; return sum; } void Query(int x, int &ll, int &rr, int k=1, int l=1, int r=n) { if(l==r) { ll=tr[k].x[0], rr=tr[k].x[1]; return ; } Push_down(k, 0); Push_down(k, 1); if(x<=mid) Query(x, ll, rr, kl, l, mid); else Query(x, ll, rr, kr, mid+1, r); return ; } int main() { while(scanf("%d%d", &n, &m)!=EOF) { for(int i=1; i<n; i++) scanf("%d", &a[i]); for(int i=1; i<=m; i++) { scanf("%d%d", &ql[i], &qr[i]); if(ql[i]>qr[i]) swap(ql[i], qr[i]); --qr[i]; } Build(); for(int i=1; i<=m; i++) Insert(ql[i], qr[i], 0, i); for(int i=m; i>=1; i--) Insert(ql[i], qr[i], 1, m-i+1); for(int i=1, ll, rr; i<n; i++) { Query(i, rr, ll); if(rr==-INF) continue; Add(m-ll+1, a[i]); Add(rr+1, -a[i]); } for(int i=1; i<=m; i++) printf("%lld\n", Get(i)); memset(s, 0, sizeof s); } return 0; }
|