A Simple Problem with Integers
 

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

思路:简单的区间更新跟区间求和,注意会爆int

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<string>
  5. #include<queue>
  6. #include<algorithm>
  7. #include<stack>
  8. #include<cstring>
  9. #include<vector>
  10. #include<list>
  11. #include<set>
  12. #include<map>
  13. #define true ture
  14. #define false flase
  15. using namespace std;
  16. #define ll long long
  17. int scan()
  18. {
  19. int res = , ch ;
  20. while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
  21. {
  22. if( ch == EOF ) return << ;
  23. }
  24. res = ch - '' ;
  25. while( ( ch = getchar() ) >= '' && ch <= '' )
  26. res = res * + ( ch - '' ) ;
  27. return res ;
  28. }
  29. struct is
  30. {
  31. ll l,r;
  32. ll num;
  33. ll lazy;
  34. }tree[*];
  35. void build_tree(ll l,ll r,ll pos)
  36. {
  37. tree[pos].l=l;
  38. tree[pos].r=r;
  39. tree[pos].lazy=;
  40. if(l==r)
  41. {
  42. //tree[pos].num=1;
  43. scanf("%lld",&tree[pos].num);
  44. return;
  45. }
  46. ll mid=(l+r)/;
  47. build_tree(l,mid,pos*);
  48. build_tree(mid+,r,pos*+);
  49. tree[pos].num=tree[pos*].num+tree[pos*+].num;
  50. }
  51. void update(ll l,ll r,ll change,ll pos)
  52. {
  53. if(tree[pos].l==l&&tree[pos].r==r)
  54. {
  55. tree[pos].lazy+=change;
  56. tree[pos].num+=(tree[pos].r-tree[pos].l+)*change;
  57. return;
  58. }
  59. if(tree[pos].lazy)
  60. {
  61. tree[pos*].num+=(tree[pos*].r+-tree[pos*].l)*tree[pos].lazy;
  62. tree[pos*+].num+=(tree[pos*+].r+-tree[pos*+].l)*tree[pos].lazy;
  63. tree[pos*].lazy+=tree[pos].lazy;
  64. tree[pos*+].lazy+=tree[pos].lazy;
  65. tree[pos].lazy=;
  66. }
  67. ll mid=(tree[pos].l+tree[pos].r)/;
  68. if(r<=mid)
  69. update(l,r,change,pos*);
  70. else if(l>mid)
  71. update(l,r,change,pos*+);
  72. else
  73. {
  74. update(l,mid,change,pos*);
  75. update(mid+,r,change,pos*+);
  76. }
  77. tree[pos].num=tree[pos*].num+tree[pos*+].num;
  78. }
  79. ll query(ll l,ll r,ll pos)
  80. {
  81. //cout<<l<<" "<<r<<" "<<pos<<endl;
  82. if(tree[pos].l==l&&tree[pos].r==r)
  83. return tree[pos].num;
  84. if(tree[pos].lazy)
  85. {
  86. tree[pos*].num+=(tree[pos*].r+-tree[pos*].l)*tree[pos].lazy;
  87. tree[pos*+].num+=(tree[pos*+].r+-tree[pos*+].l)*tree[pos].lazy;
  88. tree[pos*].lazy+=tree[pos].lazy;
  89. tree[pos*+].lazy+=tree[pos].lazy;
  90. tree[pos].lazy=;
  91. }
  92. ll mid=(tree[pos].l+tree[pos].r)/;
  93. if(l>mid)
  94. return query(l,r,pos*+);
  95. else if(r<=mid)
  96. return query(l,r,pos*);
  97. else
  98. return query(l,mid,pos*)+query(mid+,r,pos*+);
  99. }
  100. int main()
  101. {
  102. ll x,q,i,t;
  103. while(~scanf("%lld",&x))
  104. {
  105. scanf("%lld",&q);
  106. build_tree(,x,);
  107. while(q--)
  108. {
  109. char a[];
  110. ll l,r;
  111. ll change;
  112. scanf("%s%lld%lld",a,&l,&r);
  113. if(a[]=='C')
  114. {
  115. scanf("%lld",&change);
  116. update(l,r,change,);
  117. }
  118. else
  119. printf("%lld\n",query(l,r,));
  120. }
  121. }
  122. return ;
  123. }

poj 3468 A Simple Problem with Integers 线段树加延迟标记的更多相关文章

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

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

  3. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

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

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

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

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

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  8. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  9. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

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

随机推荐

  1. django 中的render和render_to_response()和locals()

    1. django中的render context在Django里表现为 Context 类,在 django.template 模块里. 它的构造函数带有一个可选的参数: 一个字典映射变量和它们的值 ...

  2. poj3468A Simple Problem with Integers(线段树的区域更新)

    http://poj.org/problem?id=3468 真心觉得这题坑死我了,一直错,怎么改也没戏,最后tjj把q[rt].lz改成了long long 就对了,真心坑啊. 线段树的区域更新. ...

  3. 使用Linux工作之Fedora KDE

    小明拿着在Windows下不断蓝屏的T440和公司建议不使用云笔记的规定,心下想着,是时候回归linux了... 一.系统的获取与启动盘的制作 fedora20 KDE版 liveusb-creato ...

  4. 何为仿射变换(Affine Transformation)

    http://www.cnblogs.com/ghj1976/p/5199086.html 变换模型是指根据待匹配图像与背景图像之间几何畸变的情况,所选择的能最佳拟合两幅图像之间变化的几何变换模型.可 ...

  5. mysql系统变量查询

    mysql系统变量包括全局变量(global)和会话变量(session),global变量对所有session生效,session变量包括global变量.mysql调优必然会涉及这些系统变量的调整 ...

  6. ubuntu14.04无法安装Curl,需要先升级sudo apt-get update

    ubuntu14.04无法安装Curl,需要先升级sudo apt-get updatesudo apt-get updatesudo apt-get install curl------------ ...

  7. [转载]LinkButton跳转页面及传递参数

    在DataList中使用LinkButton按钮(LinkButtonDelete),该按钮用于链接跳转到删除页面.在模板中双击该按钮,跳转到.cs页面.问题是我们如何获得该条信息的ID,如果不知道I ...

  8. pyDay6

    内容来自廖雪峰的官方网站 1.在Python中,代码不是越多越好,而是越少越好.代码不是越复杂越好,而是越简单越好,1行代码能实现的功能,决不写5行代码.请始终牢记,代码越少,开发效率越高. 2.取指 ...

  9. 简单的HTML5 canvas游戏工作原理

    HTML5已经不是一个新名词.它看上去很cool,有很多feature,大多数人普遍看好它的发展.对于我来说,最感兴趣的是它的canvas标签,可以结合Javascript来绘制游戏画面. 我们可以在 ...

  10. Redis 如何正确实现分布式锁

    前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...