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
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;
}
相关推荐
hdu 1166线段树代码
hdu 1166线段树
从简单入门到偏中等的几个题,线段树很灵活,主要懂了lazy操作,其他的自己yy吧。
线段树入门资料,有利于初学者学习,让他们详细了解线段树。
【hdu5306】Gorgeous Sequence 线段树区间最值操作-附件资源
NULL 博文链接:https://128kj.iteye.com/blog/1738772
大量线段树题目 zoj 1610 线段覆盖 poj 2777 线段覆盖 poj 2528 需要离散化,建树不同,需要处理不同->注意这组数据 3 1 10 1 3 6 10 the ans is 3. hdu 1754 求区间最大值 hdu 1166 求区间和 hdu 1698 成段更新 ...
题面 【题目描述】 有nnn个营地,已知每个营地的人数,有四条命令: (1)Add(1) Add(1)Add iii jjj,iii和jjj为正整数,表示第iii个营地增加jjj个人(jjj不超过303030) (2)Sub(2)Sub(2)Sub iii jjj ,iii和jjj为正...
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
300+ AC 代码 。 大数 , 线段树 , 字符串 , dp.....
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。