Cod sursa(job #2485368)

Utilizator theo2003Theodor Negrescu theo2003 Data 1 noiembrie 2019 13:53:18
Problema Datorii Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.9 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, *b, *p;
    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;*/
    auto f = fopen("datorii.in", "r");
    auto o = fopen("datorii.out", "w");
    int n, m;
    fscanf(f, "%d %d", &n, &m);
    for(int x = 1;x<=n;x++)
        fscanf(f, "%d", &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;
        fscanf(f, "%d %d %d", &a, &b, &c);
        if(a){
            fprintf(o, "%d\n", query(root, b, c));
        }else{
            add(leaf[b], -c);
        }
    }
    return 0;
}