您的位置:首页 > 产品设计 > UI/UE

hdu 4027 Can you answer these queries? 求区间N次根的和 线段树

2011-09-13 16:07 441 查看
[align=left]Problem Description[/align]
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our
secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of
the weapon, so he asks you for help.

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.

[align=left]Input[/align]
The input contains several test cases, terminated by EOF.

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 263.

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.

[align=left]Output[/align]
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐