poj 3468 线段树区间更新/查询
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
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cmath>
#define ll long long
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
struct node
{
ll l,r;
ll add;
ll sum;
} tree[];
void buildtree(ll root,ll left,ll right)
{
tree[root].l=left;
tree[root].r=right;
tree[root].add=;//wa点 所有的延迟标记都要初始化
if(left==right)//不能放到下面的if中
{ ll exm;
scanf("%lld",&exm);
tree[root].sum=exm;
return ;
}
ll mid=(left+right)>>;
buildtree(root<<,left,mid);
buildtree(root<<|,mid+,right);
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
void pushdown(ll root)
{
if(tree[root].add==) return ;
tree[root<<].add+=tree[root].add;//wa点 这里是增加而不是赋值
tree[root<<|].add+=tree[root].add;
tree[root<<].sum+=(tree[root<<].r-tree[root<<].l+)*tree[root].add;
tree[root<<|].sum+=(tree[root<<|].r-tree[root<<|].l+)*tree[root].add;
tree[root].add=;
}
void updata(ll root,ll left,ll right,ll c)
{
if(tree[root].l==left&&tree[root].r==right)
{
tree[root].add+=c;//wa点 这里是增加而不是赋值
tree[root].sum+=(right-left+)*c;
return ;
}
pushdown(root);
ll mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
updata(root<<,left,right,c);
else
{
if(left>mid)
updata(root<<|,left,right,c);
else
{
updata(root<<,left,mid,c);
updata(root<<|,mid+,right,c);
}
}
tree[root].sum=tree[root<<].sum+tree[root<<|].sum;
}
ll query(ll root,ll left,ll right)
{
if(tree[root].l==left&&tree[root].r==right)
{
return tree[root].sum;
}
pushdown(root);
ll mid=(tree[root].l+tree[root].r)>>;
if(right<=mid)
return query(root<<,left,right);
else
{
if(left>mid)
return query(root<<|,left,right);
else
return query(root<<,left,mid)+query(root<<|,mid+,right);
}
}
ll n,q;
char what;
ll l1,r1,ad;
int main()
{
while(~scanf("%lld %lld",&n,&q))
{
buildtree(,,n);
getchar();
for(ll i=; i<=q; i++)
{
scanf("%c %lld %lld",&what,&l1,&r1);
if(what=='Q')
printf("%lld\n",query(,l1,r1));
else
{
scanf("%lld",&ad);
updata(,l1,r1,ad);
}
getchar();
}
}
return ;
}
/*
10 10
1 2 3 4 5 6 7 8 9 10
C 1 10 1
Q 2 3 10 10
1 2 3 4 5 6 7 8 9 10
C 1 5 1
Q 4 6
*/
poj 3468 线段树区间更新/查询的更多相关文章
- hdu 1698+poj 3468 (线段树 区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...
- POJ 3468 线段树区间修改查询(Java,c++实现)
POJ 3468 (Java,c++实现) Java import java.io.*; import java.util.*; public class Main { static int n, m ...
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- HDU 1698 Just a Hook(线段树区间更新查询)
描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...
- POJ 3468 (线段树 区间增减) A Simple Problem with Integers
这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...
- codevs 1299 线段树 区间更新查询
1299 切水果 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 简单的说,一共N个水果排成 ...
- POJ 3225 线段树区间更新(两种更新方式)
http://blog.csdn.net/niuox/article/details/9664487 这道题明显是线段树,根据题意可以知道: (用0和1表示是否包含区间,-1表示该区间内既有包含又有不 ...
- POJ 3225 (线段树 区间更新) Help with Intervals
这道题搞了好久,其实坑点挺多.. 网上找了许多题解,发现思路其实都差不多,所以就不在重复了. 推荐一篇比较好的题解,请戳这. 另外,如果因为可能要更新多次,但最终查询只需要一次,所以没有写pushup ...
- POJ - 3468 线段树区间修改,区间求和
由于是区间求和,因此我们在更新某个节点的时候,需要往上更新节点信息,也就有了tree[root].val=tree[L(root)].val+tree[R(root)].val; 但是我们为了把懒标记 ...
随机推荐
- treap 1296 营业额统计
有一个点答案错误,求大神指教 #include<cstdio>#include<iostream>#include<cstdlib>#include<ctim ...
- 根据图片Uri获得图片文件
2013-12-17 1. 根据联系人图片Uri获得图片文件并将它显示在ImageView上, 代码如下: Uri uri = Uri.parse("content://com.androi ...
- DrawerLayout一个简单的实例(与ActionBar无关)
官方的Demo里有DrawerLayout的例子,涉及到ActionBar,这里不用ActionBar,手痒,写个超级简单的小Demo,备着以后或许会用到. 详细的内容,可以访问:http://blo ...
- wp8.1 Study6: App的生命周期管理
一.概述 应用程序的生命周期详解可以参照Windows8.1开发中msdn文档http://msdn.microsoft.com/library/windows/apps/hh464925.aspx ...
- K2十年:专注BPM
<聚·谋·变——K2中国用户大会> 导演:K2中国 主演:K2用户 时长:420分钟 票价:免费 上映日期:2015年7月17日 查看完整视频请关注K2官方微信账号
- iOS 高效添加圆角效果实战讲解
圆角(RounderCorner)是一种很常见的视图效果,相比于直角,它更加柔和优美,易于接受.但很多人并不清楚如何设置圆角的正确方式和原理.设置圆角会带来一定的性能损耗,如何提高性能是另一个需要重点 ...
- mybatis分页插件PageHelper的使用(转)
Mybatis 的分页插件PageHelper-4.1.1的使用 Mybatis 的分页插件 PageHelper 项目地址:http://git.oschina.net/free/Mybatis_P ...
- Java基础毕向东day03
Java基础毕向东day03 1.变量 2.条件结构 3.循环结构,for while,和几种特殊的情况. 4.函数重载
- CAD系统变量(参数)大全
所谓系统变量就是一些参数,这些参数有些是可以在“选项”或其他对话框中进行设置的,有些这必须通过在命令行输入变量名进行设置,当然对于高手来说,还可以通过二次开发程序来进行控制. CAD有很多的变量,例如 ...
- CentOS中vsftp安装与配置
http://blog.chinaunix.net/uid-7271021-id-3086186.html 553 Could not create file 解决办法 [root@localhost ...