线段树+剪枝优化!!!

代码如下:

 #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. Redbean:入门(一) - 增删改查

    <?php require_once 'rb.php'; $tableName = "link"; //链接数据库 R::setup("mysql:host=loc ...

  2. centos rsync安装配置

    安装 1 yum -y install rsync ---------------------服务器安装------------------------------- 创建基础配置文件 1 2 3 4 ...

  3. Redis 客户端配置及示例

    一.redis自定义配置节点 <configSections> <section name ="RedisConfig" type="Amy.Toolk ...

  4. wire与reg的区别?转载大神!

    本文转自:http://www.cnblogs.com/thymon/archive/2010/06/09/1754541.html //------------------------------- ...

  5. 查看图片真正的格式,在不知道扩展名的情况下区分是jpeg还是bmp

    用系统自带的画图软件打开图片,然后按文件-->另存为就会弹出保存窗口.保存窗口的保存类形就是"照片真正的格式".

  6. ORACLE 检查数据库表中是否存在不规范字 段的语句参考.sql

    --查看是否有除number,char,date,varchar2,clob/blob之外的类型,比如:NVARCHAR2,TIMESTAMP(6),FLOATSELECT DISTINCT a.DA ...

  7. Mysql 导出数据库和指定表中的数据

    参考地址:http://jingyan.baidu.com/article/b7001fe14240ab0e7282dde9.html [root@youo zw]# mysqldump -u roo ...

  8. 如何分离数据库 (SQL Server Management Studio)

    在 SQL Server Management Studio 对象资源管理器中,连接到 SQL Server 数据库引擎的实例上,再展开该实例. 展开“数据库”,并选择要分离的用户数据库的名称. 分离 ...

  9. Java8 Lambda sample (iwantmoon.com出品)

    转载请注明出处:http://iwantmoon.com/Post/83a4497a74674612834965857e7bc044 .Net转到Java来,不知不觉已经有两年了,之前已经用习惯的li ...

  10. SQL-AdventureWorks样例数据库

    1. 下载样例数据库文件 输入网址:http://www.codeplex.com/ , 搜索:microsoft sql server product samples 下载对应数据库的Adventu ...