Cod sursa(job #2988187)

Utilizator DomnulMilandruMilandru Nicon-David DomnulMilandru Data 3 martie 2023 19:18:04
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.68 kb

#include <fstream>
#include <vector>
using namespace std;
ifstream fin("datorii.in");
class OutParser
{
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch)
    {
        if(sp==50000)
        {
            fwrite(buff,1,50000,fout);
            sp=0;
            buff[sp++]=ch;
        }
        else
        {
            buff[sp++]=ch;
        }
    }

public:
    OutParser(const char* name)
    {
        fout=fopen(name,"w");
        buff=new char[50000]();
        sp=0;
    }
    ~OutParser()
    {
        fwrite(buff,1,sp,fout);
        fclose(fout);
    }

    OutParser& operator <<(int vu32)
    {
        if(vu32<=9)
        {
            write_ch(vu32+'0');
        }
        else
        {
            (*this) <<(vu32/10);
            write_ch(vu32%10+'0');
        }
        return *this;
    }

    OutParser& operator <<(long long vu64)
    {
        if(vu64<=9)
        {
            write_ch(vu64+'0');
        }
        else
        {
            (*this) <<(vu64/10);
            write_ch(vu64%10+'0');
        }
        return *this;
    }

    OutParser& operator <<(char ch)
    {
        write_ch(ch);
        return *this;
    }
    OutParser& operator <<(const char *ch)
    {
        while(*ch)
        {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};
OutParser fout("datorii.out");
vector<int> A;
vector<int> Arbore;
void Make(int st,int dr,int poz)
{
    if(st==dr)
     Arbore[poz]=A[st];
    else
      {
          int mij=st+(dr-st)/2;
          int p=(poz<<1);
          Make(st,mij,p);
          Make(mij+1,dr,p+1);
          Arbore[poz]=Arbore[p]+Arbore[p+1];
      }
}
void Change(int st,int dr,int place,int val,int poz)
{
    if(st==dr)
      Arbore[poz]=Arbore[poz]-val;
    else
      {
          int mij=st+(dr-st)/2;
          int p=(poz<<1);
          if(place<=mij)
            Change(st,mij,place,val,p);
           else
             Change(mij+1,dr,place,val,p+1);
        Arbore[poz]=Arbore[poz]-val;
      }
}
int Maxim(int st,int dr,int poz,int a,int b)
{
    if(st>=a && dr<=b)
       return Arbore[poz];
    if(dr<a || st>b)
       return 0;
    int mij=st+(dr-st)/2;
    int p=(poz<<1);
    return Maxim(st,mij,p,a,b)+Maxim(mij+1,dr,p+1,a,b);
}
int n,m,st,dr;
bool q;
int main()
{
    fin>>n>>m;
    A.resize(n+1);
    Arbore.resize(4*n+1);
    for(int i=1;i<=n;i++)
      fin>>A[i];
    Make(1,n,1);
    for(int i=0;i<m;i++)
    {
        fin>>q>>st>>dr;
        if(q==0)
           Change(1,n,st,dr,1);
        else
          fout<<Maxim(1,n,1,st,dr)<<'\n';
    }
    return 0;
}