// Sergey Kopeliovich (aka Burunduk1)
// 05.10.2009
// modification on the segment
#include <cstdio>
#include <ctime>
#include <algorithm>
using namespace std;
const int maxn = 1 << 8;
int t[maxn * 2];
int add[maxn * 2];
// v = vertex, [vl..vr] = segment of the vertex, [L..R] = query
int Get( int v, int vl, int vr, int L, int R )
{
if (vr < L || vl > R)
return 0;
if (L <= vl && vr <= R)
return t[v];
int vm = (vl + vr) / 2;
int sum = (min(R, vr) - max(L, vl) + 1) * add[v];
sum += Get(2 * v, vl, vm, L, R);
sum += Get(2 * v + 1, vm + 1, vr, L, R);
return sum;
}
void Add( int v, int vl, int vr, int L, int R, int x )
{
if (vr < L || vl > R)
return;
if (L <= vl && vr <= R)
{
add[v] += x;
t[v] += x * (vr - vl + 1);
return;
}
int vm = (vl + vr) / 2;
Add(2 * v, vl, vm, L, R, x);
Add(2 * v + 1, vm + 1, vr, L, R, x);
t[v] = t[2 * v] + t[2 * v + 1] + add[v] * (vr - vl + 1);
}
// Testing
int main()
{
int L = 5, R = 10;
Add(1, 0, maxn - 1, L, R, 30);
printf("%d\n", Get(1, 0, maxn - 1, L, R));
int N = (int)1e7;
L = 1, R = 1;
while (N--)
{
L = L * 3, R = R * 7;
L &= maxn - 1, R &= maxn - 1;
Add(1, 0, maxn - 1, min(L, R), max(L, R), 1);
}
printf("%d\n", Get(1, 0, maxn - 1, min(L, R), max(L, R)));
printf("time = %.2f\n", 1. * clock() / CLOCKS_PER_SEC);
return 0;
}