Cod sursa(job #1145567)

Utilizator beldeabogdanBogdan Beldea beldeabogdan Data 18 martie 2014 12:00:44
Problema Datorii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.03 kb
#include <cstdio>
#define nmax 15010
#define lsb(a) (a&(-a))
using namespace std;

int arb[nmax];
int n,m;

inline void swap(int &a,int &b) {
    int aux = b;
    b = a;
    a = aux;
}

void update(int val,int pos) {
    while (pos <= n) arb[pos] += val,pos += lsb(pos);
}

int query(int pos) {
    int sum = 0;
    while (pos != 0) sum += arb[pos],pos -= lsb(pos);
    return sum;
}

void A() {
    int a,b;
    scanf("%d %d",&a,&b);
    update(-b,a);
}

void B() {
    int a,b,r;
    scanf("%d %d\n",&a,&b);
    if (a > b) swap(a,b);
    if (a > 1) r = query(b) - query(a-1);
    else r = query(b);
    printf("%d\n",r);
}

int main() {
    freopen("datorii.in","r",stdin);
    freopen("datorii.out","w",stdout);
    scanf("%d %d\n",&n,&m);
    for (int i=1;i<=n;i++) {
        int aux;
        scanf("%d",&aux);
        update(aux,i);
    }
    for (int i=1;i<=m;i++) {
        int type;
        scanf("%d",&type);
        if (type == 0) A();
        else if (type == 1) B();
    }
    return 0;
}