线段树+剪枝优化!!!

代码如下:

 #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. hdu 4593 Robot

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4593 Robot Description A robot is a mechanical or vir ...

  2. hdu 1548 A strange lift

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1548 A strange lift Description There is a strange li ...

  3. 修改一行代码提升 Postgres 性能 100 倍

    http://www.datadoghq.com/2013/08/100x-faster-postgres-performance-by-changing-1-line/ SELECT c.key,  ...

  4. C# 字符串详细使用

    转自 http://www.cnblogs.com/candywyq/archive/2007/07/24/830021.html 1.Convert.ToInt32与Int32.Parse的恩恩怨怨 ...

  5. IIS8 web.config 重定向之后 报错 500.19

    原因是没有安装 URL Rewrite 官方下载地址:http://www.iis.net/downloads/microsoft/url-rewrite#additionalDownloads

  6. [开源应用]利用HTTPHandler+resumableJs+HTML5实现拖拽上传[大]文件

    前言: 大文件传输一直是技术上的一大难点.文件过大时,一些性提交所有的内容进内存是不现实的.大文件带来问题还有是否支持断点传输和多文件同时传输. 本文以resumableJs为例,介绍了如何在ASP. ...

  7. My First Django Project (2)

    1. 接下来是比较重要的VIEWS.py,您将会比较多的时间在这.有点想.net里面的aspx的cs概念,而aspx就是和接下来要创建的template html相似! 下面是我创建的一个view d ...

  8. 在数据表中添加一个字段的SQL语句怎么写

    如果要在数据表中添加一个字段,应该如何表示呢?下面就为您介绍表添加字段的SQL语句的写法,希望可以让您对SQL语句有更深的认识.   通用式: alter table [表名] add [字段名] 字 ...

  9. apk 加密

    为了防止apk被轻易破解,想办法对java层的代码进行加密,防止反编译,代码混淆基本没什么效果,一般情况下我会对dex进行加密,通过动态加载的方法实现java层的代码尽量被隐藏,而动态加载的实现通过j ...

  10. 套接字I/O模型之WSAEventSelect

    今天我又学习了一种新的套接字I/O模型------WSAEventSelect,他与WSAAsyncSelect一样也是一种异步事件通知模型,不同的是WSAAsyncSelect是与窗口句柄关联在一起 ...