Cod sursa(job #2833122)

Utilizator ecaterinaEcaterina Stefanescu ecaterina Data 14 ianuarie 2022 19:27:48
Problema Datorii Scor 0
Compilator c-64 Status done
Runda Arhiva de probleme Marime 2.09 kb
#include <stdio.h>

#define MAX_N 15001

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

void build(int node, int nLeft, int nRight) {
    int nMid, leftSon, rightSon;
    
    if (nLeft == nRight) {
        aint[node] = v[nLeft];
        return;
    }
    
    nMid = (nLeft+nRight)/2;
    leftSon = node+1;
    rightSon = node + 2*(nMid-nLeft+1);
    
    build(leftSon, nLeft, nMid);
    build(rightSon, nMid+1, nRight);
    
    if (aint[leftSon] > aint[rightSon]) {
        aint[node] = aint[leftSon];
    } else {
        aint[node] = aint[rightSon];
    }
}

int query(int nLeft, int nRight, int qLeft, int qRight) {
    int nMid, result;
    
    if (nLeft == nRight) {
        return v[nLeft];
    }
    
    nMid = (nLeft+nRight)/2;
    result = 0;
    
    if (qLeft <= nMid) {
        result += query(nLeft, nMid, qLeft, qRight);
    }
    
    if (nMid < qRight) {
        result += query(nMid+1, nRight, qLeft, qRight);
    }
    
    return result;
}

void update(int node, int nLeft, int nRight, int pos, int value) {
    int nMid, leftSon, rightSon;
    
    if (nLeft == nRight) {
        aint[node] = value;
        return;
    }
    
    nMid = (nLeft+nRight)/2;
    leftSon = node+1;
    rightSon = node + 2*(nMid-nLeft+1);
    
    if (pos <= nMid) {
        update(leftSon, nLeft, nMid, pos, value);
    } else {
        update(rightSon, nMid+1, nRight, pos, value);
    }
    
    if (aint[leftSon] > aint[rightSon]) {
        aint[node] = aint[leftSon];
    } else {
        aint[node] = aint[rightSon];
    }
}

int main() {
    FILE *fin, *fout;
    fin = fopen("datorii.in", "r");
    fout = fopen("datorii.out", "w");
    
    int n, q, i, op, a, b;
    
    fscanf(fin, "%d%d", &n, &q);
    for (i=0; i<n; i++) {
        fscanf(fin, "%d", &v[i]);
    }
    build (0, 0, n-1);
    
    while (q>0) {
        fscanf(fin, "%d%d%d", &op, &a, &b);
        a--;
        if (op == 0) {
            v[a] -= b;
            update(0, 0, n-1, a, b);
        } else if (op == 1) {
            b--;
            fprintf(fout, "%d\n", query(0, n-1, a, b));
        }
        q--;
    }
    
    fclose(fin);
    fclose(fout);
    return 0;
}