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 区间修改 区间查询 注意pushdown的操作就可以了
 #include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define FO freopen("in.txt","r",stdin);
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define debug(x) cout << "&&" << x << "&&" << endl;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a, b, sizeof(a));
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
//head //区间修改 区间查询
const int maxx=;
ll sum[maxx<<],lazy[maxx<<],val;
int n,q; void Pushup(int rt) {
sum[rt]=sum[rt<<]+sum[rt<<|];
} void build(int rt,int L,int R) {
lazy[rt]=;
if(L==R) {
scanf("%lld",&sum[rt]);
return;
}
int mid=(L+R)>>;
build(rt<<,L,mid);
build(rt<<|,mid+,R);
Pushup(rt);
} void Pushdown(int rt,int x) {
if(lazy[rt]) {
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
sum[rt<<]+=(x-(x>>))*lazy[rt];//左子树加左边一半的值
sum[rt<<|]+=(x>>)*lazy[rt];//右子树同理
lazy[rt]=;//清空
}
} void Updata(int rt,int L,int R,int l,int r) {
if(L>=l&&R<=r) {
lazy[rt]+=val;//累加标记
sum[rt]+=(R-L+)*val;//更新值
return;
}
int mid=(L+R)>>;
Pushdown(rt,R-L+);//这里多了一个参数 L R区间的个数
if(l<=mid) Updata(rt<<,L,mid,l,r);
if(r>mid) Updata(rt<<|,mid+,R,l,r);
Pushup(rt);
} ll Query(int rt,int L,int R,int l,int r) {
if(L>=l&&R<=r)
return sum[rt];
ll ans=;
int mid=(L+R)>>;
Pushdown(rt,R-L+);
if(l<=mid) ans+=Query(rt<<,L,mid,l,r);
if(r>mid) ans+=Query(rt<<|,mid+,R,l,r);
Pushup(rt);
return ans;
} int main() {
while(~scanf("%d%d",&n,&q)) {
build(,,n);
char s[];
while(q--) {
scanf("%s",s);
int l,r;
if(s[]=='Q') {
scanf("%d%d",&l,&r);
printf("%lld\n",Query(,,n,l,r));
} else {
scanf("%d%d%lld",&l,&r,&val);
Updata(,,n,l,r);
}
}
}
}

借此机会学习了一波树状数组,有点难理解。

单点修改 区间查询  Updata(l,val)

区间修改 单点查询  引入差分数组 c[i]=d[i]-d[i-1]; Updata(l,val);  Updata(r+1,-val); ----明白树状数组就好理解了。把多加的减去

区间修改 区间查询  公式可以推到一下,sum[n]=n*(c[1]+c[2]+...c[n])-(0*c[1]+1*c[2]+2*c[3]+...+(n-1)*c[n]);

所以再维护一个 (i-1)*(a[i]-a[i-1]) , a为原数组。

Updata(l,(l-1)*val); Updata(r+1,-r*val);   原理同上。多加的减去。

(纯属个人理解。关键还是公式的推导,和树状数组的理解)

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define lowbit(x) (x&-x) int n,q;
const int maxn=;
ll a[maxn],val,c1[maxn],c2[maxn];//维护两个树状数组 void Updata(ll c[],int pos,ll val) {
while(pos<=n) {
c[pos]+=val;
pos+=lowbit(pos);
}
} ll getsum(ll c[],int pos) {
ll ans=;
while(pos>) {
ans+=c[pos];
pos-=lowbit(pos);
}
return ans;
} ll sigma(int r) {//公式推导
ll sum1=r*getsum(c1,r);
ll sum2=getsum(c2,r);
return sum1-sum2;
} ll Query(int l,int r) {//区间求和
return sigma(r)-sigma(l-);
} int main() {
while(~scanf("%d%d",&n,&q)) {
memset(c1,,sizeof(c1));memset(c2,,sizeof(c2));
for(int i=;i<=n;i++) {
scanf("%lld",&a[i]);
Updata(c1,i,a[i]-a[i-]);//维护a[i]-a[i-1]
Updata(c2,i,(i-)*(a[i]-a[i-]));//维护(i-1)*(a[i]-a[i-1])
}
char s[];
while(q--) {
int l,r;
scanf("%s",s);
if(s[]=='Q') {
scanf("%d%d",&l,&r);
printf("%lld\n",Query(l,r));
} else {
scanf("%d%d%lld",&l,&r,&val);
//更新操作有点难理解
Updata(c1,l,val);Updata(c1,r+,-val);
Updata(c2,l,(l-)*val);Updata(c2,r+,-r*val);
}
}
}
}

kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)的更多相关文章

  1. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  2. POJ3468 A Simple Problem with Integers —— 线段树 区间修改

    题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...

  3. POJ3468 A Simple Problem with Integers(线段树延时标记)

    题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...

  4. poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

    转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...

  5. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  6. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

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

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 60745   ...

  8. POJ3468 A Simple Problem with Integers 【段树】+【成段更新】

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 57666   ...

  9. [poj3468]A Simple Problem with Integers_线段树

    A Simple Problem with Integers 题目大意:给出n个数,区间加.查询区间和. 注释:1<=n,q<=100,000.(q为操作次数). 想法:嗯...学了这么长 ...

随机推荐

  1. appium_python_android测试环境搭建

    第一步  安装appium •Appium是由.NET 开发的,所以,它会依赖 .NET framework相关组件,所以先安装.net framework 4.5,备注: Appium最低支持.ne ...

  2. python 三元表达式、列表推导式、生成器表达式

    一 三元表达式.列表推导式.生成器表达式 一 三元表达式 name=input('姓名>>: ') res='mm' if name == 'hahah' else 'NB' print( ...

  3. TS封装格式

    ts流最早应用于数字电视领域,其格式非常复杂包含的配置信息表多达十几个,视频格式主要是mpeg2.苹果公司发明的http live stream流媒体是基于ts文件的,不过他大大简化了传统的ts流,只 ...

  4. xcode减小静态库的大小(转)

    减小静态库的大小 编译生成的.a文件太大,但又没有冗余的文件可以删除已减少体积,找了很久才找到解决办法,如下: Build Settings-->Generate Debug Symbols 将 ...

  5. mysql 打印随机数

    select rand(); 这样取出来的数据是类似这样的: 0.5389902438400223 要几位自己取几位: 取得方法类似 select substr(concat("000000 ...

  6. .each循环的两种使用方法

  7. IDEA创建Maven Web 项目

    前提:安装过maven并且配置了maven的环境变量,这里就不演示了.转载了别人一篇maven详解,不了解的可以先看一下这个 链接 图文讲解: 创建项目 选择Maven 选择创建webapp项目 指定 ...

  8. intellij idea 在执行maven的操作 install等会出现中文乱码?其他程序打印正常?

    之前一直碰到过这个问题,也没在意,因为那个中文对我来说用处不大,今天看着务必难受,一定把他给解决了,查了一下,找到了解决方法,如下: 首先打开你的设置. Setting->maven->r ...

  9. ZBar在Windows上的使用 -- ImageMagick and OpenCV

    博客转载自:https://blog.csdn.net/sunflower_boy/article/details/49095265 1. 下载ZBar v0.10 http://zbar.sourc ...

  10. Excel课程学习

    1.Excel软件简介 1.1历史上的其他数据处理软件与Microsoft Excel 1977年,苹果公司开发了一款数据处理软件,当时这款软件卖的非常好,用软件的尾巴摇动硬件的狗,当时有人因为这款软 ...