poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem?
id=3468
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 83959 | Accepted: 25989 | |
| 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
对于更新树是为了避免改动到最底下而导致超时问题。所以每次改动仅仅改动相相应的区间就可以。然后记录一个add。下次更新或者查询的时候,假设查到该节点,就把add直接加到子节点上去,在将add变为0,避免下次还会反复加。这样仅仅更新到查询的子区间,不须要再往下找了,所以时间复杂度为O(n),更新树和查询树都须要这样。
由于add不为0,该add从根一直加到了该节点,之前的都加过了,假设更新到时候不加到子节点。还要通过子节点更新当前节点,当前节点的sum值里面含有的add就会被“抹掉”,就不能保证正确性了。还须要注意的就是要用__int64。
#include <iostream>
#include <cstdio> using namespace std; #define LL __int64 struct node
{
int l,r;
LL sum;
LL add;
//int flag;//用来表示有几个加数
} s[100000*4]; void InitTree(int l,int r,int k)
{
s[k].l=l;
s[k].r=r;
s[k].sum=0;
s[k].add=0;
if (l==r)
return ;
int mid=(l+r)/2;
InitTree(l,mid,2*k);
InitTree(mid+1,r,2*k+1);
} void UpdataTree(int l,int r,LL add,int k)
{ if (s[k].l==l&&s[k].r==r)
{
s[k].add+=add;
s[k].sum+=add*(r-l+1);
return ;
}
if (s[k].add!=0)//加数为0就不须要改变了
{
s[2*k].add+=s[k].add;
s[2*k+1].add+=s[k].add;
s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1);
s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1);
s[k].add=0;
}
int mid=(s[k].l+s[k].r)/2;
if (l>mid)
UpdataTree(l,r,add,2*k+1);
else if (r<=mid)
UpdataTree(l,r,add,2*k);
else
{
UpdataTree(l,mid,add,2*k);
UpdataTree(mid+1,r,add,2*k+1);
}
s[k].sum=s[2*k].sum+s[2*k+1].sum;
} LL SearchTree(int l,int r,int k)
{
if (s[k].l==l&&s[k].r==r)
return s[k].sum;
if (s[k].add!=0)
{
s[2*k].add+=s[k].add;
s[2*k+1].add+=s[k].add;
s[2*k].sum+=s[k].add*(s[2*k].r-s[2*k].l+1);
s[2*k+1].sum+=s[k].add*(s[2*k+1].r-s[2*k+1].l+1);
s[k].add=0;
}
int mid=(s[k].l+s[k].r)/2;
if (l>mid)
return SearchTree(l,r,2*k+1);
else if (r<=mid)
return SearchTree(l,r,2*k);
else
return SearchTree(l,mid,2*k)+SearchTree(mid+1,r,2*k+1);
} int main()
{
int n,q;
LL w;
while (~scanf("%d%d",&n,&q))
{
InitTree(1,n,1);
for (int i=1; i<=n; i++)
{
scanf("%lld",&w);
UpdataTree(i,i,w,1);
}
for (int i=1; i<=q; i++)
{
char ch;
int a,b;
LL c;
getchar();
scanf("%c%d%d",&ch,&a,&b);
if (ch=='C')
{
scanf("%lld",&c);
UpdataTree(a,b,c,1);
}
else if (ch=='Q')
{
LL ans=SearchTree(a,b,1);
printf ("%lld\n",ans);
}
}
}
return 0;
}
poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)的更多相关文章
- 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 ...
- 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 ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- [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(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- 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 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
随机推荐
- linux 查看 cpu
如何获得CPU的详细信息: linux命令:cat /proc/cpuinfo 用命令判断几个物理CPU,几个核等: 逻辑CPU个数:# cat /proc/cpuinfo | grep " ...
- 题解报告:hdu 1272 小希的迷宫
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem ...
- 上传Android代码到gerrit服务器
1. 配置default.xml default.xml是跟Android代码配套的,可参考google Android源码下的default.xml(.repo/manifests/default. ...
- 修改docker-toolbox/boot2docker容器镜像
进入虚拟机 vi /var/lib/boot2docker/profile 编辑在EXTRA_ARGS,加入 --registry-mirror=https://pee6w651.mirror.ali ...
- windows server 2008 r2 安裝IE11
https://support.microsoft.com/en-us/help/2847882/prerequisite-updates-for-internet-explorer-11 https ...
- VS插件-Resharper
最近代码因为Resharper出现了点问题,同事问我这个插件有什么用,下面就列几个最近常用的功能.其他功能后续慢慢更新 1.什么是Resharper ReSharper是一个JetBrains公司出品 ...
- iOS - UITableView 单选功能实现
#import <UIKit/UIKit.h> @interface TestCell : UITableViewCell @property(nonatomic,copy)NSStrin ...
- 【java基础】java中重载与重写的区别
重载(Overloading) (1) 方法重载是让类以统一的方式处理不同类型数据的一种手段.多个同名函数同时存在,具有不同的参数个数/类型.重载Overloading是一个类中多态性的一种表现. ( ...
- Android popwindow 消失监听
LisviewPop.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { //改变显示 ...
- Win32双缓冲讲解
双缓冲是一种思想,也是一种方法,它可以避免频繁的闪烁问题.如果在画布上直接绘画,由于每次都会重新擦除然后重绘,绘制需要时间,所以肉眼会看到闪烁问题.解决的方法就是在内存中先创建出一个内存dc,然后在内 ...