http://acm.hdu.edu.cn/showproblem.php?pid=1166
first blood of seg-tree 单点增减 区间求和
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=60000+123;
int T[maxn*2];
int M;
int bit(int x)/// get highest 1 in bit-number
{
if(x==0)return 0;
int n=1;
if((x>>16)==0){n+=16; x<<=16;}
if((x>>24)==0){n+=8; x<<=8;}
if((x>>28)==0){n+=4; x<<=4;}
if((x>>30)==0){n+=2; x<<=2;}
return 32-n-(x>>31);
}
void Updata(int x, int v)
{
for (T[x+=M]+=v, x>>=1; x; x>>=1) //
T[x]=T[x<<1]+T[(x<<1)+1]; /// updata father by their children
}
int request(int s, int t)
{
int res=0;
for (s+=M-1, t+=M+1; s^t^1; s>>=1, t>>=1)/// change [,] to (,); s^t==1 refer to the interval is empty
{
if(~s&1) res+=T[s^1];
if( t&1) res+=T[t^1];
}
return res;
}
int main ()
{
int cas;
scanf("%d", &cas);
for (int I=1; I<=cas; ++I)
{
printf("Case %d:\n", I);
int n;
///initialization
scanf("%d", &n);
M=1<<(bit(n));
// printf("---%d %d\n", M, bit(n));
memset (T, 0, sizeof(T));
for (int i=0; i<n; ++i)
scanf("%d", &T[i+M]);
for (int i=(n+M-1)/2; i>0; --i)/// (n+m-1)/2 is largest non-leaf node
T[i]=T[2*i]+T[2*i+1];
char op[20];