题目

题意:

输入一个 :n  。(1<=n<<100000)

输入n个数    (num<2^63)

输入一个m :代表m个操作    (1<=m<<100000;)

接下来m行,每行三个数a,b,c,如果a==0,执行操作1;如果a==1,执行操作2.

操作1:对[b,c]区间上的每一个数进行开平方操作

操作2:对[b,c]区间求和,输出。

一直超时,这道题的关键在于: 2^63−1也就最多开8次平方根,,,而且开到1时再开平方根还是1.

所以再开到区间所有数都为1时就不再对这个区间更新,也就是当sum[rt]==r-l+1 时就返回上一层,这样就减小了更新时的操作

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 using namespace std;
typedef long long LL;
const int maxn = 1e5+10; LL sum[maxn*4];
int n,m; int Max(int a,int b)
{
if(a>b) return a;
else return b;
} int Min(int a,int b)
{
if(a<b) return a;
else return b;
}
void pushup(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
return ;
} void build(int l,int r,int rt)
{
sum[rt]=0;
if(l==r) {
scanf("%lld",&sum[rt]);
return ;
}
int mid = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
}
void update(int x,int y,int l,int r,int rt)
{
if(l==r){
sum[rt]=sqrt(1.0*sum[rt]);
return;
}
if(x<=l&&r<=y&&sum[rt]==r-l+1){
return;
}
int mid = (l+r)>>1;
if(x<=mid) update(x,y,lson);
if(mid < y) update(x,y,rson);
pushup(rt);
}
LL query(int x,int y,int l,int r,int rt)
{
if(x<=l&&r<=y) return sum[rt];
int mid = (l+r)>>1;
LL ans = 0;
if(x<=mid) ans+=query(x,y,lson);
if(mid<y) ans+=query(x,y,rson);
return ans;
}
int main()
{
int Case = 1;
int a,b,c,x,y;
while(~scanf("%d",&n))
{
printf("Case #%d:\n",Case++);
build(1,n,1); scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
x=Min(b,c),y=Max(b,c);
if(a==0){//update
update(x,y,1,n,1);
}else{//query
LL res = query(x,y,1,n,1);
printf("%lld\n",res);
}
}
printf("\n");
} return 0;
}

hdu 4027 Can you answer these queries?[线段树]的更多相关文章

  1. 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 ...

  2. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...

  3. HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

    题目 线段树 简单题意: 区间(单点?)更新,区间求和  更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...

  4. hdu 4027 Can you answer these queries? 线段树

    线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...

  5. HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)

    题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...

  6. hdu 4027 Can you answer these queries?

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

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

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

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

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

  9. hdu 4027 Can you answer these queries? (区间线段树,区间数开方与求和,经典题目)

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

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

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

随机推荐

  1. cxf+spring+soap简单接口开发

    最近学了cxf框架开发webservice,简单搭了个接口,方便后续翻阅,本人才疏学浅,若有不足,请多多谅解! 一.服务端: 1.所用到的jar包: maven的pom.xml配置: <proj ...

  2. profile default1

    DEVPISAP01:/sapmnt/ISD/profile # more ISD_J20_SHADEVEAIAP01 SAPSYSTEMNAME = ISD SAPSYSTEM = 20 INSTA ...

  3. spring 之 property-placeholder 分析

    不难知道, property-placeholder 的解析是 PropertyPlaceholderBeanDefinitionParser 完成的, 但是 它仅仅是个parser , 它仅仅是读取 ...

  4. MySQL InnoDB内存压力判断以及存在的疑问

    本文出处:http://www.cnblogs.com/wy123/p/7259866.html(保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错误 ...

  5. metrics+spring+influxdb整合

    1.在maven项目的pom.xml引入metrics-spring和metrics-influxdb两个jar包 <dependency> <groupId>com.ryan ...

  6. docker-compose学习

    该实践是在已经安装了docker的基础上,如果还未安装docker,请先安装docker : https://www.cnblogs.com/theRhyme/p/9813019.html docke ...

  7. Linux学习-linux系统下安装jdk和tomcat,以及遇到的问题清单

    安装JDK 1. 在usr目录下建立java安装目录 cd /usr mkdir java   2.下载jdk包 登录网址:http://www.oracle.com/technetwork/java ...

  8. Spring MVC请求处理流程

    从web.xml中 servlet的配置开始, 根据servlet拦截的url-parttern,来进行请求转发   Spring MVC工作流程图   图一   图二    Spring工作流程描述 ...

  9. Python+Selenium学习--控制浏览器控制条

    场景 有时候web 页面上的元素并非直接可见的,就算把浏览器最大化,我们依然需要拖动滚动条才能看到想要操作的元素,这个时候就要控制页面滚动条的拖动,但滚动条并非页面上的元素,可以借助JavaScrip ...

  10. Sonar+maven+jenkins集成,Java代码走查

    Sonar服务在Sonar安装与使用篇已经介绍过,此文章不再说了 Jenkins的安装与配置方法参考http://www.cnblogs.com/chenchen-tester/p/6408815.h ...