Cod sursa(job #1438344)

Utilizator andrei1998xAndrei Ionut andrei1998x Data 19 mai 2015 18:11:13
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.14 kb
#include <fstream>
using namespace std;
ifstream fin("arbint.in");
ofstream fout("arbint.out");

int a[400010],v[100001],n,m,i,maxim,t,x,y;
void build(int nod, int st, int dr){
    //construieste arborele de intervale ce reprezinta vectorul initial
    if (st==dr)
        a[nod]=v[st];
    else
    {
        int mid=(st+dr)/2;
        build(2*nod, st, mid);
        build(2*nod+1, mid+1, dr);
        a[nod]=max(a[2*nod],a[2*nod+1]);
    }
}
void query(int nod, int st, int dr, int x, int y){
    if (x<=st && dr<=y)
        maxim=max(maxim, a[nod]);
    else{
        int mid=(st+dr)/2;
        if (x<=mid)
            query(2*nod, st, mid, x, y);
        if (y>mid)
            query(2*nod+1, mid+1, dr, x, y);
    }
}
void update(int nod,int st,int dr,int x,int y)
{
    if(st==dr)
        a[nod]=y;
    else{
        int mid=(st+dr)/2;
        if (x<=mid)
            update(2*nod, st, mid, x, y);
        if (x>mid)
            update(2*nod+1, mid+1, dr, x, y);
        a[nod]=max(a[2*nod], a[2*nod+1]);
    }
}
int main()
{
    fin>>n>>m;
    for (i=1;i<=n;i++)
        fin>>v[i];
    build(1,1,n);
    for (i=1;i<=m;i++){
        fin>>t>>x>>y;
        if (t==0)
        {
            maxim=0;
            query(1,1,n,x,y);
            fout<<maxim<<"\n";
        }
        else
            update(1,1,n,x,y);//elem de pe poz x devine y
    }

    return 0;
}
// daca operatia de update se face pe un interval, toate elem intrerv x,y devin a, problema se poate rezolva cu arbori de intervale, insa folosind si procedeul Lazy Update.
//Lucram urmatoarea problema cu update pe interval:
//Am un vector, initial null, urmeaza numai update-uri de forma (x,y,a) ce inseamna
//ca toate elem de la poz x la poz y cresc cu a, iar apoi numai interogari de forma
//p: cat este elementul de pe poz p
// ex: 10
// 2 7 2
// 3 9 4
// 5 7 3
// 1
// 3
// 5
// 7
/*
    fin>>u;
    for (i=1;i<=n;i++){
        fin>>x>>y>>a;
        v[x]=a;v[y+1]=a;
    //pe vectorul construit fac sume partiale
    }
    for (i=2;i<=n;i++)
        v[i]=v[i-1];
    for (i=1;i<=q;i++){
        fin>>p;
        fout<<v[p];
    }
*/