Pagini recente » Cod sursa (job #424706) | Cod sursa (job #2735960) | Cod sursa (job #3284439) | Cod sursa (job #1018804) | Cod sursa (job #2304569)
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");
int arb[200001];
int A[100001];
int p[100001];
int N;
void build(int a, int b, int pos) {
if(a == b) {
arb[pos] = A[a];
p[a] = pos;
}
else if(a < b)
{
build(a, (a+b)/2, pos * 2);
build((a+b)/2+1, b, pos * 2 + 1);
arb[pos] = (arb[pos*2] > arb[pos*2+1] ? arb[pos*2] : arb[pos*2+1]);
}
}
void update(int pos,int a = 0) {
//arb[pos] = value;
if(pos == 0)
return;
if(pos * 2 <= N *2 -1)
arb[pos] = (arb[pos*2] > arb[pos*2+1] ? arb[pos*2] : arb[pos*2+1]);
else
arb[pos] = A[a];
update(pos/2);
}
void show() {
for(int i=1; i<=N; i++)
fout << A[i] << ' ';
fout << '\n';
for(int i=1; i <= 2*N - 1; i++)
fout << arb[i] << ' ';
fout << '\n';
}
int max_(int a, int b) {
return (a > b ? a : b);
}
int max_of(int st, int dr, int a, int b, int pos) {
if(pos >= 2*N)
return 0;
if(a <= st && dr <= b)
return arb[pos];
int left_max = 0;
if(a <= (st+dr)/2)
left_max = max_of(st, (st+dr)/2, a, b, pos*2);
int right_max = 0;
if(b > (st+dr)/2)
right_max = max_of((st+dr)/2+1, dr, a, b, pos*2+1);
return max_(left_max, right_max);
}
int main()
{
int M;
fin >> N; fin >> M;
for (int i=1; i<=N; i++) {
fin >> A[i];
}
build(1, N, 1);
//show();
for(; M; M--) {
int cod, a , b;
fin >> cod >> a >> b;
if(cod == 0) {
fout << max_of(1, N, a, b, 1) <<'\n';
}
else {
A[a] = b;
update(p[a], a);
//show();
}
}
fin.close();
fout.close();
return 0;
}