hdu 4027 Can you answer these queries? (线段树)

来源:互联网 发布:3dmax烘焙导入unity3d 编辑:程序博客网 时间:2024/06/06 04:37

上题:

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 2 63
  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
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
Sample Output
Case #1:1976
题意:
题意就是有一个序列,和m组数(a,b,c),其中a等于1的时候表示的是查询从b~c的和,而a=0表示的是将b~c之间的数全部开平方。真是渣渣 ,看到一道题就觉得是好题。。。
做法:起初看着道题的时候 认为这不就是很裸的 字段区间更新吗,但是敲到一半,发现根本不对好吗,他现在要做的对于每个开平方的数求值,而不是同加或者同减一个数,所以我们是没有办法更新一个区段和的,因为很明显,sqrt(1+2)!=sqrt(1)+sqrt(2)的,况且我们不能把当前的状态,传到下面。之后,我们可以看到他每次更新的值的方法是开平方,最大值为2的63次方,那么也就是他最多开7次之后这个数值必为1,那么这之后我们就不必在更新了,这样他的复杂度最多为o(7n)所以这道题的正确做法就是,单点更新,之后求和。对了,这道题有个坑点,就是 x和y之间,那么也就是说他们没有确定x和y的大小关系
#include<stdio.h>#include<algorithm>#include<string.h>#include<math.h>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;const int maxn=100002;long long sum[maxn<<2],mark[maxn<<2];void pushup(int rt){sum[rt]=sum[rt<<1]+sum[rt<<1|1];mark[rt]=mark[rt<<1]&&mark[rt<<1|1];//当他们的儿子节点都是1的时候 就不必往下更新了 }void buildtree(int l,int r,int rt){mark[rt]=0;if(l==r){scanf("%lld",&sum[rt]);return ;}int m=(r+l)>>1;buildtree(lson);buildtree(rson);pushup(rt);}void update(int L,int R,int l,int r,int rt){if(r==l)//单点更新 {sum[rt]=sqrt(sum[rt]);if(sum[rt]<=1){mark[rt]=1;}return ;}int m=(l+r)>>1;if(L<=m&&!mark[rt<<1])//当他的儿子节点还有需要更新的时候 更新 {update(L,R,lson);}if(R>m&&!mark[rt<<1|1]){update(L,R,rson);}pushup(rt);}long long query(int L,int R,int l,int r,int rt){if(L<=l&&R>=r){return sum[rt];}int m=(l+r)>>1;long long ret=0;if(L<=m){ret+=query(L,R,lson);}if(R>m){ret+=query(L,R,rson);}return ret;}int main(){int n;int ca=1;int a,b,c;while(scanf("%d",&n)!=EOF){memset(mark,0,sizeof(mark));memset(sum,0,sizeof(sum));buildtree(1,n,1);int t;scanf("%d",&t);printf("Case #%d:\n",ca++);while(t--){scanf("%d%d%d",&a,&b,&c);if(b>c)//坑点  wa了一发 {int t=c;c=b;b=t;}if(a==0){update(b,c,1,n,1);}else {printf("%lld\n",query(b,c,1,n,1));}}printf("\n");//坑点 ,pe了一发 }return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 狗狗拉肚子不吃东西怎么办 小孩做作业时容易发呆怎么办 一上高速就犯困怎么办 孩子初中数学学不好怎么办 高三注意力不集中怎么办 考砸了家长打我怎么办? 高三学生困疲劳怎么办 高三晚上很困怎么办 孩子上高三压力大不想上学怎么办 高三的孩子压力大怎么办 高三复读压力大怎么办 孩子一年级做数学粗心怎么办 一年级的孩子数学总粗心怎么办 天生手脚笨的人怎么办 高三的孩子厌学怎么办 二年级小孩学习笨怎么办 孩子高二不想上怎么办 高三孩子玩手机怎么办 孩子考试粗心丢题怎么办 工作中总出错是怎么办 工作上做错事了怎么办 惹她不开心了怎么办 惹到别人不开心怎么办 孩子高三不愿意写作业怎么办? 小孩特别懒不爱学习怎么办 小孩上三年级不爱学习怎么办 一年级孩子做题粗心怎么办 一年级的孩子做题粗心怎么办 一年级娃娃做题粗心怎么办 有同学抄作业该怎么办 同学要抄我作业怎么办 同桌抄我作业我该怎么办 孩子想换同桌老师不同意怎么办 尴尬的事情心里难受怎么办 孩子撒谎不完成作业怎么办 孩子撒谎不交作业怎么办 孩子不爱写作业撒谎怎么办 大便又粗又硬怎么办 二年级末考紧张怎么办 孩子粗心做数学题老出错怎么办 生地会考考了140怎么办