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

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

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

#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#define ll long long
#define maxn 100000
#define lowbit(k) k&(-k)
using namespace std; ll bit[2][maxn + 50];
int n,q; void inc(ll bit[],int i, int m)
{
for (; i <= n; i += lowbit(i)) bit[i] += m;
} ll query(ll bit[],int i)
{
ll sum = 0;
for (; i > 0; i -= lowbit(i)){
sum += bit[i];
}
return sum;
} ll sum[maxn + 50]; int main()
{
while (cin >> n >> q)
{
memset(bit, 0, sizeof(bit));
for (int i = 1; i <= n; i++){
scanf("%lld", sum + i);
}
sum[0] = 0;
for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];
char str[3];
int a,b,c;
for (int i = 0; i < q; i++){
scanf("%s", str);
if (str[0] == 'Q'){
scanf("%d%d", &a, &b);
ll ans = (query(bit[0], b)*(b + 1) - query(bit[1], b)) - (query(bit[0], a - 1)*a - query(bit[1], a - 1));
ans += sum[b] - sum[a - 1];
printf("%lld\n", ans);
}
else{
scanf("%d%d%d", &a, &b, &c);
inc(bit[0], a, c);
inc(bit[1], a, c*a);
inc(bit[0], b + 1, -c);
inc(bit[1], b + 1, -c*(b + 1));
}
}
}
return 0;
}

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. MySQL之左连接与右连接

    左连接: select 列1,列2,列N from tableA left join tableB on tableA.列 = tableB.列(正常是一个外键列) [此处表连接成一张大表,完全当成一 ...

  2. struts2的包和命名空间

    struts2提供了命名空间的功能,主要是为了处理同一个WEB应用中包含同名Action的情形.struts2以命名空间的方式来管理Action,同一个命名空间里不能有同名的Action,不同的命名空 ...

  3. 超越luabind的luaBridge

    此编是引用他人的文章,这里记录下,主要为以后自己查找方便,原文地址:http://www.cppblog.com/sunicdavy/archive/2013/12/07/204648.html 最近 ...

  4. 【MediaKit】WPF项目中 调用摄像头拍照的开发包

    今天遇到一个 人事的项目,项目中需要调用摄像头给员工照相.如何解决这个问题呢? 介绍一个开发包给你,MediaKit.论坛里头的人都说好,但是黑兔觉得大家好才是真的好.你不妨试试~ 第一步:添加WPF ...

  5. adb连接不上手机

    昨天电脑重装了系统,今天打开之前的eclips工作环境,点击run as -> android application,一直报各种诡异的错误,总之就是连接不上手机. 其中包括 Adb conne ...

  6. 使用HttpWebRequest以及HttpWebResponse读取Http远程文件

     主页>杂项技术>.NET(C#)> 使用HttpWebRequest以及HttpWebResponse读取Http远程文件 jackyhwei 发布于 2010-08-15 21: ...

  7. 在 linux x86-64 模式下分析内存映射流程

    前言 在上一篇中我们分析了 linux 在 x86-32 模式下的虚拟内存映射流程,本章主要继续分析 linux 在 x86-64 模式下的虚拟内存映射流程. 讨论的平台是 x86-64, 也可以称为 ...

  8. Android BroadcasetReceiver

    ci@clinux:~/mp_mtk$ adb -s QGAEFYAAOFN7PNOB logcat PhoneReceiver:e *:S --------- beginning of system ...

  9. Android 与 Webservice 的快速保存

    前面写了一篇android对sqllite的快速保存博客,今天我们来看看android与webservice的交互,相信很多有经验的人自然就想到了soap.但是如果在小型项目中,具有大材小用之嫌.实际 ...

  10. 使用cookie解决微信不能存储localStorage的问题

    最近在开发基于微信的Web页面时,发现有些机型不能存储信息到localStorage中,或者是页面一旦关闭,存储的信息也失效了. 于是想到用cookie来替代localStorage,存储一些简单的数 ...