hdu4027 开方,记录
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.
Notice that the square root operation should be rounded down to integer.
InputThe input contains several test cases, terminated by EOF.
For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63.
The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
OutputFor each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.Sample Input
10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8
Sample Output
Case #1:
19
7
6 题意:就是它的每次操作是将一段区间的所有数都开一次平方,这个好像分块的时候也做过一次。
题解:因为一个数经过几次平方根就会GG变为1,然后就只需要记录这段区间是否为r-l+1即可。
是的话就不需要继续开根好了,否则就暴力下去,各个数开平方,就没了,每个数平均5次暴力,
然后就是普通线段树了,复杂度近似为O(n log n)
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
const int MAXN=;
typedef long long LL;
long long a[MAXN],tree[MAXN*];
int x,y,z,q,n,t;
using namespace std; void build(int l,int r,int p)
{
if (l==r) tree[p]=a[l];
else
{
int mid=(l+r)>>;
build(l,mid,p*);
build(mid+,r,p*+);
tree[p]=tree[p*]+tree[p*+];
}
}
LL query(int l,int r,int p,int x,int y)
{
if (l==x&&r==y) return tree[p];
else
{
int mid=(l+r)>>;
LL res;
if (y<=mid) res=query(l,mid,p*,x,y);
else if(x>=mid+) res=query(mid+,r,p*+,x,y);
else
{
res=query(l,mid,p*,x,mid);
res=res+query(mid+,r,p*+,mid+,y);
}
return res;
}
}
void change(int l,int r,int p,int x,int y)
{
if (l==r) tree[p]=LL(sqrt(tree[p]));
else
{
if (tree[p]!=(r-l+))
{
int mid=(l+r)>>;
if (y<=mid) change(l,mid,p*,x,y);
else if (x>=mid+) change(mid+,r,p*+,x,y);
else
{
change(l,mid,p*,x,mid);
change(mid+,r,p*+,mid+,y);
}
tree[p]=tree[p*]+tree[p*+];
}
}
}
int main()
{
t=;
while (~scanf("%d",&n))
{
memset(tree,,sizeof(tree));
memset(a,,sizeof(a));
printf("Case #%d:\n",++t);
for (int i=;i<=n;i++)
scanf("%lld",&a[i]);
build(,n,);
scanf("%d",&q);
for (int i=;i<=q;i++)
{
scanf("%d%d%d",&x,&y,&z);
if (y>z) swap(y,z);
if (x) printf("%lld\n",query(,n,,y,z));
else change(,n,,y,z);
}
printf("\n");
}
}
hdu4027 开方,记录的更多相关文章
- Can you answer these queries?-HDU4027 区间开方
题意: 给你n个数,两个操作,0为区间开方,1为区间求和 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 思路: 如果当该区间的数都为1,我们没必要 ...
- BZOJ 3038: 上帝造题的七分钟2【线段树区间开方问题】
3038: 上帝造题的七分钟2 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1469 Solved: 631[Submit][Status][Dis ...
- 常用sql 全记录(添加中)
-- 数据库SQL总结中........... --SQL分类: (CREATE,ALTER,DROP,DECLARE) ---DDL—数据定义语言(SELECT,DELETE,UPDATE,INSE ...
- hdu-4027线段树练习
title: hdu-4027线段树练习 date: 2018-10-10 18:07:11 tags: acm 算法 刷题 categories: ACM-线段树 # 概述 这道线段树的题可以说是我 ...
- Oracle分析函数、窗口函数简单记录汇总
一.分析函数.窗口函数一般形式 1.分析函数的形式分析函数带有一个开窗函数over(),包含三个分析子句:分组(partition by), 排序(order by), 窗口(rows) ,他们的使用 ...
- LOJ #6281. 数列分块入门 5-分块(区间开方、区间求和)
#6281. 数列分块入门 5 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 5 题目描述 给出 ...
- HDU 4027—— Can you answer these queries?——————【线段树区间开方,区间求和】
Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & ...
- MATLAB常用指令记录
help + 'command name' % 查询指令用法 Ctrl + Break % 强制终止程序运行 Shift + Enter % command window下换行不运行指令 M'; % ...
- 《机器学习实战》——k-近邻算法Python实现问题记录(转载)
py2.7 : <机器学习实战> k-近邻算法 11.19 更新完毕 原文链接 <机器学习实战>第二章k-近邻算法,自己实现时遇到的问题,以及解决方法.做个记录. 1.写一个k ...
随机推荐
- php 学习笔记 一
1.函数不对 case sensitive, 变量 case sensitive 2.全局变量 只能全局用 ,局部变量 只能函数体内用,要在 函数内访问 全局变量 要用 global 关键字申明 ,或 ...
- JavaScript--我发现!原来你是这样的JS(1)
一.前言: 前段时间看红宝书(JavaScript高级程序设计),但没有计划的去看,也没有做详细的笔记,读了之后有点空虚,感觉不对劲啊,学的东西很难记住,印象不深啊,有种挫败感,作前端的js都学不好怎 ...
- P问题、NP问题、NPC问题
看师兄们的论文经常说一句这是个NP难问题,所以采用另外一种方法来代替(比如凸松弛,把l0范数的问题松弛为l1范数的问题来求解).然后搜索了相关知识,也还是没看太懂,把一些理论知识先贴上来,希望以后再接 ...
- jre1.8使用ikvm.net8将jar转换为dll以供c#调用
由于合作方使用.net编程,jar包不能用,需要转换成dll格式,来回转换了十几个dll文件,终于生成了一个可用的.在这里将走过的弯弯绕绕总结下,希望遇到相似问题的同好们,能走得顺利些. 版本问题: ...
- 极化码的matlab仿真(2)——编码
第二篇我们来介绍一下极化码的编码. 首先为了方便进行编码,我们需要进行数组的定义 signal = randi([0,1],1,ST); %信息位比特,随机二进制数 frozen = zeros(1, ...
- JS实现60s倒计时(亲测有效),及span标签如何使用和禁用onclick事件
效果如下图:点击按钮出现60秒倒计时,60s内按钮不可用,倒计时到了时间方可再次点击获取. 另外还有一个知识点,只有input 及button这样的表单元素有disbale属性,如何设置是否可用属性的 ...
- Windows 2012建立域控(AD DS)详解
Active Directory概述: 使用 Active Directory(R) 域服务 (AD DS) 服务器角色,可以创建用于用户和资源管理的可伸缩.安全及可管理的基础机构, ...
- Cetnos搭建vsftp服务器
1.首先yum安装vsftp server 以3.0.2为例 命令:yum -y install vsftpd 2.配置文件 vsftp.conf 具体配置内容如下: anonymous_ena ...
- Windows10 VS2015下分别编译libevent 32位和64位库
Libevnt 在Windows10 VS2015下分别编译32位和64位库 直接上王道 libevent代码地址: https://github.com/libevent/libevent git ...
- 201521123071《Java程序设计》第五周学习总结
第5周作业-继承.多态.抽象类与接口 1. 本周学习总结 1.1 思维导图总结: 1.2在本周的学习中,主要学习了以下几点: - 初步接触了接口的定义,用interface关键字定义接口,使用impl ...