题目连接

题意

给出一个区间,每次把[l,r]内的值√,维护区间和。

坑:

£:l会比r大,swap.
£: 当f[i].sum=f[i].r-f[i].l+1;,不修改。因为保证每个数都大于等于1,当每个数都为1的时候,此区间不需要开根。
//纯手工
#include<cmath>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100000+10;
LL a[maxn];
struct node
{
int l,r;
LL sum;
};
node f[maxn*4];
void maketree(int i,int l,int r)
{
f[i].r=r;
f[i].l=l;
f[i].sum=0;
if(l==r)
{
f[i].sum=a[l];
return ;
}
int mid =(r+l)>>1;
maketree(i<<1,l,mid);
maketree(i<<1|1,mid+1,r);
f[i].sum=f[i<<1].sum+f[i<<1|1].sum;
}
void update(int i,int l,int r)
{
if(f[i].sum==(f[i].r-f[i].l+1))//全部为一的时候,不需要开根号
return ;
if(f[i].l==f[i].r)
{
f[i].sum=sqrt(f[i].sum*1.0);
return ;
}
int mid =(f[i].l+f[i].r)>>1;
if(r<=mid)
update(i<<1,l,r);
else if(l>=mid+1)
update(i<<1|1,l,r);
else
{
update(i<<1,l,mid);
update(i<<1|1,mid+1,r);
}
f[i].sum=f[i<<1].sum+f[i<<1|1].sum;//递归
}
LL sum(int i,int l,int r)
{
if(f[i].l==l&&f[i].r==r)
return f[i].sum;
LL ret=0;
int mid =(f[i].l+f[i].r)>>1;
if(r<=mid)
ret=sum(i<<1,l,r);
else if(l>=mid+1)
ret=sum(i<<1|1, l, r);
else
ret=sum(i<<1, l, mid)+sum(i<<1|1,mid+1,r);
return ret;
}
int main ()
{
int n,m,k=0;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
maketree(1, 1, n);
scanf("%d",&m);
printf("Case #%d:\n",++k);
int T,l,r;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&T,&l,&r);
if(l>r)
swap(l,r);
if(T==0)
update(1, l, r);
else
printf("%lld\n",sum(1, l, r));
}
printf("\n");
}
return 0;
}

HDU 4027 <线段树,区间√>的更多相关文章

  1. HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)

    题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...

  2. HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】

    Can you answer these queries? Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & ...

  3. HDU 4027 Can you answer these queries?(线段树区间开方)

    Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K ...

  4. HDU 4027(线段树)

    HDU4027 题意:操作指令为0时,对区间[x,y]之间的数字进行开平方:指令为1的时候,对区间[x,y]之间的数字求和并输出: 思路:线段树处理就OK了,但是64位内的数最多开8次平方就为1了(开 ...

  5. Can you answer these queries? HDU 4027 线段树

    Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来 ...

  6. V - Can you answer these queries? HDU - 4027 线段树 暴力

    V - Can you answer these queries? HDU - 4027 这个题目开始没什么思路,因为不知道要怎么去区间更新这个开根号. 然后稍微看了一下题解,因为每一个数开根号最多开 ...

  7. hdu 4027 Can you answer these queries?

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...

  8. hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和

    Can you answer these queries? Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/sho ...

  9. hdu 4027

    Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K ...

  10. hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***

    不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...

随机推荐

  1. infix to postfix 完整版

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  2. ActiveMQ in Action(4) - Security

    关键字: activemq 2.4 Security    ActiveMQ支持可插拔的安全机制,用以在不同的provider之间切换.2.4.1 Simple Authentication Plug ...

  3. c++的复制构造函数

    在C++中,下面三种对象需要调用拷贝构造函数(有时也称“复制构造函数”): 1) 一个对象作为函数参数,以值传递的方式传入函数体: 2) 一个对象作为函数返回值,以值传递的方式从函数返回: 3) 一个 ...

  4. LeetCode OJ 122. Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  5. MSXML insertBefore(IXMLDOMNode *newChild, VARIANT refChild) 传参

    在xml操作中经常会用到在某一个节点后或前面插入一个节点,MSXML DOM 中使用的函数是insertBefore(IXMLDOMNode *newChild, VARIANT refChild): ...

  6. CentOS环境搭建(JDK安装、mysql安装、hadoop安装等)

    1.1准备权限:让普通用户具备sudo执行权限 切换到root用户,su # vi /etc/sudoers/ 添加  koushengrui    ALL=(ALL)       ALL 这里很容易 ...

  7. 十六、oracle 索引

    一.管理索引-原理介绍索引是用于加速数据存取的数据对象.合理的使用索引可以大大降低i/o次数,从而提高数据访问性能.索引有很多种我们主要介绍常用的几种:为什么添加了索引后,会加快查询速度呢? 二.创建 ...

  8. Heap Operations(模拟题)

     Heap Operations time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  9. webstrom官方的活动模版介绍

    编辑模板变量对话框 文件|设置|生活模板--编辑变量Windows和LinuxWebStorm |偏好|生活模板--编辑变量在OS XCtrl + Alt + S 当你点击对话框打开 编辑变量按钮模板 ...

  10. 转:CSV Data Set Config 中文乱码问题

    从csv读取中文一直乱码. CSV Data Set Config的File encoding为GB2312,对应参数化文件编码也为GB2312,但读取出变量值一直为乱码,后发现是Allow quot ...