Cod sursa(job #1772910)

Utilizator tiberiu.bucur17Tiberiu Constantin Emanoil Bucur tiberiu.bucur17 Data 7 octombrie 2016 10:56:19
Problema Arbori de intervale Scor 10
Compilator c Status done
Runda Arhiva educationala Marime 3.04 kb
#include <stdio.h>
#include <ctype.h>
#define BUF_SIZE 131072
#define MAXP 262144
#define MAX_N 100000
#define max(a,b) a>b? a:b
int aint[MAXP],v[MAX_N+1];
char buf[BUF_SIZE],buf2[BUF_SIZE];
FILE *fin,*fout;
int pos=BUF_SIZE,pos2,left,right,maxim,poz,n;
inline char getch()
{
    if(pos==BUF_SIZE)
    {
        fread(buf,BUF_SIZE,1,fin);
        pos=0;
    }
    return buf[pos++];
}
inline int Read()
{
    int x=0;
    char ch=getch();
    while(!isdigit(ch))
        ch=getch();
    do
    {
        x=x*10+ch-'0';
        ch=getch();
    } while(isdigit(ch));
    return x;
}
inline void putch(char ch)
{
    buf2[pos2++]=ch;
    if(pos2==BUF_SIZE)
    {
        fwrite(buf2,BUF_SIZE,1,fout);
        pos2=0;
    }
}
inline void Write(int x)
{
    char s[10];
    int k=0;
    do
    {
        s[k++]=x%10+'0';
        x/=10;
    } while(x);
    while(k--)
        putch(s[k]);
}
void dfs(int p,int st,int dr)
{
    if(st==dr)
        aint[p]=v[st];
    else
    {
        int m=(st+dr)/2;
        dfs(2*p,st,m);
        dfs(2*p+1,m+1,dr);
        aint[p]=max(aint[2*p],aint[2*p+1]);
    }
}
inline void query()
{
    int p,st,dr,m,cp,cst,cdr;
    if(left==right)
        maxim=v[left];
    else
    {
        st=p=1;dr=n;m=(st+dr)/2;
        while(right<=m || m<left)
        {
            if(right<=m)
                dr=m,p*=2;
            else
                st=m+1,p=2*p+1;
            m=(st+dr)/2;
        }
        cst=st;cp=p;cdr=dr;
        dr=m;p*=2;
        while(st<left)
        {
            m=(st+dr)/2;
            if(m<left)
                st=m+1,p=2*p+1;
            else
            {
                maxim=max(maxim,aint[2*p+1]);
                dr=m,p*=2;
            }
        }
        maxim=max(maxim,aint[p]);
        st=cst;dr=cdr;p=cp;
        st=m+1;p=2*p+1;
        while(dr>right)
        {
            m=(st+dr)/2;
            if(m>=right)
                dr=m,p*=2;
            else
            {
                maxim=max(maxim,aint[2*p]);
                st=m+1;p=2*p+1;
            }
        }
        maxim=max(maxim,aint[p]);
    }
}
inline void update()
{
    int p,st,dr,m;
    p=st=1;dr=n;
    while(st<dr)
    {
        m=(st+dr)/2;
        if(poz<=m)
            dr=m,p*=2;
        else
            st=m+1,p=2*p+1;
    }
    aint[p]=v[st];
    while(p/=2)
        aint[p]=max(aint[2*p],aint[2*p+1]);
}
int main()
{
    fin=fopen("arbint.in","r");
    fout=fopen("arbint.out","w");
    int i,m,a,b,c;
    n=Read();m=Read();
    for(i=1;i<=n;i++)
        v[i]=Read();
    dfs(1,1,n);
    for(i=0;i<m;i++)
    {
        a=Read();b=Read();c=Read();
        if(!a)
        {
            left=b;right=c;
            maxim=0;
            query();
            Write(maxim);
            putch('\n');
        }
        else
        {
            poz=b;
            v[b]=c;
            update();
        }
    }
    if(pos2)
        fwrite(buf2,pos2,1,fout);
    fclose(fin);
    fclose(fout);
    return 0;
}