Cod sursa(job #2446434)

Utilizator MatteoalexandruMatteo Verzotti Matteoalexandru Data 8 august 2019 21:13:11
Problema Datorii Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
// Matteo Verzotti - C++
// Solutie cu arbori de intervale
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <ctime>
#include <map>
#include <chrono>
#include <cmath>
#define INF 0x3f3f3f3f
#define MAX(a,b) a>b ? a:b
#define MIN(a,b) a<b ? a:b
#define lsb(i) i & (-i)

using namespace std;

const int N = 15000;

int aint[4 * N]; // oof mai multa memorie
int v[5 + N];
int n;

void Build(int nod, int st, int dr){
    if(st == dr) aint[nod] = v[st];
    else{
        int mid, fiu;
        fiu = 2 * nod;
        mid = (st + dr) >> 1;
        Build(fiu, st, mid);
        Build(fiu + 1, mid + 1, dr);
        aint[nod] = aint[fiu] + aint[fiu + 1];
    }
}

void Update(int nod, int st, int dr, int x, int y){
    if(st == dr) aint[nod] -= y;
    else{
        int mid, fiu;
        fiu = 2 * nod;
        mid = (st + dr) >> 1;
        if(x <= mid) Update(fiu, st, mid, x, y);
        else Update(fiu + 1, mid + 1, dr, x, y);
        aint[nod] = aint[fiu] + aint[fiu + 1];
    }
}

int Query(int nod, int st, int dr, int x, int y){
    if(x <= st && dr <= y) return aint[nod];
    else{
        int ans = 0, mid, fiu;
        fiu = 2 * nod;
        mid = (st + dr) >> 1;
        if(x <= mid) ans += Query(fiu, st, mid, x, y);
        if(mid < y) ans += Query(fiu + 1, mid + 1, dr, x, y);
        return ans;
    }
}

int main()
{
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    int m, p, x, y, i;
    scanf("%d%d", &n, &m);
    for(i = 1; i <= n; i++)
        scanf("%d", &v[i]);
    Build(1,1,n); // mai rapid decat aib
    for(i = 1; i <= m; i++){
        scanf("%d%d%d", &p, &x, &y);
        if(p == 0) Update(1,1,n,x,y);
        else printf("%d\n",Query(1,1,n,x,y));
    }
    // oof mai greu de scris
    return 0;
}