A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 92921   Accepted: 28910
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.
题目大意:
给你n个数,然后会进行q次操作,Q a b为查询区间[a,b]所有数的和sum,
C a b c ,为将区间[a,b]所有数整体加c。
思路分析:如果只是改变某一个数的值,我们直接用一个裸的线段树来维护就行,但是这个题是
区间的值整体发生变化,如果再进行和以前一样的操作,则update的复杂度变成了LlogL(L为
区间长度)很不高效,势必超时,因此我们可以采用lazy标记的方法,用空间换时间,开一个lazy
数组,每次不必访问到根节点,只需要到更新的区间所在的节点就可以,大大提高了程序效率,只需要
明确lazy标记什么时候往儿子传就可以,一个是要往子节点更新,另一个是即将要查询子节点,都要将
lazy标记向下传一层,传给自己的儿子,增值会爆int,需要用long long
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn=+;
typedef long long ll;
ll tree[*maxn];
ll lazy[*maxn];
ll num[maxn];
void build (int p,int l,int r)
{
if(l==r) {tree[p]=num[l];return;}
int mid=(l+r)>>;
build(p<<,l,mid);
build((p<<)|,mid+,r);
tree[p]=tree[p<<]+tree[(p<<)|];
}
void pushdown(int p,int m)//把lazy标记下放到儿子节点
{
if(lazy[p])
{
lazy[p<<]+=lazy[p];
lazy[(p<<)|]+=lazy[p];
tree[p<<]+=(m-(m>>))*lazy[p];
tree[(p<<)|]+=(m>>)*lazy[p];
lazy[p]=;
}
}
void update(int p,int l,int r,int x,int y,int v)
{
if(x<=l&&y>=r)
{
lazy[p]+=v;
tree[p]+=(ll)v*(r-l+);//区间长度
return;
}
pushdown(p,r-l+);
int mid=(l+r)>>;
if(y<=mid) update(p<<,l,mid,x,y,v);
else if(x>mid) update((p<<)|,mid+,r,x,y,v);
else {update(p<<,l,mid,x,mid,v),update((p<<)|,mid+,r,mid+,y,v);}
tree[p]=tree[p<<]+tree[(p<<)|];
}
ll find(int p,int l,int r,int x,int y)
{
if(x<=l&&y>=r){return tree[p];}
pushdown(p,r-l+);
int mid=(l+r)>>;
ll ans=;
if(y<=mid) ans=find(p<<,l,mid,x,y);
else if(x>mid) ans=find((p<<)|,mid+,r,x,y);
else ans=find(p<<,l,mid,x,mid)+find((p<<)|,mid+,r,mid+,y);
return ans;
}
int main()
{
int n,q;
char s[];
int a,b,c;
while(~scanf("%d%d",&n,&q))
{
memset(tree,,sizeof(tree));
memset(lazy,,sizeof(lazy));
for(int i=;i<=n;i++)
scanf("%lld",&num[i]);
build(,,n);
while(q--)
{
scanf("%s",s);
if(s[]=='Q')
{
scanf("%d%d",&a,&b);
printf("%lld\n",find(,,n,a,b));
}
else
{
scanf("%d%d%d",&a,&b,&c);
update(,,n,a,b,c);
}
}
}
}

poj3468 线段树+lazy标记的更多相关文章

  1. POJ3237 Tree(树剖+线段树+lazy标记)

    You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbe ...

  2. 线段树+lazy标记 2019年8月10日计蒜客联盟周赛 C.小A的题

    题目链接:https://nanti.jisuanke.com/t/40852 题意:给定一个01串s,进行m次操作,|s|<=1e6,m<=5e5 操作有两种 l r 0,区间[l,r] ...

  3. HDU_1698 Just a Hook(线段树+lazy标记)

    pid=1698">题目请点我 题解: 接触到的第一到区间更新,须要用到lazy标记.典型的区间着色问题. lazy标记详情请參考博客:http://ju.outofmemory.cn ...

  4. POJ 3225 线段树+lazy标记

    lazy写崩了--. 查了好久 /* U-> [l,r]–>1 I-> [1,l-1] [r+1,+无穷] –>0 D-> [l,r]–>0 C-> [1,l ...

  5. 线段树+Lazy标记(我的模版)

    #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ...

  6. C++-POJ2777-Count Color[线段树][lazy标记][区间修改]

     分析:https://www.bilibili.com/read/cv4777102 #include <cstdio> #include <algorithm> using ...

  7. 线段树lazy标记??Hdu4902

    Nice boat Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  8. hdu-3397 Sequence operation 线段树多种标记

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目大意: 0 a b表示a-b区间置为0 1 a b表示a-b区间置为1 2 a b表示a- ...

  9. HDU 3468:A Simple Problem with Integers(线段树+延迟标记)

    A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...

随机推荐

  1. css图片磨砂化

    Css代码: .blur { filter: url(blur.svg#blur); /* FireFox, Chrome, Opera */ -webkit-filter: blur(10px); ...

  2. Collection Views and Building Custom Layouts-备

    UICollectionView的结构回顾 首先回顾一下Collection View的构成,我们能看到的有三个部分: Cells Supplementary Views 追加视图 (类似Header ...

  3. 『安全工具』Nmap 强悍的端口扫描工具

    作为时下流行的端口扫描工具,Nmap有因其扫描的隐密性有“端口扫描之王”之称 上图是黑客帝国(The Matrix)中崔妮蒂用Nmap入侵核发电站的能源管理系统 0x 01 Nmap介绍 Nmap是一 ...

  4. mybatis mapper namespace

    http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#insert_update_and_delete org.apache.ibatis.excep ...

  5. javascript 误用this指针 的情况

    理解了this指针后,我们再来看看一些很容易误用this指针的情况. 示例1——内联式绑定Dom元素的事件处理函数 <script type="text/javascript" ...

  6. 关于NAT穿透的一些理解

    前些天在知乎回答了一个智能家居远程控制方面的问题,感觉自己对无公网IP地址环境下的穿透问题有些了解.昨天同事拿来一个网络摄像头,安装在ADSL路由器上网的环境下,可以远程查看视频,效果还挺不错,问我厂 ...

  7. COJ 0802 非传统题(二)

    (颓了这么多天是时候干点正事了QAQ) 非传统题(二) 难度级别:B: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 还是很久很久以前,chx ...

  8. Android把图片保存到SQLite中

    1.bitmap保存到SQLite 中 数据格式:Blob db.execSQL("Create table " + TABLE_NAME + "( _id INTEGE ...

  9. POJ_1321——棋盘问题,回溯+剪枝

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  10. fuel部署openStack

    https://code.launchpad.net/fuel [fuel项目] http://www.imgburn.com/ [各种镜像制作工具]