Pagini recente » Cod sursa (job #1620444) | Cod sursa (job #2460897) | Cod sursa (job #2083387) | Profil StefanGtz | Cod sursa (job #2890789)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
void update(int n, vector<int> &aib, int pos, int increment)
{
while (pos <= n) {
aib[pos] += increment;
pos += (pos & -pos);
}
}
int query(const vector<int> &aib, int pos)
{
int s = 0;
while (pos != 0) {
s += aib[pos];
pos -= (pos & -pos);
// pos &= (pos - 1);
}
return s;
}
int main()
{
ifstream fin("datorii.in");
ofstream fout("datorii.out");
int n, m;
fin >> n >> m;
vector<int> aib(n+1, 0);
for (int i = 1; i <= n; ++i) {
int x;
fin >> x;
update(n, aib, i, x);
}
for (int i = 0; i < m; ++i) {
int op;
fin >> op;
if (op == 0) {
int t, v;
fin >> t >> v;
update(n, aib, t, -v);
}
else if (op == 1) {
int p, q;
fin >> p >> q;
if (p == 1)
fout << query(aib, q) << "\n";
else {
fout << query(aib, q) - query(aib, p-1) << "\n";
}
}
}
return 0;
}