hdu - 4027 Can you answer these queries?
http://acm.hdu.edu.cn/showproblem.php?pid=4027
/**
* 题意:给你n个数,对这些数进行操作,有m组操作
* q == 0 [x,y]区间内的每个数开方
* q == 1 输出[x,y]区间内的数的和
* 题解:线段树(简单的插线问线)注:当数更新到1后不再往下更新,即node[x].sum == r - l + 1,
* 不再往下更新了;
* 坑爹的c++输入输出,以后再也不能因为方便用了(无尽超时)
**/
code:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long LL;
#define lson(x) (x<<1)
#define rson(x) ((x<<1)|1)
#define M 5000100
struct 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);elseprintf("%I64d\n",Query(x,y,1));}printf("\n");}return 0;
}
hdu - 4027 Can you answer these queries?相关推荐
- HDU 4027 Can you answer these queries?(线段树/区间不等更新)
传送门 Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/6576 ...
- hdu 4027 Can you answer these queries?
http://acm.hdu.edu.cn/showproblem.php?pid=4027 [更新区间,查询区间]的线段树,必须抓住修改6次以后每个数必然会变成1,然后以后的修改都将不起作用,在此之 ...
- HDU - 4027 Can you answer these queries?(线段树)
题目链接:点击查看 题目大意:给定n艘敌军的舰队,每艘舰队都有一定的耐力值,随后进行m次操作,共包括两种操作,分别是输出区间[l,r]中的耐力 值之和,以及将区间[l,r]中的每个的耐力值都开平方 题 ...
- HDU 1027 G - Can you answer these queries?
http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Time Limit: 4000/2000 M ...
- HDU - 4027
Can you answer these queries? 链 接 链接 链接 HDU - 4027 题 目 是 区 间 修 改 区 间 查 询 , 但 是 区 间 开 根 号 , 求 区 间 和 题 ...
- SPOJ GSS3-Can you answer these queries III-分治+线段树区间合并
Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间 ...
- 线性代数四之动态DP(广义矩阵加速)——Can you answer these queries III,保卫王国
动态DP--广义矩阵加速 SP1716 GSS3 - Can you answer these queries III description solution code [NOIP2018 提高组] ...
- SPOJ GSS2 Can you answer these queries II (线段树离线) - xgtao -
Can you answer these queries II 这是一道线段树的题目,维护历史版本,给出N(<=100000)个数字(-100000<=x<=100000),要求求出 ...
- GSS2 - Can you answer these queries II
GSS2 - Can you answer these queries II 题意: 给你1e51e51e5 的序列,每次询问区间l到rl到rl到r,每个相同的数只算一次的最大子段和. 思路: 乍一眼 ...
最新文章
- withRouter组件
- oracle何时使用to_date函数
- Swoole练习 Web
- GitHub user guide:Hello world
- Linux下让进程在后台可靠运行的几种方法
- 【深度学习】PyTorch 历史版本安装-祖传老代码运行刚需
- 《大型网站技术架构:核心原理与案例分析》笔记05
- linux脚本文件个数,Linux上面执行mysql脚本判断文件个数
- Redis 集群搭建和简单使用
- 将 .NET Framework 项目转换为 .NET Standard 项目
- 从客户端...中检测到有潜在危险的 Request.Form 值
- 新手如何准确的控制油门
- 二分法查找 - python实现
- vue 打包之后不兼容ie_vue中使用的一些问题(IE不兼容,打包样式不生效)
- Python 获取 网卡 MAC 地址
- OpenCV-绘制箭头cv::arrowedLine
- SpringBoot配置RunDashboard
- linux常用命令之文件操作
- 医学统计学傻瓜教程(2019版)*
- 人工智能是怎么用在人脸识别上的?