Cod sursa(job #2611890)

Utilizator andreic06Andrei Calota andreic06 Data 7 mai 2020 19:29:02
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <iostream>
#include <cstdio>

using namespace std;
const int N = 15 * 1e3;
const int dim = 1 << 17;

int barbati[N+5], f[N+1];
int AIB[N+1];

static inline int baieti ( int x ) {
      return x & ( -x );
}

void update ( int poz, int q, int n ) {
    for ( int i = poz; i <= n; i += baieti( i ) )
       AIB[i] += q;
}

int q ( int poz ) {
   int s = 0;
   for ( int i = poz; i > 0; i -= baieti( i ) )
      s += AIB[i];
   return s;
}

char next_ch () {
    static int bp = dim;
    static char buff[dim];

    if ( bp == dim ){
      bp = 0;
      fread ( buff, 1, dim, stdin );
    }
    return buff[bp++];
}
int get () {
   int res = 0;
   char ch;

   while ( !isdigit ( ch = next_ch() ) );

   do
     res = res * 10 + ch - '0';
   while ( isdigit ( ch = next_ch() ) );

   return res;

}

int main()
{
   freopen ( "datorii.in", "r", stdin );
   freopen ( "datorii.out", "w", stdout );
   ios::sync_with_stdio (false);
   cin.tie (), cout.tie ();

   int n, t, CASE;

   n = get () ,t = get ();
   for ( int i = 1; i <= n; i ++ )
      barbati[i] = get (), f[i] = f[i-1] + barbati[i];

   for ( int i = 1; i <= n; i ++ )
      AIB[i] = f[i] - f[i-baieti(i)];

   for ( int i = 1; i <= t; i ++ ) {
      CASE = get ();

      if ( CASE == 0 ) {
        int x, poz;
        poz = get (), x = get ();
        update ( poz, -x, n );
      }
      else {
        int st, dr;
        st = get (), dr = get ();
        cout << q ( dr ) - q ( st - 1 ) << '\n';
      }
   }
    return 0;
}