线段树---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 ...
随机推荐
- C# Redis写入程序
直接贴代码,需要引用ServiceStack.Common.dll,ServiceStack.Interfaces.dll,ServiceStack.Redis.dll,ServiceStack.Te ...
- Ubuntu16.04上安装neo4j数据库
什么是neo4j数据库? neo4j数据库是图数据库的一种,属于nosql的一种,常见的nosql数据库还有redis.memcached.mongDB等,不同于传统的关系型数据库,nosql数据也有 ...
- window7下 cmd命令行 Mysql导出表结构 + 表数据
命令格式 mysqldump -uroot -p 密码 库名 > 自定义路径/库名.sql
- python 银行系统
目前代码只写到这 主要部分已经实现 功能部分展现 首先我们需要五个类 用户类 : 成员属性 name id 以及 card 卡类: 成员属性 卡号 密码 余额 锁 界面类: 管理员界 ...
- stm32串口中断总结
串口文件uart.c需要被用到; 串口通信是对GPIO端口引脚的功能复用,因此需要用到gpio.c; 因为中断的产生,因此中断文件也是需要用到的: 中断响应函数需要自己编写: 接收中断:在接收移位寄存 ...
- MySQL数据库删除数据(有外键约束)
在MySQL中删除一张表或一条数据的时候,出现有外键约束的问题,于是就去查了下方法: SELECT @@FOREIGN_KEY_CHECKS; 查询当前外键约束是否打开 ; 设置为1的时候外键约束是打 ...
- Android接口与架构(驱动开发)翻译官方文档
Android接口与架构 Android在设备的规格与驱动方面给了你很大的自由来实现.HAL层提供了一个标准的方式来打通Android系统层与硬件层.Android系统是开源的,所以你能够在接口和性能 ...
- CentOS 7.2重启网络报错 Failed to start LSB: Bring up/down
CentOS 7.2重启网络报错 Failed to start LSB: Bring up/down 我的虚拟机原本有两块网卡,一块叫eno16777736,另一块叫eno5033674.本来是正常 ...
- Linux入门进阶第四天(下)——程序管理(补充内容)
1.PID 触发任何一个事件时,系统都会将他定义成为一个程序,并且给予这个程序一个 ID ,称为 PID,同时依据启发这个程序的使用者与相关属性关系,给予这个 PID 一组有效的权限设置. 同一个程序 ...
- 2016-2017-2 20155227实验三《敏捷开发与XP实践》实验报告
2016-2017-2 20155227实验三<敏捷开发与XP实践>实验报告 实验内容 一.实验内容 XP基础 XP核心实践 相关工具 二.实验过程 (一)敏捷开发与XP 1.XP是以开发 ...