我的线段树真的没救了......还是多练几道吧.......

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

Hint

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

题意:给出一个数字串,有两个操作:1. 求l-r之间的和 2.将l-r的每个数加x

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 200010
#define lson root<<1
#define rson root<<1|1
using namespace std; long long node[N<<],lazy[N<<];
int m,n; void pushup(int root)
{
node[root]=node[lson]+node[rson];
} void pushdown(int root,int len)
{
if(lazy[root])
{
lazy[lson]+=lazy[root];
lazy[rson]+=lazy[root];
node[lson]+=(len-(len>>))*lazy[root];
node[rson]+=(len>>)*lazy[root];
lazy[root]=;
}
} void build(int l,int r,int root)
{
lazy[root]=;
if(l==r)
{
scanf("%lld",&node[root]);
return;
}
int mid=(l+r)>>;
build(l,mid,lson);
build(mid+,r,rson);
pushup(root);
} void update(int left,int right,int val,int l,int r,int root)
{
if(left<=l&&right>=r)
{
lazy[root]+=val;
node[root]+=val*(r-l+);
return;
}
pushdown(root,r-l+);
int mid=(l+r)>>;
if(left<=mid)
{
update(left,right,val,l,mid,lson);
}
if(right>mid)
{
update(left,right,val,mid+,r,rson);
}
pushup(root);
} long long query(int left,int right,int l,int r,int root)
{
if(left<=l&&right>=r)
{
return node[root];
}
pushdown(root,r-l+);
int mid=(l+r)>>;
long long ans=;
if(left<=mid)
{
ans+=query(left,right,l,mid,lson);
}
if(right>mid)
{
ans+=query(left,right,mid+,r,rson);
}
return ans;
} int main()
{
scanf("%d%d",&n,&m);
build(,n,);
for(int i=;i<=m;i++)
{
char s;
int a,b,c;
scanf(" %c",&s);
if(s=='Q')
{
scanf(" %d %d",&a,&b);
printf("%lld\n",query(a,b,,n,));
}
else
{
scanf(" %d %d %d",&a,&b,&c);
update(a,b,c,,n,);
}
}
return ;
}

每天刷题,身体棒棒!

POJ3468(线段树 区间修改 lazy-tag)的更多相关文章

  1. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  2. 题解报告:poj 3468 A Simple Problem with Integers(线段树区间修改+lazy懒标记or树状数组)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  4. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  5. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  6. HDU 3911 Black And White(线段树区间合并+lazy操作)

    开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...

  7. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...

  8. poj3468(线段树区间更新&区间求和模板)

    题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...

  9. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

随机推荐

  1. String类的构造方法(2)

    写了常见的几个而已. 1:new 一个String类的时候系统会自动传一个空构造 public String(); 注意: 当对象初始化是 null时 和 对象是 "" 时,两者是 ...

  2. Python 接口测试(四)

    五:python数列化和反序列化 把python的对象编码转换为json格式的字符串,反序列化可以理解为:把json格式 字符串解码为python数据对象.在python的标准库中,专门提供了json ...

  3. oracle数据库备份、还原 (如何将Oracle 11g备份的dat文件导入到10g数据库里面)

    如何将Oracle 11g备份的dat文件导入到10g数据库里面 解决方法:      导出的时候后面加上目标数据库的版本号   导出: 在SQL plus下执行:create or replace  ...

  4. Sublimetext3安装Emmet插件步骤

    看清楚哦~~这是Sublime text 3不是2的版本,两者的安装还是有区别的,下面的方法是我感觉比较简单的,其他的要命令什么的感觉太复杂了,经测试是OK的. 先关闭Sublime text 3: ...

  5. js X年X周 转成 具体日期

    function getWeekDate(theyear,weekcount) { var year = theyear; var week = weekcount; if(year=="& ...

  6. 简单的CSS颜色查看工具

    可以通过输入ARGB(A代表透明度)格式或者HEX格式查看颜色,也可以进行ARGB格式和者HEX格式转换,如下图 使用C#编写,我已将源代码压缩上传 下载地址:http://files.cnblogs ...

  7. GPUImage库的使用

    GPUImage开源项目地址:https://github.com/BradLarson/GPUImage GPUImage使用说明:https://github.com/BradLarson/GPU ...

  8. Appium python自动化测试系列之appium环境搭建(二)

    ​2.1 基础环境搭建 当我们学习新的一项技术开始基本都是从环境搭建开始,本书除了第一章节也是的,如果你连最基础的环境都没有那么我们也没必要去说太多,大概介绍一下: 1.因为appium是支持andr ...

  9. appium python ios 自动化

    mac下搭建appium python selenium来针对ios应用进行自动化测试,并不是官网上的例子,自己程序调试成功. 前言:因为appium的安装前要先确定nodejs的安装.python的 ...

  10. Android模拟器检测常用方法

    在Android开发过程中,防作弊一直是老生常谈的问题,而模拟器的检测往往是防作弊中的重要一环,接下来有关于模拟器的检测方法,和大家进行一个简单的分享. 1.传统的检测方法. 传统的检测方法主要是对模 ...