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
|
#include<bits/stdc++.h> #define re register typedef long long ll; template<class T> inline void checkMax(T &x,T y) { x=x>y?x:y; } template<class T> inline void checkMin(T &x,T y) { x=x<y?x:y; } const int MAXN=1e6+10,MAXM=3e6+10; const ll INF=4e18; namespace FIO { const int L = 1 << 15; int l = 0; char buf[L | 100], out[L | 100], *S, *E; #define gh() (S == E ? E = (S = buf) + fread(buf, 1, L, stdin), (S == E ? EOF : *S++) : *S++) #define IL inline IL void flus(){ fwrite(out, 1, l, stdout), l = 0; } struct Fluser{ ~Fluser(){ flus(); } } fluser; IL void putc(char x) { out[l++] = x; if (!(l ^ L)) flus(); } void read(char *s) { char ch = gh(); while (ch < 33 || ch > 126) ch = gh(); for (; ch >= 33 && ch <= 126; ch = gh()) *s++ = ch; *s = '\0'; } template<typename T> IL void read(T &x) { char ch = gh(), t = 0; for (; ch < '0' || ch > '9'; ch = gh()) t |= ch == '-'; for (x = 0; ch >= '0' && ch <= '9'; ch = gh()) x = x * 10 + (ch ^ 48); if (t) x = -x; } template<typename T> IL void write(T x) { if (x < 0) x = -x, putc('-'); if (x > 9) write(x / 10); putc(x % 10 + '0'); } IL void write(const char *s){ while (*s) putc(*s++); } template<typename T, typename ...Args> void read(T &x, Args &...args){ read(x), read(args...); } template<typename T, typename ...Args> void write(T x, Args ...args){ write(x), write(args...); } #undef gh #undef IL } using FIO::read; using FIO::write; int N,Q,Len; ll Val[MAXN],Lg[MAXN],Pos[2][MAXN],prep,sum_; ll q[32][MAXN],s[32][MAXN]; ll t[32][MAXN],b[32][MAXN]; #define ls p<<1 #define rs p<<1|1 void buildAx(int p,int l,int r,int d) { if(l==r) return Pos[0][l]=p,void(); int mid=(l+r)>>1; q[d][mid]=s[d][mid]=prep=sum_=Val[mid],checkMax(sum_,0ll); for(int i=mid-1;i>=l;--i) { prep+=Val[i],sum_+=Val[i],s[d][i]=std::max(s[d][i+1],prep); q[d][i]=std::max(q[d][i+1],sum_),checkMax(sum_,0ll); } q[d][mid+1]=s[d][mid+1]=prep=sum_=Val[mid+1],checkMax(sum_,0ll); for(int i=mid+2;i<=r;++i) { prep+=Val[i],sum_+=Val[i],s[d][i]=std::max(s[d][i-1],prep); q[d][i]=std::max(q[d][i-1],sum_),checkMax(sum_,0ll); } buildAx(ls,l,mid,d+1),buildAx(rs,mid+1,r,d+1); } inline ll queryMax(int l,int r) { if(l==r) return Val[l]; int d=Lg[Pos[0][l]]-Lg[Pos[0][l]^Pos[0][r]]; return std::max(std::max(q[d][l],q[d][r]),s[d][l]+s[d][r]); } void buildIn(int p,int l,int r,int d) { if(l==r) return Pos[1][l]=p,void(); int mid=(l+r)>>1; t[d][mid]=b[d][mid]=prep=sum_=Val[mid],checkMin(sum_,0ll); for(int i=mid-1;i>=l;--i) { prep+=Val[i],sum_+=Val[i],b[d][i]=std::min(b[d][i+1],prep); t[d][i]=std::min(t[d][i+1],sum_),checkMin(sum_,0ll); } t[d][mid+1]=b[d][mid+1]=prep=sum_=Val[mid+1],checkMin(sum_,0ll); for(int i=mid+2;i<=r;++i) { prep+=Val[i],sum_+=Val[i],b[d][i]=std::min(b[d][i-1],prep); t[d][i]=std::min(t[d][i-1],sum_),checkMin(sum_,0ll); } buildIn(ls,l,mid,d+1),buildIn(rs,mid+1,r,d+1); } inline ll queryMin(int l,int r) { if(l==r) return Val[l]; int d=Lg[Pos[1][l]]-Lg[Pos[1][l]^Pos[1][r]]; return std::min(std::min(t[d][l],t[d][r]),b[d][l]+b[d][r]); } signed main() { read(N,Q),Len=1; while(Len<N) Len<<=1; for(int i=1;i<=N;++i) read(Val[i]); for(int i=2,l=Len<<1;i<=l;++i) Lg[i]=Lg[i>>1]+1; buildAx(1,1,Len,1); buildIn(1,1,Len,1); for(int ql,qr;Q--;) { read(ql,qr); ll resax=queryMax(ql,qr),resin=queryMin(ql,qr); write(std::max(resax,-resin),"\n"); } return 0; }
|