线段树+剪枝优化!!!

代码如下:

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<stdlib.h>
#define ll __int64
#define pi acos(-1.0)
#define lson i<<1
#define rson i<<1|1
#define MAX 100001
using namespace std;
ll a[MAX];
struct tree
{
int l,r;
ll sum;
bool flag;
}T[*MAX];
void built(int i,int l,int r)
{
T[i].l=l;
T[i].r=r;
T[i].flag=;
if(l==r){
T[i].sum=a[l];
if(a[l]<=) T[i].flag=;
return ;
}
int m=(l+r)>>;
built(lson,l,m);
built(rson,m+,r);
T[i].sum=T[lson].sum+T[rson].sum;
T[i].flag=T[lson].flag||T[rson].flag;
}
void update(int i,int l,int r)
{
if(!T[i].flag) return ;
if(T[i].l==l&&T[i].r==r&&l==r){
T[i].sum=(ll)sqrt(T[i].sum*1.0);
if(T[i].sum<=) T[i].flag=;
return ;
}
int m=(T[i].l+T[i].r)>>;
if(r<=m) update(lson,l,r);
else if(l>m) update(rson,l,r);
else{
update(lson,l,m);
update(rson,m+,r);
}
T[i].sum=T[lson].sum+T[rson].sum;
T[i].flag=T[lson].flag||T[rson].flag;
}
ll query(int i,int l,int r)
{
if(T[i].l==l&&T[i].r==r)
return T[i].sum;
int m=(T[i].l+T[i].r)>>;
if(r<=m) return query(lson,l,r);
else if(l>m) return query(rson,l,r);
else return query(lson,l,m)+query(rson,m+,r);
//T[i].sum=T[lson].sum+T[rson].sum;
}
int main()
{
int n,m,ca=,i,j,q,x,y;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++) scanf("%I64d",&a[i]);
built(,,n);
scanf("%d",&m);
printf("Case #%d:\n",++ca);
for(i=;i<m;i++){
scanf("%d%d%d",&q,&x,&y);
if(x>y) swap(x,y);
if(q) printf("%I64d\n",query(,x,y));
else update(,x,y);
}
printf("\n");
}
return ;
}

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? (线段树成段更新 && 开根操作 && 规律)

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

  5. hdu 4027 Can you answer these queries?

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

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

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

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

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

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

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

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

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

随机推荐

  1. 在Java如何保证方法是线程安全的

    废话开篇 都说Java程序好些,但是我觉得Java编程这东西,没个十年八年的真不敢说自己精通Java编程,由于工作原因,开始转战Java多线程,以前没怎么接触过,所以想留点脚印在这两条路上. 切入正题 ...

  2. linux清除swap

    执行top会显示Cpu(s):  0.7%us,  0.3%sy,  0.0%ni, 99.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%stMem:   2044500 ...

  3. java intellij 写控制台程序 窗口程序

    建一个空项目,建一个main函数  用application,就可以运行了 /**  * Created by robin on 15/10/11. */public class hello { pu ...

  4. 不靠谱的Paypal及海外网站

    昨天因为报名参加某个比赛,需要用到Paypal付款,整整折腾了我一个小时.我先是使用自己的银行储蓄卡,到最后一步需要银行确认,提示我需要安装个插件才能输入密码.安装完插件后需要重启浏览器,整个流程又要 ...

  5. JS如何获取多个相同class标签并分别再输出各自的文本

    function getClass(boo) { var span = $("span.w");//获取所有的span标签 <span class="w" ...

  6. P1297: [SCOI2009]迷路

    首先知道,如果没有路径长度的要求,且给定的邻接矩阵只有0和1表示通与不通的话,从S->E走N次的方案数就是这个矩阵自乘N次后的(S,E)的数值.这样的话只需要快速幂+矩阵乘法即可过关. (转载请 ...

  7. VS2013中常用的一些快捷键

    1.编辑时需要前后文切换: 需要自己设置为:ctrl+alt +方向键,向前查看,设置为右方向键,向后查看,设置为左方向键 依次选择:工具->选项->环境->键盘-> 2.查看 ...

  8. zookeeper数据迁移

    在不停机的情况下,实现集群之间数据迁移代码: private void create(ZooKeeper zk1, ZooKeeper zk2, String path) throws Excepti ...

  9. c++事件内核对象(event)进程间激活(转)

    源出处:http://blog.csdn.net/richerg85/article/details/7538493 此文主要说明的是,c++中创建的一个事件内核对象可以在不同的程序(进程)间共用,也 ...

  10. JS多种方法实现随机颜色;

    JS随机颜色有很多地方要用到:比如大家看到很多标签连接都是五颜六色.实现随机颜色的方法有多种,下面来看看具体的实现代码: 方法一: var getRandomColor = function() { ...