Can you answer these queries?
Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u
Description
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
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
Sample Input
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
19
7
6
/*
题意:有 n艘战舰,每艘战舰都有一定的能量值,炮弹每次炮轰区间内的战舰,区间内战舰的能量值变为原来的想下取整的开方数,
然后查询区间内的战舰总能量 初步思路:addv数组用来储存这棵树上的节点被轰过几次,向上向下更新的时候不会写了,试一下笨办法单点更新更新函数,不能找
到满足的区间就更新,必须要跟新到叶子节点才能,addv数组现在的作用是记录 #错误:把case i漏掉了
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
/******************************线段树区间更新模板*************************************/
const int MAXN=+;
#define lson i*2,l,m
#define rson i*2+1,m+1,r
ll sum[MAXN<<];
ll addv[MAXN<<]; void PushUp(int i)
{
sum[i]=sum[i*]+sum[i*+];
addv[i]=addv[i*]&&addv[i*+];
} void build(int i,int l,int r)
{
addv[i]=;
if(l==r)
{
scanf("%lld",&sum[i]);
return ;
}
int m=(l+r)>>;
build(lson);
build(rson);
PushUp(i);
} void update(int ql,int qr,int i,int l,int r)
{
if(l==r)//必须更新到叶子节点
{
sum[i]= sqrt(sum[i]);
if(sum[i]<=) addv[i]=;
return ;
}
int m=(l+r)>>;
if(ql<=m&&!addv[i*]) update(ql,qr,lson);
if(m<qr&&!addv[i*+]) update(ql,qr,rson);
PushUp(i);
} ll query(int ql,int qr,int i,int l,int r)
{
if(ql<=l&&r<=qr)
{
return sum[i];
}
int m=(l+r)>>;
ll res=;
if(ql<=m) res+=query(ql,qr,lson);
if(m<qr) res+=query(ql,qr,rson);
return res;
}
/******************************线段树区间更新模板*************************************/
void init(){
memset(sum,,sizeof sum);
memset(addv,,sizeof addv);
}
int n,q;
int str,x,y;
int Case=;
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF){
printf("Case #%d:\n",Case++);
init();
build(,,n);
scanf("%d",&q);
for(int i=;i<q;i++){
scanf("%d%d%d",&str,&x,&y);
if(x>y) swap(x,y);
if(str){
printf("%lld\n",query(x,y,,,n));
}else{
update(x,y,,,n);
}
}
printf("\n");
}
return ;
}
Can you answer these queries?的更多相关文章
- SPOJ GSS3 Can you answer these queries III[线段树]
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...
- hdu 4027 Can you answer these queries?
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Description Proble ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树
GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...
- GSS6 4487. Can you answer these queries VI splay
GSS6 Can you answer these queries VI 给出一个数列,有以下四种操作: I x y: 在位置x插入y.D x : 删除位置x上的元素.R x y: 把位置x用y取替 ...
- GSS5 spoj 2916. Can you answer these queries V 线段树
gss5 Can you answer these queries V 给出数列a1...an,询问时给出: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[ ...
- GSS3 SPOJ 1716. Can you answer these queries III gss1的变形
gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于 ...
- GSS1 spoj 1043 Can you answer these queries I 最大子段和
今天下午不知道要做什么,那就把gss系列的线段树刷一下吧. Can you answer these queries I 题目:给出一个数列,询问区间[l,r]的最大子段和 分析: 线段树简单区间操作 ...
- BZOJ2482: [Spoj1557] Can you answer these queries II
题解: 从没见过这么XXX的线段树啊... T_T 我们考虑离线做,按1-n一个一个插入,并且维护区间[ j,i](i为当前插入的数)j<i的最优值. 但这个最优值!!! 我们要保存历史的最优值 ...
- SPOJ 1557. Can you answer these queries II 线段树
Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...
随机推荐
- PHP数组运算符
PHP数组预算符有==(等于),===(恒等于),!=(不等于),<>(不等于),+(联合): 注意:没有-(减号)运算符: $a=array("a"=>&quo ...
- Java 制作证书的工具keytool用法总结
一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...
- Opengl4.5 中文手册—B
索引 A B C D E F G H I J K L M N O P Q ...
- php中常用的字符串查找函数strstr()、strpos()实例解释
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) 1.$haystack被查找的字 ...
- Ionic3学习笔记(五)动画之使用 animate.css
本文为原创文章,转载请标明出处 目录 前言 animate.css 的使用 animate.scss 的使用 1. 前言 animate.css 是一款强大的.跨浏览器的预设CSS3动画库,内置了很多 ...
- UIAlertController基本使用与内存泄露分析!!!
最近开发过程中,发现内存会无故增加,在做内存优化的过程中,无意间发现了内存泄露的情况,那就是从iOS8.0 苹果开始推荐我们使用的UIAlertController!!! 看到这你是不是会嘲笑我第一次 ...
- Nginx详细安装部署教程
一.Nginx简介 Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有很多,下面直接进入安装步骤 二.Nginx安装 1.下载N ...
- springboot与thrift集成实现服务端和客户端
我们这里用一个简单的小功能来演示一下如何使用springboot集成thrift 这个功能是,判断hdfs路径存在. 1.先解决依赖 <dependencies> <dependen ...
- 通过ssh秘钥的方式可以连接上CE68交换机
结论:按照CE68交换机的用户手册中的指导,可以通过ssh 秘钥的方式连接上交换机. 1.先按照eNSP连接到网卡的方式,给CE68配置一个ip地址: 192.168.56.2 2.按照交换机的用户指 ...
- CMake安装(源码方式)
CMake主页是 https://cmake.org/download/ 一.不指定安装目录方式(不需要配置环境变量) 1.安装必备包(存在的包不用卸载,yum会自动更新) yum install - ...