-
Notifications
You must be signed in to change notification settings - Fork 0
/
KGSS.cpp
74 lines (72 loc) · 1.98 KB
/
KGSS.cpp
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
#include<bits/stdc++.h>
using namespace std;
long long a[100003];
pair<long long ,long long> tree[400003];
void create(long long ss,long long se,long long node)
{
if(ss==se)
{
tree[node].first=a[ss];
tree[node].second=-1;
}
else
{
create(ss,(ss+se)/2,2*node);
create((ss+se)/2+1,se,2*node+1);
tree[node].first=max(tree[2*node].first,tree[2*node+1].first);
tree[node].second=max(min(tree[2*node].first,tree[2*node+1].first),max(tree[2*node].second,tree[2*node+1].second));
}
}
pair<long long,long long> querry(long long ss,long long se,long long node,long long beg,long long end)
{
if(ss>end or se<beg)
return make_pair(-1,-1);
if(ss>=beg and se<=end)
return tree[node];
pair<long long,long long> a=querry(ss,(ss+se)/2,2*node,beg,end);
pair<long long,long long> b=querry((ss+se)/2+1,se,2*node+1,beg,end);
pair<long long,long long> c;
c.first=max(a.first,b.first);
c.second=max(min(a.first,b.first),max(a.second,b.second));
return c;
}
void update(long long ss,long long se,long long node,long long i,long long x)
{
if(i<ss or i>se)
return ;
if(ss==se)
{
tree[node].first=x;
return ;
}
update(ss,(ss+se)/2,2*node,i,x);
update((ss+se)/2+1,se,2*node+1,i,x);
tree[node].first=max(tree[2*node].first,tree[2*node+1].first);
tree[node].second=max(min(tree[2*node].first,tree[2*node+1].first),max(tree[2*node].second,tree[2*node+1].second));
}
int main()
{
long long n;
scanf("%lld",&n);
for(long long i=1;i<=n;++i)
scanf("%lld",&a[i]);
create(1,n,1);
long long q;
scanf("%lld",&q);
char ch;
long long x,y;
pair<long long,long long> ans;
while(q--)
{
cin>>ch;
scanf("%lld %lld",&x,&y);
if(ch=='U')
update(1,n,1,x,y);
else
{
ans=querry(1,n,1,x,y);
printf("%lld\n",ans.first+ans.second);
}
}
return 0;
}