线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers
题意:O(-1)
思路:O(-1)
线段树功能:update:成段增减 query:区间求和
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
题目大意:
N个数 M个操作 每个操作分Q a b得区间(a,b)的和;C a b c 区间[a,b]里面每个元素的value加c
由于是成段更新 要用到延迟标记type; 每次C操作 只更新到和某个节点的区间范围一致,下次在访问的时候 延迟标记下移
#include <cstdio>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 111111;
struct Tree{
__int64 value;
__int64 type;
}tree[maxn<<2]; void PushUp(int rt) {
tree[rt].value = tree[rt<<1].value + tree[rt<<1|1].value;
}
void PushDown(int rt,int ms) {
if (tree[rt].type) {
tree[rt<<1].type += tree[rt].type;
tree[rt<<1|1].type += tree[rt].type ;
tree[rt<<1].value += (ms - (ms >> 1)) * tree[rt].type;
tree[rt<<1|1].value += (ms >> 1) * tree[rt].type;
tree[rt].type = 0;
}
}
void build(int l,int r,int rt) {
tree[rt].type=0;
__int64 val;
if (l == r){
scanf("%I64d",&val);
tree[rt].value=val;
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt) {
if (L <= l && r <= R) {//标记1 //更新到这就行,不一定更新到最底端
tree[rt].type += c;
tree[rt].value += c * (r - l + 1);
return ;
}
PushDown(rt , r - l + 1);//延迟标记,这时候后推, (标记2)
//比如建了 [1,4] 然后我们第一次的时候是更新[1,2]
// [1,2][3,4] [1,2]的type就是非0,value往上更新;
// [1][2][3][4] 第二次如果还是要更新[1,2] 在标记1直接更新
int m = (l + r) >> 1; // 而如果你是要更新[1,1](必定访问到标记2) 那么先进行标记2 让两儿子的value更新
if (L <= m) update(L , R , c , lson); //记得这时候儿子type被赋值,自己type=0;
if (R > m) update(L , R , c , rson);
PushUp(rt);
}
__int64 query(int L,int R,int l,int r,int rt) {
if (L <= l && r <= R) {
return tree[rt].value;
}
PushDown(rt , r - l + 1); //注意这里
int m = (l + r) >> 1; //如果之前标记[1,4]type!=0 然后我们现在
__int64 ret = 0; //如果是访问[1,4]第一步直接得到返回值是没有问题
if (L <= m) ret += query(L , R , lson); //但访问[1,3]的时候要要把之前的[1,4]延迟标记下移
if (R > m) ret += query(L , R , rson);
return ret;
}
int main() {
int N , Q;
scanf("%d%d",&N,&Q);
build(1 , N , 1);
while (Q --) {
char op[2];
int a , b , c;
scanf("%s",op); //防止用%c 会接受回车 新技能get√
if (op[0] == 'Q') {
scanf("%d%d",&a,&b);
printf("%lld\n",query(a , b , 1 , N , 1));
} else {
scanf("%d%d%d",&a,&b,&c);
update(a , b , c , 1 , N , 1);
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- poj 3468 线段树 成段增减 区间求和
题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...
- 【POJ3468】【zkw线段树】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 【算法系列学习】线段树 区间修改,区间求和 [kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers
https://vjudge.net/contest/66989#problem/C #include<iostream> #include<cstdio> #include& ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
随机推荐
- Dynamics CRM 常用的JS
常用JS(一) Xrm.Page.context.getUserId(): //获取当前用户id Xrm.Page.context.getUserName(): //获取当前用 ...
- linux通过命令查找大文件
一:如果linux根分区使用量达到100%,会造成如下现象: root不能登录 系统不能正常启动 二:通过命令查找根分区内的大文件 1.du -sh /* 2>/dev/null | sort ...
- IDEA常见错误解决
tomcat控制台乱码 在tomcat的edit configurations里加入参数:-Dfile.encoding=UTF-8 导入的项目在重写时报 @Override is not all ...
- sublime_text3常用配置
安装(pojie)不再赘述. 一.设置字体与编码 preferences->Settings->Settings-User,在大括号中输入如下内容: “font_size”:16.0, “ ...
- 20155202 2016-2017-2 《Java程序设计》第10周学习总结
20155202 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 计算机网络基础 一.网络模型 模型分类: OSI,TCP/IP,五层协议的体系结构,以及各 ...
- 20155327 2016-2017-3 《Java程序设计》第4周学习总结
20155327 2016-2017-3 <Java程序设计>第4周学习总结 教材学习内容总结 一. 理解封装.继承.多态的关系 封装:把客观事物封装成抽象的类,并且类可以把自己的数据和方 ...
- 20155339 2016-2017-2 《Java程序设计》第1周学习总结
20155339 2016-2017-2 <Java程序设计>第1周学习总结 教材学习内容总结 第一章 一直以为JAVA应该只是一种语言,研读了书本之后发现原来JAVA也代表了解决问题的平 ...
- echarts 拐点添加图片
series : [ { name:'搜索引擎', type:'line', symbol:'emptyCircle', symbolSize: 5, itemStyle: { normal: { l ...
- installshield 判断mdmcpq.inf和usbser.sys 是否 存在
1.产品上位机程序,需要驱动支持,在安装 exe程序的时候,连同NET框架4.0和 .inf驱动文件,一起安装, 安装驱动的时候,会发现, 如果系统 C:\Windows\Inf 缺少mdmcpq. ...
- java 定义三分钟之前的时间
public String getCurrentTime(){SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ...