A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 135904   Accepted: 42113
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

思路:线段树水题,建议手写线段树,熟悉模板,注意数据需要使用long long

》》点击进入原题测试《《

  1. #include<string>
  2. #include<iostream>
  3. #include<algorithm>
  4.  
  5. #define lson l,m,rt<<1
  6. #define rson m+1,r,rt<<1|1
  7. #define ll long long
  8.  
  9. using namespace std;
  10.  
  11. const int maxn = 1e5 + ;
  12.  
  13. struct Tree{
  14. ll l, r, w, f;
  15. }tree[maxn << ];
  16. ll ans;
  17.  
  18. inline void PushDown(int rt)
  19. {
  20. tree[rt << ].f += tree[rt].f;
  21. tree[rt << | ].f += tree[rt].f;
  22. tree[rt << ].w += tree[rt].f*(tree[rt << ].r - tree[rt << ].l + );
  23. tree[rt << | ].w += tree[rt].f*(tree[rt << | ].r - tree[rt << | ].l + );
  24. tree[rt].f = ;
  25. }
  26. void build(int l, int r, int rt)
  27. {
  28. tree[rt].l = l;
  29. tree[rt].r = r;
  30. tree[rt].f = ;
  31. if (l == r){
  32. cin >> tree[rt].w;
  33. return;
  34. }
  35. int m = (l + r) >> ;
  36. build(lson);
  37. build(rson);
  38. tree[rt].w = tree[rt << ].w + tree[rt << | ].w;
  39. }
  40. void update(int L, int R, int l, int r, int rt)
  41. {
  42. if (L <= l&& r <= R){
  43. tree[rt].w += ans*(r - l + );
  44. tree[rt].f += ans;
  45. return;
  46. }
  47. if (tree[rt].f)PushDown(rt);
  48. ll m = (l + r) >> ;
  49. if (L <= m)update(L, R, lson);
  50. if (R > m) update(L, R, rson);
  51. tree[rt].w = tree[rt << ].w + tree[rt << | ].w;
  52. }
  53. ll query(ll L, ll R, ll l, ll r, ll rt)
  54. {
  55. if (L <= l&&r <= R){
  56. return tree[rt].w;
  57. }
  58. if (tree[rt].f)PushDown(rt);
  59. ll m = (l + r) >> , cnt = ;
  60. if (L <= m)cnt += query(L, R, lson);
  61. if (R > m)cnt += query(L, R, rson);
  62. return cnt;
  63. }
  64. void Print(int l, int r, int rt)
  65. {
  66. if (l == r){
  67. cout << rt << " = " << tree[rt].w << endl;
  68. return;
  69. }
  70. //cout << rt << " = " << tree[rt].w << endl;
  71. int m = (l + r) >> ;
  72. if (l <= m)Print(lson);
  73. if (r > m)Print(rson);
  74. }
  75. int main()
  76. {
  77. std::ios::sync_with_stdio(false);
  78.  
  79. ll n, q;
  80. cin >> n >> q;
  81.  
  82. build(, n, );
  83. string flag; ll a, b;
  84. while (q--){
  85. cin >> flag >> a >> b;
  86. if (flag == "C"){
  87. cin >> ans;;
  88. update(a, b, , n, );
  89. //Print(1, n, 1);
  90. }
  91. else if (flag == "Q"){
  92. cout << query(a, b, , n, ) << endl;
  93. }
  94. }
  95.  
  96. return ;
  97. }

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 线段树加延迟标记

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

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

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

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

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

随机推荐

  1. Could not find modernizr-2.6.2 in any of the sources

  2. Linux系统下 为命令配置别名

    1.什么是别名 在管理和维护Linux系统的过程中,将会使用到大量命令,有一些很长的命令或用法经常被用到,重复而频繁的输入某个很长命令或用法是不可取的.这时可以使用 别名 功能将这个过程简单化. Li ...

  3. HTML5移动Web开发

    1. 响应式web设计 说到这个,移动开发面对的屏幕尺寸那叫一个丰富,其中安卓阵营就够让人头痛的.我们在PC端常用的两种布局方式就是固定布局和弹性布局,前者设置一个绝大多数电脑能正常显示的固定宽度居中 ...

  4. 【weiphp】安装中报错

    问题描述:安装的第三部报错“SQLSTATE[HY000]: General error: 2030 This command is not supported in the prepared sta ...

  5. HDU4947GCD Array(莫比乌斯反演+树状数组)

    题面 传送门 题解 orz ljz 相当于每一个数要加上 \[v\times [\gcd(i,n)=d]=v\times [\gcd(i/d,n/d)=1]=v\times \sum_{p|{i\ov ...

  6. DFS HDU 5305 Friends

    题目传送门 /* 题意:每个点都要有偶数条边,且边染色成相同的两部分,问能有多少种染色方法 DFS+剪枝:按照边数来DFS,每种染色数为该点入度的一半,还有如果点不是偶数边就不DFS 这是别人的DFS ...

  7. ACM_物品分堆(01背包)

    物品分堆 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个物品,物品i的重量为Wi,现在想要把这个n个物品分类两堆,求最小的 ...

  8. S - Cyclic Components (并查集的理解)

    Description You are given an undirected graph consisting of nn vertices and mm edges. Your task is t ...

  9. 清除WebSphere部署应用所对应的JSP缓存

    Web应用部署在WebSphere Application Server v8.5后程序一般放置在<AppServer>/profiles/<profile_name>/ins ...

  10. jquery实现文字自动向上滚动,鼠标放上去停止,移开继续滚动代码...

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