Cod sursa(job #2485347)

Utilizator theo2003Theodor Negrescu theo2003 Data 1 noiembrie 2019 13:20:08
Problema Datorii Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <fstream>
using namespace std;
//#define ull unsigned long long int
ifstream cin("datorii.in");
ofstream cout("datorii.out");
int l[15001];
struct aint{
    aint *a = NULL, *b = NULL;
    int l, r, v = 0;
    aint(int x, int y){
        l = x;
        r = y;
    }
};
int build(aint *node){
    if(node->l == node->r){
        return node->v = l[node->l];
    }else{
        int m = node->l + (node->r - node->l)/2;
        node->a = new aint(node->l, m);
        node->b = new aint(m + 1, node->r);
        return node->v = build(node->a) + build(node->b);
    }
}
void add(aint *node, int target, int val){
    while(node->l != node->r){
        node->v += val;
        if(target <= node->a->r)
            node = node->a;
        else node = node->b;
    }
    node->v += val;
}
int query(aint *node, int l, int r){
    if((l == node->l) && (r == node->r))
        return node->v;
    else if(r <= node->a->r)
        return query(node->a, l, r);
    else if(l >= node->b->l)
        return query(node->b, l, r);
    else return query(node->a, l, node->a->r) + query(node->b, node->b->l, r);
}
int main(){
    /*cout<<15000<<'\n';
    for(int x = 0;x<15000;x++)
        cout<<1000<<' ';
    cout<<100000<<'\n';
    for(int x = 0;x<100000;x++)
        cout<<"1 1 15000\n";
    return 0;*/
    int n, m;
    cin>>n>>m;
    for(int x = 1;x<=n;x++)
        cin>>l[x];
    aint *root = new aint(1, n);
    //cout<<
    build(root);
    for(int x = 0;x<m;x++){
        int a, b, c;
        cin>>a>>b>>c;
        if(a){
            cout<<query(root, b, c)<<'\n';
        }else{
            add(root, b, -c);
        }
    }
    return 0;
}