poj3468(线段树区间更新&区间求和模板)
题目链接: http://poj.org/problem?id=3468
题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和;
C x y z 表示区间 [x, y] 内所有数加上 z ;
思路: 线段树区间更新&区间求和模板;
代码:
#include <iostream>
#include <stdio.h>
#define ll long long
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; const int MAXN = 1e5 + ;
ll sum[MAXN << ];
ll add[MAXN << ]; void push_up(int rt){//向上更新
sum[rt] = sum[rt << ] + sum[rt << | ];
} void push_down(int rt, int m){
if(add[rt]){//若有标记,则将标记向下移动一层
add[rt << ] += add[rt];
add[rt << | ] += add[rt];
sum[rt << ] += (m - (m >> )) * add[rt];
sum[rt << | ] += (m >> ) * add[rt];
add[rt] = ;//取消本层标记
}
} void build(int l, int r, int rt){//建树
add[rt] = ;
if(l == r){
scanf("%lld", &sum[rt]);
return;
}
int mid = (l + r) >> ;
build(lson);
build(rson);
push_up(rt);//向上更新
} void update(int L, int R, ll key, int l, int r, int rt){//区间更新
if(L <= l && R >= r){
sum[rt] += (r - l + ) * key;
add[rt] += key;
return;
}
push_down(rt, r - l + );//向下更新
int mid = (l + r) >> ;
if(L <= mid) update(L, R, key, lson);
if(R > mid) update(L, R, key, rson);
push_up(rt);//向上更新
} ll query(int L, int R, int l, int r, int rt){//区间求和
if(L <= l && R >= r) return sum[rt];
push_down(rt, r - l + );//向下更新
int mid = (l + r) >> ;
ll ans = ;
if(L <= mid) ans += query(L, R, lson);
if(R > mid) ans += query(L, R, rson);
return ans;
} int main(void){
int n, m;
scanf("%d%d", &n, &m);
build(, n, );
while(m--){
char str[];
int x, y;
ll z;
scanf("%s", str);
if(str[] == 'C'){
scanf("%d%d%lld", &x, &y, &z);
update(x, y, z, , n, );
}else{
scanf("%d%d", &x, &y);
printf("%lld\n", query(x, y, , n, ));
}
}
}
poj3468(线段树区间更新&区间求和模板)的更多相关文章
- 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 ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
- hdu2795(线段树单点更新&区间最值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 题意:有一个 h * w 的板子,要在上面贴 n 条 1 * x 的广告,在贴第 i 条广告时要 ...
- HDU 1166 敌兵布阵(线段树点更新区间求和裸题)
Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任 ...
- hdu1394(枚举/树状数组/线段树单点更新&区间求和)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给出一个循环数组,求其逆序对最少为多少: 思路:对于逆序对: 交换两个相邻数,逆序数 +1 ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- HDU 3308 LCIS(线段树单点更新区间合并)
LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...
随机推荐
- smartforms 二维码打印
1. 安装TBarCode_SAPwin软件 1) 下载Barcode软件 下载TBarCode_SAPwin 软件.(如需生成SAP"字符控制序列"则需一并下载TBarCode ...
- me12里更改信息记录的净价和有效价格,以及信息记录的条件价格
转自 http://blog.csdn.net/zeewjj/article/details/7941525REPORT ztest. DATA:l_kbetr LIKE konp-kbetr.l_k ...
- org.apache.catalina.Lifecycle
org.apache.catalina.Lifecycle start() * ----------------------------- * | ...
- graphics基础
- (2)struts2配置祥解
struts工作流程 反射 : 1.构造对象使用构造器 //类似为Servlet public class AddAction { public AddAction(){ System.out.pri ...
- python的上下文管理器
直接上代码: f = open('123.txt','w') try: f.write('hello world') except Exception: pass finally: f.close() ...
- LoadRunner中的函数
函数是LoadRunner提供给性能测试工程师的利器,有了它,性能测试工程师可以对脚本进行更为自由的开发,更适应实际测试的需求,进一步扩展脚本的功能. LoadRunner函数的格式: 返回值 函数 ...
- CF785CAnton and Permutation(分块 动态逆序对)
Anton likes permutations, especially he likes to permute their elements. Note that a permutation of ...
- bzoj 3796 Mushroom追妹纸 —— 后缀数组
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3796 先把三个串拼在一起,KMP 求 s1 , s2 中每个位置和 s3 的匹配情况: 注意 ...
- WPF 后台触发 Validate UI‘s Element
wpf中有validateRule类, 用于界面元素的验证, 如何后台去控制validateRule呢? 1. UI层要binding写好的ValidateRule,分为Binding和MultiBi ...