今天学了很多关于树状数组的技巧。一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改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. Model Builder中Table2Table中字段映射的问题

    ArcGIS10中使用过程中,Bug不少.尽管有了SP3,但模型耦合的深层次的应用中还是错误不少.目前只是遇到一个,利用躲避的方法解决一个.例如,从NetCDF中抽出的数据表,必须在内存和数据库中都存 ...

  2. PCB常用度量衡单位

    1英尺=12英寸 1英寸inch=1000密尔mil 1mil=25.4um 1mil=1000uin (mil密耳有时也成英丝) 1um=40uin(有些公司称微英寸为麦,其实是微英寸) 1OZ=2 ...

  3. String 两种实例化方式的区别

    package com.java1234.chap03.sec08; public class Demo3 { public static void main(String[] args) { //1 ...

  4. canvas 绘点图

    canvas 绘点图 项目中需要一个记录点实时变动的信息,在此记录一下: <!DOCTYPE html> <html lang="en"> <head ...

  5. Jquery操作Cookie取值错误的解决方法

    使用JQuery操作cookie时 发生取的值不正确,结果发现cookie有四个不同的属性,分享下错误的原因及解决方法. 使用JQuery操作cookie时 发生取的值不正确的问题:  结果发现coo ...

  6. php 判断字符串在另一个字符串中位置

    $email='user@example.com';        //定义字符串$result=strstr($email,'@');         //返回子字符串echo $result; / ...

  7. Python中Cookie的处理(一)Cookie库

    Cookie用于服务器实现会话,用户登录及相关功能时进行状态管理.要在用户浏览器上安装cookie,HTTP服务器向HTTP响应添加类似以下内容的HTTP报头: Set-Cookie:session= ...

  8. 【nodejs】关于 alert 和 document

    Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation.保留所有权利. C:\Windows\system32>n ...

  9. iOS 进阶 第十天(0410)

    0410 在tableViewCell之间添加一根线,通栏 iOS应用数据存储的常用方式 plist存储文件 plist读取文件 下面是plist存储读取的图解: 注意:plist只能存储常见的属性. ...

  10. Daject初探 - 一个开源关系型数据库对象关系映射(ORM)模型

    Daject简介 Daject是用php写的一个关系型数据库抽象模型,通过该模型,可以在不写任何SQL或写很少的SQL就能执行大多数数据库查询操作.Daject具有面向对象,跨数据库的优点,通过数据库 ...