HDU4027(线段树单点更新区间)
Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 11333 Accepted Submission(s): 2650
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.
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.
#include"cstdio"
#include"cmath"
#include"algorithm"
using namespace std;
const int MAXN=;
typedef long long LL;
struct node{
int l,r;
LL sum;
}a[MAXN*]; void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
if(l==r)
{
scanf("%lld",&a[rt].sum);
return ;
} int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
} void update(int rt,int l,int r)
{
if(a[rt].sum==(a[rt].r-a[rt].l+)) return ;//优化,各个叶子节点的值为1后不用再更新
if(a[rt].l==a[rt].r)
{
a[rt].sum=(LL)sqrt(a[rt].sum*1.0);
return ;
} int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid) update(rt<<,l,r);
else if(mid<l) update((rt<<)|,l,r);
else{
update(rt<<,l,mid);
update((rt<<)|,mid+,r);
}
a[rt].sum=a[rt<<].sum+a[(rt<<)|].sum;
} LL query(int rt,int l,int r)
{
if(a[rt].l==l&&a[rt].r==r)
{
return a[rt].sum;
} int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid) return query(rt<<,l,r);
else if(mid<l) return query((rt<<)|,l,r);
else{
return query(rt<<,l,mid)+query((rt<<)|,mid+,r);
}
} int main()
{
int cas=;
int n;
while(scanf("%d",&n)!=EOF)
{
build(,,n);
printf("Case #%d:\n",cas++);
int m;
scanf("%d",&m);
while(m--)
{
int t,x,y;
scanf("%d%d%d",&t,&x,&y);//x有可能比y大
if(x>y) swap(x,y);
if(t==)
{
update(,x,y);
}
else
{
printf("%lld\n",query(,x,y));
}
}
printf("\n");
} return ;
}
HDU4027(线段树单点更新区间)的更多相关文章
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
- hdu2795(线段树单点更新&区间最值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...
- HDU 3308 LCIS(线段树单点更新区间合并)
LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...
- 【HDU】1754 I hate it ——线段树 单点更新 区间最值
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 1754 I Hate It 线段树 单点更新 区间最值
线段树功能:update:单点更新 query:区间最值 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define r ...
随机推荐
- 深入Asyncio(六)Tasks and Futures
Tasks and Futures 大多数的工作只涉及到Task.create_task()方法,就像前面代码一样,Future是Task的父类,提供与loop交互的所有功能. Future对象表示某 ...
- View数据呈现相关技术
一.了解Razor语法 1.Razor基本语法 a)输出单一变量时不需要加分号做结尾.如: <p>现在时刻:@DateTime.Now</p> b)输出一段含有空白字元或运算子 ...
- 认识XmlReader
认识XmlReader 摘要 XmlReader类是组成.NET的关键技术之一,极大地方便了开发人员对Xml的操作.通过本文您将对XmlReader有一个很好的认识,并将其应用到实际开发中. 目录 ...
- EasyDSS RTMP流媒体解决方案之Windows服务安装方案
Windows服务安装 EasyDSS_Solution流媒体解决方案,可以通过start一键启动.在实际应用中,我们希望可以设置成系统服务,那么下面我将会介绍,如何在windows中将流媒体解决方案 ...
- (1)Web 应用是一个状态机,视图与状态是一一对应的。 (2)所有的状态,保存在一个对象里面。
Redux 入门教程(一):基本用法 - 阮一峰的网络日志 http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_u ...
- cocos2d-js添加百度MSSP插屏(通过jsb反射机制)
1.导入jar包.... 2.修改AndroidManifest.xml文件 添加: <meta-data android:name="BaiduMobAd_APP_ID" ...
- Java使用到的常用类总结
基本类型 常用:int.long.double.boolean. 不常用:byte.float.char.short
- Eclipse javax.servlet.jsp.PageContext cannot be resolved to a type 错误解决办法
不要 直接将jsp-api.jar拷贝到lib目录下,而是通过外部jar包引用.项目 右键->Properties->Libraries->Add External JARS-选择 ...
- 【linux】记录一次系统被攻击的处理过程
今天登录zabbix监控网页的时候发现非常卡,登录到系统里面以后,通过top看,CPU已经100%了,有一个叫做httpds的进程占用,第一反映就是系统被入侵了,下面记录了处理过程,仅供各位参考 通过 ...
- 不使用 spring-boot-starter-parent 构建 spring boot 应用
创建 spring-boot 应用通用方法是配置 pom.xml,定义 为 spring-boot-start-parent.如下: <parent> <groupId>org ...