You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
Case #1: 19 7 6
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define bignum long long
const int maxn=110000;
struct Trie
{
int left,right;
bignum sum;
int pw;
};
Trie tree[maxn*4];
bignum a[maxn];
void buildtree(int id,int l,int r)
{
tree[id].left=l,tree[id].right=r;
tree[id].pw=1;
if(l!=r)
{
int mid=(l+r)>>1;
buildtree(id<<1,l,mid);
buildtree((id<<1)|1,mid+1,r);
tree[id].sum=tree[id<<1].sum+tree[(id<<1)|1].sum;
}
else
{
tree[id].sum=a[l];
}
}
void update(int id,int l,int r)//[l,r]pw*2,sum reset
{
if(tree[id].pw>64) return ;//important
if(tree[id].left==tree[id].right)
{
tree[id].sum=sqrt(tree[id].sum+0.0);
tree[id].pw*=2;
return ;
}
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) update(id<<1,l,r);
else if(l>=mid+1) update((id<<1)|1,l,r);
else
{
update(id<<1,l,mid);
update((id<<1)|1,mid+1,r);
}
if(tree[id<<1].pw==tree[(id<<1)|1].pw) tree[id].pw=tree[id<<1].pw;
else tree[id].pw=-1;
tree[id].sum=tree[id<<1].sum+tree[(id<<1)|1].sum;
}
bignum query(int id,int l,int r)
{
if(tree[id].left==l&&tree[id].right==r)
{
return tree[id].sum;
}
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) return query(id<<1,l,r);
else if(l>=mid+1) return query((id<<1)|1,l,r);
else
{
return query(id<<1,l,mid)+query((id<<1)|1,mid+1,r);
}
}
int main()
{
int n,pl=1;
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
buildtree(1,1,n);
int q;scanf("%d",&q);
printf("Case #%d:\n",pl++);
while(q--)
{
int d,l,r;scanf("%d%d%d",&d,&l,&r);
if(l>r) swap(l,r);
if(d==0)
{
update(1,l,r);
}
else
{
bignum cnt=query(1,l,r);
printf("%I64d\n",cnt);
}
}
printf("\n");
}
return 0;
}