题目链接:http://poj.org/problem?id=3468

题意:给出一个数列,两种操作:(1)将区间[L,R]的数字统一加上某个值;(2)查询区间[L,R]的数字之和。

思路:数列A,那么区间[1,x]的和为:


struct BIT
{
    i64 a[N];

    void clear()
    {
        clr(a,0);
    }

    void add(int x,int t)
    {
        while(x<N) a[x]+=t,x+=x&-x;
    }

    i64 get(int x)
    {
        i64 ans=0;
        while(x) ans+=a[x],x-=x&-x;
        return ans;
    }
};

BIT a,b;

int n,m;
i64 p[N];

int main()
{
    Rush(n)
    {
        RD(m);
        int i;
        FOR1(i,n) RD(p[i]),p[i]+=p[i-1];
        a.clear(); b.clear();
        char op[10];
        int L,R,x;
        while(m--)
        {
            RD(op);
            if(op[0]=='Q')
            {
                RD(L,R);
                PR(p[R]-p[L-1]+(R+1)*a.get(R)-b.get(R)-L*a.get(L-1)+b.get(L-1));
            }
            else
            {
                RD(L,R,x);
                a.add(L,x);
                a.add(R+1,-x);
                b.add(L,x*L);
                b.add(R+1,-x*(R+1));
            }
        }
    }
}

  

POJ 3468 A Simple Problem with Integers(树状数组)的更多相关文章

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

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

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

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

  3. POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问

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

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

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

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

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

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

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

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

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

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

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

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

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

  10. 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 ...

随机推荐

  1. html5游戏引擎-Pharse.js学习笔记(一)

    1.前言 前几天随着flappy bird这样的小游戏的火爆,使我这种也曾了解过html5技术的js业余爱好者也开始关注游戏开发.研究过两个个比较成熟的html5游戏引擎,感觉用引擎还是要方便一些.所 ...

  2. PHP的会话处理函数session

    (๑•ᴗ•๑) PHP Session 变量 当运行一个应用程序时,你会打开它,做些更改,然后关闭它.这很像一次会话.计算机清楚你是谁.它知道你何时启动应用程序,并在何时终止.但是在因特网上,存在一个 ...

  3. android禁止ScrollView自动滚动

    当Scrollview嵌套listview,或者子View的内容过多时,当内容加载完成后,ScrollView中内容的长度会发生改变,ScrollView会自动往下滚动,解决办法:在ScollView ...

  4. POJ 3723 Conscription 最小生成树

    题目链接: 题目 Conscription Time Limit: 1000MS Memory Limit: 65536K 问题描述 Windy has a country, and he wants ...

  5. 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]。不能使用除法。

    // test14.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  6. Comparable & Comparator

    Comparable & Comparator 都是用来实现集合中元素的比较.排序的,只是 Comparable 是在集合内部定义的方法实现的排序,Comparator 是在集合外部实现的排序 ...

  7. DB2 中将date类型的转换成timestamp

    方法1: TIMESTAMP(Char(date)||'00.00.00') SELECT T2.RECORDNO,T2.DANGERTIME,T2.BIZORIGIN,T3.COMMONNAME A ...

  8. hdu 4588 Count The Carries

    思路:容易发现二进制表示的数的最低位规律是01010101……:接着是001100110011……:接着是:0000111100001111…… 这样我们发现每一位的循环节是2^(i+1),前2^i是 ...

  9. 关于Python的super用法

    Python中对象方法的定义很怪异,第一个参数一般都命名为self(相当于其它语言的this),用于传递对象本身,而在调用的时候则不必显式传递,系统会自动传递. 举一个很常见的例子: >> ...

  10. 0环境设置 - SQLPLUS设置

    define _editor=vi - SQL*PLUS默认编辑器set serveroutput on size 1000000 - 默认打开DBMS_OUTPUT, 不用每次使用都执行这个命令来启 ...