Cod sursa(job #1877788)

Utilizator tudormaximTudor Maxim tudormaxim Data 13 februarie 2017 18:47:09
Problema Datorii Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.15 kb
/// Batog
#include <bits/stdc++.h>
using namespace std;

ifstream fin ("datorii.in");
ofstream fout ("datorii.out");

const int maxn = 15005;
int V[maxn], B[maxn], rad;

inline int Bucket (int x) {
    return (x / rad);
}

inline int First (int x) {
    return (x * rad);
}

void Update(int pos, int val) {
    V[pos] -= val;
    B[Bucket(pos)] -= val;
}

int Query(int x, int y) {
    int ans = 0, i;
    int left = Bucket(x);
    int right = Bucket(y);
    for (i = left + 1; i < right; i++) {
        ans += B[i];
    }
    for (i = x; i < First(left + 1); i++) {
        ans += V[i];
    }
    for (i = First(right); i <= y; i++) {
        ans += V[i];
    }
    return ans;
}

int main() {
    ios_base :: sync_with_stdio (false);
    int n, m, type, x, y, i;
    fin >> n >> m;
    rad = sqrt(n);
    for (i = 1; i <= n; i++) {
        fin >> V[i];
        B[Bucket(i)] += V[i];
    }
    while (m--) {
        fin >> type >> x >> y;
        if (type == 0) {
            Update(x, y);
        } else {
            fout << Query(x, y) << '\n';
        }
    }
    fin.close();
    fout.close();
    return 0;
}