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 ...
随机推荐
- mybatis快速入门(五)
今天写写user表和orders表的mybatis的高级映射,一对一映射和一对多映射 1.创建一个orders.java文件 1.1一对一映射,一条订单对应一个用户 package cn.my.myb ...
- 深入理解计算机系统chapter9
从概念上来讲:虚拟存储器被组织为一个存放在磁盘上的N个连续的字节大小的单元组成的数组. 磁盘上数组的内容被缓存到主存中 1. 读写内存的安全性 物理内存本身是不限制访问的,任何地址都可以读写,而操作系 ...
- Delphi系列书籍pdf 118本 网友吐血整理
第一步:进入官网首页http://bulo.hujiang.com/home/ 第二部:home/替换u/779988/diary/627936/ 来自沪江部落
- 详解MySQL基准测试和sysbench工具
前言 作为一名后台开发,对数据库进行基准测试,以掌握数据库的性能情况是非常必要的.本文介绍了MySQL基准测试的基本概念,以及使用sysbench对MySQL进行基准测试的详细方法. 文章有疏漏之处, ...
- 洗礼灵魂,修炼python(3)--从一个简单的print代码揭露编码问题,运行原理和语法习惯
前期工作已经准备好后,可以打开IDE编辑器了,你可以选择python自带的IDLE,也可以选择第三方的,这里我使用pycharm--一个专门为python而生的IDE 按照惯例,第一个python代码 ...
- uvalive 3029 City Game
https://vjudge.net/problem/UVALive-3029 题意: 给出一个只含有F和R字母的矩阵,求出全部为F的面积最大的矩阵并且输出它的面积乘以3. 思路: 求面积最大的子矩阵 ...
- java数据库编程之高级查询
第三章:高级查询(-) 3.1:修改表 3.1.1:修改表 语法: Alter table <旧表名> rename [ TO] <新表名>; 例子:Alter table ` ...
- Linux-jdk1.7-tomcat7 简易安装
一.jdk 安装 安装包:jdk-7u80-linux-x64.tar.gz 2 解压 [root@localhost package]# tar -zxvf jdk-7u80-linux-x64.t ...
- 富文本编辑器CKEditor的使用
由于最近在架构一个pc端b/s结构的项目,项目中有个论坛模块,当用户发帖时,需要用到富文本编辑器,考虑了一下,决定使用CKEditor富文本编辑器,虽然现在问世的富文本编辑器很丰富,比如还有百度的UE ...
- Python实战之int学习笔记及简单练习
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__ ...