|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +/*Structure that is used to |
| 5 | +store value*/ |
| 6 | +struct node |
| 7 | +{ |
| 8 | + int sum,prefixsum; |
| 9 | + int suffixsum,maxsum; |
| 10 | +}; |
| 11 | + |
| 12 | +node tree[4*50010]; |
| 13 | +int arr[50010]; |
| 14 | + |
| 15 | +/*This function is used to build the |
| 16 | +segment tree*/ |
| 17 | +void build(int index,int start,int end) |
| 18 | +{ |
| 19 | + if(start == end) |
| 20 | + { |
| 21 | + tree[index].sum = arr[start]; |
| 22 | + tree[index].prefixsum = arr[start]; |
| 23 | + tree[index].suffixsum = arr[start]; |
| 24 | + tree[index].maxsum = arr[start]; |
| 25 | + } |
| 26 | + else |
| 27 | + { |
| 28 | + int mid = (start+end)/2; |
| 29 | + build(2*index+1,start,mid); |
| 30 | + build(2*index+2,mid+1,end); |
| 31 | + |
| 32 | + /*Note how we use the array to access the left |
| 33 | + and right subtree*/ |
| 34 | + tree[index].sum = |
| 35 | + tree[2*index+1].sum + tree[2*index+2].sum; |
| 36 | + tree[index].prefixsum = |
| 37 | + max(tree[2*index+1].prefixsum, |
| 38 | + tree[2*index+1].sum + tree[2*index+2].prefixsum); |
| 39 | + tree[index].suffixsum = |
| 40 | + max(tree[2*index+2].suffixsum, |
| 41 | + tree[2*index+2].sum + tree[2*index+1].suffixsum); |
| 42 | + tree[index].maxsum = |
| 43 | + max(tree[index].prefixsum, |
| 44 | + max(tree[index].suffixsum, |
| 45 | + max(tree[2*index+1].maxsum, |
| 46 | + max(tree[2*index+2].maxsum, |
| 47 | + tree[2*index+1].suffixsum |
| 48 | + +tree[2*index+2].prefixsum |
| 49 | + ) |
| 50 | + ) |
| 51 | + ) |
| 52 | + ); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/*this function is used to return the result |
| 57 | +for each query*/ |
| 58 | +node query(int index,int start,int end,int l,int r) |
| 59 | +{ |
| 60 | + node result; |
| 61 | + result.sum=result.prefixsum=INT_MIN; |
| 62 | + result.suffixsum=result.maxsum=INT_MIN; |
| 63 | + |
| 64 | + if(r<start || end<l) |
| 65 | + return result; |
| 66 | + if(l<=start && end<=r) |
| 67 | + return tree[index]; |
| 68 | + |
| 69 | + int mid = (start+end)/2; |
| 70 | + if (l > mid) |
| 71 | + return query(2*index+2, mid+1, end, l, r); |
| 72 | + if (r <= mid) |
| 73 | + return query(2*index+1, start, mid, l, r); |
| 74 | + |
| 75 | + node left = query(2*index+1,start,mid,l,r); |
| 76 | + node right = query(2*index+2,mid+1,end,l,r); |
| 77 | + |
| 78 | + result.sum = left.sum + right.sum; |
| 79 | + result.prefixsum = |
| 80 | + max(left.prefixsum, left.sum + right.prefixsum); |
| 81 | + result.suffixsum = |
| 82 | + max(right.suffixsum, right.sum + left.suffixsum); |
| 83 | + result.maxsum = |
| 84 | + max(result.prefixsum, |
| 85 | + max(result.suffixsum, |
| 86 | + max(left.maxsum, |
| 87 | + max(right.maxsum, |
| 88 | + left.suffixsum + right.prefixsum)))); |
| 89 | + return result; |
| 90 | +} |
| 91 | + |
| 92 | +int main() |
| 93 | +{ |
| 94 | + int n,m,a,b; |
| 95 | + scanf("%d",&n); |
| 96 | + |
| 97 | + for(int i=0; i<n; ++i) |
| 98 | + scanf("%d",&arr[i]); |
| 99 | + |
| 100 | + build(0,0,n-1); |
| 101 | + |
| 102 | + scanf("%d",&m); |
| 103 | + |
| 104 | + for(int i=0; i<m; ++i) |
| 105 | + { |
| 106 | + scanf("%d%d",&a,&b); |
| 107 | + printf("%d\n",query(0,0,n-1,a-1,b-1).maxsum); |
| 108 | + } |
| 109 | + return 0; |
| 110 | +} |
0 commit comments