A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 149972   Accepted: 46526

题目链接:http://poj.org/problem?id=3468

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

题解:

线段树模板题,注意一下lazy标记的下传操作,标记也是long long 型的。

代码如下:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <cmath>
  6. using namespace std;
  7. typedef long long ll;
  8. const int N = 1e5+;
  9. int n,m;
  10. ll a[N];
  11. ll ans;
  12. struct Tree{
  13. int l,r;
  14. ll f,w;
  15. }tre[(N<<)+];
  16. void build(int o,int l,int r){
  17. tre[o].l=l;tre[o].r=r;tre[o].f=;
  18. if(l==r){
  19. tre[o].w=a[l];
  20. return ;
  21. }
  22. int mid=l+r>>;
  23. build(o<<,l,mid);
  24. build(o<<|,mid+,r);
  25. tre[o].w=tre[o<<].w+tre[o<<|].w;
  26. }
  27. void down(int o){
  28. tre[o<<].f+=tre[o].f;
  29. tre[o<<|].f+=tre[o].f;
  30. tre[o<<].w+=tre[o].f*(tre[o<<].r-tre[o<<].l+);
  31. tre[o<<|].w+=tre[o].f*(tre[o<<|].r-tre[o<<|].l+);
  32. tre[o].f=;
  33. }
  34. void update(int o,int l,int r,int val){
  35. int L=tre[o].l,R=tre[o].r;
  36. if(L>=l && R<=r){
  37. tre[o].w+=(ll)val*(R-L+);
  38. tre[o].f+=val;
  39. return ;
  40. }
  41. down(o);
  42. int mid=L+R>>;
  43. if(l<=mid) update(o<<,l,r,val);
  44. if(r>mid) update(o<<|,l,r,val);
  45. tre[o].w=tre[o<<].w+tre[o<<|].w;
  46. }
  47. void query(int o,int l,int r){
  48. int L=tre[o].l,R=tre[o].r;
  49. if(L>=l && R<=r){
  50. ans+=tre[o].w;
  51. return ;
  52. }
  53. down(o);
  54. int mid=L+R>>;
  55. if(l<=mid) query(o<<,l,r);
  56. if(r>mid) query(o<<|,l,r);
  57. }
  58. int main(){
  59. scanf("%d%d",&n,&m);
  60. for(int i=;i<=n;i++) scanf("%I64d",&a[i]);
  61. build(,,n);
  62. char s[];
  63. for(int i=;i<=m;i++){
  64. scanf("%s",s);
  65. if(s[]=='Q'){
  66. int l,r;ans=;
  67. scanf("%d%d",&l,&r);
  68. query(,l,r);
  69. printf("%I64d\n",ans);
  70. }else{
  71. int a,b,c;
  72. scanf("%d%d%d",&a,&b,&c);
  73. update(,a,b,c);
  74. }
  75. }
  76. return ;
  77. }

POJ3468:A Simple Problem with Integers(线段树模板)的更多相关文章

  1. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  2. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  3. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  4. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  5. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  6. poj 3468 A Simple Problem with Integers 线段树 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3468 线段树模板 要背下此模板 线段树 #include <iostream> #include <vector> ...

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

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

  8. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

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

  10. Poj 3468-A Simple Problem with Integers 线段树,树状数组

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

随机推荐

  1. lintcode: Check Sum of Square Numbers

    Check Sum of Square Numbers Given a integer c, your task is to decide whether there're two integers ...

  2. linux服务器操作小技巧

    python程序后台一直运行,并将打印信息输出到文件中 nohup -u test.py > out.txt & -u 表示无缓冲,直接将打印信息输出带文件中 &表示程序后台运行

  3. 操作系统及Python解释器工作原理讲解

    操作系统介绍 操作系统位于计算机硬件与应用软件之间 是一个协调.管理.控制计算机硬件资源与软件资源的控制程序 操作系统功能: 控制硬件 把对硬件复杂的操作封装成优美简单的接口(文件),给用户或者应用程 ...

  4. BZOJ 3790 神奇项链 hash/后缀自动机+贪心

    Description 母亲节就要到了,小 H 准备送给她一个特殊的项链.这个项链可以看作一个用小写字母组成的字符串,每个小写字母表示一种颜色. 为了制作这个项链,小 H 购买了两个机器.第一个机器可 ...

  5. Swift-创建UIButton(其他UI组件雷同)

    let button = UIButton.init(frame: CGRectMake(, , , )) button.setTitle("按钮", forState: UICo ...

  6. 3DMAX2016安装教程【图文】

    下载安装包之后,双击setup.exe. 下面是安装图片教程: 点击安装 点击下一步. 如图输入序列号和产品密钥. 填写安装路径,然后下一步. 开始安装,等待. 安装成功.

  7. SVM之核函数

    SVM之问题形式化 SVM之对偶问题 >>>SVM之核函数 SVM之解决线性不可分 写在SVM之前——凸优化与对偶问题 上一篇SVM之对偶问题中讨论到,SVM最终形式化为以下优化问题 ...

  8. perf打印调用栈的过程

    perf_prepare_sample-->perf_callchain-->get_perf_callchain 上面的调用栈会使用 perf_event_output--> 0x ...

  9. 【bzoj4008】[HNOI2015]亚瑟王 概率dp

    题目描述 $n$ 张牌,$r$ 轮游戏,每轮从左向右操作,遇到第 $i$ 张牌有 $p_i$ 的概率选中,选中会产生 $d_i$ 的贡献,丢弃掉该牌并结束这一轮,否则继续下一张.问最终的期望贡献. 输 ...

  10. 转:概率主题模型简介 --- ---David M. Blei所写的《Introduction to Probabilistic Topic Models》的译文

    概率主题模型简介 Introduction to Probabilistic Topic Models      转:http://www.cnblogs.com/siegfang/archive/2 ...