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

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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


//更新某一段区域的时候,采用延迟标记~~
代码:
 #include "cstdio" //poj 3468 lazy操作
#include "cstring"
#include "iostream"
using namespace std; #define N 100005
#define LL long long struct node{
int x,y;
LL sum;
LL add; //记录以当前节点为根节点的树中需要增加的值
}a[*N]; void Build(int t,int x,int y)
{
a[t].x = x;
a[t].y = y;
a[t].sum = a[t].add = ;
if(a[t].x == a[t].y) //到了叶子节点
{
scanf("%lld",&a[t].sum);
return ;
}
int mid = (a[t].x + a[t].y)/;
Build(t<<,x,mid);
Build(t<<|,mid+,y);
a[t].sum = a[t<<].sum + a[t<<|].sum;
} void Push_down(int t) //将add(增值)向下推一级
{
LL add = a[t].add;
a[t<<].add += add;
a[t<<|].add += add;
a[t<<].sum += add*(a[t<<].y-a[t<<].x+);
a[t<<|].sum += add*(a[t<<|].y-a[t<<|].x+);
a[t].add = ;
} LL Query(int t,int x,int y)
{
if(a[t].x==x &&a[t].y==y)
return a[t].sum;
Push_down(t);
int mid = (a[t].x + a[t].y)/;
if(y<=mid)
return Query(t<<,x,y);
if(x>mid)
return Query(t<<|,x,y);
else
return Query(t<<,x,mid) + Query(t<<|,mid+,y);
} void Add(int t,int x,int y,int k)
{
if(a[t].x==x && a[t].y==y) //不在推到叶子节点,t下的子孙要增加的值存入a[t].add中
{
a[t].add += k;
a[t].sum += (a[t].y-a[t].x+)*k;
return ;
}
a[t].sum += (y-x+)*k;
Push_down(t);
int mid = (a[t].x+a[t].y)/;
if(y<=mid)
Add(t<<,x,y,k);
else if(x>mid)
Add(t<<|,x,y,k);
else
{
Add(t<<,x,mid,k);
Add(t<<|,mid+,y,k);
}
} int main()
{
int n,m;
char ch;
int x,y,k;
scanf("%d %d",&n,&m);
Build(,,n);
while(m--)
{
getchar();
scanf("%c %d %d",&ch,&x,&y);
if(ch=='Q')
printf("%lld\n",Query(,x,y));
else
{
scanf("%d",&k);
Add(,x,y,k);
}
}
return ;
}
 

数据结构--线段树--lazy延迟操作的更多相关文章

  1. 线段树区间更新操作及Lazy思想(详解)

    此题题意很好懂:  给你N个数,Q个操作,操作有两种,‘Q a b ’是询问a~b这段数的和,‘C a b c’是把a~b这段数都加上c. 需要用到线段树的,update:成段增减,query:区间求 ...

  2. POJ 2777——线段树Lazy的重要性

    POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...

  3. 分块+lazy 或者 线段树+lazy Codeforces Round #254 (Div. 2) E

    E. DZY Loves Colors time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. JuQueen(线段树 lazy)

    JuQueen Time Limit: 5 Sec  Memory Limit: 512 MB Description Input Output Sample Input 10 10 5 state ...

  5. hdu 3436 线段树 一顿操作

    Queue-jumpers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  6. Fast Arrangement (线段树,延迟标志)

    个人心得:线段树的延迟标志确实是减少了很多时间,思想比较简单,但是实现得时候和建立延迟的时候比较麻烦. 按照我的一些理解,就是更新时找到完全覆盖的区间时,更新延迟标志,不再往下更新,但此时父节点啥的都 ...

  7. HDU 3954 Level up(多颗线段树+lazy操作)

    又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...

  8. poj 3237 树链剖分模板(用到线段树lazy操作)

    /* 本体在spoj375的基础上加了一些操作,用到线段树的lazy操作模板类型 */ #include<stdio.h> #include<string.h> #includ ...

  9. 算法手记 之 数据结构(线段树详解)(POJ 3468)

    依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...

随机推荐

  1. 调试报“The source file is different from when the module was built.”问题的解决

    It is related to the checksums which is used to ensure that you are stepping in matching source. You ...

  2. 【循序渐进学Python】10.模块和包

    1.导入模块 任何Python程序都可以作为模块导入,只要Python解释器能找到我们定义的模块所在位置即可,一般来讲,在一个模块被导入时,Python解释器会按照下面的步骤进行搜索: 在当前所在目录 ...

  3. 基于Eclipse的Go语言可视化开发环境

    http://jingyan.baidu.com/article/d7130635032e2f13fdf475b8.html 基于Eclipse的Go语言可视化开发环境 | 浏览:2924 | 更新: ...

  4. AC自动机基础知识讲解

    AC自动机 转载自:小白 还可参考:飘过的小牛 1.KMP算法: a. 传统字符串的匹配和KMP: 对于字符串S = ”abcabcabdabba”,T = ”abcabd”,如果用T去匹配S下划线部 ...

  5. webform(内置对象)

    一.内置对象 (一)Response - 响应请求对象1.定义:Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response对象只提供了一个数据集合cookie ...

  6. Webform(内置对象-Response与Redirect、QueryString传值、Repeater删改)

    一.内置对象(一)Response - 响应请求对象1.定义:Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response对象只提供了一个数据集合cookie, ...

  7. datepicker冲突

    公司里的项目由于发展较快,很多东西都没有好好梳理一下,以至于有很多的潜在的问题. 最近就遇到了一个比较坑的问题.datepicker 有两个插件库中的datepicker插件比较有名.一个是jQuer ...

  8. thinkPHP学习笔记(1)

    现在对前端的要求越来越高了  基本上身为一个前端人员需要会一种后台语言,于是选择了当下流行的php.因为是自学对我这个不怎么懂代码的人来说还是有点难度的. 1.先看看thinkphp的目录结构 ├─T ...

  9. 在cmd中获取ip地址和主机名

    将下面的文件放到一个bat文件当中,以管理员身份运行. @echo off &setlocal enabledelayedexpansion Rem '/*========获取本机的IP地址( ...

  10. IOS UILabel 根据内容自适应高度

    iOS Label 自适应高度  适配iOS7以后的版本 更多 self.contentLabelView = [[UILabel alloc] init]; self.contentLabelVie ...