forked from pranavanurag/SPOJSolutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKGSS.cpp
More file actions
87 lines (78 loc) · 1.59 KB
/
Copy pathKGSS.cpp
File metadata and controls
87 lines (78 loc) · 1.59 KB
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
#include <stdio.h>
#include <algorithm>
using namespace std;
struct Node
{
int Max, Max2;
Node() {Max = 0, Max2 = 0;}
};
Node SegTree[400001];
int A[100001], N, Q;
void Merge(Node& Ans, Node &Left, Node &Right)
{
int Temp[4] = {Left.Max, Left.Max2, Right.Max, Right.Max2};
sort(Temp, Temp + 4);
Ans.Max = Temp[3];
Ans.Max2 = Temp[2];
}
void Build(int i, int STx1, int STx2)
{
if (STx1 > STx2)
return;
if (STx1 == STx2)
SegTree[i].Max = A[STx1];
else
{
int STxm = (STx1 + STx2)/2;
Build(2*i, STx1, STxm);
Build(2*i + 1, STxm + 1, STx2);
Merge(SegTree[i], SegTree[2*i], SegTree[2*i + 1]);
}
}
void Update(int i, int STx1, int STx2, int x, int V)
{
if (STx1 > STx2 || STx1 > x || STx2 < x)
return;
if (STx1 == STx2)
SegTree[i].Max = V;
else
{
int STxm = (STx1 + STx2)/2;
Update(2*i, STx1, STxm, x, V);
Update(2*i + 1, STxm + 1, STx2, x, V);
Merge(SegTree[i], SegTree[2*i], SegTree[2*i + 1]);
}
}
Node Query(int i, int STx1, int STx2, int x1, int x2)
{
if (STx1 > STx2 || x1 > x2 || STx1 > x2 || STx2 < x1)
return Node();
if (STx1 >= x1 && STx2 <= x2)
return SegTree[i];
int STxm = (STx1 + STx2)/2;
Node Ans, Left = Query(2*i, STx1, STxm, x1, x2), Right = Query(2*i + 1, STxm + 1, STx2, x1, x2);
Merge(Ans, Left, Right);
return Ans;
}
int main()
{
scanf("%d", &N);
for (int i = 1; i <= N; i++)
scanf("%d", &A[i]);
Build(1, 1, N);
scanf("%d", &Q);
while (Q--)
{
int L, R;
char P;
scanf(" %c %d %d", &P, &L, &R);
if (P == 'U')
Update(1, 1, N, L, R);
else
{
Node Ans = Query(1, 1, N, L, R);
printf("%d\n", Ans.Max + Ans.Max2);
}
}
return 0;
}