AKVQLD03 - How to Handle the Fans
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
void addNode(long long int seg[], int l, int r, int pos, int index, int val)
{
if(l == r)
{
seg[index] += val;
}
else
{
int mid = (l + r) / 2;
if(pos >= l && pos <= mid)
{
addNode(seg, l, mid, pos, 2 * index + 1, val);
}
else
{
addNode(seg, mid + 1, r, pos, 2 * index + 2, val);
}
seg[index] = seg[2 * index + 1] + seg[2 * index + 2];
}
}
int findSum(long long int seg[], int l, int r, int qs, int qe, int index)
{
if(qe < l || qs > r)
return 0;
if(qs <= l && qe >= r)
return seg[index];
int mid = (l + r) / 2;
int p1 = findSum(seg, l, mid, qs, qe, 2 * index + 1);
int p2 = findSum(seg, mid + 1, r, qs, qe, 2 * index + 2);
return p1 + p2;
}
int main()
{
long int n, q;
int a, b;
cin >> n >> q;
cin.get();
const long int h = ceil(log2(n));
const long int size = 2 * pow(2 , h);
long long int seg[size];
for(int i = 0; i < size - 1; i++)
seg[i] = 0;
string st;
while(q--)
{
cin >> st;
cin >> a >> b;
cin.get();
if(st == "add")
{
addNode(seg, 0, n - 1, a - 1, 0, b);
}
else
{
cout << findSum(seg, 0, n - 1, a - 1, b - 1, 0) << endl;
}
}
return 0;
}
Comments
Post a Comment