Cod sursa(job #1510820)

Utilizator serbanSlincu Serban serban Data 25 octombrie 2015 17:23:05
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.8 kb
#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;
    bool ok = true;
    while(where && ok) {
        int aux = where->mx;
        where->mx = max(where->ls->mx, where->rs->mx);
        if(aux == where->mx)
            ok = false;
        where = where->f;
    }
}

int caut(int a, int b, val *where) {
    int mx = 0;
    if(a <= where->st && where->dr <= b) {
        return where->mx;
    }
    else {
        int mij = (where->dr - where->st) / 2 + where->st;
        if(mij >= a) {
            mx = caut(a, b, where->ls);
        }
        if(mij < b) {
            mx = max(mx, caut(a, b, where->rs));
        }
        return mx;
    }
}

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;
}