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.
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define maxn 100004
ll a[maxn],c1[maxn],c2[maxn];
ll n,m;
ll lowbit(ll x){
return x&(-x);
}
void updata(ll index,ll x,ll val)
{
if(index==)
for(int i=x;i<=n;i+=lowbit(i))
c1[i]+=val;//建立树状数
else
for(int i=x;i<=n;i+=lowbit(i))
c2[i]+=(x-)*val;//区间改变
} ll getsum(ll x)
{
ll sum1=,sum2=;
for(int i=x;i>;i-=lowbit(i))//无论变不变化都可用来求和
sum1+=c1[i],sum2+=c2[i];//区间求和//(l,r)的和为getsum(r)-getsum(l-1)
return x*sum1-sum2;
} int main()
{
a[]=;
scanf("%lld%lld",&n,&m);
for(int i = ; i <= n; i++){
scanf("%lld",&a[i]);
ll t=a[i]-a[i-];
updata(,i,t);
updata(,i,t); //输入初值的时候,也相当于更新了值
}
char v;
ll ans1,ans2;
ll l,r,add;
for(int i=;i<=m;i++){
getchar();
scanf("%c",&v);
if(v=='C'){
scanf("%lld%lld%lld",&l,&r,&add);
updata(,l,add);
updata(,r+,-add);
updata(,l,add);
updata(,r+,-add);
}
else{
scanf("%lld%lld",&l,&r);
ans1=getsum(r);
ans2=getsum(l-);
printf("%lld\n",ans1-ans2);
}
}
return ;
}
 
 

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. POJ3468 A Simple Problem with Interger [树状数组,差分]

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

  5. A Simple Problem with Integers_树状数组

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

  6. luogu 2519 [HAOI2011]problem a 动态规划+树状数组

    发现每一次 $[b[i]+1,n-a[i]]$ 这个区间的分数必须相同,否则不合法. 而一个相同的区间 $[l,r]$ 最多只能出现区间长度次. 于是,就得到了一个 $dp:$ 将每一种区间的出现次数 ...

  7. 【ZOJ2112】【整体二分+树状数组】带修改区间第k大

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...

  8. POJ3468——树状数组支持两个区间操作

    题目:http://poj.org/problem?id=3468 推断过程可自己查,得式子:fixsum(x) = (x+1) * ∑(i=1,x)fi - ∑(i=1,x)i*fi; 其中 f 是 ...

  9. nyoj 123 士兵杀敌(四) 树状数组【单点查询+区间修改】

    士兵杀敌(四) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战 ...

  10. 牛客小白月赛6 F 发电 树状数组单点更新 求区间乘积 模板

    链接:https://www.nowcoder.com/acm/contest/136/F来源:牛客网  HA实验是一个生产.提炼“神力水晶”的秘密军事基地,神力水晶可以让机器的工作效率成倍提升.   ...

随机推荐

  1. codeforces 1283D. Christmas Trees(bfs)

    链接: https://codeforces.com/contest/1283/problem/D 题意:给定n个不同的整数点,让你找m个不同的整数点,使得这m个点到到这n个点最小距离之和最小. 思路 ...

  2. android button setMinHeight setMinWidth 无效解决办法

    setMinWidth(0);setMinHeight(0);setMinimumWidth(0);//必须同时设置这个setMinimumHeight(0);//必须同时设置这个 两个方法同时设置才 ...

  3. 使用鼠标左键事件实现VR中的Eye Gaze Input

    1.光标以及光标动画的显示 using System.Collections; using System.Collections.Generic; using UnityEngine; using U ...

  4. python网络编程(通过tcp或者udp协议通信)

    1.基于tcp协议传送文件: 客户端: import socketimport osimport jsonimport structclient = socket.socket()client.con ...

  5. C++-POJ2352-Stars[数据结构][树状数组]

    /* 虽然题目没说,但是读入有以下特点 由于,输入是按照按照y递增,如果y相同则x递增的顺序给出的 所以,可以利用入读的时间进行降为处理 */ 于是我们就得到了一个一维的树状数组解法啦 值得一提:坐标 ...

  6. k8s集群应用例如jenkins启动问题排查思路

    k8s集群应用例如jenkins启动问题排查思路 待办 rancher上的事件报告>pods日志>pods内容器日志(现获取容器id再查看容器日志,获取容器id 使用的是相应问题pod的名 ...

  7. Golang利用第三方包获取本机cpu使用率以及内存使用情况

    第三方包下载 $ github.com/shirou/gopsutil 获取内存方面的信息 package main import ( "fmt" "github.com ...

  8. Customized Mini LED Keychain For Better Brand Identity

    Looking for products that tell people the brand name? Then you'll find an affordable product that wi ...

  9. PHP中spl_autoload_register()函数用法实例详解

    本文实例分析了PHP中spl_autoload_register()函数用法.分享给大家供大家参考,具体如下: 在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这 ...

  10. eclipse中引入聚合工程

    一般我们在导入项目的时候都是直接import project, 这对普通java 项目,还是 web 项目,或者是单体的项目都是没有问题的,但是在导入聚合项目的时候这样倒入会使maven的子模块没法被 ...