Pagini recente » Cod sursa (job #3175969) | Cod sursa (job #1279170) | Cod sursa (job #2082040) | Cod sursa (job #1761696) | Cod sursa (job #2215656)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
vector <int> ST;
int N, M;
void Build() {
for (int node = N - 1; node; --node)
ST[node] = max(ST[node << 1], ST[(node << 1) | 1]);
}
inline void Update(const int &idx, const int &val) {
int node = idx + N - 1;
ST[node] = val;
for(node >>= 1; node; node >>= 1)
ST[node] = max(ST[node << 1], ST[(node << 1) | 1]);
}
inline int Query(const int &lft, const int &rgt) {
int lftNode = lft + N - 1;
int rgtNode = rgt + N - 1;
int ans = 0;
while (lftNode <= rgtNode) {
ans = max(ans, max(ST[lftNode], ST[rgtNode]));
lftNode = (lftNode + 1) >> 1; // try to go to the next branch
rgtNode = (rgtNode - 1) >> 1; // try to go to the prev branch
}
return ans;
}
int main() {
fin >> N >> M;
ST.resize(2 * N);
for (int idx = 0; idx < N; ++idx)
fin >> ST[idx + N];
Build();
for (; M; --M) {
int code, a, b;
fin >> code >> a >> b;
if (code == 0)
fout << Query(a, b) << '\n';
else
Update(a, b);
}
}