A Simple Problem with Integers(树状数组区间变化和区间求和)
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
#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(树状数组区间变化和区间求和)的更多相关文章
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...
- POJ3468 A Simple Problem with Interger [树状数组,差分]
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- A Simple Problem with Integers_树状数组
Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...
- luogu 2519 [HAOI2011]problem a 动态规划+树状数组
发现每一次 $[b[i]+1,n-a[i]]$ 这个区间的分数必须相同,否则不合法. 而一个相同的区间 $[l,r]$ 最多只能出现区间长度次. 于是,就得到了一个 $dp:$ 将每一种区间的出现次数 ...
- 【ZOJ2112】【整体二分+树状数组】带修改区间第k大
The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...
- POJ3468——树状数组支持两个区间操作
题目:http://poj.org/problem?id=3468 推断过程可自己查,得式子:fixsum(x) = (x+1) * ∑(i=1,x)fi - ∑(i=1,x)i*fi; 其中 f 是 ...
- nyoj 123 士兵杀敌(四) 树状数组【单点查询+区间修改】
士兵杀敌(四) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战 ...
- 牛客小白月赛6 F 发电 树状数组单点更新 求区间乘积 模板
链接:https://www.nowcoder.com/acm/contest/136/F来源:牛客网 HA实验是一个生产.提炼“神力水晶”的秘密军事基地,神力水晶可以让机器的工作效率成倍提升. ...
随机推荐
- Tp5整理
一.命名规则 目录级和文件命名 目录采用小写字母+下划线命名: 类文件名采用驼峰法命名(比如:ArticleDetail.php),其它文件与目录命名规则同: 类名与类文件名须保持一致,采用驼峰法: ...
- C++——程序的结构
1.作用域和可见性 1.1 函数原型中的参数其作用域仅在()内.因此参数名称可有可无,但是参数类型需要声明. 1.2 块作用域 在块中声明的标识符其作用域自声明处起,限于块中. 1.3 类作用域 类作 ...
- 移动 APP 端与服务器端用户身份认证的安全方案
最近要做一个项目是java开发后端服务,然后移动APP调用.由于之前没有接触过这块,所以在网上搜索相关的方案.然后搜到下面的一些方案做一些参考. 原文:移动 APP 端与服务器端用户身份认证的安全方案 ...
- 使用鼠标左键事件实现VR中的Eye Gaze Input
1.光标以及光标动画的显示 using System.Collections; using System.Collections.Generic; using UnityEngine; using U ...
- 管理QT的组件
1.在qt的安装目录找到'%QTROOT%\MaintenanceTool.exe'. 2.点击MaintenanceTool的设置,可以设置默认储存库.临时储存库.用户定义储存库,选择其中的临时储存 ...
- spring(三):BeanDefiniton
- 常见python面试题
1,简述列举了解的编程语言及语言间的区别? Python 解释型语言,代码简洁,易懂 C语言 编译型语言,底层语言 c++ 编译型语言,在C语言基础上加了面向对象 Java 混合型语言,可拓展性高 G ...
- 路飞-后台home模块
home模块 创建home模块 """ 前提:在 luffy 虚拟环境下 1.终端从项目根目录进入apps目录 >: cd luffyapi & cd ap ...
- 使用命令把SpringBoot项目打包成可运行的jar包(简洁,操作性强)
前几天接到一个需求,要把原系统中一个数据处理小功能搬出原系统,拉出来单独做一个SpringBoot项目,然后打成jar包扔到Windows服务器上运行,这样数据处理的时候如果遇到堵塞就不至于整个系统都 ...
- crontab实践
1.crontab概要 2.crontab使用 3.关键配置信息 3.1如何配置定时任务 4.注意事项 参考 https://www.cnblogs.com/keithtt/p/6946498.htm ...