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

本文共 3788 字,大约阅读时间需要 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.
 
#include
#include
#include
using namespace std;struct node{ int l,r; long long sum;}st[4*100005];long long ship[100005];void create(int l,int r,int k);void change(int x,int y,int k);long long query(int x,int y,int k);int main(){ int N,M,T,x,y; int i,cnt=0; while(scanf("%d",&N)!=EOF) { ++cnt; for(i=1;i<=N;++i) scanf("%I64d",&ship[i]); create(1,N,1); scanf("%d",&M); printf("Case #%d:\n",cnt); while(M--) { scanf("%d%d%d",&T,&x,&y); if(x==0&&y==0) continue; if(x==0) ++x; if(y==0) ++y; if(x>y) { int temp; temp=x; x=y; y=temp; } if(T==0) change(x,y,1); else { printf("%I64d\n",query(x,y,1)); } } printf("\n"); } return 0;}void create(int l,int r,int k){ st[k].l=l; st[k].r=r; if(l==r) { st[k].sum=ship[l]; return; } int mid=(l+r)/2; create(l,mid,2*k); create(mid+1,r,2*k+1); st[k].sum=st[2*k].sum+st[2*k+1].sum;}void change(int x,int y,int k){ if(st[k].sum==st[k].r-st[k].l+1) return; if(st[k].l==st[k].r) { double v=sqrt(st[k].sum); st[k].sum=(long long)(v); return; } int mid=(st[k].l+st[k].r)/2; int i; if(x>mid) change(x,y,2*k+1); else if(y<=mid) change(x,y,2*k); else { change(x,mid,2*k); change(mid+1,y,2*k+1); } st[k].sum=st[2*k].sum+st[2*k+1].sum;}long long query(int x,int y,int k){ if(x==st[k].l&&y==st[k].r) return st[k].sum; int mid=(st[k].l+st[k].r)/2; if(y<=mid) return query(x,y,2*k); else if(x>mid) return query(x,y,2*k+1); else return query(x,mid,2*k)+query(mid+1,y,2*k+1);}

 

转载于:https://www.cnblogs.com/liyakun/archive/2013/04/30/3051965.html

你可能感兴趣的文章
ES6新增const常量、let变量
查看>>
Android 隐式 Intent 跳转注意事项
查看>>
hdu1556 Color the ball
查看>>
浏览器自动化项目【构思】(完)
查看>>
第一次开通博客了
查看>>
hihocoder #1190 : 连通性·四 点双联通分量
查看>>
Panoramic Photography
查看>>
排序算法之插入排序、冒泡排序和选择排序
查看>>
对于Java静态内部类的理解
查看>>
「零秒思考」是个神话,不过这款笔记术你值得拥有zz
查看>>
suricata.yaml (一款高性能的网络IDS、IPS和网络安全监控引擎)默认配置文件(图文详解)...
查看>>
Hadoop Hive概念学习系列之hive里的视图(十二)
查看>>
UVa 11728 Alternate Task
查看>>
2016.年末总结
查看>>
软工学习心得(1)
查看>>
ASP.NET一个简易的WebServer,用控制台程序模拟IIS 托起web服务
查看>>
python中subprocess.Popen的args和shell参数的使用
查看>>
【BZOJ-4422】Cow Confinement 线段树 + 扫描线 + 差分 (优化DP)
查看>>
Java NIO系列教程(八) SocketChannel
查看>>
CodeForces - 894A-QAQ(思维)
查看>>