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

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

                                    

/***  题意:给你n个数,对这些数进行操作,有m组操作*        q == 0  [x,y]区间内的每个数开方*        q == 1 输出[x,y]区间内的数的和*  题解:线段树(简单的插线问线)注:当数更新到1后不再往下更新,即node[x].sum == r - l + 1,*        不再往下更新了;*  坑爹的c++输入输出,以后再也不能因为方便用了(无尽超时)**/code:#include
#include
#include
#include
#include
using namespace std;typedef long long LL;#define lson(x) (x<<1)#define rson(x) ((x<<1)|1)#define M 5000100struct Node{ int l,r; LL sum;}node[M*3];void pushup(int x){ node[x].sum = node[lson(x)].sum + node[rson(x)].sum;}void build(int l,int r,int x){ node[x].l = l; node[x].r = r; if(l == r){ scanf("%I64d",&node[x].sum); return ; } int mid = (l + r) >> 1; build(l,mid,lson(x)); build(mid+1,r,rson(x)); pushup(x);}void update(int l,int r,int x){ if(node[x].r == r && node[x].l == l&&node[x].sum == r - l +1) return ; if(node[x].l == node[x].r) { node[x].sum = sqrt(node[x].sum*1.0); return ; } int mid = (node[x].l + node[x].r) >> 1; if(r <= mid) update(l,r,lson(x)); else if(l > mid) update(l,r,rson(x)); else { update(l,mid,lson(x)); update(mid+1,r,rson(x)); } pushup(x);}LL Query(int l,int r,int x){ if(node[x].r == r && node[x].l == l) return node[x].sum; LL ans = 0; int mid = (node[x].l + node[x].r) >> 1; if(r <= mid) ans = Query(l,r,lson(x)); else if(l > mid) ans = Query(l,r,rson(x)); else { ans += Query(l,mid,lson(x)); ans += Query(mid+1,r,rson(x)); } return ans;}int main(){ int n,m,q,x,y,ca = 1; while(scanf("%d",&n)!=EOF){ build(1,n,1); scanf("%d",&m); printf("Case #%d:\n",ca++); for(int i = 0;i < m;i++){ scanf("%d%d%d",&q,&x,&y); if(x > y) swap(x,y); if(q == 0) update(x,y,1); else printf("%I64d\n",Query(x,y,1)); } printf("\n"); } return 0;}

转载地址:http://bcsgi.baihongyu.com/

你可能感兴趣的文章
docker 创建一个新镜像
查看>>
docker server gave HTTP response to HTTPS client 问题处理办法
查看>>
docker 导入与导出镜像
查看>>
docker 创建本地registry并push镜像
查看>>
docker 在push镜像到本地registry出现的500 Internal Server Error
查看>>
Linux磁盘空间查看及空间满的处理
查看>>
Linux下clock计时函数学习
查看>>
OpenMp多线程编程计时问题
查看>>
Linux/Unix 环境下实现精确计算程序运行的时间
查看>>
C++实现统计代码运行时间计时器的简单实例
查看>>
c++ 内联函数(一看就懂)
查看>>
比较fscanf 和getline读取文件效率
查看>>
(文件)输出不使用科学技术法
查看>>
LaTeX 算法代码排版 --latex2e范例总结
查看>>
常用泰勒展开
查看>>
vector length_error
查看>>
Shell脚本处理浮点数的运算和比较实例
查看>>
bash shell for循环1到100
查看>>
latex中长公式换行,很好的办法
查看>>
nohup命令
查看>>