Cod sursa(job #957307)

Utilizator mitrutstrutMitrea Andrei Ionut mitrutstrut Data 4 iunie 2013 20:06:41
Problema Arbori indexati binar Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <stdio.h>
#include <fstream>
using namespace std;
 
#define in "aib.in"
#define out "aib.out"
#define dim 100001
 
int N, M, T;
int S[dim];
 
int main()
{
    int K, X, Y, h;
    freopen(in,"r",stdin);
    freopen(out,"w",stdout);
     
    S[0] = 0;
     
    scanf("%d%d", &N, &M);
    for ( int i = 1; i <= N; i++ )
    {
        scanf("%d", &h);
        S[i] = h + S[i-1];
    }
     
    for ( ; M; M-- )
    {
        scanf("%d", &K);
         
        if ( K < 2 )
        {
             scanf("%d%d", &X, &Y);
              
             if ( K == 1 )
             {
                  printf("%d\n", S[Y] - S[X-1] );
                   
                  continue;
             }
              
             for ( int i = X; i <= N; i++ )
                 S[i] += Y;
        }
        else if ( K == 2 )
        {
             int ok = 0;
             scanf("%d", &X);
              
             int i;
             for ( i = 1; i <= N && !ok; i++ )
                 if ( S[i] == X ) ok = 1;
              
             if ( !ok ) printf("-1\n");
             else       printf("%d\n", i-1);
        }
    }
}