Pagini recente » Cod sursa (job #3340001) | Cod sursa (job #3353935) | Cod sursa (job #409504) | Cod sursa (job #3340201) | Cod sursa (job #3333404)
#include <fstream>
#include <vector>
#include <algorithm>
std::ifstream in("arbint.in");
std::ofstream out("arbint.out");
using std::vector;
unsigned N, M;
struct ArbInt {
vector<unsigned> maxi;
ArbInt() {
this->maxi.resize(N << 2);
std::fill(this->maxi.begin(), this->maxi.end(), 0);
unsigned value;
for(unsigned i = 1; i <= N; i += 1) {
in >> value;
this->write(i, value);
}
}
void write(
const unsigned target,
const unsigned value,
const unsigned left = 1,
const unsigned right = N,
const unsigned idx = 1
) {
if(target == left && target == right) {
this->maxi[idx] = value;
return;
}
const unsigned middle = (left + right) / 2;
if(target <= middle) {
this->write(target, value, left, middle, idx * 2);
}
else {
this->write(target, value, middle + 1, right, idx * 2 + 1);
}
this->maxi[idx] = std::max(
this->maxi[idx * 2 + 1],
this->maxi[idx * 2]
);
}
unsigned read(
const unsigned target_left,
const unsigned target_right,
const unsigned left = 1,
const unsigned right = N,
const unsigned idx = 1
) {
if(target_left <= left && right <= target_right) {
return this->maxi[idx];
}
const unsigned middle = (left + right) / 2;
unsigned ret = 0;
if(middle >= target_left && left <= target_right) {
ret = this->read(target_left, target_right, left, middle, idx * 2);
}
if(middle <= target_right && right >= target_left) {
ret = std::max(
this->read(target_left, target_right, middle + 1, right, idx * 2 + 1),
ret
);
}
return ret;
}
};
int main()
{
in >> N >> M;
ArbInt arb_int;
unsigned t, x, y;
for(;M != 0; M -= 1) {
in >> t >> x >> y;
if (t == 0) {
out << arb_int.read(x, y) << '\n';
}
if (t == 1) {
arb_int.write(x, y);
}
}
return 0;
}