A Simple Problem with Integers

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://poj.org/problem?id=3468

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

题意

区间加,区间查询和

题解:

线段树,记住懒操作~

代码:

  1. //qscqesze
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <sstream>
  11. #include <queue>
  12. #include <typeinfo>
  13. #include <fstream>
  14. #include <map>
  15. #include <stack>
  16. typedef long long ll;
  17. using namespace std;
  18. //freopen("D.in","r",stdin);
  19. //freopen("D.out","w",stdout);
  20. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  21. #define maxn 200001
  22. #define mod 10007
  23. #define eps 1e-9
  24. int Num;
  25. char CH[];
  26. //const int inf=0x7fffffff; //нчоч╢С
  27. const int inf=0x3f3f3f3f;
  28. /*
  29.  
  30. inline void P(int x)
  31. {
  32. Num=0;if(!x){putchar('0');puts("");return;}
  33. while(x>0)CH[++Num]=x%10,x/=10;
  34. while(Num)putchar(CH[Num--]+48);
  35. puts("");
  36. }
  37. */
  38. //**************************************************************************************
  39. inline ll read()
  40. {
  41. ll x=,f=;char ch=getchar();
  42. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  43. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  44. return x*f;
  45. }
  46. inline void P(int x)
  47. {
  48. Num=;if(!x){putchar('');puts("");return;}
  49. while(x>)CH[++Num]=x%,x/=;
  50. while(Num)putchar(CH[Num--]+);
  51. puts("");
  52. }
  53. struct node
  54. {
  55. int l,r;
  56. ll sum,add;
  57. void fun(ll tmp)
  58. {
  59. add+=tmp;
  60. sum+=(r-l+)*tmp;
  61. }
  62. }a[maxn*];
  63. ll d[maxn];
  64. void relax(int x)
  65. {
  66. if(a[x].add)
  67. {
  68. a[x<<].fun(a[x].add);
  69. a[x<<|].fun(a[x].add);
  70. a[x].add=;
  71. }
  72. }
  73. void build(int x,int l,int r)
  74. {
  75. a[x].l=l,a[x].r=r;
  76. if(l==r)
  77. {
  78. a[x].sum=d[l];
  79. }
  80. else
  81. {
  82. int mid=(l+r)>>;
  83. build(x<<,l,mid);
  84. build(x<<|,mid+,r);
  85. a[x].sum=a[x<<].sum+a[x<<|].sum;
  86. }
  87. }
  88. void update(int x,int st,int ed,ll c)
  89. {
  90. int l=a[x].l,r=a[x].r;
  91. if(st<=l&&r<=ed)
  92. a[x].fun(c);
  93. else
  94. {
  95. relax(x);
  96. int mid=(l+r)>>;
  97. if(st<=mid)update(x<<,st,ed,c);
  98. if(ed>mid) update(x<<|,st,ed,c);
  99. a[x].sum=a[x<<].sum+a[x<<|].sum;
  100. }
  101. }
  102. ll query(int x,int st,int ed)
  103. {
  104. int l=a[x].l,r=a[x].r;
  105. if(st<=l&&r<=ed)
  106. return a[x].sum;
  107. else
  108. {
  109. relax(x);
  110. int mid=(l+r)>>;
  111. ll sum1=,sum2=;
  112. if(st<=mid)
  113. sum1=query(x<<,st,ed);
  114. if(ed>mid)
  115. sum2=query(x<<|,st,ed);
  116. return sum1+sum2;
  117. }
  118. }
  119. int main()
  120. {
  121. int n=read(),m=read();
  122. for(int i=;i<=n;i++)
  123. d[i]=read();
  124. build(,,n);
  125. char s[];
  126. int bb,cc,dd;
  127. for(int i=;i<m;i++)
  128. {
  129. scanf("%s",s);
  130. if(s[]=='Q')
  131. {
  132. bb=read(),cc=read();
  133. printf("%lld\n",query(,bb,cc));
  134. }
  135. else
  136. {
  137. bb=read(),cc=read(),dd=read();
  138. update(,bb,cc,dd);
  139. }
  140. }
  141. }

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   Description You have N integers, A1, A2, ... , AN. You need to deal ...

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

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

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

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

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

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

  6. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. POJ 3468 A Simple Problem with Integers 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

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

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

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

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. python基础之常用的高阶函数

    前言 高阶函数指的是能接收函数作为参数的函数或类:python中有一些内置的高阶函数,在某些场合使用可以提高代码的效率. map() map函数可以把一个迭代对象转换成另一个可迭代对象,不过在pyth ...

  2. 安装Https证书

    安装证书 IIS 6 支持PFX格式证书,下载包中包含PFX格式证书和密码文件.以沃通证书为例: 文件说明: 1. 证书文件214083006430955.pem,包含两段内容,请不要删除任何一段内容 ...

  3. Mathtype公式位置偏上

    Mathtype公式位置偏上 部分Mathtype公式与文档文字没有很好的对齐,而是浮起来了,也就是说Mathtype公式的位置比正常文字稍高,这是我写论文时碰到的一个很麻烦的问题.然后就是行距稍微大 ...

  4. html基础-css-选择器

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  5. 渗透常用SQL注入语句合集

    1.判断有无注入点; and 1=1 and 1=2 2.猜表一般的表的名称无非是admin adminuser user pass password 等..and 0<>(select ...

  6. #ifndef的用法

    作用:防止头文件的重复包含和编译 定义 #ifndef x #define x ... #endif 这是宏定义的一种,它可以根据是否已经定义了一个变量来进行分支选择,一般用于调试等等.实际上确切的说 ...

  7. appium+python自动化39-adb shell输入中文(ADBKeyBoard)

    前言 上一篇提到"adb shell input textyoyo" 可以通过adb 输入英文的文本,由于不支持unicode编码,所以无法输入中文,github上有个国外的大神写 ...

  8. 常用对称加密算法(DES/AES)类(PHP)

    看注释,啥也不说了,欢迎各种跨平台测试! /** * 常用对称加密算法类 * 支持密钥:64/128/256 bit(字节长度8/16/32) * 支持算法:DES/AES(根据密钥长度自动匹配使用: ...

  9. webpack 入门总结和实践(按需异步加载,css单独打包,生成多个入口文件)

    为什么是webpack webpack一下自己就

  10. 面试的65个回答技巧-适用于BAT公司

    互联网职业群分享的资料,里面大多是BAT公司的人,很多是猎头.这些技巧对于职场人来说,是非常宝贵的. 1.请你自我介绍一下你自己? 回答提示:一般人回答这个问题过于平常,只说姓名.年龄.爱好.工作经验 ...