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环境 ubuntu12.04 32 nginx+php5+mysql
最近几个客户都订购了阿里云服务器,如何配置服务器就比较重要了 比较喜欢ubuntu的系统,这里以12.04 32位来说 服务器配置采用 nginx+php5+mysql 首先是apt-get的更新 a ...
- CDH入门
cloudera(CDH)官网介绍:安装包.离线包该如何下载.官方文档等介绍http://www.aboutyun.com/thread-8908-1-1.html(出处: about云开发) 进入C ...
- 基于MyBatis3.0.6的基本操作介绍
每 一 个 MyBatis 的 应 用 程 序 都 以 一 个 SqlSessionFactory 对 象 的 实 例 为 核 心 .SqlSessionFactory本身是由SqlSessionFa ...
- web应用中的异常处理
楼主前几天写了一篇“Java子线程中的异常处理(通用)”文章,介绍了在多线程环境下3种通用的异常处理方法. 但是平时大家的工作一般是基于开发框架进行的(比如Spring MVC,或Spring Boo ...
- Problem 2144 Shooting Game fzu
Problem 2144 Shooting Game Accept: 99 Submit: 465Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- oracle排序的几种方法
1.创建数据库表 CREATE TABLE USER_INFO( USERID VARCHAR2(10 BYTE) NOT NULL, USERNAME ...
- Python自学笔记-Django分页器小实例
from django.core.paginator import Paginator iter = 'abcdefhijklmnopqw' paginator = Paginator(iter,4) ...
- git 忽略文件夹
$ vim .gitignore 添加要忽略的文件或文件夹 esc + :wq 退出vim命令行
- JAVA提高七:类加载器
今天我们学习类加载器,关于类加载器其实和JVM有很大关系,在这里这篇文章只是简单的介绍下类加载器,后面学习到JVM的时候还会详细讲到类加载器,本文分为下面几个小节讲解: 一.认识类加载器 1.什么是类 ...
- python学习笔记(一)之入门
1.python的安装 官网下载.exe文件直接安装即可,在安装过程中选择加入环境变量,就不用在安装后再去增添环境变量了. 本文选择的是python3.6版本,没有选择2.7版本. 2.启动pytho ...