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 ...
随机推荐
- dev RichText高亮
需要引用的DLL DevExpress.CodeParser DevExpress.Office DevExpress.RichEdit DevExpress.XtraRichEdit MySyn ...
- lncRNA研究
------------------------------- Long noncoding RNAs are rarely translated in two human cell lines. ( ...
- [UVa1210]Sum of Consecutive Prime Numbers(前缀和,打表)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- nancy中的诊断功能面板1
nancyfx中有一项 诊断功能 ,可以查看网站的基本信息和其他相关信息,还包括查看会话信息,请求输出信息等. 假设你已经安装完了nancyfx.现在开始使用诊断功能: 一 安装 在你的Bootstr ...
- python介绍(转载)
Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...
- How to run an manually installed program from terminals in Linux / Ubuntu
Say we have installed qt programs and we want to run qtcreator from the command line. What we need h ...
- 关于PHP HTML <input type="file" name="img"/>上传图片,图片大小,宽高,后缀名。
在我们的系统中,不免要上传图片,视频等文件,在上传中,需要做的一些判断,文件大小等方面. 注意: 在php.ini 中的post_max_size,upload_max_filesize默认为2M,在 ...
- struts2标签之列求和
struts2标签之列求和 <table width="100%" border="0" cellpadding="0" cellsp ...
- Python学习(3)变量类型
目录 变量赋值 多个变量赋值 标准数据类型 Python数字 Python字符串 Python列表 Python元组 Python元字典 Python数据类型转换 type数据类型查看 变量赋值 Py ...
- ubuntu虚拟环境virtualenv中djanggo连接mysql
在ubuntu服务器上安装MYSQLDB,执行:sudo apt-get install python-mysqldb, 若提示: ---------------------------------- ...