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的任 ...
随机推荐
- CF(439E - Devu and Birthday Celebration)莫比乌斯容斥
题意:将n个糖果插入f-1个挡板分成f分(a1,a2,a3...af). 问有多少种分法能够使得gcd(a1,a2,a3...af)=1; 解法.莫比乌斯容斥,首先按1为单位分,这时候有C(n-1,f ...
- PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP
$URL['PHP_SELF'] = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : (isset($_SERVER['SCRIPT_NAME ...
- CSS相对|绝对(relative/absolute)定位
1.破坏性和包裹性 position:absolute与float:left,两者有两大共性:包裹性,破坏性. 包裹性 包裹性换种说法就是让元素inline-block化,例如一个div标签默认宽度是 ...
- HTML5/CSS3超酷环形动画菜单
在线演示 本地下载
- View源码-Touch事件
在Android-27中查看源码: 首先我们来查看单个View的触摸事件的处理,在View的dispatchTouchEvent方法中看看源码是如何处理的. public boolean dispat ...
- SqlServer 按逗号分隔
SELECT ORDER_ID,LTRIM(MAX(SYS_CONNECT_BY_PATH(GOODS_NAME, ',')), ',') GOODS_NAME FROM (SELECT GOODS_ ...
- JS高级调试技巧:捕获和分析 JavaScript Error详解
前端工程师都知道 JavaScript 有基本的异常处理能力.我们可以 throw new Error(),浏览器也会在我们调用 API 出错时抛出异常.但估计绝大多数前端工程师都没考虑过收集这些异常 ...
- Contiki clock模块
一.functions for handling system time clock_time_t clock_time(void);//return the current system time ...
- js/html/css做一个简单的图片自动(auto)轮播效果//带注释
FF(firefox)/chrom/ie稳定暂无bug...注意:请自己建立一个images文件,放入几张900*238的图片(注意图片格式和名字与程序中一致). 1. [图片] 1.JPG 2. [ ...
- idea创建maven-archetype-webapp项目无java目录
使用idea创建一个maven-archetype-webapp项目 查看项目的目录结构,在main的目录下没有java目录 在main目录下创建java目录 使用快捷键 ctrl+alt+shift ...