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 这点我很纠结..您出题人故意 ...
随机推荐
- MySQL常见的应用异常记录
>>Error Code: 1045. Access denied for user 'test'@'%' (using password: YES) 使用MySQL的select * i ...
- 第十五节:Asp.Net Core中的各种过滤器(授权、资源、操作、结果、异常)
一. 简介 1. 说明 提到过滤器,通常是指请求处理管道中特定阶段之前或之后的代码,可以处理:授权.响应缓存(对请求管道进行短路,以便返回缓存的响应). 防盗链.本地化国际化等,过滤器用于横向处理业务 ...
- OpenCV 静态库 CMAKE 文件
cmake_minimum_required(VERSION 3.12)project(computer_cv) set(CMAKE_CXX_STANDARD 11) find_package(Ope ...
- react项目添加本地音频
<audio src="./res/audio/alarm.mp3" autoplay="autoplay" loop="loop" ...
- iphone如何安装mitmproxy的pem文件(当iphone无法使用邮箱时)
背景描述: 我要安装mitmproxy描述文件 mitmproxy-ca-cert.pem , 说是用iphone自带的邮箱接收然后安装即可,但悲剧的邮箱坏了[提示:无法连接服务器],查了不少方法都无 ...
- 计算标准差——Python
计算标准差 题目描述: 编写一个函数计算一系列数的标准差. ...
- lnmp1.4安装包
https://lnmp.org/install.html nginx中虚拟机中的配置 location ~ .*\.(php|php5)?$ { try_files $uri =404; fastc ...
- B树索引最通俗易懂的介绍
先来一段有莫的对话: 前几天下班回到家后正在处理一个白天没解决的bug,厕所突然传来对象的声音: 对象:xx,你有<时间简史>吗? 我:我去!妹子,你这啥癖好啊,我有时间也不会去捡屎 ...
- vs2017添加引用提示“找不到 Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager 服务的实例”解决方案
vs2017添加引用提示“找不到 Microsoft.VisualStudio.Shell.Interop.IVsReferenceManager 服务的实例” 不知道是不是安装时候的问题?解决方法: ...
- Java自学-异常处理 自定义异常
Java 自定义异常 示例 1 : 创建自定义异常 一个英雄攻击另一个英雄的时候,如果发现另一个英雄已经挂了,就会抛出EnemyHeroIsDeadException 创建一个类EnemyHeroIs ...