-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIHateIt_1754.cpp
124 lines (99 loc) · 2.44 KB
/
IHateIt_1754.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
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
113
114
115
116
117
118
119
120
121
122
123
124
#include <bits/stdc++.h>
using namespace std;
ifstream fin("IHateIt_1754.in");
ofstream fout("IHateIt_1754.out");
class SegTree
{
private:
int sizeA;
vector<int> a;
vector<int> segTree;
void build(int l, int r, int i)
{
if (l == r)
{
segTree[i] = a[l];
return;
}
int m = (l + r) / 2;
build(l, m, i * 2);
build(m + 1, r, i * 2 + 1);
segTree[i] = max(segTree[i * 2], segTree[i * 2 + 1]);
}
int query(int l, int r, int i, int nowLeft, int nowRight)
{
if (nowRight < l || r < nowLeft)
{
return INT_MIN;
}
if (nowLeft >= l && nowRight <= r)
{
return segTree[i];
}
int m = (nowLeft + nowRight) / 2;
return max(query(l, r, i * 2, nowLeft, m), query(l, r, i * 2 + 1, m + 1, nowRight));
}
void update(int pos, int _new, int i, int nowLeft, int nowRight)
{
if (pos < nowLeft || pos > nowRight)
{
return;
}
if (nowLeft == nowRight)
{
segTree[i] = _new;
return;
}
int m = (nowLeft + nowRight) / 2;
update(pos, _new, i * 2, nowLeft, m);
update(pos, _new, i * 2 + 1, m + 1, nowRight);
segTree[i] = max(segTree[i * 2],segTree[i * 2+1]);
}
public:
SegTree(const vector<int> & p_Sour)
{
a = p_Sour;
sizeA = a.size();
segTree.assign(sizeA * 4, INT_MIN);
build(0, sizeA - 1, 1);
}
int query(int l, int r)
{
return query(l, r, 1, 0, sizeA - 1);
}
void update(int index, int newVal)
{
update(index, newVal, 1, 0, sizeA - 1);
}
};
int main()
{
while (true)
{
int n = 0, m = 0;
fin >> n >> m;
if (n + m == 0)
break;
vector<int> a(n);
for (int i = 0; i <= n - 1; ++i)
{
fin >> a[i];
}
SegTree tree(a);
for (int c = 1; c <= m; ++c)
{
char ch;
int _i, _j;
fin >> ch >> _i >> _j;
if (ch == 'Q')
{
fout << tree.query(_i - 1, _j - 1) << '\n';
}
else
{
tree.update(_i - 1, _j);
}
}
}
return 0;
}