Cod sursa(job #1838678)

Utilizator al_k_ponyClaudiu Babin al_k_pony Data 1 ianuarie 2017 16:17:39
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
# include <bits/stdc++.h>
# define maxn 15005
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);
# define pb push_back
# define mp make_pair
using namespace std;

int a[maxn],t[4 * maxn],n,m,x,y,z,ans;

void build(int nod,int l,int r)
{
    if(l == r)
    {
        t[nod] = a[l];
        return;
    }
    int mid = (l + r) >> 1;
    build(nod << 1,l,mid);
    build(nod << 1 | 1,mid + 1,r);
    t[nod] = t[nod << 1] + t[nod << 1 | 1];
}

void upd(int nod,int l,int r,int pos,int val)
{
    if(l == r)
    {
        t[nod] -= val;
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid)
        upd(nod << 1,l,mid,pos,val);
    else
        upd(nod << 1 | 1,mid + 1,r,pos,val);
    t[nod] = t[nod << 1] + t[nod << 1 | 1];
}

int qry(int nod,int tl,int tr,int l,int r)
{
    if(l > r) return 0;
    if(l == tl && r == tr)
        return t[nod];
    int mid = (tl + tr) >> 1;
    return qry(nod << 1,tl,mid,l,min(mid,r)) +
           qry(nod << 1 | 1,mid + 1,tr,max(mid+1,l),r);
}

int32_t main(){_
	freopen("datorii.in","r",stdin);
	freopen("datorii.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i = 1;i<=n;i++)
        scanf("%d",&a[i]);
    build(1,1,n);
    for(int i = 1;i<=m;i++)
    {
        scanf("%d %d %d",&x,&y,&z);
        if(x == 0)
            upd(1,1,n,y,z);
        else
            printf("%d\n",qry(1,1,n,y,z));
    }
    return 0;
}