Pagini recente » Monitorul de evaluare | Cod sursa (job #2251051) | Cod sursa (job #1412365) | Cod sursa (job #2890114) | Cod sursa (job #1510799)
#include <bits/stdc++.h>
using namespace std;
struct val {
int st, dr;
int mx = 0;
val *ls = NULL, *rs = NULL, *f = NULL;
};
val *p[100005];
void fac(int st, int dr, val *&where) {
where = new val;
where->st = st;
where->dr = dr;
if(dr - st > 0) {
int mij = (dr - st) / 2 + st;
fac(st, mij, where->ls);
where->ls->f = where;
fac(mij + 1, dr, where->rs);
where->rs->f = where;
}
else {
p[st] = where;
}
}
void urc(int poz) {
val *where = p[poz]->f;
while(where) {
where->mx = max(where->ls->mx, where->rs->mx);
where = where->f;
}
}
int caut(int a, int b, val *where) {
int mx = 0;
if(where->st >= a && where->dr <= b) {
return where->mx;
}
else if(where->st != where->dr) {
mx = caut(a, b, where->ls);
return max(mx, caut(a, b, where->rs));
}
else return 0;
}
int main()
{
FILE *f = fopen("arbint.in", "r");
FILE *g = fopen("arbint.out", "w");
int n, m;
fscanf(f, "%d %d", &n, &m);
val *start;
fac(1, n, start);
int x;
for(int i = 1; i <= n; i ++) {
fscanf(f, "%d", &x);
p[i]->mx = x;
}
for(int i = 1; i <= n; i ++) {
urc(i);
}
int tip, a, b;
for(int i = 1; i <= m; i ++) {
fscanf(f, "%d %d %d", &tip, &a, &b);
if(tip) {
p[a]->mx = b;
urc(a);
}
else {
fprintf(g, "%d\n", caut(a, b, start));
}
}
return 0;
}