Cod sursa(job #2289206)

Utilizator RAZVAN_NISTORNistor Razvan RAZVAN_NISTOR Data 24 noiembrie 2018 11:54:59
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.32 kb
#include <bits/stdc++.h>
#define Nmx 100005
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");
int v[Nmx],A[4*Nmx];
int n,m,poz,start,stop;
void build(int l,int r,int nod)
{
    if(l==r)
        A[nod]=v[l];
    else
    {
        int m=(l+r)/2;
        build(l,m,2*nod);
        build(m+1,r,2*nod+1);
        A[nod]=A[2*nod]+A[2*nod+1];
    }
}
void update(int l,int r,int nod)
{
    if(l==r)A[nod]=v[l];
    else
    {
        int m=(l+r)/2;
        if(poz<=m)
            update(l,m,2*nod);
        else
            update(m+1,r,2*nod+1);
        A[nod]=A[2*nod]+A[2*nod+1];
    }
}
int query(int l,int r,int nod)
{
    if(start<=l&&stop>=r)
        return A[nod];
    else
    {
        int m=(l+r)/2,ml=0,mr=0;
        if(start<=m)
            ml=query(l,m,2*nod);
        if(m+1<=stop)
            mr=query(m+1,r,2*nod+1);
        return ml+mr;
    }
}
int main()
{
    int i,x,y;
    bool t;
    f>>n>>m;
    for(i=1;i<=n;i++)
        f>>v[i];
    build(1,n,1);
    for(i=1;i<=m;i++)
    {
        f>>t>>x>>y;
        if(t==1)
        {
            start=x;
            stop=y;
            g<<query(1,n,1)<<"\n";
        }
        else
        {
            v[x]-=y;
            poz=x;
            update(1,n,1);
        }
    }
    return 0;
}