Cod sursa(job #1795442)

Utilizator r00t_Roman Remus r00t_ Data 2 noiembrie 2016 14:34:27
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <cstdio>
#include<algorithm>
#include<climits>
#include <fstream>

using namespace std;

ifstream fin("datorii.in");
ofstream fout("datorii.out");

int v[450010],i,n,x,y,op,Q,minim,val;


void update1(int nod, int s,int d)
{
    if(s==d)
        {
            v[nod]+=y;
            return;
        }
    else
    {
        int m=(s+d)/2;
        if(x<=m)
            update1(2*nod,s,m);
        else
            update1(2*nod+1,m+1,d);
        v[nod]=v[2*nod]+v[2*nod+1];
    }
}

void update2(int nod, int s,int d)
{
    if(s==d)
        {
            v[nod]-=y;
            return;
        }
    else
    {
        int m=(s+d)/2;
        if(x<=m)
            update2(2*nod,s,m);
        else
            update2(2*nod+1,m+1,d);
        v[nod]=v[2*nod]+v[2*nod+1];
    }
}

int sum;
void query(int nod, int s,int d)
{
     if(x<=s && y>=d)
        {
            sum+=v[nod];
        }
    else
    {
        int m=(s+d)>>1;
        if(x<=m)
            query(2*nod,s,m);
        if(y>=m+1)
            query(2*nod+1,m+1,d);

    }
}


int main()
{
    int n,m;
    fin>>n>>m;
    for(int i=1;i<=n;i++){
        fin>>y;
        x=i;
        update1(1,1,n);
    }
    for(int i=1;i<=m;i++){
        fin>>op>>x>>y;
        if(op==1){
            query(1,1,n);
            fout<<sum<<'\n';
            sum=0;

        }

        else
            update2(1,1,n);
    }

    return 0;
}