博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4027 Can you answer these queries?
阅读量:7079 次
发布时间:2019-06-28

本文共 3644 字,大约阅读时间需要 12 分钟。

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 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
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
 
题目大意:有n个战舰,每个战舰有一个耐力值;现在有q次操作,每一次操作可以查询某一个区间内的战舰的耐力值的和,也可改变某一个区间内战舰的耐力值(降低为原来的根号倍 即x = 根号x)
思路:这里我们用到了区间查询和修改所以应该很容易的想到用线段树吧,但是这里的区间修改有所不同就是是将区间内每一个数变为原来的根号级。大概就是这样吧 下面直接上代码!
1 #include
2 #include
3 #include
4 #include
5 using namespace std; 6 const int maxn = 100005; 7 typedef long long LL; 8 LL sum[maxn<<2],add[maxn<<2]; 9 int n,q;10 void pushup(int rt){11 sum[rt] = sum[rt<<1]+sum[rt<<1|1];12 }13 void build(int l,int r,int rt)14 {15 if(l==r){16 scanf("%lld",&sum[rt]);17 return ;18 }19 int mid = (l+r)>>1;20 build(l,mid,rt<<1);21 build(mid+1,r,rt<<1|1);22 pushup(rt);23 }24 void update(int L,int R,int l,int r,int rt)25 {26 if(l==r){27 sum[rt] = sqrt(sum[rt]);28 return;29 }30 if(L<=l&&r<=R&&(sum[rt]==(r-l+1)))return;31 int mid = (l+r)>>1;32 if(L<=mid)update(L,R,l,mid,rt<<1);33 if(R>mid)update(L,R,mid+1,r,rt<<1|1);34 pushup(rt);35 }36 LL query(int L,int R,int l,int r,int rt)37 {38 if(L<=l&&r<=R)return sum[rt];39 LL ans = 0;40 int mid = (l+r)>>1;41 if(L<=mid) ans+= query(L,R,l,mid,rt<<1);42 if(R>mid) ans += query(L,R,mid+1,r,rt<<1|1);43 return ans;44 }45 int main()46 {47 int t = 1;48 while(scanf("%d",&n)!=EOF){49 build(1,n,1);50 scanf("%d",&q);51 int op,l,r;52 printf("Case #%d:\n",t++);53 while(q--){54 scanf("%d%d%d",&op,&l,&r);55 if(l>r)swap(l,r);56 if(!op)update(l,r,1,n,1);57 else if(op==1)printf("%lld\n",query(l,r,1,n,1));58 }59 printf("\n");60 }61 return 0;62 }

 

转载于:https://www.cnblogs.com/wangrunhu/p/9599692.html

你可能感兴趣的文章
.NET Core中的去虚
查看>>
REST是否会步SOAP的后尘?
查看>>
微服务意味着分布式系统
查看>>
前端大神用React刻了一个Windows XP
查看>>
10种避免大型部署的方法
查看>>
Yelp的实时流技术之二:将MySQL表数据变更实时流到Kafka中
查看>>
数据不可变之linked-in/rocketdata
查看>>
Java 10新特性前瞻
查看>>
从蚂蚁金服实践入手,带你深入了解 Service Mesh
查看>>
通过DevOps考古学了解生产环境
查看>>
nginx lua指令执行顺序
查看>>
新书问答:Agile Management
查看>>
精益企业中架构师的角色
查看>>
angular-cli创建angular2项目并添加ng2-bootstrap
查看>>
leetcode讲解之开篇--刷题技巧
查看>>
Ruby 2.5.0概览
查看>>
改变从内部开始:开发者与管理者的协作
查看>>
mac使用minikube安装kubernetes
查看>>
Chrome开发者工具中关于“Deferred long-running timer task(s) ”的警告
查看>>
聊聊跨域
查看>>