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

The sums may exceed the range of 32-bit integers.

题目大意:  

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:成段增减:区间求和的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  2. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...

  3. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 57666   ...

  4. 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 ...

  5. 【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 ...

  6. 【算法系列学习】线段树 区间修改,区间求和 [kuangbin带你飞]专题七 线段树 C - A Simple Problem with Integers

    https://vjudge.net/contest/66989#problem/C #include<iostream> #include<cstdio> #include& ...

  7. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  8. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  9. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

随机推荐

  1. Sppring MVC核心应用-2

    一.Spring MVC框架中400状态码的请求错误:控制台BindException异常, 解决方法: 二.Sping 表单标签 三.数据校验 实现JSR 303验证步骤 四.REST风格 五.Sp ...

  2. Centos7.5搭建Hadoop2.8.5完全分布式集群部署

    一.基础环境设置 1. 准备4台客户机(VMware虚拟机) 系统版本:Centos7.5 节点配置: 192.168.208.128 --Master 192.168.208.129 --Slave ...

  3. helpera64开发板下制作ubuntu rootfs镜像(二)

    上一篇路径:https://www.cnblogs.com/jizizh/p/10380513.html Helpera64开发板ubuntu剩于工作: 1.背光调节 答:/sys/class/bac ...

  4. 利用Docker设置Node.js

      docker是一个开源的应用容器引擎,可以为我们提供安全.可移植.可重复的自动化部署的方式.docker采用虚拟化的技术来虚拟化出应用程序的运行环境.如上图一样.docker就像一艘轮船.而轮船上 ...

  5. 【树形DP】MZOJ_1063_士兵守卫

    本题也是这三天来在下写的几篇树形DP之一,但是不知道为什么洛谷上面老是unknown error,...直接去了UVa,说我编译错误...我在想是不是头文件的原因,于是被逼无奈,交了一道c89的代码. ...

  6. 20155337祁家伟 2016-2017-2 《Java程序设计》第2周学习总结

    20155337 2016-2017-2 <Java程序设计>第2周学习总结 教材学习内容总结 这周我学习了从JDK到IDE的学习内容,简单来说分为以下几个部分 使用命令行和IDE两种方式 ...

  7. 微信小程序居中代码

    html页面: { text-align:center; } wxss页面: { width: 100%; height: 100%; display: flex; justify-content: ...

  8. PHP 行为测试工具 Codeception (介绍)

    原文地址:https://phphub.org/topics/25 Codeception 简介 Codeception 简单来说, 分为以下几种测试 Acceptance Tests 验收测试 Fu ...

  9. Mac 安装PHP Redis 扩展

    其实 Mac 安装 Redis 还是很简单,以下为个人搭建配置.注意:文章中的“*”代表任意版本号 安装 Redis 服务 安装 brew install redis 使用 # 启动 redis-se ...

  10. Maven学习(十四)-----Maven 构建配置文件

    Maven 构建配置文件 什么是构建配置文件? 生成配置文件是一组可以用来设置或覆盖 Maven 构建配置值的默认值.使用生成配置文件,你可以针对不同的环境,如:生产V/S开发环境自定义构建. 配置文 ...