Pagini recente » Cod sursa (job #2371828) | Cod sursa (job #2280766) | Cod sursa (job #3267071) | Cod sursa (job #2476123) | Cod sursa (job #1417589)
#include <stdio.h>
#include <math.h>
#define MAX_N 15000
#define MAX_SQRT 123
int a[MAX_N];
int s[MAX_SQRT];
int rad;
inline void update(int day, int sum) {
a[day] -= sum;
s[day / rad] -= sum;
}
inline int query(int lo, int hi) {
int bucketLO = (lo / rad); // bucket-ul in care se gaseste lo
int bucketHI = ((hi + 1) / rad); // bucket-ul in care se gaseste hi
int ans = 0;
if (bucketLO == bucketHI) { // sunt in acelasi bucket
for (int i = lo; i <= hi; i++) {
ans += a[i];
}
} else {
for (int i = bucketLO + 1; i < bucketHI; i++) { // adun bucket-urile dintre cele 2 pozitii
ans += s[i];
}
for (int i = lo; i < (bucketLO * rad + rad); i++) { // resturi stanga
ans += a[i];
}
for (int i = bucketHI * rad; i <= hi; i++) { // resturi dreapta
ans += a[i];
}
}
return ans;
}
int main(void) {
FILE *f, *g;
int n, m;
int x, y;
f = fopen("datorii.in", "r");
fscanf(f, "%d%d", &n, &m);
rad = (int) sqrt(n - 1) + 1;
for (int i = 0; i < n; i++) {
fscanf(f, "%d ", &a[i]);
s[i / rad] += a[i];
}
g = fopen("datorii.out", "w");
for (; m; m--) {
char c = fgetc(f);
fscanf(f, "%d%d ", &x, &y);
if (c == '0') {
update(x - 1, y);
} else {
fprintf(g, "%d\n", query(x - 1, y - 1));
}
}
fclose(f);
fclose(g);
return 0;
}