Cod sursa(job #254814)

Utilizator bodyionitaIonita Bogdan Constantin bodyionita Data 7 februarie 2009 17:45:56
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.18 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;    
   }