A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 75541   Accepted: 23286
Case Time Limit: 2000MS

Description

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

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.

Source

朴素的向下更新会产生很多不必要的操作,所以lazy思想便是,用到再更新,不用就暂存到当前区间。

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <climits>
#include <queue>
#define ll long long using namespace std; //lazy思想的线段树区间更新,区间查询,感觉不难 const int MAX = ;
ll num[MAX]; struct nodes
{
int left,right;
ll large,add;
} tree[MAX*]; void pushup(int root)//代替递归向上更新
{
tree[root].large = tree[root*].large + tree[root*+].large;
}
void pushdown(int root)//递归向下更新
{
if(tree[root].add)
{
tree[root*].add += tree[root].add;
tree[root*+].add += tree[root].add;
tree[root*].large += tree[root].add * (tree[root*].right - tree[root*].left + );
tree[root*+].large += tree[root].add * (tree[root*+].right - tree[root*+].left + );
tree[root].add = ;
}
}
void build(int root,int left,int right)//建树过程,也是初始化更新过程
{
tree[root].left = left;
tree[root].right = right;
tree[root].add = ;//不能忘记这个值的初始化为0
if(left == right)
{
tree[root].large = num[left];
return;//注意这里的递归结束条件
} int mid = (left+right)/; build(*root,left,mid);
build(*root+,mid+,right); pushup(root);//该子区间建立好后,该区间更新
} void update(int root,int left,int right,ll val)
{
if(left == tree[root].left && right == tree[root].right) // 刚好进入到所需区间,就可以更新到当前区间直接返回
{
tree[root].add += val; // 用当前节点的add变量记录区间每个元素的累加量
tree[root].large += val * (right - left + );//更新当前区间和
return;
}
pushdown(root); //否则向下更新一层,继续找合适区间
int mid = (tree[root].left+tree[root].right)/;
if(left <= mid && right > mid)
{
update(*root,left,mid,val);
update(*root+,mid+,right,val);
}
else if(left > mid)
{
update(*root+,left,right,val);
}
else
{
update(*root,left,right,val);
}
pushup(root);
} ll query(int root ,int left,int right)
{
if(left == tree[root].left && right == tree[root].right) //如果是合适区间,直接返回节点值
{
return tree[root].large;
}
pushdown(root); //否则向下更新一层,继续查找合适区间
int mid = (tree[root].left+tree[root].right)/;
ll ans = ;
if(right > mid && left <= mid)
{
ans += query(*root,left,mid);
ans += query(*root+,mid+,right);
}
else if(left > mid)
{
ans += query(*root+,left,right);
}
else
{
ans += query(*root,left,right);
}
return ans;
} int main(void)
{
int n,q,i,x,y;
ll c;
char cmd;
scanf("%d %d",&n,&q);
for(i = ; i <= n; i++)
scanf("%lld",&num[i]);
build(,,n);
for(i = ; i < q; i++)
{
getchar();
scanf("%c",&cmd);
if(cmd == 'Q')
{
scanf("%d %d",&x,&y);
printf("%lld\n",query(,x,y));
}
else
{
scanf("%d %d %lld",&x,&y,&c);
update(,x,y,c);
}
}
return ;
}

poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)的更多相关文章

  1. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  2. (简单) 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 ...

  3. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  4. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

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

  5. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  6. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  7. POJ 3468 A Simple Problem with Integers 线段树 区间更新

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  8. POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)

    伸展数最基本操作的模板,区间求和,区间更新.为了方便理解,特定附上一自己搞的搓图 这是样例中的数据输入后建成的树,其中的1,2是加入的边界顶点,数字代表节点编号,我们如果要对一段区间[l, r]进行操 ...

  9. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

随机推荐

  1. 【默默努力】react-drag-grid

    先放项目地址:https://github.com/Bilif/react-drag-grid 项目运行效果 感谢无私开源的程序员 先看项目入口文件 //index.js import React f ...

  2. Android 开发 MediaRecorder音频录制

    前言 MediaRecorder类是Android sdk提供的一个专门用于音视频录制,一般利用手机麦克风采集音频和摄像头采集图像.这个类是属于简单的音频录制类,录制音频简单容易但是对音频流的控制也比 ...

  3. 廖雪峰Java11多线程编程-3高级concurrent包-9Fork_Join

    线程池可以高效执行大量小任务: Fork/Join线程池可以执行一种特殊的任务: 把一个大任务拆成多个小任务并行执行 Fork/Join是在JDK 1.7引入的 示例:计算一个大数组的和 Fork/J ...

  4. java使用stream流批量读取并合并文件,避免File相关类导致单文件过大造成的内存溢出。

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  5. webGL动画

    在做这个项目之前,我也和很多人的想法一样觉得:H5做动画性能不行,只能完成简单动画,可是事实并非如此.所以借此篇分享振奋下想在H5下做酷炫游戏的人心. 体验游戏请长按二维码识别: 好吧,知道你懒.不想 ...

  6. 01_Hibernate持久化

    一.简介 思考:为什么使用Hibernate? Hibernate对JDBC访问数据库的代码进行了封装. Hibernate是一个基于JDBC的主流持久化框架. Hibernate的性能比较好,它是一 ...

  7. mybatis的Example的使用

    MyBatis的Mapper接口以及Example的实例函数及详解 一.mapper接口中的方法解析 mapper接口中的函数及方法 方法 功能说明 int countByExample(UserEx ...

  8. Linux下使用SSH命令行传输文件到远程服务器

    目标:CentOS 7 调整 home分区 扩大 root分区 总体过程: 把/home内容备份,然后将/home文件系统所在的逻辑卷删除,扩大/root文件系统,新建/home ,恢复/home内容 ...

  9. urllib与urllib2的学习总结

    先啰嗦一句,我使用的版本是python2.7,没有使用3.X的原因是我觉得2.7的扩展比较多,且较之前的版本变化不大,使用顺手.3.X简直就是革命性的变化,用的蹩手.3.x的版本urllib与urll ...

  10. Joining Byte Blocks(哈希+带花树)

    题目链接 Problem Statement As you are probably aware, the Internet protocols specify a canonical byte or ...