hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!!
代码如下:
#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? 线段树的更多相关文章
- 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 ...
- 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 ...
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...
- HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)
题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...
- hdu 4027 Can you answer these queries?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...
- HDU 4027 Can you answer these queries?(线段树区间开方)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
- HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】
Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & ...
- hdu 4027 Can you answer these queries? (区间线段树,区间数开方与求和,经典题目)
Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K ...
- HDU 4027 Can you answer these queries?(线段树的单点更新+区间查询)
题目链接 题意 : 给你N个数,进行M次操作,0操作是将区间内的每一个数变成自己的平方根(整数),1操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...
随机推荐
- Android UmengShareSDK第三方登录
Android UmengShareSDK 第三方登录- 今天就不废话了,集成平台第三方登录.市面上集成平台有shareSDK 和 Ument两种,shareSDK的ipa和服务好些,如果自己研究会很 ...
- Openstack:ice-house安装过程
#apt-get install ntpdpkg-reconfigure tzdata --> Asia -->Shuanghai #apt-get install python-mysq ...
- 使用Groovy构建自己的脚本环境
场景 在进行Web服务端开发的时候,发布前通常需要测试一遍.对于一个大一点的项目,最好的办法是写个自动化测试程序. 以Groovy为例,写测试代码之前通常的有如下几个操作 引用相关的类库 import ...
- springboot常见应用属性
# ===================================================================# COMMON SPRING BOOT PROPERTIES ...
- C语言中结构体的初始化
直接上例子: struct point { int x; int y; int z; } //常规写法 struct point pt1 = {100, 300, 200}; //初始化个数少于实际个 ...
- 基于PBOC电子钱包的消费过程详解
智能卡金融行业应用电子钱包的消费交易流程,开发人员可参考 首先终端和卡片有一个共同的密钥叫做消费密钥:PurchKey (针对每种特定的交易,比如,圈存,消费,都有特定的密钥与之对应) 假设Purch ...
- 十个优秀的C语言学习资源推荐
学习C语言,需要一点一滴,沉下心来,找个安静的地方,泡上一杯咖啡,在浓郁的香味中一起品味她.-- Boatman Yang 人们通常认为计算机编程很烦,但是有些人却从中发现了乐趣.每一个程序员不得不跟 ...
- How to find and fix Bash Shell-shock vulnerability CVE-2014-6271 in unix like system
type command - env x='() { :;}; echo vulnerable' bash -c 'echo hello' in your terminal. if your sy ...
- hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...
- NYOJ-206 矩形的个数 AC 分类: NYOJ 2013-12-29 22:19 265人阅读 评论(0) 收藏
这题目是小学奥数题目,方法可以百度到,但是,有个难点就是,数据类型大小不够,如果是1000x1000的矩阵,那么就会超过int的范围,所以,就引进了long long的数据类型 #include< ...