HDU 4027 Can you answer these queries?(线段树/区间不等更新)

传送门

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 18290    Accepted Submission(s): 4308

Description

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 263.
  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

思路

题意:有N个数,有两种操作,当T = 0时,区间L到R的数都变为自己的开方(向下取整),当 T = 1时,计算出区间L到R的和。

思路:由于数最大不超过2的63次方,因此开六次就为1,因此通过lazy数组记录当前的数能否继续开放下去,不能的话,就不必更新,因为一直是1不变。

 

#include<bits/stdc++.h>
using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef __int64 LL;
const int maxn = 100005;
LL sum[maxn<<2];
bool lazy[maxn<<2];void PushUp(int rt){sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];lazy[rt] = lazy[rt << 1] && lazy[rt << 1 | 1];
}void build(int l,int r,int rt){if (l == r){scanf("%I64d",&sum[rt]);return;}int m = (l + r) >> 1;build(lson);build(rson);PushUp(rt);
}void upd(int L,int R,int l,int r,int rt){if (l == r){sum[rt] = sqrt(sum[rt]);if (sum[rt] <= 1)   lazy[rt] = 1;return;}int m = (l + r) >> 1;if (L <= m && !lazy[rt << 1]) upd(L,R,lson);if (R > m && !lazy[rt << 1 | 1])  upd(L,R,rson);PushUp(rt);
}LL qry(int L,int R,int l,int r,int rt){if (L <= l && r <= R){return sum[rt];}LL ret = 0;int m = (l + r) >> 1;if (L <= m) ret += qry(L,R,lson);if (R > m)  ret += qry(L,R,rson);return ret;
}int main(){int cases = 1;int N,M,T,a,b;while (~scanf("%d",&N)){memset(lazy,false,sizeof(lazy));memset(sum,0,sizeof(sum));printf("Case #%d:\n",cases++);build(1,N,1);scanf("%d",&M);while (M--){scanf("%d%d%d",&T,&a,&b);if (a > b)  swap(a,b);if (T == 0){upd(a,b,1,N,1);}else{printf("%I64d\n",qry(a,b,1,N,1));}}printf("\n");}return 0;
}

  

#include<bits/stdc++.h>
using namespace std;
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
typedef __int64 LL;
const int maxn = 100005;
LL sum[maxn];void PushUp(int rt){sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
}void build(int l,int r,int rt){if (l == r){scanf("%I64d",&sum[rt]);return;}int m = (l + r) >> 1;build(lson);build(rson);PushUp(rt);
}void upd(int L,int R,int l,int r,int rt){if (sum[rt] == r - l + 1)   return;if (l == r){sum[rt] = sqrt(sum[rt]);return;}int m = (l + r) >> 1;if (L <= m) upd(L,R,lson);if (R > m)  upd(L,R,rson);PushUp(rt);
}LL qry(int L,int R,int l,int r,int rt){if (L <= l && r <= R){return sum[rt];}LL ret = 0;int m = (l + r) >> 1;if (L <= m) ret += qry(L,R,lson);if (R > m)  ret += qry(L,R,rson);return ret;
}int main(){int cases = 1;int N,M,T,a,b;while (~scanf("%d",&N)){printf("Case #%d:\n",cases++);build(1,N,1);scanf("%d",&M);while (M--){scanf("%d%d%d",&T,&a,&b);if (a > b)	swap(a,b);if (T == 0){upd(a,b,1,N,1);}else{printf("%I64d\n",qry(a,b,1,N,1));}}}return 0;
}

  

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/7352039.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/572122.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Go_秒懂函数、参数、可变参数、匿名函数、回调函数、内置函数

函数是将具有独立功能的代码块组成一个整体&#xff0c;使其具有特殊功能的代码集。它将复杂的算法过程分解为若干个小任务&#xff0c;隐藏相关细节&#xff0c;使得程序结构更加清晰&#xff0c;易于维护。通过接收输入参数完成一段算法指令&#xff0c;输出或存储相关结果。…

NE2018届校招内推笔试——数据挖掘

【单选题|2分/题】 1、在只有两类的情况下&#xff0c;二维特征向量通过共享相同的协方差矩阵的正态分布生成&#xff0c;其中协方差矩阵为&#xff1a; 均值向量分别为&#xff1a;&#xff0c;则根据贝叶斯分类&#xff0c;样本分类为&#xff1a;&#xff08;&#xff09; A…

Go_数组遍历、最大值、求和、多维数组

数组&#xff1a; 数组就是用来存储数据的容器&#xff0c;存储多个数据时数据类型要一致。如果想要保存任意类型数据&#xff0c;需要声明为接口类型数组数组定义完成后&#xff0c;可以对数组进行赋值操作。数组是通过下标来进行操作的&#xff0c;下标的范围是从0开始到数组…

全局变量和环境变量的区别

全局变量:启动脚本了.在各线程,以及主程序中.可以互相传递值.每次启动脚本,初始值都一样.环境变量:启动脚本了.在各线程,以及主程序中.可以互相传递值.每次启动脚本,初始值是上次停止脚本时的值. 例子:Global 全局变量Dimenv 环境变量全局变量 全局变量 1环境变量 环境变量 …

不满足依赖关系

今晚上脑残&#xff0c;替换了实体&#xff0c;把报错的也都替换完成了&#xff0c;但是运行报错&#xff1a; 大概的意思就是说不满足XXXXXX依赖关系&#xff0c;但是找了半天都没有找到&#xff0c;最后是mapper的实体类全路径替换的时候&#xff0c;脑残在后面加上了.java。…

Go_切片(初始化、遍历、截取、修改、append、copy、切片作为函数参数、切片求和、切片求最大值)

切片&#xff1a; 切片的长度是不固定的&#xff0c;可以追加数据&#xff0c;可以理解是一个动态数组&#xff0c;切片的底层是一个结构体切片类型&#xff08;slice&#xff09;本身并不是动态数组或数组指针。它内部通过指针引用底层数组&#xff0c;设定相关属性将操作限定…

Golang——递归的使用

递归指的是函数定义中调用函数本身的现象&#xff08;自己调自己&#xff09;把一个复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算 递归注意事项&#xff1a; 递归一定要有出口。否则内存溢出(出口…

阿里巴巴Java开发手册——速读记录

本随笔基于阿里巴巴Java开发手册V1.2&#xff0c;陆陆续续记录一些现阶段能理解的&#xff0c;有启发的内容&#xff0c;并将持续更新 最佳实践——插件使用已经发布为随笔&#xff01;http://www.cnblogs.com/jiangbei/p/7668654.html 一、编程规范 1.命名规范 &#xff08;1&…

Go_map集合初始化、键值操作、map作为函数参数的使用

map&#xff1a; map是key-value的数据结构&#xff0c;类似于Java的集合&#xff0c;又称为字典&#xff0c;像新华字典就是key:value类型展示的map是无序的&#xff0c;其中key是不允许重复的&#xff0c;key不存在相当于添加&#xff0c;存在相当于修改map的key必须可以进行…

Angular 小试牛刀[1]:Getting Started

首先&#xff0c;Angular2 与 Angular1.x 版本没有多大关系&#xff0c;甚至可以说是两个完全不一样的框架&#xff0c;故 Angular 指的是 Angular2 及以上的版本。而 Angular 与 TypeScript 的完美结合&#xff0c;对于一个 .NET 开发者来说&#xff0c;实在是找不到不用它的理…

Go_指针的使用、数组指针和指针数组、指针与切片、指针与结构体、多级指针

指针&#xff1a; 指针是一个特殊的变量&#xff0c;因为它存储的数据是另一个变量的内存地址&#xff0c;指针本身也是有内存地址的指针的数据类型有int、float、bool、string、数组、结构体指针的作用就是可以通过变量/对象的内存地址去操作变量/对象 注意&#xff1a; 取址运…

linux驱动编写(Kconfig文件和Makefile文件)

在Linux编写驱动的过程中&#xff0c;有两个文件是我们必须要了解和知晓的。这其中&#xff0c;一个是Kconfig文件&#xff0c;另外一个是Makefile文件。如果大家比较熟悉的话&#xff0c;那么肯定对内核编译需要的.config文件不陌生&#xff0c;在.config文件中&#xff0c;我…

Go_结构体与数组、切片、map、方法、作为函数参数、type、Tag

结构体&#xff1a; 结构体是由一系列具有相同类型或不同类型的数据构成的数据集合结构体可以很好的管理数据&#xff0c;使用结构体可以提高程序的易读性&#xff0c;类似于Java的类一样结构体内成员名必须唯一&#xff0c;可用_补位&#xff0c;支持使用自身类型的指针成员。…

vue项目中如何引入ElementUI

1.在cmd中输入命令&#xff1a; npm i element-ui -S 2.在src/main.js文件中增加代码&#xff1a; import ElementUI from element-ui import element-ui/lib/theme-default/index.cssVue.use(ElementUI) 转载于:https://www.cnblogs.com/xuemei/p/7372332.html

Golang——深浅拷贝

浅拷贝&#xff1a;只是拷贝了内存地址&#xff0c;会指向原来的内存&#xff0c;指向原来的内存地址&#xff0c;操作的时候会互相影响 go中的赋值、函数传参、函数返回值都是浅拷贝 深拷贝&#xff1a;会把变量的数据都拷贝一份&#xff0c;包括地址&#xff0c;新的内存地址…

[Java][web]利用Spring随时随地获得Request和Session

利用Spring随时随地获得Request和Session 一、准备工作&#xff1a; 在web.xml中加入 <listener> <listener-class> org.springframework.web.context.request.RequestContextListener</listener-class> </listener>二、用法&#xff1a;…

Golang实现通讯录小案例

代码&#xff1a; package mainimport "fmt"func main() {for {menu()} }type User struct {userName string/**key&#xff1a;表示电话的类型value&#xff1a;电话号码*/addressPhone map[string]string }// 定义切片 var userList make([]User, 0)func menu() …

MySql5.6版修改用户登录密码

1、使用 mysqladmin 方式: 打开命令提示符界面, 执行命令: mysqladmin -u root -p password 新密码 执行后提示输入旧密码完成密码修改, 当旧密码为空时直接按回车键确认即可。 2、UPDATE user 语句&#xff08;我自己用的就是这个方法&#xff09; 这种方式必须是先用root帐户…

Go_面向对象(抽象、封装、继承)

抽象 抽象是一种编程思维方式&#xff0c;是从多个事物中提取共性 例&#xff1a;产品经理和程序员都有工作的方法&#xff0c;但是工作内容不同&#xff0c;可以把工作抽象出来定义为一个方法&#xff0c;具体细节由调用者补充 银行存取款案例&#xff1a; 账号结构体取款方法…

Go_接口、多态、接口继承与转换、空接口、类型断言

接口 接口用于定义对象的行为&#xff0c;接口只指定对象应该做什么&#xff0c;实现这种行为由对象来决定。接口只是把所有具有共性的方法定义在一起&#xff0c;任何类型实现了接口中所有的方法&#xff0c;就是实现了这个接口。接口存在的意义就是用来定义规范&#xff0c;…