Cod sursa(job #999974)

Utilizator bodyionitaIonita Bogdan Constantin bodyionita Data 21 septembrie 2013 19:28:34
Problema Arbori de intervale Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.65 kb
#include <cstdio>
#include <cstring>
using namespace std;
int j,a,b,arb[300000],x,poz,i,nr,n,m,nr2;
char s[1000001];
inline int interogare(int nod, int st, int dr)
{
    int m,x1,x2;
    x1=x2=0;
    if (a<=st && dr<=b) return arb[nod];
    m=(st+dr)/2;
    if (a<=m) x1=interogare(nod*2,st,m);
    if (m<b) x2=interogare(nod*2+1,m+1,dr);
    if (x1<x2) x1=x2;
    return x1;
}
inline void actualizare(int nod, int st, int dr)
{
    int m;
    if (st==poz && poz==dr) {arb[nod]=x;return;}
    m=(st+dr)/2;
    if (poz<=m) actualizare(nod*2,st,m); else actualizare(nod*2+1,m+1,dr);
    if (arb[nod*2]<arb[nod*2+1]) arb[nod]=arb[nod*2+1]; else arb[nod]=arb[nod*2];
}
int main()
{
    freopen("arbint.in","r",stdin);
    freopen("arbint.out","w",stdout);
    scanf("%d%d\n",&n,&m);
    gets(s);
    nr2=strlen(s);
    for (i=0;i<nr2;i++)
    {
        x=0;
        while (s[i]>='0' && s[i]<='9')
        {
            x=x*10+s[i]-'0';
            i++;
        }
        poz++;
        actualizare(1,1,n);
    }
    for (i=1;i<=m;i++)
    {
        gets(s);
        nr2=strlen(s);
        j=nr=a=b=0;
        while (s[j]>='0' && s[j]<='9')
        {
            nr=nr*10+s[j]-'0';
            j++;
        }
        j++;
        while (s[j]>='0' && s[j]<='9')
        {
            a=a*10+s[j]-'0';
            j++;
        }
        j++;
        while (s[j]>='0' && s[j]<='9')
        {
            b=b*10+s[j]-'0';
            j++;
        }
        if (nr==0) printf("%d\n",interogare(1,1,n));
        else
        {
            poz=a;
            x=b;
            actualizare(1,1,n);
        }
    }
    return 0;
}