A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 69589   Accepted: 21437
Case Time Limit: 2000MS

Description

You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source

 
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long LL;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; LL sum[MS];
LL C[][MS];
int N,Q; int lowbit(int x)
{
return x&(-x);
} void updata(int no,int x,LL value)
{
while(x<=N)
{
C[no][x]+=value;
x+=lowbit(x);
}
} LL getsum(int no,int x)
{
LL res=;
while(x>)
{
res+=C[no][x];
x-=lowbit(x);
}
return res;
} void solve()
{
scanf("%d%d",&N,&Q);
sum[]=;
for(int i=;i<=N;i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-];
}
memset(C,,sizeof(C));
int l,r;
LL c;
char cmd[];
for(int i=;i<=Q;i++)
{
scanf("%s",cmd);
if(cmd[]=='Q')
{
scanf("%d%d",&l,&r);
LL ans=sum[r]-sum[l-]+(getsum(,r)-getsum(,r)*(N-r))-(getsum(,l-)-getsum(,l-)*(N-l+));
printf("%lld\n",ans);
}
else
{
scanf("%d%d%lld",&l,&r,&c);
updata(,l,c*(N-l+));
updata(,r+,(N-r)*(-c)); updata(,l,c);
updata(,r+,-c);
}
}
} int main()
{
solve();
return ;
}

A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。的更多相关文章

  1. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

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

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

  3. HDU 4267 A Simple Problem with Integers 多个树状数组

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

  4. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...

  5. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

  6. A Simple Problem with Integers~POJ - 3468

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

  7. A Simple Problem with Integers POJ - 3468 (分块)

    题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...

  8. C - A Simple Problem with Integers - poj 3468(区间更新)

    题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...

  9. POJ 3468(树状数组的威力)

    之前说过这是线段树的裸题,但是当看了http://kenby.iteye.com/blog/962159 这篇题解后我简直震惊了,竟然能如此巧妙地转化为用树状数组来处理,附上部分截图(最好还是进入原网 ...

随机推荐

  1. 第三百五十五天 how can I 坚持

    快一年了,三百五十五天了,等写个程序算算时间,看看日期和天数能不能对的上,哈哈. 计划还是未制定,天气预报还是没有写完,立马行动,发完这个博客,立马行动. 计划:设计模式1个月,三大框架3个月,计算机 ...

  2. python's descriptor II

    [python's descriptor II] For instance, a.x has a lookup chain starting with a.__dict__['x'], then ty ...

  3. 创建svn仓库脚本

    #!/bin/bash svnadmin create $1 cp /home/svndata/conf/svnserver.conf /home/svndata/$1/conf/svnserve.c ...

  4. 做 fzu oj 1106 题目学到的

    题目如下 这道题的意识就是给一个数问是否可以又阶乘之和构成,而难点主要是在于如果是7的话就是1!+3!,并不是单纯的从1的阶乘开始加,而是没顺序的,所以这题就得用到递归. (大概就是函数自己调用函数自 ...

  5. UVaLive 6693 Flow Game (计算几何,线段相交)

    题意:给个棋盘,你可以在棋盘的边缘处放2个蓝色棋子2个黄色棋子,问连接2组同色棋子的最小代价,如果线路交叉,输-1. 析:交叉么,可以把它们看成是两条线段,然后如果相交就是不行的,但是有几种特殊情况, ...

  6. apache与IIS端口冲突修改和需要使用 SSL 查看该资源”错误

    改变Apache端口等配置修改方法 www.educity.cn 发布者:jsb200421 来源:网络转载 发布日期:2014年01月02日 如何改变Apache端口:找到Apache安装目录下co ...

  7. Pongo建立信号基站-实际上还是考中位数

    题目: 要建立一个信号基站服务n个村庄,这n个村庄用平面上的n个点表示.假设基站建立的位置在(X,Y),则它对某个村庄(x,y)的距离为max{|X – x|, |Y – y|}, 其中| |表示绝对 ...

  8. mac 安装PIL

    PIL是python Image library 在mac终端中通过以下命令安装: # download curl -O -L http://effbot.org/media/downloads/Im ...

  9. (剑指Offer)面试题22:栈的压入、弹出序列

    题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等. 例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序 ...

  10. (剑指Offer)面试题14:调整数组顺序使奇数位于偶数前面

    题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 如果去掉约束条件: ...