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. Wavesurfer.js音频播放器插件的使用教程

    Wavesurfer.js是一款基于HTML5 canvas和Web Audio的音频播放器插件,本文主要记录它及其视觉效果插件Regions插件的使用方法. 1.创建实例 引入插件 import W ...

  2. Redis的特性以及优势(附官网)

    NoSQL:一类新出现的数据库(not only sql) 泛指非关系型的数据库 不支持SQL语法 存储结构跟传统关系型数据库中的那种关系表完全不同,nosql中存储的数据都是KV形式 NoSQL的世 ...

  3. MySQL数据库实验:任务一 创建数据库和表

    目录 任务一 创建数据库和表 [实训目的与要求] [实训原理] [实训步骤] 一.熟悉MySQL环境 二.利用MySQL命令行窗口创建数据库及表 三.利用界面工具创建数据库及表 任务一 创建数据库和表 ...

  4. docker swarm实现java项目的发布/滚动更新/回滚/镜像管理

    使用docker swarm滚动更新java项目,部署集群,这一切的前提是使用Jenkins+maven进行项目打包,分发等功能 具体可以参考我的另外三篇文章 https://www.cnblogs. ...

  5. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  6. 'express'不是内部或外部命令, 也不是可运行的程序, 或批处理文件

    1. npm install -g express-generator 安装新的express框架2. express -h 错误提示: 'express'不是内部或外部命令, 也不是可运行的程序, ...

  7. Oracle入门第四天(上)——表管理与数据处理

    一.常见数据库对象 1.基本对象 对应的对象英文名参考:https://docs.oracle.com/cd/B19306_01/server.102/b14220/intro.htm#sthref6 ...

  8. 20155203 2016-2017-2《Java程序设计》课程总结

    目录 一.每周作业链接汇总 自认为写得最好一篇博客是?为什么? 作业中阅读量最高的一篇博客是?谈谈经验 作业中与师生交互最多的一篇博客是?谈谈收获 二.实验报告链接汇总 三.代码托管链接 四.课堂项目 ...

  9. 20155322 2016-2017-2 《Java程序设计》第三周学习总结

    20155322 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 本周学习的内容主要为教材的第四第五章,下面是总结: 第四章 主要讨论了五个问题:类与对象.基 ...

  10. 可能是全网首个支持阿里云Elasticsearch Xapck鉴权的Skywalking

    可能是全网首个支持阿里云Elasticsearch Xapck鉴权的Skywalking 对Skywalking有兴趣的同学参见:年轻人的第一个APM-Skywalking 之前在搭建Skywalki ...