-
Notifications
You must be signed in to change notification settings - Fork 0
/
Can you answer these queries III.txt
112 lines (112 loc) · 2.92 KB
/
Can you answer these queries III.txt
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
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
struct nod{
int psum,ssum,sum,msum;
};
nod tree[8*60000];int ar[60000];
void build(int index,int start,int end)
{
if(start==end)
{
tree[index].psum=ar[start];
tree[index].ssum=ar[start];
tree[index].sum=ar[start];
tree[index].msum=ar[start];
}
else
{
int mid=(start+end)/2;
build(2*index+1,start,mid);
build(2*index+2,mid+1,end);
tree[index].sum=tree[2*index+1].sum+tree[2*index+2].sum;
tree[index].psum=max(tree[2*index+1].psum,tree[2*index+1].sum+tree[2*index+2].psum);
tree[index].ssum=max(tree[2*index+2].ssum,tree[2*index+2].sum+tree[2*index+1].ssum);
tree[index].msum=max(tree[2*index+1].msum,max(tree[2*index+2].msum,max(tree[index].ssum,max(tree[index].psum,tree[2*index+1].ssum+tree[2*index+2].psum))));
}
}
nod query(int index,int start,int end,int l,int r)
{
nod result;
result.ssum=result.psum=result.sum=result.msum=INT_MIN;
if(r<start||l>end)
{
return result;
}
if(l<=start&&end<=r)
{
return tree[index];
}
int mid=(start+end)/2;
if(l>mid)
{
return query(2*index+2,mid+1,end,l,r);
}
if(r<=mid)
{
return query(2*index+1,start,mid,l,r);
}
nod left=query(2*index+1,start,mid,l,r);
nod right=query(2*index+2,mid+1,end,l,r);
result.sum=left.sum+right.sum;
result.psum=max(left.psum,left.sum+right.psum);
result.ssum=max(right.ssum,right.sum+left.ssum);
result.msum =
max(result.psum,
max(result.ssum,
max(left.msum,
max(right.msum,
left.ssum + right.psum))));
return result;
}
void update(int index,int start,int end,int idx,int value)
{
if(start==end)
{
tree[index].psum=value;
tree[index].ssum=value;
tree[index].sum=value;
tree[index].msum=value;
}
else
{
int mid=(start+end)/2;
if(idx<=mid)
{
update(2*index+1,start,mid,idx,value);
}
else
{
update(2*index+2,mid+1,end,idx,value);
}
tree[index].sum=tree[2*index+1].sum+tree[2*index+2].sum;
tree[index].psum=max(tree[2*index+1].psum,tree[2*index+1].sum+tree[2*index+2].psum);
tree[index].ssum=max(tree[2*index+2].ssum,tree[2*index+2].sum+tree[2*index+1].ssum);
tree[index].msum=max(tree[2*index+1].msum,max(tree[2*index+2].msum,max(tree[index].ssum,max(tree[index].psum,tree[2*index+1].ssum+tree[2*index+2].psum))));
}
}
int main()
{
//cout << "Hello world!" << endl;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t,i,j,k,m,n;
cin>>n;
for(i=0;i<n;i++)
{
cin>>ar[i];
}
build(0,0,n-1);
cin>>m;
while(m--)
{
cin>>k>>i>>j;
if(k)
{
cout<<query(0,0,n-1,i-1,j-1).msum<<"\n";
}
else
update(0,0,n-1,i-1,j);
}
return 0;
}