poj 3468 A Simple Problem with Integers(线段树、延迟更新)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 74705 | Accepted: 22988 | |
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
Source
field=source&key=POJ+Monthly--2007.11.25">POJ Monthly--2007.11.25
, Yang Yi题目意思:给定Q (1 ≤ Q ≤ 100,000)个数A1,A2 … AQ,, 以及可能多次进行的两个操作:
1)对某个区间Ai … Aj的每一个数都加n(n可变) 2) 求某个区间Ai … Aj的数的和。
就是线段树的更新与查询操作,这里要用到延迟更新。
注意sum和add都要设成int64型,由于在更新过程中。add也可能会由于累加而超出int型的范围。
#include<stdio.h>
#include<string.h>
#define M 100005
struct tree{
int l,r;
__int64 sum,add;
}tree[M<<2];
void pushup(int root)
{
if(tree[root].l==tree[root].r)return;
tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
return;
}
void pushdown(int root)
{
if(tree[root].l==tree[root].r)return;
if(tree[root].add==0)return;
tree[root<<1].add+=tree[root].add;
tree[root<<1|1].add+=tree[root].add;
tree[root<<1].sum=tree[root<<1].sum+(tree[root<<1].r-tree[root<<1].l+1)*tree[root].add;
tree[root<<1|1].sum=tree[root<<1|1].sum+(tree[root<<1|1].r-tree[root<<1|1].l+1)*tree[root].add;
tree[root].add=0;
return;
}
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
tree[root].sum=0;
tree[root].add=0;
if(l==r){
tree[root].sum=0;
return;
}
int mid=l+r>>1;
build(l,mid,root<<1);
build(mid+1,r,root<<1|1);
pushup(root);
}
void update(int l,int r,int root,__int64 z)
{
if(tree[root].l==l&&tree[root].r==r)
{ tree[root].sum=tree[root].sum+(r-l+1)*z; tree[root].add=tree[root].add+z; //有可能出现多次的更新操作。所以要将全部的add都累加起来。
return;
}
pushdown(root);
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid)update(l,r,root<<1,z);
else if(l>mid)update(l,r,root<<1|1,z);
else {
update(l,mid,root<<1,z);
update(mid+1,r,root<<1|1,z);
}
pushup(root);
return;
}
__int64 Query(int l,int r,int root)
{
if(l==tree[root].l&&r==tree[root].r){
return tree[root].sum;
}
pushdown(root);
int mid=tree[root].l+tree[root].r>>1;
if(r<=mid)return Query(l,r,root<<1);
else if(l>mid)return Query(l,r,root<<1|1);
else {
return Query(l,mid,root<<1)+Query(mid+1,r,root<<1|1);
}
}
int main()
{
int N,Q,i,j,k,a,b;
__int64 c,d;
char s[20];
while(scanf("%d%d",&N,&Q)!=EOF)
{
build(1,N,1);
for(i=1;i<=N;i++)
{
scanf("%I64d",&d);
update(i,i,1,d);
}
while(Q--)
{
scanf("%s%d%d",s,&a,&b);
if(s[0]=='Q'){
printf("%I64d\n",Query(a,b,1));
}
if(s[0]=='C'){
scanf("%I64d",&c); update(a,b,1,c); }
}
}
return 0;
}
poj 3468 A Simple Problem with Integers(线段树、延迟更新)的更多相关文章
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- POJ 3468 A Simple Problem with Integers 线段树 区间更新
#include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...
- POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
随机推荐
- 企业支付宝账号开发接口教程--JAVA-UTF-8(实际操作完善中...SpringMVC+JSP)
关于即时到账的开发.审核通过.简单测试如下. 希望看的可以收藏或者赞一下哦. 1:拥有自己的支付宝企业账号.去产品商店选择适合自己的方案.并签约合同. 2:选择合适的商家收款产品并去签约.填写相应的信 ...
- 核心动画中的几种layer
第10章其他有用的层 免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播.同时,转载时不要移除本申明.如产生任何纠纷,均与本博客所有人.发表该翻译稿之 ...
- 【简●解】[ZJOI2005]午餐
[简●解][ZJOI2005]午餐 [关键词] \(DP\) 排序/贪心 [分析] 首先,一个很明显的贪心思路,就是吃的慢的人先打饭.所以把数据按吃饭时间从大到小排一遍序. 根据\(dp\)的尿性,比 ...
- hdu3094 A tree game
题目描述 题解: 树上删边. $SG[u]$^=$SG[son[u]]+1$ 代码: #include<cstdio> #include<cstring> ; template ...
- Java性能调优概述
目录 Java性能调优概述 性能优化有风险和弊端,性能调优必须有明确的目标,不要为了调优而调优!!!盲目调优,风险远大于收益!!! 程序性能的主要表现点 执行速度:程序的反映是否迅速,响应时间是否足够 ...
- 条款6:若不想使用编译器自动生成的函数,就该明确拒绝(Explicity disallow the use of compiler-generated functions you do not want)
class uncopyable{ protected: uncopyable(){}; ...
- Java多线程入门Ⅱ
线程的让步 线程让出自己占用的CPU资源 线程让出资源,不指定让给谁 线程让出资源,指定让给谁 方法1: public static void yield(); 线程实现交替打印 import jav ...
- 【BZOJ 3555】 [Ctsc2014]企鹅QQ(哈希)
Description PenguinQQ是中国最大.最具影响力的SNS(Social Networking Services)网站,以实名制为基础,为用户提供日志.群.即时通讯.相册.集市等丰富强大 ...
- tomcat——大致简介和执行过程
jsp简介 JSP: JAVA Server Page 使用JAVA语言编写的一种在服务器运行的动态页面 JSP = JAVA + HTML JSP 的执行过程 1: 翻译阶段 把JSP源文件翻译成 ...
- 大数据学习——hadoop2.x集群搭建
1.准备Linux环境 1.0先将虚拟机的网络模式选为NAT 1.1修改主机名 vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=itcast ### ...