Cod sursa(job #2010589)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 13 august 2017 18:05:06
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.06 kb
#include <bits/stdc++.h>
#define Nmax 15001
using namespace std;
ifstream f("datorii.in");
ofstream g("datorii.out");
int v[Nmax];
int arb[4*Nmax];
int val,lsh,rsh;
void update(const int &p, const int &q, const int &poz)
{
    if(p==q) arb[poz]=v[p];
    else
    {
        int m=(p+q)/2;
        update(p,m,2*poz);
        update(m+1,q,2*poz+1);
        arb[poz]=arb[2*poz]+arb[2*poz+1];
    }
}
int querry(const int &p, const int &q, const int &poz)
{
    if(lsh<=p and q<=rsh) return arb[poz];
    else
    {
        int m=(p+q)/2;
        int s1=0,s2=0;
        if(lsh<=m) s1=querry(p,m,2*poz);
        if(m<rsh) s2=querry(m+1,q,2*poz+1);
        return s1+s2;
    }
}
int main()
{
    int n,m,i,j,op;
    f>>n>>m;
    for(i=1;i<=n;i++)
    f>>v[i];
    update(1,n,1);
    for(;m;--m)
    {
        f>>op>>i>>j;
        if(op==0)
        {
            v[i]-=j;
            update(1,n,1);
        }
        else
        {
            lsh=i;
            rsh=j;
            g<<querry(1,n,1)<<'\n';
        }
    }

    return 0;
}