HDU 4027 Can you answer these queries? 线段树水题
来源:互联网 发布:python 语音识别库 编辑:程序博客网 时间:2024/05/07 23:25
给定1-n,有两种操作,一是l,r范围内的数字都开方一次,另外是l,r的区间和。
数字最大范围是2的63次方,那么2的63次方开方8次就变为1了...所以每个数字最多被更新八次就变成1了,变成1之后就没有必要再次更新了。所以直接单点更新区间求和就好了...
求和函数 query(l,mid,k*2)+query(mid+1,r,k*2+1); 写成query(l,mid,k*2)+(mid+1,r,k*2+1);竟然在本地毫无问题,WA了半个小时没找到错误...
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<set>#include<vector>#include<iterator>#include<map>#include<queue>using namespace std;#define LL long longstruct data{ int l,r; LL sum;}data[1000000];LL a[200000];void build(int l,int r,int k){ data[k].l=l; data[k].r=r; if(l==r) { scanf("%I64d",&data[k].sum); return ; } int mid=(l+r)/2; build(l,mid,k*2); build(mid+1,r,k*2+1); data[k].sum=data[k*2].sum+data[k*2+1].sum;}void updata(int l,int r,int k){ if(data[k].sum<=(LL)data[k].r-data[k].l+1) return ; if(data[k].l==data[k].r) { data[k].sum=(LL)sqrt(data[k].sum); return ; } int mid=(data[k].l+data[k].r)/2; if(r<=mid) updata(l,r,k*2); else if(l>mid) updata(l,r,k*2+1); else { updata(l,mid,k*2); updata(mid+1,r,k*2+1); } data[k].sum=data[k*2].sum+data[k*2+1].sum;}LL query(int l,int r,int k){ if(data[k].l>=l&&data[k].r<=r) return data[k].sum; int mid=(data[k].l+data[k].r)/2; if(r<=mid) return query(l,r,k*2); else if(l>mid) return query(l,r,k*2+1); else return query(l,mid,k*2)+query(mid+1,r,k*2+1);}int main(){ int n; int cas=1; while(cin>>n) { build(1,n,1); int m; cin>>m; printf("Case #%d:\n",cas++); while(m--) { int a,b,c; scanf("%d %d %d",&a,&b,&c); if(b>c) swap(b,c); if(a==0) { updata(b,c,1); } else { cout<<query(b,c,1)<<endl; } } cout<<endl; } return 0;}
0 0
- HDU 4027 Can you answer these queries? 线段树水题
- HDU 4027 Can you answer these queries? 【线段树】
- HDU 4027 Can you answer these queries 线段树
- hdu 4027(线段树)Can you answer these queries?
- HDU 4027 线段树 Can you answer these queries?
- hdu 4027 线段树 Can you answer these queries?
- HDU 4027 Can you answer these queries?(线段树)
- hdu 4027 Can you answer these queries? 线段树
- HDU 4027 Can you answer these queries? 线段树裸题
- hdu 4027 Can you answer these queries? 线段树
- hdu-4027-Can you answer these queries?-线段树
- hdu 4027 Can you answer these queries? 线段树
- HDU 4027 Can you answer these queries? (线段树)
- hdu 4027 Can you answer these queries?(线段树)
- HDU 4027 Can you answer these queries?(线段树)
- HDU 4027 Can you answer these queries ? ( 线段树 )
- Hdu 4027 Can you answer these queries?(线段树)
- hdu 4027 Can you answer these queries? 线段树
- js判断用户的浏览设备是移动设备还是PC
- 谈谈对计算机专业的层次化理解: 计算机专业如何入门(问题 引导 学习思路)
- Digital Clock 数论水题
- MySQL中行列转换的SQL技巧
- 一个广为人知但鲜有人用的技巧:对象池
- HDU 4027 Can you answer these queries? 线段树水题
- 零基础学python-2.1 输出 print()
- 判断IE版本并提示
- cpuid function
- 静默安装WinPcap
- 快快快!27个提升效率的iOS开源库推荐
- iOS开发之如何配置cocoaPods
- 欢迎使用CSDN-markdown编辑器
- SVN安装配置及安全注意事项