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操作是求区间和. 思路 :单点更新,区间查询,就是要注意在更新的时候要优化,要不然会超时,因为所有的 ...
随机推荐
- poj 1679 The Unique MST
题目连接 http://poj.org/problem?id=1679 The Unique MST Description Given a connected undirected graph, t ...
- go语言示例-Timer计时器的用法
计时器用来定时执行任务,分享一段代码: package main import "time" import "fmt" func main() { //新建计时 ...
- win10里安装.net3.5
在CMD窗口里面输入这样一段,来安装Microsoft.MET Framework 3.5 Dism /online /enable-feature /featurename:NetFX3 /All ...
- bootstrap API地址
http://wenzhixin.net.cn/p/bootstrap-table/docs/examples.html#pagination-table
- ubuntu 屏幕截图
无意间发现这个组合 Fn+Shift+t,按下之后,鼠标变成一个十字, 选择一个范围,okay,可以截图了
- [网络配置相关]——ifconfig命令、ip命令、route命令
ifconfig命令 1. 查看已被激活的网卡的详细信息 # ifconfig eth0 Link encap:Ethernet HWaddr 00:30:67:F2:10:CF inet addr: ...
- objective-c自学总结(一)---面向对象
本人大二本科在读,利用一个月多一点的时间对OC语言基础进行了自学,在下一阶段UI学习开始之前, 对这一阶段的自学进行一些总结.在此特别感谢刘晓斌学长和无线互联3G学院 首先说一下对OC的整体感觉,这是 ...
- 团队开发——Alpha版总结会议
本组目前存在的问题: 1.在选题的时候,题目选的比较有难度,造成后期工作量较大,实现有难度(未能正确估计项目的难度). 2.最初规划时,设计的功能较多,但是技术水平达不到,导致目前完成功能较少. 3. ...
- 3、颜色的字符串、十进制、十六进制相互转换(color convert between dec、hex and string )
int color_int=***; 1.(十进制整数)转换成(十六进制的字符串) String color_hex = String.format("#%06X", (0xFFF ...
- 第五周&第六周
学习进度表 周数 专业学习目标 学习时间 新增代码行 博客发表量 人文方面学习 知识总结 第四周 认真掌握老师上课所讲的内容,在课外多学习一些知识 5小时 50 1 阅读 ...