Pagini recente » Cod sursa (job #2107245) | Cod sursa (job #1205540) | Cod sursa (job #2165543) | Cod sursa (job #1281967) | Cod sursa (job #1443421)
#include <vector>
#include <fstream>
using namespace std;
class fenwick_tree{
vector<int> v;
public:
void update(int poz, const int val){
for( ; poz < v.size(); poz += poz&-poz){
v[poz] += val; } }
fenwick_tree(ifstream& f, const int n):
v(n+1, 0){
for(int i = 1, x; i <= n; ++i){
f >> x;
update(i, x); } }
int query_fenwick(int poz){
int rez = 0;
for( ; poz > 0; poz -= poz&-poz){
rez += v[poz]; }
return rez; }
int query_fenwick(const int a, const int b){
return query_fenwick(b) - query_fenwick(a-1); } };
int main() {
ifstream f("datorii.in");
ofstream g("datorii.out");
int n, m;
f >> n >> m;
fenwick_tree arb(f, n);
for (int i = 0, t, a, b; i < m; ++i) {
f >> t >> a >> b;
switch (t) {
case 0:
arb.update(a, -b);
break;
case 1:
g << arb.query_fenwick(a, b) << '\n';
break;
}
}
return 0;
}