hdu4027Can you answer these queries?【线段树】
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
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.
Input
The 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.
Output
For 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
因为更新的时候是进行开方 所以常规的更新就不行了
但是因为是开方 对64位的数来说 开方的次数是有限制的
而且当一个数开方成都是1以后再开方也还是1 所以一个区间都是1的时候就不需要继续更新了
刚开始query用的是之前的写法 结果T了
看题解改成了这样就过了
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define inf 1e18
using namespace std;
int m, n;
const int maxn = 100005;
int weapon[maxn];
long long tree[maxn << 2], lazy[maxn<<2];
void pushup(int rt)//更新
{
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
void build(int l, int r, int rt)
{
if(l == r){
scanf("%lld", &tree[rt]);
return;
}
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
}
/*void update(int L, int C, int l, int r, int rt)
{
if(l == r){
tree[rt] += C;
return;
}
int m = (l + r) >>1;
if(L <= m) update(L, C, l, m, rt << 1);
else update(L, C, m + 1, r, rt << 1 | 1);
pushup(rt);
}*/
void update(int L, int R, int l, int r, int rt)
{
if(tree[rt] == r - l + 1){//已经全部是1了就不用更新了
return;
}
if(l == r){
tree[rt] = sqrt(tree[rt]);
return;
}
int m = (l + r) >> 1;
if(L <= m) update(L, R, l, m, rt << 1);
if(R > m) update(L, R, m + 1, r, rt << 1 | 1);
pushup(rt);
}
long long query(int L, int R, int l, int r, int rt)
{
if(l == L && r == R){
return tree[rt];
}
int m = (l + r) >> 1;
//pushdown(rt, m - l + 1, r - m);
long long ans = 0;
if(R <= m) return query(L, R, l, m, rt << 1);
if(L > m) return query(L, R, m + 1, r, rt << 1 | 1);
return query(L, m, l, m, rt << 1) + query(m + 1, R, m + 1, r, rt<<1|1);
}
int main()
{
int cas = 1;
while(scanf("%d", &n) != EOF){
build(1, n, 1);
scanf("%d", &m);
printf("Case #%d:\n", cas++);
while(m--){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(b > c){
swap(b, c);
}
if(a == 0){
update(b, c, 1, n, 1);
}
else{
printf("%lld\n", query(b, c, 1, n, 1));
}
}
printf("\n");
}
return 0;
}
hdu4027Can you answer these queries?【线段树】的更多相关文章
- HDU-4027-Can you answer these queries?线段树+区间根号+剪枝
传送门Can you answer these queries? 题意:线段树,只是区间修改变成 把每个点的值开根号: 思路:对[X,Y]的值开根号,由于最大为 263.可以观察到最多开根号7次即为1 ...
- hdu4027Can you answer these queries?(线段树)
链接 算是裸线段树了,因为没个数最多开63次 ,开到不能再看就标记.查询时,如果某段区间被标记直接返回结果,否则继续向儿子节点更新. 注意用——int64 注意L会大于R 这点我很纠结..您出题人故意 ...
- 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 ...
- HDU4027 Can you answer these queries? —— 线段树 区间修改
题目链接:https://vjudge.net/problem/HDU-4027 A lot of battleships of evil are arranged in a line before ...
- HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)
题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都 ...
- hdu 4027 Can you answer these queries? 线段树
线段树+剪枝优化!!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #includ ...
- 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? (线段树成段更新 && 开根操作 && 规律)
题意 : 给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号.第二种输入"1 l r",表示查询区间[l,r ...
随机推荐
- JAVA内存泄露分析及解决
一,问题产生 项目采用Tomcat6.0为服务器,数据库为mysql5.1,数据库持久层为hibernate3.0,以springMVC3.0为框架,项目开发完成后,上线前夕进行稳定性拷机,测 ...
- logback -- 配置详解 -- 一 -- <configuration>及子节点
附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...
- Go并发编程(四)
并发基础 多进程 多线程 基于回调的非阻塞/异步IO 协程 协程 与传统的系统级线程和进程相比,协程的最大优势在于其“轻量级”,可以轻松创建上百万个而不会导致系统资源衰竭, ...
- flexbox常用布局上下固定,中间滚动
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- pycharm 激活
方法1: (1)更新**hosts**文件 [hosts文件百度云下载地址](https://pan.baidu.com/s/1o9ZujxS) **hosts**文件在windows中的地址为: C ...
- Python爬虫学习笔记-2.Requests库
Requests是Python的一个优雅而简单的HTTP库,它比Pyhton内置的urllib库,更加强大. 0X01 基本使用 安装 Requests,只要在你的终端中运行这个简单命令即可: pip ...
- linux系统如何操作隐藏文件
在linux下,以点"."开头命名的文件在系统中被视为隐藏文件.因此,如果想隐藏某个文件或目录,一种简单的办法就是把文件名命名为点开头. 对于目录backcron,可以这样操作隐藏 ...
- async await yield
问题:async 和yield有什么区别? 无奈只能用“书到用时方恨少”来解释这个问题了.其实也是自己从开始编程就接触的是nodejs中的async 以及await ,yield几乎.貌似好像都没使用 ...
- x64枚举DPC定时器
@写在前面 不同于x86,x64的DPC是被加密了的.对于x64DPC的兴趣始于我已经流产的scalpel计划.当时问某牛怎么遍历,得到的答案是“500大洋给代码”.真是R了狗了,好歹小哥我 ...
- Matlab练习——多项式和一元方程求解
1. 一元函数 f(x) = x^3 + (x - 0.98)^2 / (x + 1.25)^3 - 5 * (x + 1 / x),求f(x) = 0 的根 %在函数文件中定义函数 function ...