Cod sursa(job #253136)

Utilizator 630r63Ilinca George Mihai 630r63 Data 5 februarie 2009 14:41:48
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.02 kb
   #include <fstream>  
   #define lg_max 100000  
   using namespace std;  
   ifstream fin ("datorii.in");  
   ofstream fout ("datorii.out");  
   int heap[(lg_max<<1)<<1];  
   int n,m,val,poz;  
   int sir[lg_max];  
   int S=0;  
   void build(int nod,int st,int dr)  
   {  
        if (st==dr)  
        {  
             heap[nod]=sir[st];  
             return ;  
        }  
        int mij=(st+dr)>>1;  
        int stg=(nod<<1);  
        int dre=(nod<<1)+1;  
        build(stg,st,mij);  
        build(dre,mij+1,dr);  
        heap[nod]=heap[stg]+heap[dre];  
   }       
   void update(int nod,int st,int dr)  
   {  
        if (st==dr)  
        {  
             heap[nod]-=poz;  
             return ;  
        }  
        int mij=(st+dr)>>1;  
        int stg=(nod<<1);  
        int dre=(nod<<1)+1;  
        if (val<=mij)  
             update(stg,st,mij);  
        else  
             update(dre,mij+1,dr);  
        heap[nod]=heap[stg]+heap[dre];  
   }       
   void citire()  
   {  
        fin>>n>>m;  
        for (int i=1;i<=n;i++)  
             fin>>sir[i];  
             build(1,1,n);  
   }  
   void suma(int nod,int st,int dr)  
   {  
        if (val<=st && poz>=dr)  
        {  
             S+=heap[nod];  
             return ;  
        }  
        int mij=(st+dr)>>1;  
        int stg=(nod<<1);  
        int dre=(nod<<1)+1;  
        if (val<=mij)  
             suma(stg,st,mij);  
        if (poz>mij)  
             suma(dre,mij+1,dr);  
   }       
   void afisare()  
   {  
        int ok;  
        for (int i=0;i<m;i++)  
        {  
             fin>>ok>>val>>poz;  
             if (ok==0)  
             {  
                  update(1,1,n);  
             }  
             else  
             {  
                  S=0;  
                  suma(1,1,n);  
                  fout<<S<<"\n";  
             }  
        }  
   }       
   int main ()  
   {  
        citire();  
        afisare();  
        return 0;  
   }