A Simple Problem with Integers

Time Limit:5000MS   Memory Limit:131072K
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.
 
题解:本题也是线段树系列的模板题之一,要求的是成段更新+懒惰标记。PS:原题的说明有问题,上面说的“C a b c”中的c的范围其实在int32之外,需要使用long long,否则定是WA,真心坑爹,连WA多次,还是在discuss中看到的原因。
 
稍微讲解下代码中的一些细节: step<<1 与 step<<1|1,意思分别是step*2 和step*+1,具体为什么,可以回去复习一下位运算
 
  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cstdio>
  5. #include<algorithm>
  6. #include<cmath>
  7. #include<queue>
  8. #include<map>
  9.  
  10. #define N 100010
  11. #define M 15
  12. #define mod 1000000007
  13. #define mod2 100000000
  14. #define ll long long
  15. #define maxi(a,b) (a)>(b)? (a) : (b)
  16. #define mini(a,b) (a)<(b)? (a) : (b)
  17.  
  18. using namespace std;
  19.  
  20. int n,q;
  21. int x[N];
  22. char ch;
  23. int a,b,c;
  24.  
  25. struct node
  26. {
  27. ll mark,sum;
  28. }tree[*N];
  29.  
  30. void update(int l,int r,int i)
  31. {
  32. if(!tree[i].mark) return;
  33. int mid=(l+r)/;
  34. tree[*i].sum+=tree[i].mark*(ll)(mid-l+);
  35. tree[*i+].sum+=tree[i].mark*(ll)(r-mid);
  36. tree[*i].mark+=tree[i].mark;
  37. tree[*i+].mark+=tree[i].mark;
  38. tree[i].mark=;
  39.  
  40. }
  41.  
  42. ll query(int tl,int tr,int l,int r,int i)
  43. {
  44. if(tl>r || tr<l)
  45. return ;
  46. if(tl<=l && r<=tr)
  47. return tree[i].sum;
  48. update(l,r,i);
  49. int mid=(l+r)/;
  50.  
  51. return query(tl,tr,l,mid,*i)+query(tl,tr,mid+,r,*i+);
  52. }
  53.  
  54. void addd(int tl,int tr,int l,int r,int i,int val)
  55. {
  56. if(tl>r || tr<l)
  57. return;
  58. if(tl<=l && tr>=r){
  59. tree[i].sum+=val*(ll)(r-l+);
  60. tree[i].mark+=val;
  61. return;
  62. }
  63. update(l,r,i);
  64. int mid=(l+r)/;
  65. addd(tl,tr,l,mid,*i,val);
  66. addd(tl,tr,mid+,r,*i+,val);
  67. tree[i].sum=tree[*i].sum+tree[*i+].sum;
  68. }
  69.  
  70. void build_tree(int l,int r,int i)
  71. {
  72. if(l==r){
  73. tree[i].sum=x[l];
  74. return;
  75. }
  76. int mid=(l+r)/;
  77. build_tree(l,mid,*i);
  78. build_tree(mid+,r,*i+);
  79. tree[i].sum=tree[*i].sum+tree[*i+].sum;
  80. }
  81.  
  82. int main()
  83. {
  84. int i;
  85. // freopen("data.in","r",stdin);
  86. //scanf("%d",&T);
  87. //for(int cnt=1;cnt<=T;cnt++)
  88. //while(T--)
  89. while(scanf("%d%d",&n,&q)!=EOF)
  90. {
  91. // printf(" %d %d \n",n,q);
  92. for(i=;i<=n;i++) scanf("%d",&x[i]);
  93. // printf(" 1\n");
  94. memset(tree,,sizeof(tree));
  95. build_tree(,n,);
  96.  
  97. // printf(" 2\n");
  98. getchar();
  99. while(q--){
  100. // printf(" 3\n");
  101. scanf("%c",&ch);
  102. // printf(" %c\n",ch);
  103. if(ch=='C'){
  104. //printf(" 31\n");
  105. scanf("%d%d%d",&a,&b,&c);
  106. getchar();
  107. addd(a,b,,n,,c);
  108.  
  109. }
  110. else{
  111. // printf(" 32\n");
  112. scanf("%d%d",&a,&b);
  113. getchar();
  114. ll ans=query(a,b,,n,);
  115. printf("%I64d\n",ans);
  116. }
  117. }
  118. }
  119.  
  120. return ;
  121. }

POJ 3468 线段树 成段更新 懒惰标记的更多相关文章

  1. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

  2. poj 3468 线段树成段更新

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

  3. poj 3648 线段树成段更新

    线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...

  4. poj 3468 线段树 成段增减 区间求和

    题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample O ...

  5. poj 3669 线段树成段更新+区间合并

    添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...

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

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

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

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

  8. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  9. HDU-1698-Just a Hook-区间更新+线段树成段更新

    In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. T ...

随机推荐

  1. 文本编辑器vim/vi用法完全解读

    vi用法 1.启动vim 2.命令模式和输入模式 3.退出vi 4.vi与ex命令 5.移动光标 6.跳转 7.搜索 8.插入文本 9.修改文本 10.替换文本 11.删除文本 12.恢复和撤销改变 ...

  2. Linux系统GEDIT编译运行C++

    作为NOIP第一年强制使用Linux系统的考生,真的很难受,被迫还要学一波Linux系统. 正常的Windows对于较基础的程序员来说非常方便好用,但是对于高级程序员来说就是一个坑,于是就有了Linu ...

  3. iOS dateformatter设置GMT格式时间--iOS开发系列---项目中成长的知识四

    今天在项目中开始接手客户端的签名这个模块,签名这个会在项目结束过后再单独写一下自己的心得! 今天讲讲在签名的过程中我们需要向服务器传送一个Date值,格式要求是格林威治时间,也就是GMT时间! 格式要 ...

  4. iptables(1)工具详解

    一. iptables 查看链表,创建链表,类命令 1. iptables [-t table] -N chain : 创建一条自定义规则的链 1 2     # iptables -t filter ...

  5. 优化mysql查询

    mysql提供了一个特别的explain语句,用来分析查询语句的性能 : explain select ... 1.在所有用于where,order by,group by的列上添加索引 创建索引 添 ...

  6. 力扣题目汇总(反转字符串中的单词,EXCEL表列序号,旋置矩阵)

    反转字符串中的单词 III 1.题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode ...

  7. python爬虫基础08-selenium大全2/8-Chrome Webdriver启动选项

    Selenium笔记(2)Chrome Webdriver启动选项 本文集链接:https://www.jianshu.com/nb/25338984 在Selenium中使用不同的Webdriver ...

  8. (转)在Xcode 7上直接使用Clang Address Sanitizer

    原文地址: http://www.cocoachina.com/ios/20150730/12830.html WWDC 2015上,除了Swift 2.0外,还有一个令人激动的消息:可以直接在Xco ...

  9. 与LCD_BPP相关的函数

    board/freescale/mx6q_sabresd/mx6q_sabresd.c:    panel_info.vl_bpix = LCD_BPP; common/lcd.c:   off  = ...

  10. Ribbitmq

    rittbiMQ: 连接远程rabbitmq server sudo rabbitmqctl addser mihon mihon123 sudo rabbitmqctl set_permission ...