poj------(3468)A Simple Problem with Integers(区间更新)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 60745 | Accepted: 18522 | |
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
#include<cstdio>
#include<cstring>
const int maxn=;
struct node
{
int lef,rig;
__int64 sum,cnt;
int mid(){
return lef+(rig-lef>>);
}
};
node reg[maxn<<]; void Build(int left ,int right,int pos)
{
reg[pos]=(node){left,right,,};
if((left==right))
{
scanf("%I64d",®[pos].sum);
return ;
}
int mid=reg[pos].mid();
Build(left,mid,pos<<);
Build(mid+,right,pos<<|);
reg[pos].sum=reg[pos<<].sum+reg[pos<<|].sum;
}
void Update(int left,int right,int pos,int val)
{
if(reg[pos].lef>=left&®[pos].rig<=right)
{
reg[pos].cnt+=val;
reg[pos].sum+=val*(reg[pos].rig-reg[pos].lef+);
return ;
}
if(reg[pos].cnt)
{
reg[pos<<].cnt+=reg[pos].cnt;
reg[pos<<|].cnt+=reg[pos].cnt;
reg[pos<<].sum+=reg[pos].cnt*(reg[pos<<].rig-reg[pos<<].lef+);
reg[pos<<|].sum+=reg[pos].cnt*(reg[pos<<|].rig-reg[pos<<|].lef+);
reg[pos].cnt=;
}
int mid=reg[pos].mid();
if(left<=mid)
Update(left,right,pos<<,val);
if(right>mid)
Update(left,right,pos<<|,val);
reg[pos].sum=reg[pos<<].sum+reg[pos<<|].sum;
}
__int64 Query(int left,int right,int pos)
{
if(left<=reg[pos].lef&®[pos].rig<=right)
{
return reg[pos].sum;
}
if(reg[pos].cnt) //再向下更新一次
{
reg[pos<<].cnt+=reg[pos].cnt;
reg[pos<<|].cnt+=reg[pos].cnt;
reg[pos<<].sum+=reg[pos].cnt*(reg[pos<<].rig-reg[pos<<].lef+);
reg[pos<<|].sum+=reg[pos].cnt*(reg[pos<<|].rig-reg[pos<<|].lef+);
reg[pos].cnt=;
}
int mid=reg[pos].mid();
__int64 res=;
if(left<=mid)
res+=Query(left,right,pos<<);
if(mid<right)
res+=Query(left,right,pos<<|);
return res;
}
int main()
{
int n,m,a,b,c;
char ss;
while(scanf("%d%d",&n,&m)!=EOF)
{
Build(,n,);
while(m--)
{
getchar();
scanf("%c %d%d",&ss,&a,&b);
if(ss=='Q')
printf("%I64d\n",Query(a,b,));
else{
scanf("%d",&c);
Update(a,b,,c);
}
}
}
return ;
}
poj------(3468)A Simple Problem with Integers(区间更新)的更多相关文章
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- 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 Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- 在CentOS下安装Redis
Redis比较傲娇,在windows上还没有很好的安装方式,不得已搞了个虚拟机玩玩. 装Redis十分简单,按照下面的几个命令来就行了. 安装命令 wget http://download.redis ...
- 详解C#委托,事件与回调函数
.Net编程中最经常用的元素,事件必然是其中之一.无论在ASP.NET还是WINFrom开发中,窗体加载(Load),绘制(Paint),初始化(Init)等等.“protected void Pag ...
- LTE Module User Documentation(翻译6)——物理误差模型、MIMO模型、天线模型
LTE用户文档 (如有不当的地方,欢迎指正!) 9 PHY Error Model 物理误差模型包含数据误差模型和下行控制误差模型,两者默认为激活.可以使用 ns-3 属性系统去激活,具体为: ...
- org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决
org.apache.http.client.CircularRedirectException: Circular redirect to "http://xxx"问题解决 ...
- bootstrap学习笔记<九>(菜单,按钮。导航基本元素)
有了bootstrap作导航不再麻烦,几个样式,几个标签就能轻松搞定. 下面就来分解学习导航条的制作. 一.首先是下拉菜单 <div class="dropdown"> ...
- web设计经验<六>令网站看起来不专业的10个设计误区
不管你是不是一个羽翼未丰企业的领导,专业的网站能为你带来的东西比你想象的多很多.退一万步来说,“考虑到我们是一个小厂”,粗糙的网站也许能被用户理解,但是不一定能接受.每天大家所浏览的大量的网站,已经从 ...
- java实现excel与mysql的导入导出
注意:编码前先导入poi相关jar包 1 /** * 读excel 到list * * @param file excel file * @param fields 字段数组 * @return * ...
- Git学习(3)创建版本库
什么是版本库呢?版本库又名仓库,英文名repository,你可以简单理解成一个目录,这个目录里面的所有文件都可以被Git管理起来,每个文件的修改.删除,Git都能跟踪,以便任何时刻都可以追踪历史,或 ...
- white-space: nowrap
CSS:需要加上宽度(width:100px).超出隐藏(overflow:hidden;).强制在同一行显示(white-space: nowrap;).省略号(text-overflow:elli ...
- 转:strcmp函数实现及分析
转自:strcmp函数实现及详解 strcmp函数是C/C++中基本的函数,它对两个字符串进行比较,然后返回比较结果,函数形式如下:int strcmp(constchar*str1,constcha ...