HDU4027 Can you answer these queries?(线段树 单点修改)
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 题意:给出一串数字,每次操作将l-r中的所有数开根并向下取整.每次查询询问l-r的区间和,强制在线. 题解:好吧,我真的不会怎么区间修改....传说yyk大佬有办法,各位好奇的同志可以问他去了...于是乎点修改,但如果一个一个慢慢来肯定会超时,仔细研究发现其实不管多大的数,只要小于long long,最多7次开根就变成了1.因为2^7>63
2的2^7次方已经超过了long long的范围了
.所以对于已经被开根成1的数,我们已经没有必要去再开根算了,这样即使100000的数据最大复杂度也不高.至于怎么算一段数是否已被开根成一,只要区间和等于区间长度就可以了. 代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define N 100010
#define lson root<<1
#define rson root<<1|1
using namespace std; long long node[N<<],n; void pushup(int root)
{
node[root]=node[lson]+node[rson];
} void build(int l,int r,int root)
{
if(l==r)
{
scanf("%lld",&node[root]);
return;
}
int mid=(l+r)>>;
build(l,mid,lson);
build(mid+,r,rson);
pushup(root);
} void update(int left,int right,int l,int r,int root)
{
if(l==r)
{
node[root]=sqrt(node[root]);
return;
}
if(left<=l&&right>=r&&node[root]==r-l+)
{
return;
}
int mid=(l+r)>>;
if(left<=mid)
{
update(left,right,l,mid,lson);
}
if(right>mid)
{
update(left,right,mid+,r,rson);
}
pushup(root);
} long long query(int left,int right,int l,int r,int root)
{
if(left<=l&&right>=r)
{
return node[root];
}
int mid=(l+r)>>;
long long ans=;
if(left<=mid)
{
ans+=query(left,right,l,mid,lson);
}
if(right>mid)
{
ans+=query(left,right,mid+,r,rson);
}
return ans;
} int main()
{
int n,m,ttt=;
while(scanf("%d",&n)!=EOF)
{
build(,n,);
scanf("%d",&m);
printf("Case #%d:\n",++ttt);
for(int i=;i<=m;i++)
{
int k,ll,rr;
scanf("%d %d %d",&k,&ll,&rr);
if(ll>rr)
{
swap(rr,ll);
}
if(k==)
{
update(ll,rr,,n,);
}
if(k==)
{
printf("%lld\n",query(ll,rr,,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 ...
- 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 ...
- Ocean的礼物(线段树单点修改)
题目链接:http://oj.ismdeep.com/contest/Problem?id=1284&pid=0 A: Ocean的礼物 Time Limit: 5 s Memory ...
- 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? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)
题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...
随机推荐
- hadoop超租约报错:
解决方法:修改linux打开文件最大限制Java代码 echo "fs.file-max = 65535" >> /etc/sysctl.conf echo " ...
- WebService的简单介绍与入门使用
WebService是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述.发布.发现.协调和配置这些应用程序,用于开发分布式 ...
- MongDB开启权限认证
在生产环境中MongoDB已经使用有一段时间了,但对于MongoDB的数据存储一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),最近在酷壳网看了一篇技术文章(https://cools ...
- BZOJ-1012-[JSOI2008]最大数maxnumber(线段树)
Description 现在请求你维护一个数列,要求提供以下两种操作:1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度.2. 插 ...
- 9月24日noip模拟赛解题报告
1.校门外的树(tree.c/cpp/pas 128M,1s) Description LSGJ扩建了,于是校门外有了一条长为L的路.路上种了一排的树,每相邻两棵树之间的距离为1,我们可以把马路看成一 ...
- Spark sql ---JSON
介绍Spark SQL的JSON支持,这是我们在Databricks中开发的一个功能,可以在Spark中更容易查询和创建JSON数据.随着网络和移动应用程序的普及,JSON已经成为Web服务API以及 ...
- Linux入门之常用命令(2)
(三) 链接文件 ln [-s] [源文件] [目标文件] -s表示符号链接 没有则是硬链接 硬链接是一个独立文件 (相当于一个副本) 符号链接是一个链接文件(相当于一个快捷方式) 但是修 ...
- vim与sublime,程序员的屠龙刀和倚天剑
对程序员来说,写代码是再熟悉不过的事情了,windows系统自带有记事本软件,能写写小规模的代码,可是代码量大了,它的局限性就暴露得很明显了:没有语法高亮,没有自动提示,不支持项目管理,界面难看-- ...
- Cow Exhibition 变种背包
Cow Exhibition Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- PHP浮点型(float)转换为整形(int)/round()保留小数点后几位
round(x,y); x:需要转换的变量 y:保留几位小数 <?php echo round(3.112312321) //输出3 echo round(3.112312321,3) //输出 ...