Cod sursa(job #337297)

Utilizator crisojogcristian ojog crisojog Data 3 august 2009 12:27:26
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <stdio.h>  
#include <fstream>  
using namespace std; 
int n,m,t,c,s;  
int arb[100010];  
 
inline int minim(int a, int b)
{
	if(a>b) return b;
	return a;
}

void updateplus(int poz, int val)  
{  
     c = 0;  
     while(poz<=n)  
     {  
           arb[poz]+=val;  
           while(!(poz&(1<<c))) c++;  
           poz+=(1<<c);  
           c+=1;  
     }  
}  
void updateminus(int poz, int val)  
{  
     c = 0;  
     while(poz<=n)  
     {  
           arb[poz]-=val;  
           while(!(poz&(1<<c))) c++;  
           poz+=(1<<c);  
           c+=1;  
     }  
}  

int query(int poz)  
{  
    c=0;
	s=0;  
    while(poz>0)  
    {  
		s+=arb[poz];  
        while(!(poz&(1<<c))) c++;  
        poz-=(1<<c);  
        c+=1;  
    }  
      
    return s;  
}  

int main()  
{  
    freopen("datorii.in","r",stdin);  
    freopen("datorii.out","w",stdout);  
    
	int cod,x,y;
	  
    scanf("%d%d", &n, &m);  
    for(int i=1;i<=n;++i)  
    {  
        scanf("%d", &t);  
        updateplus(i,t);  
    }  
      
    for ( ; m; m-- )  
    {  
        scanf("%d", &cod);  
        
        scanf("%d%d", &x, &y);  
        if (cod==0 ) updateminus(x,y);  
        else printf("%d\n", query(y)-query(x-1));  
        
       
    }  
	return 0;
}