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

hdu4027-Can you answer these queries?(线段树)

2014-08-08 16:47 330 查看

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)

Total Submission(s): 8330 Accepted Submission(s): 1904

[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


题意:给定100000个数,两种操作,0 i j表示将i j这段的数字都开根号(向下取整),1 i j表示查询i j之间的所有值的和。。。(所有的和都不超过64位)..
都是坑啊!!!
#include<cstdio>
#include<math.h>
#include<iostream>
using namespace std;
int N,M;
__int64 a[100010];
struct node
{
int left,right;
bool flag;//标记该值是否为‘1’,
__int64 power;
}p[400040];
void build(int i,int l,int r)
{
p[i].left=l;
p[i].right=r;
p[i].flag=1;
if(l==r)
{
p[i].power=a[l];
if(a[l]<=1)p[i].flag=0;
return ;
}
int m=(l+r)/2;
build(i*2,l,m);
build(i*2+1,m+1,r);
p[i].power=p[i*2].power+p[i*2+1].power;
p[i].flag=p[i*2].flag||p[i*2+1].flag;
}
void update(int i,int l,int r)
{
if(p[i].flag==0)return ;
if(p[i].left==p[i].right&&p[i].left==l&&p[i].right==r)
{
p[i].power=(__int64)sqrt(1.0*p[i].power);
if(p[i].power<=1)p[i].flag=0;
return ;
}
int m=(p[i].left+p[i].right)/2;
if(r<=m)
update(i*2,l,r);
else if(l>m)
update(i*2+1,l,r);
else
{
update(i*2,l,m);
update(i*2+1,m+1,r);
}
p[i].power=p[i*2].power+p[i*2+1].power;
p[i].flag=p[i*2].flag||p[i*2+1].flag;
}
__int64 query(int i,int l,int r)
{
if(p[i].left==l&&p[i].right==r)
return p[i].power;
__int64 ans=0;
int m=(p[i].left+p[i].right)/2;
if(r<=m)ans+=query(i*2,l,r);
else if(l>m)ans+=query(i*2+1,l,r);
else ans+=(query(i*2,l,m)+query(i*2+1,m+1,r));
return ans;
}
int main()
{
int k=1,i,x,y,z,mm,b;
while(scanf("%d",&N)!=EOF)
{
for(i=1;i<=N;i++)
scanf("%I64d",&a[i]);
build(1,1,N);
printf("Case #%d:\n",k++);
scanf("%d",&M);
while(M--)
{
scanf("%d%d%d",&x,&mm,&b);
z=mm>b?mm:b;
y=mm>b?b:mm;
if(x==0)
update(1,y,z);
if(x==1)
printf("%I64d\n",query(1,y,z));
}
printf("\n");//天坑,一来是没看出来,。。。。
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: