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 ...
随机推荐
- Cheatsheet: 2014 03.01 ~ 03.31
.NET Should I be concerned about PDB files? async and await -Simplified-Internals Web Performance tr ...
- Calling / Running a report in Oracle forms 10g / 11g
Calling / Running a report in Oracle forms 10g / 11g Below is the procedure to call a report in Orac ...
- 哈理工软件学院"兆方美迪"杯第六届程序设计大赛【高年级组】--决赛 题解
比赛链接:http://acm-software.hrbust.edu.cn/contest.php?cid=1082 A.好SB啊真是,还以为lis-数有多少个数不一样. #include < ...
- sql 语句 嵌套子查询 执行顺序分析
--创建测试数据create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))inser ...
- CUBRID学习笔记 16 元数据支持
简化了很多 ,在sqlserver需要用语句实现的功能 接口如下 public DataTable GetDatabases(string[] filters) public DataTable Ge ...
- Sbt的使用初步和用sbt插件生成eclipse工程
以前一直是用maven去管理java项目,现在开始写scala项目了但是在scala-ide中去编译scala项目和sbt的区别一直没弄清楚受到文章:http://my.oschina.net/yjw ...
- Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence 二分
E. Restoring Increasing Sequence time limit per test 1 second memory limit per test 256 megabytes in ...
- js文件的装载和执行
1.浏览器对script引用的js文件分两步,下载,下载完毕后马上执行:这两步都会阻塞浏览器继续解析. 2.加入defer属性,<script defer type="text/jav ...
- 利用tomcat配置网站
1: 首先将tomcat考到C盘: 2:建立我们存放web应用的目录,我建立在D:\myWeb ,然后将自己的web应用考到myWeb目录下: 3:wApp的目录结构为: WEB-INF: 结构: ...
- Windows Live Writer配置
Windows Live Writer手工配置步骤: 1.在菜单中选择"Weblog";,然后选择"Another Weblog Service". 2.在We ...