hdu1166(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
线段树功能:update:单点增减 query:区间求和
#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 50010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std; int sum[N<<];
int a[N];
void Pushup(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=a[l];
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt);
}
void update(int pos,int num,int l,int r,int rt)
{
if(l==r)
{
sum[rt]+=num;
return;
}
int m=(l+r)>>;
if(pos<=m)update(pos,num,lson);
else update(pos,num,rson);
Pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return sum[rt];
}
int m=(l+r)>>;
int res=;
if(L<=m)res+=query(L,R,lson);
if(m<R)res+=query(L,R,rson);
return res;
}
int main()
{
int t,n,cas=;
char op[];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
build(,n,);//printf("%d ",sum[20]);
printf("Case %d:\n",cas++);
while(scanf("%s",op)>)
{
if(op[]=='E')break;
int a,b;
scanf("%d%d",&a,&b);
if(op[]=='Q')
printf("%d\n",query(a,b,,n,));
else if(op[]=='A')
update(a,b,,n,);
else update(a,-b,,n,);
}
}
}
hdu1166(线段树)的更多相关文章
- HDU1754 && HDU1166 线段树模板题
HDU1754 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题目分析:对于给出的一个很长的区间,对其进行单点更新值和区间求最大值的操作,由于 ...
- 14-敌兵布阵(HDU1166线段树 & 树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=1166 敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
- HDU1166线段树(单点更新,区间求和)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu1166 线段树
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
- HDU1166 线段树裸题 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 线段树(单点更新)HDU1166、HDU1742
在上一篇博文里面,我提到了我不会线段树,现在就努力地学习啊! 今天AC一题感觉都很累,可能是状态不佳,在做HDU1166这题目时候,RE了无数次. 原因是:我的宏定义写错了,我已经不是第一犯这种错误了 ...
- HDU1166(线段树 +更新单点,求区间总和)、HDU1754(线段树 + 更新单点,求区间最大值)
线段树简单应用 先附上几张图便与理解,大佬文章传送门1.传送门2 HDU1166:题目描述 线段树 +更新单点,求区间总和 代码如下(递归版) #include<iostream> #in ...
- 【生活没有希望】hdu1166敌兵布阵 线段树
线段树水题刷刷,生活没有希望 最近看到代码跟树状数组差不多短的非递归线段树,常数也很小——zkw线段树 于是拿道水题练练手 短到让人身无可恋 ;pos;pos/=) a[pos]+=x;} ,ans= ...
- hdu1166敌兵布阵_线段树单点更新
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
随机推荐
- Python Base64转码解码
Python Base64 提供了好几种方法例如: encode, decode, encodestring, decodestring, b64encode, b64decode, standard ...
- Python Unittest 自动化单元测试框架Demo
python 测试框架(本文只涉及 PyUnit) https://wiki.python.org/moin/PythonTestingToolsTaxonomy 环境准备 首先确定已经安装有Pyth ...
- 用DELPHI的RTTI实现对象的XML持久化
去年我花了很多时间尝试用DELPHI进行基于XML的WEB应用开发.起初的设想是很美好的,但结果做出来的东西很简陋.一部分原因就在于XML到Object之间的数据绑定实现太麻烦(另一部分是因为对XSL ...
- 14.8.3 Identifying the File Format in Use 确认使用的文件格式;
14.8.3 Identifying the File Format in Use 确认使用的文件格式: 如果 你启用一个不同的文件格式使用 innodb_file_format configurat ...
- Trufun云端建模平台之云端UML工具发布
Trufun云端建模平台包括云端UML工具,云端BPMN工具,云端思维导图工具. 云端UML工具是目前最先进的基于HTML5的UML2.x建模工具,所有代码基于JAVA开发,支持类图.用例图.活动图. ...
- VI01增强问题
函数'SD_SCD_ITEM_PRICING_DATA_GET',其实在增强中和交货相关的数据在这个函数中都可以取到,没有必要再从LIKP.LIPS等等中重新取数. include程序RV64A631 ...
- Hibernate(五)——经典解析一对一关联映射
前面两篇介绍了多对一.一对多的映射.今天分享下一对一的关联映射关系.有两种策略可以实现一对一的关联映射:主键关联.唯一外键关联. 主键关联——两个表有完全相同的主键值,来表示它们的一对一的关系.数据库 ...
- javascript (二) 事件
<script></script> 函数写法: function fun_name(){ x=docment.getElementById("demo") ...
- Qt 向word中插入文字(使用QAxWidget和QAxObject)
pro 文件中要加入 CONFIG += qaxcontainer 2. main.cpp #include <QApplication> #include <QAxWidget&g ...
- 如何在Android Studio上使用Github
首先,登陆到Github上并创建一个新repository.在屏幕右上角,点击“+”标记,并且选择“New repository”. 现在完成这个表格以新建一个叫做testproject的新repos ...