Cod sursa(job #2816373)

Utilizator andrei_marciucMarciuc Andrei andrei_marciuc Data 11 decembrie 2021 12:16:14
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.84 kb
#include <stdio.h>
 
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
 
static inline int min( int a, int b ) {
    return ( a <= b ? a : b );
}
 
static inline int max( int a, int b ) {
    return ( a >= b ? a : b );
}
 
FILE *fin;
 
int poz, valBuff;
static char buff[ ( 1 << 7 ) ];
static inline char nextChar() {
    if( poz == valBuff ) {
        fread( buff, 1, valBuff, fin );
        poz = 0;
    }
 
    return buff[ poz++ ];
}
 
static bool f[ 100 ];
static inline int readInt() {
    int rez = 0;
    int ch;
 
    while( !f[ ch = nextChar() ] );
    do
        rez = rez * 10 + ch - '0';
    while( f[ ch = nextChar() ] );
 
    return rez;
}
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define val( a ) ( ( a ^ ( a - 1 ) ) & a )    

int aib[ 15005 ];
int n;

static inline void update( int x, int val ) {
    while( x <= n ) {
        aib[ x ] += val;
        x += val( x );
    }
}

static inline int ans( int x ) {
    int rez = 0;
    while( x > 0 ) {
        rez += aib[ x ];
        x -= val( x );
    }

    return rez;
}

int main() 
{   
    f[ '0' ] = f[ '1' ] = f[ '2' ] = f[ '3' ] = f[ '4' ] = 1;
    f[ '6' ] = f[ '7' ] = f[ '8' ] = f[ '9' ] = f[ '5' ] = 1;
    valBuff = sizeof( buff );
 
    fin = fopen( "datorii.in", "r" );
    
    fread( buff, 1, valBuff, fin );
    
    n = readInt();
    int q = readInt();
    for( int i = 1; i <= n; i++ )
        update( i, readInt() );
    
    int cerinta, x, y;
    FILE *fout = fopen( "datorii.out", "w" );
    while( q-- ) {
        cerinta = readInt();
        x = readInt();
        y = readInt();

        if( cerinta == 0 )
            update( x, -y );
        else fprintf( fout, "%d\n", ans( y ) - ans( x - 1 ) );
    }
 
    fclose( fin );
    fclose( fout );
    return 0;
}