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
| #include <iostream> #include <string.h> #include <stdio.h> #define ios ios::sync_with_stdio(0),cin.tie(0); #define N 100005 #define int long long using namespace std; const int INF = 1e14;
struct Node{ int val=0,tag=0,sz=0; int rv(){ return val+tag*sz; } }seg[4*N];
int arr[N],n,m;
void build(int l,int r,int cur){ seg[cur].sz = 1; if(r<=l)return; if(r-l==1){ seg[cur].val = arr[l]; return; } int m = (l+r)/2; build(l,m,2*cur); build(m,r,2*cur+1); seg[cur].val = max(seg[2*cur].val,seg[2*cur+1].val); }
void push(int id){ seg[2*id].tag += seg[id].tag; seg[2*id+1].tag += seg[id].tag; seg[id].val = seg[id].rv(); seg[id].tag = 0; }
void modify(int cur,int l,int r,int ql,int qr,int val){ if (r<=l||ql>=r||qr<=l)return; if (ql<=l && qr>=r) { seg[cur].tag += val; return; } int mid = (l+r)/2; modify(cur*2,l,mid,ql,qr,val); modify(cur*2+1,mid,r,ql,qr,val); seg[cur].val = max(seg[2*cur].rv(),seg[2*cur+1].rv()); }
int query(int cur,int l,int r,int ql,int qr){ if(r<=l || ql>=r || qr<=l)return -INF; if(ql<=l && qr>=r)return seg[cur].rv(); push(cur); int mid = (l+r)/2; return max(query(cur*2,l,mid,ql,qr),query(cur*2+1,mid,r,ql,qr)); }
signed main(){ ios; cin>>n>>m; for(int i=1;i<=n;i++)cin>>arr[i]; build(1,n+1,1); while(m--){ int p;cin>>p; if(p==1){ int x,y,k;cin>>x>>y>>k; modify(1,1,n+1,x,y+1,k); } else if(p==2){ int x,y;cin>>x>>y; int ans = query(1,1,n+1,x,y+1); cout<<ans<<endl; } } }
|