hdu4027-Can you answer these queries? -(线段树+剪枝)
题意:给n个数,m个操作,分两种操作,一种是将一段区间的每个数都开根号,另一种是查询区间和。
解题:显然对每个数开根号不能用lazy的区间更新。一个一个更新必然爆时间,对1开根号还是1,如果一段区间都是1,就不用更新了,判断r-l+1 = query()。其他都是线段树的模板。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; ll sum[*];
ll a[];
int n,m; void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=a[l];
return;
}
int mid=(l+r)/;
build(l,mid,rt*);
build(mid+,r,rt*+); sum[rt]=sum[rt*]+sum[rt*+];
} void update(int L,int R,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=sqrt(sum[rt]);
return ;
}
if( L<=l && r<=R && sum[rt]==(ll)(r-l+) )///当前区间整个都在要修改的L和R之间,并且该区间里都是1
return ;///直接返回,对每个1开根号后也是1,不变
int mid=(l+r)/;
if(L<=mid)
update(L,R,l,mid,rt*);
if(mid+<=R)
update(L,R,mid+,r,rt*+); sum[rt]=sum[rt*]+sum[rt*+];
} ll query(int L,int R,int l,int r,int rt)
{
if( L<=l && r<=R )
return sum[rt];
int mid=(l+r)/;
ll res=;
if(L<=mid)
res+=query(L,R,l,mid,rt*);
if(mid+<=R)
res+=query(L,R,mid+,r,rt*+); return res;
} int main()
{
int cnt=;
while( scanf("%d",&n)!=EOF )
{
memset(a,,sizeof(a));
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
build(,n,);
printf("Case #%d:\n",cnt++);
scanf("%d",&m);
int T,L,R;
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&T,&L,&R);
if(L>R)
swap(L,R);
if(T==)
update(L,R,,n,);
else
printf("%lld\n",query(L,R,,n,));
}
printf("\n");///坑
}
return ;
}
hdu4027-Can you answer these queries? -(线段树+剪枝)的更多相关文章
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- HDU4027 Can you answer these queries? 线段树
思路:http://www.cnblogs.com/gufeiyang/p/4182565.html 写写线段树 #include <stdio.h> #include <strin ...
- HDU4027 Can you answer these queries?(线段树 单点修改)
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- 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? 线段树区间开根号,区间求和
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?线段树+区间根号+剪枝
传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...
- 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 ...
- hdu4027Can you answer these queries?(线段树)
链接 算是裸线段树了,因为没个数最多开63次 ,开到不能再看就标记.查询时,如果某段区间被标记直接返回结果,否则继续向儿子节点更新. 注意用——int64 注意L会大于R 这点我很纠结..您出题人故意 ...
随机推荐
- 在flask中使用sqlalchemy插入数据返回新增的id
user = User(‘name’=‘张三’)db.session.add(user)db.session.flush()#输出新插入数据的主键print(user.id)#此时数据才插入到数据库中 ...
- [转帖]k8s 基本使用(下)
k8s 基本使用(下) https://www.jianshu.com/p/116ce601a60f 如果你没有看过上篇的话,推荐阅读完 k8s 基本使用(上)后再阅读本篇内容. kubectl cr ...
- Linux中历史命令
历史命令,即之前登录session会话中的在命令行键入的命令. 在Linux中可以有两种方式查询历史命令 history命令 当前用户目录中的隐藏文件.bash_history 一.history 作 ...
- docker compose项目
本文参考: https://www.cnblogs.com/jmcui/p/9395375.html https://www.cnblogs.com/jmcui/p/9512795.html 1.Do ...
- Redis 获取和设置密码
1.config get reuqirepass //获取当前密码 2.config set requirepass "password"//设置当前密码,双引号里面为密码
- c#NAudio 录音功能实现
在网上找了很多类似录音教程效果都不好,或根本不能录音,代码由网上借鉴修改(完整实现录音播放功能) 1.首先新建引用类 RecordController public class RecordCont ...
- 2019 东方财富网java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.东方财富网等公司offer,岗位是Java后端开发,因为发展原因最终选择去了东方财富网,入职一年时间了,也成为 ...
- [Linux] 树莓派编译python3.7.4
python3.7.4 源码编译后遇到ssl错误: pip is configured with locations that require TLS/SSL, however the ssl mod ...
- 根父类:Object 类
一.Object类 Java中规定: 如果一个类没有显式声明它的父类(即没有写extends xx),那么默认这个类的父类就是java.lang.Object. 类 Object 是类层次结构的根类. ...
- JavaScript 之 页面加载事件
一.onload 加载事件 onload 是 window 对象的一个事件,也可以省略 window 直接使用. 常用方式: <head><script> windown.on ...