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 这点我很纠结..您出题人故意 ...
随机推荐
- JavaSE 面试题: 成员变量与局部变量
JavaSE 面试题 成员变量与局部变量 public class Test { static int s; int i; int j; { int i = 1; i++; j++; s++; } p ...
- Qt 文件选项对话框弹出两次
1 问题 在Qt 5.12.0 版本中,用 QFileDialog 类来做文件选择时候,发现当弹出对话框后,选择完文件后,又弹出文件选择对话框. 2 原因查找 2.1 代码 QFileDialog ...
- SpringBoot:4.SpringBoot整合Mybatis实现数据库访问
在公司项目开发中,使用Mybatis居多.在 SpringBoot:3.SpringBoot使用Spring-data-jpa实现数据库访问 中,这种jpa风格的把sql语句和java代码放到一起,总 ...
- golang学习笔记--包导入及go 常用命令及参数
包导入:包导入路劲即代码包在工作区的src目录下的相对路径. 同一个源码文件中导入的多个代码包的最后一个元素不能重复,否则引起编译错误,如果只导入不使用,同样会引起编译错误 若想导入最后一个元素名相同 ...
- 简明 Java 错误处理机制
用个最简单的例子来展示为什么需要错误处理,以及怎么用. import java.io.FileInputStream; import java.io.FileNotFoundException; pu ...
- 如何在ASP.NET Core Web API中使用Mini Profiler
原文如何在ASP.NET Core Web API中使用Mini Profiler 由Anuraj发表于2019年11月25日星期一阅读时间:1分钟 ASPNETCoreMiniProfiler 这篇 ...
- phoenix kerberos 连接配置
1. 官网资料 Use JDBC to get a connection to an HBase cluster like this: Connection conn = DriverManager. ...
- 基于Jquery的下拉列表控件(个人觉得实用)
Selectize.js Selectize 是一个基于 jQuery 的 <select> UI 控件,对于标签选择和下拉列表功能非常有用. Selectize 的目标是通过简单而强大的 ...
- jQuery---jQ动画(普通,滑动,淡入淡出,自定义动画,停止动画),jQuery的事件,jQ事件的绑定/解绑,一次性事件,事件委托,事件冒泡,文档加载
jQuery---jQ动画(普通,滑动,淡入淡出,自定义动画,停止动画),jQuery的事件,jQ事件的绑定/解绑,一次性事件,事件委托,事件冒泡,文档加载 一丶jQuery动画 show,hide, ...
- Javaweb项目-下拉列表显示后台数据库的数据
下面将演示前端下拉列表显示后台数据库中class表的说有班级的名称 环境: Tomcat-8.5.40 mysql-8.0.13 eclipse-4.9.0 springmvc框架 一.从mysql中 ...