Cod sursa(job #2462263)

Utilizator mihailescu_eduardMihailescu Eduard-Florin mihailescu_eduard Data 26 septembrie 2019 22:27:05
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.6 kb
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

// Using Segment tree arbori de intervale 0 index based

#include <bits/stdc++.h>

using namespace std;

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

static const int NMAX = 15005;

int aint[NMAX*4];
int v[NMAX];

void build(int node, int low, int high)
{
    if(low == high)
    {
        aint[node] = v[low];
        return;
    }

    int mid = low + (high- low)/2;
    build(node*2+1,low,mid);
    build(node*2+2,mid+1,high);

    aint[node] = aint[node*2+1] + aint[node*2+2];
}

void update(int node, int low, int high, int pos, int val)
{
    if(low == high)
    {
        aint[node] -= val;
        return;
    }
    int mid = low + (high- low)/2;
    if(pos <= mid)
        update(node*2+1,low,mid,pos,val);
    else
        update(node*2+2,mid+1,high,pos,val);
    
    aint[node] = aint[node*2+1] + aint[node*2+2];
}

int query(int node, int low, int high, int a, int b)
{
    if(a> high || b < low)
        return 0;
    if(a<= low && b >= high)
        return aint[node];
    else
    {
        int mid = low + (high- low)/2;
        return query(node*2+1,low,mid,a,b) + query(node*2+2,mid+1,high,a,b);
    }   
}

int main()
{
    

    int n,m;
    fin >> n >> m;
    for(int i = 0; i< n; ++i)
    {
        fin >> v[i];
    }
    build(0,0,n-1);
    for(int i = 0; i< m; ++i)
    {
        int cod, a,b;
        fin >> cod >> a >> b;
        if(cod)
        {
            a--;b--;
            fout<< query(0,0,n-1,a,b) << '\n';
        }
        else
        {
            a--;
            update(0,0,n-1,a,b);
        }
        
    }

    return 0;
}