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, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

  1. 10 5
  2. 1 2 3 4 5 6 7 8 9 10
  3. Q 4 4
  4. Q 1 10
  5. Q 2 4
  6. C 3 6 3
  7. Q 2 4

Sample Output

  1. 4
  2. 55
  3. 9
  4. 15

Hint

The sums may exceed the range of 32-bit integers.

Source

 
代码:
  1. #include<cstdio>
  2. #include<cstring>
  3. const int maxn=;
  4. struct node
  5. {
  6. int lef,rig;
  7. __int64 sum,cnt;
  8. int mid(){
  9. return lef+(rig-lef>>);
  10. }
  11. };
  12. node reg[maxn<<];
  13.  
  14. void Build(int left ,int right,int pos)
  15. {
  16. reg[pos]=(node){left,right,,};
  17. if((left==right))
  18. {
  19. scanf("%I64d",&reg[pos].sum);
  20. return ;
  21. }
  22. int mid=reg[pos].mid();
  23. Build(left,mid,pos<<);
  24. Build(mid+,right,pos<<|);
  25. reg[pos].sum=reg[pos<<].sum+reg[pos<<|].sum;
  26. }
  27. void Update(int left,int right,int pos,int val)
  28. {
  29. if(reg[pos].lef>=left&&reg[pos].rig<=right)
  30. {
  31. reg[pos].cnt+=val;
  32. reg[pos].sum+=val*(reg[pos].rig-reg[pos].lef+);
  33. return ;
  34. }
  35. if(reg[pos].cnt)
  36. {
  37. reg[pos<<].cnt+=reg[pos].cnt;
  38. reg[pos<<|].cnt+=reg[pos].cnt;
  39. reg[pos<<].sum+=reg[pos].cnt*(reg[pos<<].rig-reg[pos<<].lef+);
  40. reg[pos<<|].sum+=reg[pos].cnt*(reg[pos<<|].rig-reg[pos<<|].lef+);
  41. reg[pos].cnt=;
  42. }
  43. int mid=reg[pos].mid();
  44. if(left<=mid)
  45. Update(left,right,pos<<,val);
  46. if(right>mid)
  47. Update(left,right,pos<<|,val);
  48. reg[pos].sum=reg[pos<<].sum+reg[pos<<|].sum;
  49. }
  50. __int64 Query(int left,int right,int pos)
  51. {
  52. if(left<=reg[pos].lef&&reg[pos].rig<=right)
  53. {
  54. return reg[pos].sum;
  55. }
  56. if(reg[pos].cnt) //再向下更新一次
  57. {
  58. reg[pos<<].cnt+=reg[pos].cnt;
  59. reg[pos<<|].cnt+=reg[pos].cnt;
  60. reg[pos<<].sum+=reg[pos].cnt*(reg[pos<<].rig-reg[pos<<].lef+);
  61. reg[pos<<|].sum+=reg[pos].cnt*(reg[pos<<|].rig-reg[pos<<|].lef+);
  62. reg[pos].cnt=;
  63. }
  64. int mid=reg[pos].mid();
  65. __int64 res=;
  66. if(left<=mid)
  67. res+=Query(left,right,pos<<);
  68. if(mid<right)
  69. res+=Query(left,right,pos<<|);
  70. return res;
  71. }
  72. int main()
  73. {
  74. int n,m,a,b,c;
  75. char ss;
  76. while(scanf("%d%d",&n,&m)!=EOF)
  77. {
  78. Build(,n,);
  79. while(m--)
  80. {
  81. getchar();
  82. scanf("%c %d%d",&ss,&a,&b);
  83. if(ss=='Q')
  84. printf("%I64d\n",Query(a,b,));
  85. else{
  86. scanf("%d",&c);
  87. Update(a,b,,c);
  88. }
  89. }
  90. }
  91. return ;
  92. }

poj------(3468)A Simple Problem with Integers(区间更新)的更多相关文章

  1. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  3. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  5. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  6. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  7. 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 ...

  8. 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 ...

  9. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  10. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

随机推荐

  1. Cheatsheet: 2014 03.01 ~ 03.31

    .NET Should I be concerned about PDB files? async and await -Simplified-Internals Web Performance tr ...

  2. 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 ...

  3. 哈理工软件学院"兆方美迪"杯第六届程序设计大赛【高年级组】--决赛 题解

    比赛链接:http://acm-software.hrbust.edu.cn/contest.php?cid=1082 A.好SB啊真是,还以为lis-数有多少个数不一样. #include < ...

  4. sql 语句 嵌套子查询 执行顺序分析

    --创建测试数据create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))inser ...

  5. CUBRID学习笔记 16 元数据支持

    简化了很多 ,在sqlserver需要用语句实现的功能 接口如下 public DataTable GetDatabases(string[] filters) public DataTable Ge ...

  6. Sbt的使用初步和用sbt插件生成eclipse工程

    以前一直是用maven去管理java项目,现在开始写scala项目了但是在scala-ide中去编译scala项目和sbt的区别一直没弄清楚受到文章:http://my.oschina.net/yjw ...

  7. 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 ...

  8. js文件的装载和执行

    1.浏览器对script引用的js文件分两步,下载,下载完毕后马上执行:这两步都会阻塞浏览器继续解析. 2.加入defer属性,<script defer type="text/jav ...

  9. 利用tomcat配置网站

    1: 首先将tomcat考到C盘: 2:建立我们存放web应用的目录,我建立在D:\myWeb  ,然后将自己的web应用考到myWeb目录下: 3:wApp的目录结构为: WEB-INF: 结构: ...

  10. Windows Live Writer配置

    Windows Live Writer手工配置步骤: 1.在菜单中选择"Weblog";,然后选择"Another Weblog Service". 2.在We ...