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 ...
随机推荐
- 未能加载文件或程序集“CefSharp, Version=1.25.XXXX”或它的某一个依赖项。试图加载格式不正确的程序。
在使用CefSharp的过程中遇到一个坑爹的错误. 从GitHub的项目主页:https://github.com/cefsharp/CefSharp/wiki/Frequently-asked-qu ...
- JSONObject.fromObject
JSONObject.fromObjectjava.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRun ...
- ___security_cookie机制,防止栈溢出
从研究底层和汇编以来,已经多次接触到“栈溢出”这个名词了. 这次在汇编码中看到了个不明就里的 ___security_cookie ,查了下,原来是编译器的安全检查机制.转载一篇文章: 首先,secu ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- MSDN资料
http://www.microsoft.com/china/msdn/events/webcasts/shared/webcast/Series.aspx http://www.microsoft. ...
- ORACLE SQL 分组
select max(cost),suppliercode from tel_bill where period = '2014005' group by suppliercode;select * ...
- hdu4720Naive and Silly Muggles
链接 一直理解的最小覆盖圆就是外接圆..原来还要分钝角和锐角... 钝角的话就为最长边的中点,对于这题分别枚举一下外接圆以及中点的圆,判一下是不是在园外. #include <iostream& ...
- Python学习(22)python网络编程
Python 网络编程 Python 提供了两个级别访问的网络服务.: 低级别的网络服务支持基本的 Socket,它提供了标准的 BSD Sockets API,可以访问底层操作系统Socket接口的 ...
- java文件操作(读流)
try{ InputStream is = new FileInputStream("abc.txt"); InputStreamReader ir = new InputStre ...
- Oracle Regexp_substr
Oracle中REGEXP_SUBSTR函数 Oracle中REGEXP_SUBSTR函数的使用说明: 题目如下: 在oracle中,使用一条语句实现将'17,20,23'拆分成'17','2 ...