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 ...
随机推荐
- 谈MVVM
什么是MVVM? MVVM(模型-视图-视图模型,Model-View-ViewModal)是一种架构模式,并非一种框架,它是一种思想,一种组织与管理代码的艺术.它利用数据绑定,属性依赖,路由事件,命 ...
- appium 判断app是否安装
#coding:utf-8 #Import the common package import os import unittest from appium import webdriver from ...
- URI和URL的区别 一起学习呗
一直存在很多技术上的争论,其中最为妙的恐怕就是web地址应该叫什么的问题.通常情况就是这样:有人把地址栏的内容叫"URL",这时候有些人就来劲了:"不!其实那就是URI. ...
- python 求解线性方程组
Python线性方程组求解 求解线性方程组比较简单,只需要用到一个函数(scipy.linalg.solve)就可以了.比如我们要求以下方程的解,这是一个非齐次线性方程组: 3x_1 + x_2 - ...
- Linux系统下C语言如何调用scalapack中的函数
在并行计算中经常需要调用scalapck(并行化的lapack)函数库里面的函数进行编程,这里简单介绍在C语言如何调用scalapck中的矩阵向量乘的函数. 注意:scalapack中的函数是用for ...
- NET Core度身定制的AOP框架
NET Core度身定制的AOP框架 多年从事框架设计开发使我有了一种强迫症,那就是见不得一个应用里频繁地出现重复的代码.之前经常Review别人的代码,一看到这样的程序,我就会想如何将这些重复的代码 ...
- 九度OJ 1016 火星A+B AC版
#include <iostream> #include <string.h> #include <sstream> #include <math.h> ...
- unity3D HTC VIVE开发-物体高亮功能实现
在VR开发时,有时需要用到物体高亮的功能.这里使用Highlighting System v3.0.1.unitypackage插件实现. Highlighting System v3.0.1的介绍访 ...
- 201521123098 《Java程序设计》第7周学习总结
1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 该方法调用了ind ...
- 201521123018 《Java程序设计》第3周学习总结
1. 本章学习总结 1. 学习了类的创建: 2. 学会利用快捷方式完成变量的getter和setter的设定: 3. 学会了静态变量和非静态变量的区别和定义: 4. 学习了构造函数的基本编写方法. 2 ...