`
java-mans
  • 浏览: 11340835 次
文章分类
社区版块
存档分类
最新评论

hdu4027 Can you answer these queries? 线段树

 
阅读更多

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4014Accepted Submission(s): 979


Problem Description
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.

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

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

Sample Input
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

Sample Output
Case #1:
19
7
6

Source
/*一个10^5的序列,有10^5个操作,每个操作为a,b,c
a=0时将b到c区间内的数都开根号下取整,a=1时求b到c段的和
其中所有数的和不超过2^63。
可以发现所有的数最多开7次方,就会变成1了,再开方就不变了。
所以定一个标记allone表示这一段已经全是1了,以后的开房遇到allone为true就不向下进行了,提高效率。
线段树求和的变型,线段树提高效率的关键在于寻找合适的lazy标记,到满足一定条件的时候就不继续更新到点。
*注意在HDOJ里,64位整数,定义用__int64或longlong,输入输出只能用%I64d*/
#include<stdio.h>
#include<math.h>
int n;
struct haha
{
int flag;//有效防止超时
int left;
int right;
__int64 num;
}tree[100000*4];//至少要为4
__int64 a[100000+5];
void build(int node,int left,int right)
{
int mid;
tree[node].flag=0;
tree[node].left=left;
tree[node].right=right;
if(left==right)
{
tree[node].num=a[left];
if(a[left]==1) tree[node].flag=1;
return ;
}
mid=(left+right)/2;
build(node*2,left,mid);
build(node*2+1,mid+1,right);
tree[node].num=tree[node*2].num+tree[node*2+1].num;
if(tree[node].num==1) tree[node].flag=1;
}
void update(int node,int left,int right)
{
int mid;
if(tree[node].flag==1) return;
if(tree[node].left==tree[node].right)
{
tree[node].num=(__int64)sqrt((double)tree[node].num);
if(tree[node].num==1) tree[node].flag=1;
return;
}
mid=(tree[node].left+tree[node].right)/2;
if(right<=mid) update(node*2,left,right); //{tree[node].num=tree[node*2].num;}这句话千万不能加
else if(left>mid) update(node*2+1,left,right); //因为不管怎样当前节点的sum都是由其两子路的sum加出来的
else
if(left>=tree[node].left&&tree[node].right>=right)
{
update(node*2,left,mid);
update(node*2+1,mid+1,right);
}
else return ;
tree[node].num=tree[node*2+1].num+tree[node*2].num;
tree[node].flag=tree[node*2+1].flag&&tree[node*2].flag;//千万不要忘记及时更新标记符号
}
__int64 queue(int node,int left,int right)
{
int mid;
if(tree[node].left==left&&tree[node].right==right)
return tree[node].num;
mid=(tree[node].left+tree[node].right)/2;
if(left>mid) return queue(node*2+1,left,right);
else if(right<=mid) return queue(node*2,left,right);
else if(left>=tree[node].left&&tree[node].right>=right)
{
return queue(node*2,left,mid)+queue(node*2+1,mid+1,right);
}
else return 0;
}
int main()
{
int i,cas=0,m,flag,left,right,temp;
while(scanf("%d",&n)!=EOF)
{
printf("Case #%d:\n",++cas);
for(i=1;i<=n;i++)
scanf("%I64d",&a[i]);
for(i=1;i<=4*n;i++)
tree[i].flag=0;
build(1,1,n);
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&flag,&left,&right);
if(left>right) {temp=left;left=right;right=temp;}//这个地方很不要脸 很难发现 而且没有就RW
if(!flag)
update(1,left,right);
else printf("%I64d\n",queue(1,left,right));
}
printf("\n");
}
return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics