Cod sursa(job #1774679)

Utilizator tiberiu.bucur17Tiberiu Constantin Emanoil Bucur tiberiu.bucur17 Data 9 octombrie 2016 12:15:28
Problema Datorii Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 2.27 kb
#include <stdio.h>
#include <ctype.h>
#define MAXN 15001
#define BUF_SIZE 131072
FILE *fin,*fout;
char buf[BUF_SIZE],buf2[BUF_SIZE];
int pos=BUF_SIZE,pos2,arb[MAXN],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();
    while(isdigit(ch))
    {
        x=x*10+ch-'0';
        ch=getch();
    }
    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]);
}
inline void insert(int x,int poz)
{
    int st,dr,p,m;
    st=p=1;dr=n;
    while(st<dr)
    {
        arb[p]+=x;
        m=(st+dr)/2;p*=2;
        if(poz<=m)
            dr=m;
        else
            st=m+1,++p;
    }
    arb[p]+=x;
}
inline int suma(int left,int right)
{
    int p,s,st,dr,m,cst,cdr,cp;
    p=st=1;s=0;dr=n;m=(st+dr)/2;
    while(right<=m || m<left)
    {
        p*=2;
        if(right<=m)
            dr=m;
        else
            st=m+1,++p;
        m=(st+dr)/2;
    }
    cst=st;cdr=dr;cp=p;
    dr=m;p*=2;
    while(st<left)
    {
        m=(st+dr)/2;
        if(m<left)
            st=m+1,p=2*p+1;
        else
        {
            dr=m;
            s+=arb[2*p+1];p*=2;
        }
    }
    s+=arb[p];
    st=cst;dr=cdr;p=cp;
    m=(st+dr)/2;st=m+1;p=2*p+1;
    while(dr>right)
    {
        m=(st+dr)/2;
        if(m>=right)
            dr=m,p*=2;
        else
        {
            st=m+1;
            s+=arb[2*p];p=2*p+1;
        }
    }
    return s+arb[p];
}
int main()
{
    fin=fopen("datorii.in","r");
    fout=fopen("datorii.out","w");
    int m,x,p,q,i;
    n=read();m=read();
    for(i=1;i<=n;i++)
    {
        x=read();
        insert(x,i);
    }
    for(i=0;i<m;i++)
    {
        x=read();p=read();q=read();
        if(x)
            write(suma(p,q)),putch('\n');
        else
            insert(-q,p);
    }
    if(pos2)
        fwrite(buf2,pos2,1,fout);
    fclose(fin);
    fclose(fout);
    return 0;
}