poj3468 A Simple Problem with Integers(线段树区间更新)
https://vjudge.net/problem/POJ-3468
线段树区间更新(lazy数组)模板题
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
ll sum[<<], lazy[<<];
void PushUP(int rt)
{
sum[rt] = sum[rt<<]+sum[rt<<|];
}
void PushDown(int rt, int m)
{
if(lazy[rt]){//若有标记下移
lazy[rt<<] += lazy[rt];
lazy[rt<<|] += lazy[rt];
sum[rt<<] += (m-(m>>))*lazy[rt];
sum[rt<<|] += (m>>)*lazy[rt];
lazy[rt] = ;//取消本层标记
}
}
void build(int l, int r, int rt)
{
lazy[rt] = ;//一定写在if外初始化
if(l == r){
scanf("%lld", &sum[rt]);
return ;
}
int m = (l+r)>>;
build(lson);
build(rson);
PushUP(rt);
}
ll Query(int L, int R, int l, int r, int rt)
{
if(L<=l&&R>=r){
return sum[rt];
}
PushDown(rt, r-l+);//向下更新
int m = (l+r)>>;
ll ans=;
if(L <= m) ans += Query(L, R, lson);
if(R > m) ans += Query(L, R, rson);
return ans;
}
void Update(int L, int R, ll add, int l, int r, int rt)
{
if(L<=l&&R>=r){
sum[rt] += (r-l+)*add;
lazy[rt] += add;
return ;
}
PushDown(rt, r-l+);//向下而更新
int m=(l+r)>>;
if(L <= m) Update(L, R, add, lson);
if(R > m) Update(L, R, add, rson);
PushUP(rt);
}
int main()
{
int n, m, a, b;
ll c;
char op[];
scanf("%d%d", &n, &m);
build(, n, );
for(int i = ; i < m; i++){
scanf("%s", &op);
if(op[] == 'Q'){
scanf("%d%d", &a, &b);
printf("%lld\n", Query(a, b, , n, ));
}
else{
scanf("%d%d%lld", &a, &b, &c);
Update(a, b, c, , n, );
}
}
return ;
}
poj3468 A Simple Problem with Integers(线段树区间更新)的更多相关文章
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- A Simple Problem with Integers 线段树 区间更新 区间查询
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 115624 Accepted: 35897 Case Time Lim ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
随机推荐
- bat批处理文件 批量修改文件名
需要将一系列文件如out-M0.png, out-M1.png, out-M2.png, ... 重新命名为 color-0.png, color-1.png, color-2.png, ... 方法 ...
- HTML LIST 输入框自动查询追加框,自动过滤 HTML5
<!DOCTYPE HTML> <html> <body> <form action="/example/html5/demo_form.asp&q ...
- Application Initialization UI for IIS 7.5
IIS Application Initialization for IIS 7.5 enables website administrators to improve the responsiven ...
- eclipse 编辑代码区字体大小
wiondow-->preferences-->general-->appearance-->colors and fonts-->java-->java edit ...
- sparkStreaming消费kafka-1.0.1方式:direct方式(存储offset到zookeeper)-- 2
参考上篇博文:https://www.cnblogs.com/niutao/p/10547718.html 同样的逻辑,不同的封装 package offsetInZookeeper /** * Cr ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第十二集之FastDFS的使用】
(自己的项目路径)相关项目在web部分中priv.lirenhe.fastdfs 官方提供一个fastdfs开发项目,下载下来maven install 以下内容是借鉴网上的一篇文章,但是不知道网址了 ...
- day15 函数的使用方法:递归函数
这里归纳的知识点主要就是: 浮点数,复数, int,整形:float,浮点数:complx,复数: # 实数: # 有理数 有限小数 1.2 # 无限循环小数 1.23232323232323.... ...
- 使用mini-textbox控件时 不能获取value值
当使用class=“mini-textbox”文本框时 使用document.getElementById('id').value 获得的文本框输入值是‘undefined’而使用正常的input 不 ...
- TF之AE:AE实现TF自带数据集AE的encoder之后decoder之前的非监督学习分类—Jason niu
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #Import MNIST data from t ...
- HDU 1257 最少拦截系统 【贪心】
<题目链接> 题目大意: 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度 ...