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的任 ...
随机推荐
- CUDA:零拷贝主机内存
The easy way to achieve copy/compute overlap!1.Enable Host Mapping* Runtime: cudaSetDeviceFlags() wi ...
- div中p标签自动换行
只需要设置div的width属性,p标签加上word-break:break-word属性就会自动换行 ----------------2016.7.1-------------------- 今天在 ...
- linux批量更改权限
用命令 sudo chmod 777 -Rfv /home/name/* 注释:1.777 为 要修改成 的 文件的 权限:2.-R 是 子目录 下的 文件 也修改:3.-f 强制:4. -v是 显示 ...
- 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛 G 旋转矩阵 【模拟】
链接:https://www.nowcoder.com/acm/contest/90/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 构建工具是如何用 node 操作 html/js/css/md 文件的
构建工具是如何用 node 操作 html/js/css/md 文件的 从本质上来说,html/js/css/md ... 源代码文件都是文本文件,文本文件的内容都是字符串,对文本文件的操作其实就是对 ...
- c语言学习的第13天1
#include <stdio.h> int main(void) { int a[5]={1,2,3,4,5}; printf("%#x, %#x\n",a,& ...
- frame标签
frame中有一个属性scrolling,可以这样设置它 <frame src="top.html" noresize scrolling="no"/&g ...
- C语言实现wc基本功能
GitHub地址:https://github.com/hhx007/wc 项目要求 wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数. 这个项目要求写一个命令行程序,模仿已有w ...
- Css公共文件结构
一般一个网站会有这么三个样式: global.css | reset.css(格式化样式) common.css(公共组件样式) layout.css(当前页面样式) 清除全站所有页面的浏览器默认样式 ...
- POJ3468 A Simple Problem with Integers(数状数组||区间修改的RMQ问题)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...