hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166
题意:中文题诶~
思路:线段树单点更新,区间求和模板
代码:
#include <iostream>
#include <stdio.h>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; const int MAXN = 5e4 + ;
int sum[MAXN << ]; void push_up(int rt){//向上求和
sum[rt] = sum[rt << ] + sum[rt << | ];
} //建树
void build(int l, int r, int rt){//sum[rt] 对应区间 [l, r] 的和
if(l == r){
scanf("%d", &sum[rt]);
return;
}
int mid = (l + r) >> ;
build(lson);
build(rson);
push_up(rt);//向上更新节点
} //单点更新
void updata(int p, int add, int l, int r, int rt){//在p点增加add
if(l == r){//找到p点
sum[rt] += add;//单点更新
return;
}
int mid = (l + r) >> ;
if(p <= mid) updata(p, add, lson);
else updata(p, add, rson);
push_up(rt);//向上更新节点
} //区间求和
int query(int L, int R, int l, int r, int rt){//对[L, R]区间求和
if(L <= l && R >= r) return sum[rt];//当前区间[l, r]包含在求和区间[L, R]中
int ans = ;
int mid = (l + r) >> ;
if(L <= mid) ans += query(L, R, lson);//L在mid左边
if(R > mid) ans += query(L, R, rson);//R在mid右边
return ans;
} int main(void){
int t, n;
scanf("%d", &t);
for(int i = ; i <= t; i++){
printf("Case %d:\n", i);
scanf("%d", &n);
build(, n, );
char s[];
while(scanf("%s", s) && s[] != 'E'){
int x, y;
scanf("%d%d", &x, &y);
if(s[] == 'Q') printf("%d\n", query(x, y, , n, ));
else if(s[] == 'A') updata(x, y, , n, );
else updata(x, -y, , n, );
}
}
return ;
}
hdu1166(线段树单点更新&区间求和模板)的更多相关文章
- 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 ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- 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 1166 敌兵布阵(线段树点更新区间求和裸题)
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
随机推荐
- JVM--内存区的划分
转自:http://www.jianshu.com/p/7ebbe102c1ae Java虚拟机在执行Java程序的过程中会把它管理的内存划分为若干个不同的数据区域. java虚拟机运行时数据区 一. ...
- Database: index
The whole point of having an index is to speed up search queries by essentially cutting down the num ...
- 常见的CSS命名
1:header(头部)logo topbar lang search topmenu banner nav headbox active(活动的) selectselectTop selectLi ...
- redhat5.1上安装oracle 10.2g客户端及配置使用
一)安装 1.命令 rpm -q gcc make binutils setarch compat-db compat-gcc compat-gcc-c++ compat-libstdc++ comp ...
- sql中使用timestamp增量抽取数据
网址:http://www.cnblogs.com/shuaifei/p/4469526.html 最近的项目中需要对上百万级的数据进行增量抽取操作,因此了解了一下TIMESTAMP的应用,特此记录 ...
- struts2框架xml验证
struts2验证分为3步: 1.获取需要验证的信息,使用同名属性,提供getter,setter方法.然后框架使用反射将值自动注入. 2.对信息进行验证,成功失败作出对应的选择. xml验证和手动验 ...
- fiddler篡改请求数据
有时需要修改请求或返回结果来验证网站存在的漏洞,因此需要使用到fiddler的断点功能. 如何修改请求前数据? 1.设置请求前断点 Rules--Automatic breakpoints--befo ...
- codeforces 658D D. Bear and Polynomials(数学)
题目链接: D. Bear and Polynomials time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- leetcode 307. Range Sum Query - Mutable(树状数组)
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...
- 第十四章-MySQL
1 安装 MySQL常见的版本 GA: 广泛使用的版本 RC: 最接近正式版本 Alpha和Bean: 内测版本和公测版本 有两种安装方式: 安装包和压缩包 1) 安装msi文件 2) 解压zip文件 ...