这题用线段树轻松解了,重新用树状数组解,关键点是区间更新。
公式推导如下:
sum[x] = org_sum[x] + delta[1]*x + delta[2]*(x-1) + delta[x]*1
           = org_sum[x] + Sigma(delta[1..x]) * (x+1) - Sigma(delta[i]*i)
树状数组增加两个结点信息分别存delta[i] 和 delta[i] * i就好了。

  1. /* 3468 */
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5. #include <map>
  6. #include <queue>
  7. #include <set>
  8. #include <stack>
  9. #include <vector>
  10. #include <deque>
  11. #include <algorithm>
  12. #include <cstdio>
  13. #include <cmath>
  14. #include <ctime>
  15. #include <cstring>
  16. #include <climits>
  17. #include <cctype>
  18. #include <cassert>
  19. #include <functional>
  20. #include <iterator>
  21. #include <iomanip>
  22. using namespace std;
  23. //#pragma comment(linker,"/STACK:102400000,1024000")
  24.  
  25. #define sti set<int>
  26. #define stpii set<pair<int, int> >
  27. #define mpii map<int,int>
  28. #define vi vector<int>
  29. #define pii pair<int,int>
  30. #define vpii vector<pair<int,int> >
  31. #define rep(i, a, n) for (int i=a;i<n;++i)
  32. #define per(i, a, n) for (int i=n-1;i>=a;--i)
  33. #define clr clear
  34. #define pb push_back
  35. #define mp make_pair
  36. #define fir first
  37. #define sec second
  38. #define all(x) (x).begin(),(x).end()
  39. #define SZ(x) ((int)(x).size())
  40. #define lson l, mid, rt<<1
  41. #define rson mid+1, r, rt<<1|1
  42.  
  43. typedef struct {
  44. __int64 s, ss;
  45. } node_t;
  46.  
  47. const int maxn = 1e5+;
  48. int a[maxn];
  49. __int64 tot[maxn];
  50. node_t nd[maxn];
  51. int n, q;
  52.  
  53. int lowest(int x) {
  54. return -x & x;
  55. }
  56.  
  57. __int64 sum(int x) {
  58. __int64 s = , ss = ;
  59. int xx = x;
  60.  
  61. while (x) {
  62. s += nd[x].s;
  63. ss += nd[x].ss;
  64. x -= lowest(x);
  65. }
  66.  
  67. return (xx+) * s - ss;
  68. }
  69.  
  70. void update(int x, int delta) {
  71. __int64 tmp = 1LL * x * delta;
  72.  
  73. while (x <= n) {
  74. nd[x].s += delta;
  75. nd[x].ss += tmp;
  76. x += lowest(x);
  77. }
  78. }
  79.  
  80. int main() {
  81. ios::sync_with_stdio(false);
  82. #ifndef ONLINE_JUDGE
  83. freopen("data.in", "r", stdin);
  84. freopen("data.out", "w", stdout);
  85. #endif
  86.  
  87. scanf("%d %d", &n, &q);
  88. rep(i, , n+) {
  89. scanf("%d", &a[i]);
  90. tot[i] = tot[i-] + a[i];
  91. }
  92.  
  93. __int64 ans;
  94. char op[];
  95. int l, r, d;
  96.  
  97. while (q--) {
  98. scanf("%s %d %d", op, &l, &r);
  99. if (op[] == 'Q') {
  100. ans = tot[r] - tot[l-] + sum(r) - sum(l-);
  101. printf("%I64d\n", ans);
  102. } else {
  103. scanf("%d", &d);
  104. update(l, d);
  105. update(r+, -d);
  106. }
  107. }
  108.  
  109. #ifndef ONLINE_JUDGE
  110. printf("time = %d.\n", (int)clock());
  111. #endif
  112.  
  113. return ;
  114. }

数据发生器。

  1. from copy import deepcopy
  2. from random import randint, shuffle
  3. import shutil
  4. import string
  5.  
  6. def GenDataIn():
  7. with open("data.in", "w") as fout:
  8. t = 1
  9. bound = 10**3
  10. # fout.write("%d\n" % (t))
  11. for tt in xrange(t):
  12. n = randint(100, 200)
  13. q = randint(100, 200)
  14. fout.write("%d %d\n" % (n, q))
  15. L = []
  16. for i in xrange(n):
  17. x = randint(1, bound)
  18. L.append(x)
  19. fout.write(" ".join(map(str, L)) + "\n")
  20. for i in xrange(q):
  21. op = randint(0, 1)
  22. if op:
  23. l = randint(1, n)
  24. r = randint(l, n)
  25. k = randint(1, bound)
  26. fout.write("C %d %d %d\n" % (l, r, k))
  27. else:
  28. l = randint(1, n)
  29. r = randint(l, n)
  30. fout.write("Q %d %d\n" % (l, r))
  31.  
  32. def MovDataIn():
  33. desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
  34. shutil.copyfile("data.in", desFileName)
  35.  
  36. if __name__ == "__main__":
  37. GenDataIn()
  38. MovDataIn()

【POJ】3468 A Simple Problem with Integers的更多相关文章

  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. 【HDOJ】4267 A Simple Problem with Integers

    树状数组.Easy. /* 4267 */ #include <iostream> #include <string> #include <map> #includ ...

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

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

  4. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  5. POJ 3468 A Simple Problem with Integers(分块入门)

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

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

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  7. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

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

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

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

  9. POJ 3468——A Simple Problem with Integers——————【线段树区间更新, 区间查询】

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

随机推荐

  1. 一句话解释jquery中offset、pageX, pageY、position、scrollTop, scrollLeft的区别

    offset   元素相对文档的偏移 pageX, pageY 事件(鼠标)相对文档的偏移 注意:文档是指document, 而不是当前窗口,是包含了滚动位置的,即滚动条的位置对这些值是不产生影响的 ...

  2. NodeJS + Socket.io聊天服务器连接数达到1024后就连不上了

    如果是亚马逊的Engine Yard服务器,解决办法为: 1.查看端口占用情况,找到nodejs进程号,例如我这里是8000端口 lsof -i:8000  找到pid 例如为 8213 2.设置no ...

  3. [Interview][CodingExam]

    這次去Interview, 其中有一個公司 把我列為 2/25的考慮對象, 在Final 的 1/2, 我被刷掉了. 因為第一輪的程式,我稍微google了一下,參考了既有的寫法. 即使第二輪我用完全 ...

  4. A3992学习记录

    ATmega64+A3992驱动步进电机 //ATmega 64a 电机驱动板程序//编译环境 AVR Studio 4.17/AVR GCC//系统外部时钟16M//作者:虞恺 //日期:2012. ...

  5. thinkphp操作数据库

    1.实现or操作: $where=array( 'city'=>array('like',array('%'.$_GET['city'].'%')); 'hangye'=>array('l ...

  6. SPI协议及其工作原理详解

    一.概述. SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在外围设备微控 ...

  7. 拓展,Fibonacci螺旋

    #该程序由023递归这课中的fibonacci数列递归写法修改而成 #在写的过程中发现,如果要正确引导用户的每一次输入,写的代码比主程序还要多 #当然,为了使程序在用户交互过程中显得更加友好,提供错误 ...

  8. vim使用总结

    tar -xf vim.tar -C ~ vim /etc/vimrc vim /root/.vimrc set ts=4 设置tab有多少空格 set ai 自动对齐 set nu set mous ...

  9. buffer busy wait在RAC环境下出现

    昨天运维组的同时反映有套系统用户反映很慢,需要协助帮忙检查什么原因引起的性能问题.导出了从8点到11点的AWR报告进行分析,发现等待事件里大部分的指标都正常,就是buffer busy wait的平均 ...

  10. mysql mysqldump只导出表结构或只导出数据的实现方法

    mysql mysqldump只导出表结构或只导出数据的实现方法,需要的朋友可以参考下. mysql mysqldump 只导出表结构 不导出数据 复制代码代码如下: mysqldump --opt ...