POJ3468(线段树 区间修改 lazy-tag)
我的线段树真的没救了......还是多练几道吧.......
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
题意:给出一个数字串,有两个操作: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)的更多相关文章
- 题解报告: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 ...
- 题解报告: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 ...
- 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序加上一个线 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- HDU 3911 Black And White(线段树区间合并+lazy操作)
开始以为是水题,结果...... 给你一些只有两种颜色的石头,0为白色,1为黑色. 然后两个操作: 1 l r 将[ l , r ]内的颜色取反 0 l r 计算[ l , r ]内最长连续黑色石头的 ...
- 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 ...
- poj3468(线段树区间更新&区间求和模板)
题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...
- poj2528 Mayor's posters(线段树区间修改+特殊离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
随机推荐
- HTML 4.01+5基礎知識
HTML 4.01+5 1.Html結構:html>head+body 2.Html快捷鍵:!加Tab(在sublime中) 3.雙標籤: ①常用標籤 h1.h2.h3.h4.h5.h6 p.c ...
- IOS SDWebImage实现基本原理详解(转载)
1)当我门需要获取网络图片的时候,我们首先需要的便是URl没有URl什么都没有,获得URL后我们SDWebImage实现的并不是直接去请求网路,而是检查图片缓存中有没有和URl相关的图片,如果有则直接 ...
- Oracle的trim( )、ltrim( )、rtrim( )三个函数的用法及注意事项
学习一下用法整理trim().ltrim().rtrim()的用法 trim().ltrim().rtrim()三个函数有两个作用,分别是: 一.去除字符串前后空格(基本用法) trim(string ...
- "svn: E155010: 提交失败"问题解决
习惯于通过命令行操作svn,今天如往常一样提交代码: AnnytekiMacBook-Air:weiyibao Anny$ svn ci -m "code" 居然报错,如下: sv ...
- LCM Cardinality 暴力
LCM Cardinality Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit St ...
- Palindrome poj3974
Palindrome Time Limit: 15000MS Memory Limit: 65536K Total Submissions: 3280 Accepted: 1188 Descr ...
- SpringMVC加载.roperties文件属性值的方法?
1.在xml文件中引入来获取属性值就不说了. 2.在controller层获取引用配置文件中的属性值: (1).编写工具类 @Configuration @PropertySource(value=& ...
- Python系列之多线程、多进程
线程是操作系统直接支持的执行单元,因此,高级语言通常都内置多线程的支持,Python也不例外,并且,Python的线程是真正的Posix Thread,而不是模拟出来的线程. Python的标准库提供 ...
- mongodb生产环境(副本集模式)集群搭建配置
mongodb副本集模式由如下几部分组成: 1.路由实例mongos 2.配置实例configsvr 3.副本集集群replset(一主多从) tips: 1.以上实例都是mongod守护进程 2.以 ...
- SQL server 数据库备份大
首先简单的介绍一下Sql server 备份的类型有: 1:完整备份(所有的数据文件和部分的事务日志文件) 2:差异备份(最后一次完成备份后数据库改变的部分) 3:文件和文件组备份(对指定的文件和文件 ...