Cod sursa(job #500723)

Utilizator andrey_porscheGraur Andrei andrey_porsche Data 12 noiembrie 2010 22:26:07
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
# include <cstdio>

# define verf ++poz == hg ? fread ( ch, 1, hg, stdin ), poz = 0 : 0

const char *FIN = "datorii.in", *FOU = "datorii.out" ;
const int MAX = 15005, hg = 1 << 13 ;

int Sum[MAX] ;
int N, M, poz ;

char ch[hg] ;

inline void cit ( int &x ) {
    if ( ch[0] == '\0' ) fread ( ch, 1, hg, stdin ) ;
    else for ( ; ch[poz] < '0' || ch[poz] > '9' ; verf ) ;
    for ( x = 0 ; ch[poz] >= '0' && ch[poz] <= '9' ; x = x * 10 + ch[poz] - '0', verf ) ;
}

int main ( void ) {
    freopen ( FIN, "r", stdin ) ;
    freopen ( FOU, "w", stdout ) ;

    cit ( N ) , cit ( M ) ;
    for ( int i = 1, a, b; i <= N; ++i ) {
        cit ( a ) ;
        for ( b = i; b <= N; Sum[b] += a, b += b & -b ) ;
    }

    for ( int a, b, tip ; M ; --M ) {
        cit ( tip ) , cit ( a ) , cit ( b ) ;
        if ( tip ) {
            int sol = 0 ;
            for ( ; b > 0 ; sol += Sum[b], b -= b & -b ) ;
            for ( --a ; a > 0 ; sol -= Sum[a], a -= a & -a ) ;
            printf ( "%d\n", sol ) ;
        } else {
            for ( ; a <= N; Sum[a] -= b, a += a & -a ) ;
        }
    }
}