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 ...
随机推荐
- UVA 10428 - The Roots(牛顿迭代法)
UVA 10428 - The Roots option=com_onlinejudge&Itemid=8&page=show_problem&category=494& ...
- 【Scala】Scala的Predef对象
隐式引用(Implicit Import) Scala会自己主动为每一个程序加上几个隐式引用,就像Java程序会自己主动加上java.lang包一样. Scala中.下面三个包的内容会隐式引用到每一个 ...
- lombok插件安装
eclipse安装lombok插件 lombok注解介绍 记得最后,加入的配置文件中的jar包,最好写成相对路径,这样.eclipse移动位置后,不会报错.
- tps 与 事务平均响应时间关系对答(转)
问者:每秒处理的事务数和事务的平均响应时间 怎么个关系,有关系吗 kaku21:举个例子:一个高速路 有10个入口,每个入口每秒钟只能进1辆车,请问1秒钟最多能进几辆车?? 问者:10 kaku21: ...
- 【BZOJ4197】[Noi2015]寿司晚宴 状压DP+分解质因数
[BZOJ4197][Noi2015]寿司晚宴 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴 ...
- 九度OJ 1173:查找 (排序、查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5808 解决:3163 题目描述: 输入数组长度 n 输入数组 a[1...n] 输入查找个数m 输入查找数字b[1...m ...
- 关于BlockingQueue
1 什么是BlockingQueue 2 BlockingQueue有什么用 3 ArrayBlockingQueue的用途 1 它是一个线程安全的队列 2 它是一个容量固定的队列 3 它为什么叫bl ...
- javascript实现日期时间动态显示
.aspx代码例如以下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Def ...
- 转换(旋转)transform
div { transform:rotate(180deg); -ms-transform:rotate(180deg); /* IE 9 */ -moz-transform:rotate(180de ...
- Swift 烧脑体操(二) - 函数的参数
前言 Swift 其实比 Objective-C 复杂很多,相对于出生于上世纪 80 年代的 Objective-C 来说,Swift 融入了大量新特性.这也使得我们学习掌握这门语言变得相对来说更加困 ...