Cod sursa(job #2485362)

Utilizator theo2003Theodor Negrescu theo2003 Data 1 noiembrie 2019 13:43:46
Problema Datorii Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.76 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, *p = NULL;
    int l, r, v = 0;
    aint(int x, int y, aint *z){
        l = x;
        r = y;
        p = z;
    }
};
aint *leaf[15001];
int build(aint *node){
    if(node->l == node->r){
        leaf[node->l] = node;
        return node->v = l[node->l];
    }else{
        int m = node->l + (node->r - node->l)/2;
        node->a = new aint(node->l, m, node);
        node->b = new aint(m + 1, node->r, node);
        return node->v = build(node->a) + build(node->b);
    }
}
void add(aint *node, int val){
    while(node->p != NULL){
        node->v += val;
        node = node->p;
    }
    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(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    /*cout<<15000<<'\n';
    for(int x = 0;x<15000;x++)
        cout<<1000<<'\n';
    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, NULL);
    /*cout<<*/build(root);//<<"<-TOTAL SUM\n";
    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(leaf[b], -c);
        }
    }
    return 0;
}