Cod sursa(job #227983)

Utilizator vlad_DVlad Dumitriu vlad_D Data 6 decembrie 2008 04:05:34
Problema Arbori de intervale Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include <cstdio>

using namespace std;

int n, m;
int tree[500007];
void update(int nod, int a, int b, int pos, int val);
int get_max(int nod, int a, int b, int pa, int pb);
int main() {
    freopen("arbint.in", "r", stdin);
    freopen("arbint.out", "w", stdout);
    
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        int val;
        scanf("%d", &val);
        update(1, 1, n, i, val);                
        }    
    while (m--) {
          int cod, a, b;
          scanf("%d%d%d", &cod, &a, &b);
          if (cod == 0) {printf("%d\n", get_max(1, 1, n, a, b));}
          if (cod == 1) {update(1, 1, n, a, b);}
          }
    return 0;
    }
int get_max(int nod, int a, int b, int pa, int pb) {
    if (pb < a || pa > b || b < a || pb < pa) return -1;
    if (pa <= a && pb >= b) return tree[nod];
    int mid = a + b; mid /= 2;
    int A = get_max(2*nod, a, mid, pa, pb);
    int B = get_max(2*nod + 1, mid + 1, b, pa, pb);
    if (A > B) return A;
    return B;     
    }
void update(int nod, int a, int b, int pos, int val) {
     if (pos < a || pos > b || a > b) return;
     if (a == pos && pos == b) {tree[nod] = val; return;}
     tree[nod] = -1;
     int mid = a + b; mid /= 2;
     update(2 * nod, a, mid, pos, val);
     update(2 * nod + 1, mid + 1, b, pos, val);
     tree[nod] = tree[2 * nod];
     if (tree[2 * nod+1] > tree[nod]) tree[nod] = tree[2 * nod + 1];
     }