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 ...
随机推荐
- ServletContext读取配置文件
package servlet; import java.io.FileInputStream;import java.io.IOException;import java.io.InputStrea ...
- PHP中输出文件,怎么区别什么时候该用readfile() , fread(), file_get_contents(), fgets()
我在服务器端(Apache环境)上放了一个安卓apk安装包的下载链接,使用readfile()读取apk文件输出下载后,手机安装apk显示解析包错误.但apk本身没问题,下载后文件的大小也是完整的.服 ...
- Linux下服务端口被占用
有一次,在启动ejabberd的时候,报错如下: 10:30:15 =CRASH REPORT==== crasher: initial call: supervisor:ejabberd_liste ...
- JavaScript之this的工作原理
JavaScript 有一套完全不同于其它语言的对 this 的处理机制. 在五种不同的情况下 ,this 指向的各不相同. 1.全局范围内 当在全部范围内使用 this,它将会指向全局对象. 2.函 ...
- JavaScritpt的DOM初探之Node(一)
DOM(文档对象模型)是针对HTML和XML文档的一个API. DOM描绘了一个层次化的节点树.同意开发者加入,移除和改动页面的某一部分.DOM脱胎于微软公司的DHTLM(动态HTML),可是如今它已 ...
- API自动化测试利器——Postman
自从开始做API开发之后,我就在寻找合适的API测试工具.一开始不是很想用Chrome扩展,用的WizTools的工具,后来试过一次Postman之后就停不下来了,还买了付费的Jetpacks.推出T ...
- 动态库对外暴露api的方法
1 windows的动态库 在要export的函数声明的前面加上__declspec(dllexport)标识这个函数是从该dll中export出来给其它模块使用的. declspec是declare ...
- 云服务器 ECS Linux CentOS 修改内核引导顺序
由于 CentOS 7 使用 grub2 作为引导程序,所以和 CentOS 6 有所不同,并不是修改 /etc/grub.conf 来修改启动项,需要如下操作: 1. 查看系统内部有多少个内核: c ...
- NotePad++ 正则表达式替换
NotePad++ 正则表达式替换 高级用法 [转] - aj117 - 博客园 https://www.cnblogs.com/tibit/p/6387199.html const getQAPar ...
- css 字体的unicode码
微软雅黑: YaHei宋体: SimSun黑体: SimHei ;