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的任 ...
随机推荐
- python之virtualenv 与 virtualenvwrapper 详解
在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...
- i=i+2 与i+=2
i=i+2 比 i+=2多了一次对变量 i 的运算.后者效率高
- Linux-3.14.12内存管理笔记【kmalloc与kfree实现】【转】
本文转载自:http://blog.chinaunix.net/uid-26859697-id-5573776.html kmalloc()是基于slab/slob/slub分配分配算法上实现的,不少 ...
- <算法笔记>关于快速排序的算法优化排序(顺便给百度百科纠个错)
快速排序是排序算法之中的基本中的基本,虽然越来越多的接口函数将快速排序“完美的封装了起来”,比如C++中的qsort或者<algorithm>中的sort(与stable_sort相对应) ...
- JS高级调试技巧:捕获和分析 JavaScript Error详解
前端工程师都知道 JavaScript 有基本的异常处理能力.我们可以 throw new Error(),浏览器也会在我们调用 API 出错时抛出异常.但估计绝大多数前端工程师都没考虑过收集这些异常 ...
- Spring 4.3 的新功能和增强
转载自https://my.oschina.net/waylau/blog/698186 核心容器改进 核心容器额外提供了更丰富的元数据来改进编程. 默认 Java 8 的方法检测为 bean 属性的 ...
- bootstrap.min.css.map
问题:引入bootstrap..min.css的时候出现了URL:bootstrap.min.css.map 404的错误. 解决方法:将bootstrap.min.css里的最后一行/*# sour ...
- python绘制圆和椭圆
源自:https://blog.csdn.net/petermsh/article/details/78458585 1. 调用包函数绘制圆形Circle和椭圆Ellipse from matplot ...
- ffmpeg截取视频
ffmpeg -i ./suen071520.mp4 -vcodec copy -acodec copy -ss 00:55:00 -to 01:14:50 ./suen071520sp3.mp4-- ...
- 【Lintcode】095.Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...