今天学了很多关于树状数组的技巧。一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用树状数组做的题,只需多维护一个bit即可。具体的思路见下面的链接:

http://hi.baidu.com/billdu/item/053f6a15ca301b0a8ebde400

要理解里面的橙色块求的时候是打竖看的,不是打横看的。

  1. #pragma warning(disable:4996)
  2. #include<iostream>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<vector>
  6. #include<algorithm>
  7. #include<cmath>
  8. #include<string>
  9. #define ll long long
  10. #define maxn 100000
  11. #define lowbit(k) k&(-k)
  12. using namespace std;
  13.  
  14. ll bit[2][maxn + 50];
  15. int n,q;
  16.  
  17. void inc(ll bit[],int i, int m)
  18. {
  19. for (; i <= n; i += lowbit(i)) bit[i] += m;
  20. }
  21.  
  22. ll query(ll bit[],int i)
  23. {
  24. ll sum = 0;
  25. for (; i > 0; i -= lowbit(i)){
  26. sum += bit[i];
  27. }
  28. return sum;
  29. }
  30.  
  31. ll sum[maxn + 50];
  32.  
  33. int main()
  34. {
  35. while (cin >> n >> q)
  36. {
  37. memset(bit, 0, sizeof(bit));
  38. for (int i = 1; i <= n; i++){
  39. scanf("%lld", sum + i);
  40. }
  41. sum[0] = 0;
  42. for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];
  43. char str[3];
  44. int a,b,c;
  45. for (int i = 0; i < q; i++){
  46. scanf("%s", str);
  47. if (str[0] == 'Q'){
  48. scanf("%d%d", &a, &b);
  49. ll ans = (query(bit[0], b)*(b + 1) - query(bit[1], b)) - (query(bit[0], a - 1)*a - query(bit[1], a - 1));
  50. ans += sum[b] - sum[a - 1];
  51. printf("%lld\n", ans);
  52. }
  53. else{
  54. scanf("%d%d%d", &a, &b, &c);
  55. inc(bit[0], a, c);
  56. inc(bit[1], a, c*a);
  57. inc(bit[0], b + 1, -c);
  58. inc(bit[1], b + 1, -c*(b + 1));
  59. }
  60. }
  61. }
  62. return 0;
  63. }

POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问的更多相关文章

  1. HDU 4267 A Simple Problem with Integers --树状数组

    题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val  操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...

  2. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  3. POJ3468 A Simple Problem with Interger [树状数组,差分]

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  4. A Simple Problem with Integers_树状数组

    Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...

  5. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  6. hdu 2642二维树状数组 单点更新区间查询 模板题

    二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...

  7. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  8. poj3468 A Simple Problem with Integers(线段树/树状数组)

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

  9. kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

随机推荐

  1. 常用的HTML 标签二

    <marquee></marquee> 滚动的文字,也称"走马灯" 语法格式 <marquee 属性="属性值">内容< ...

  2. ZigBee HA示例程序分析

    ZigBee协议栈中自带的HomeAutomation例程,虽然也是操作灯泡,但是,是通过ZCL来统一处理的,符合HA profile规范,互连互操作性较好.下面就简要分析以下ZCL的使用. 在任务数 ...

  3. Cllimbing Stairs [LeetCode 70]

    1- 问题描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe ...

  4. 必须知道的.NET FrameWork

    这部分内容呢,对普通开发者来说,意义不是特别大.但是作为一个有情怀的开发者,应该追根溯源,懂一些自己用的工具, 的粗浅知识.并且这部分内容可以用来个HR吹嘘一下自己的情怀.基本就是这样. .NET生成 ...

  5. Webserver issues | PHP manager for IIS

    4 down vote accepted In order to successfully install the PHP manager for IIS 8, you need the .NET 3 ...

  6. Color About——First

    最近在Android开发学习中遇到TextView背景色以及文字颜色着色问题,在此做个记录. 首先对于颜色的选择,我推荐W3C School,上面有对网页颜色的详尽说明以及实例,还提供了对于,同一种颜 ...

  7. .NET特性-Attribute

    两篇文章有助于学习Attribute特性的概念. http://blog.csdn.net/byondocean/article/details/6802111 http://www.cnblogs. ...

  8. WIN7右下角的声音图标不见了

    有时候电脑启动了,但是声音图标却不见了,造成调试声音相当的麻烦,那么怎么来处理呢? 一:ctrl+shit+Esc键打开任务管理器 二:找到exeplore.exe,结束进程. 三:重新建立进程 上述 ...

  9. c# 空接合(??)运算符的运用

    相信很多人都看到??运算符,但是不一定每个人都知道它是用来做什么的,之前我也看到过很多次,但是因为一直没有去用过,所以也没有了解他的作用,今天又看到了,所以查了的MSDN,原来??运算符叫做空接合运算 ...

  10. android开发 PopupWindow 设置充满屏幕

    View qrcode_view = this.getLayoutInflater().inflate(R.layout.taskdetail_qrcode,null); final PopupWin ...