kuangbin专题七 POJ3468 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 区间修改 区间查询 注意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 (线段树或树状数组)的更多相关文章
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- poj------(3468)A Simple Problem with Integers(区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 60745 ...
- POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 57666 ...
- [poj3468]A Simple Problem with Integers_线段树
A Simple Problem with Integers 题目大意:给出n个数,区间加.查询区间和. 注释:1<=n,q<=100,000.(q为操作次数). 想法:嗯...学了这么长 ...
随机推荐
- hibernate 事务的隔离级别
脏读不可重复读幻读可序列化(符合事务的四个特性的正常情况 ) 解释: 脏读:事务A对数据1做了更新,但是还没有来得及提交 此时事务B对数据1进行了查询获得了事务A更新后的数据, 但是事务A因为一些原因 ...
- python正则以及collections模块
正则 一.认识模块 什么是模块:一个模块就是一个包含了python定义和声明的文件,文件名就是加上.py的后缀,但其实import加载的模块分为四个通用类别 : 1.使用python编写的代码(.p ...
- asp.net的验证码插件及方法、ashx验证码一般处理程序
需要引入一个ashx的一般处理程序! 把这个程序在前台当作一个图片使用就可以! 前台代码: <td> <img title="看不清?" style=" ...
- jumpserver跳板机的搭建
搭建的跳板机基于0.3.2,别问我为什么不用0.5版本的,我能说我没有搭建成功么,步骤贼多,功能不完善,不建议生产环境使用 步骤其实很简单: github wiki :https://github.c ...
- Jquery前端选择器
----------------------祖先后代选择器------------------------------ 1.祖先 后代:根据一个元素可以取得指定的所有子元素(不管中间有多少后代)$(& ...
- 【转】TinyMCE(富文本编辑器)
效果预览:http://www.tinymce.com/tryit/full.php [转]TinyMCE(富文本编辑器)在Asp.Net中的使用方法 转自:http://www.cnblogs.co ...
- viewpagerindicator+UnderlinePageIndicator+ viewpage切换
布局文件activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- 返回键的复写onBackPressed()介绍
本篇文章是对Android中返回键的复写onBackPressed()进行了详细的分析介绍,需要的朋友参考下 在android开发中,当不满足触发条件就按返回键的时候,就要对此进行检测.尤其是当前Ac ...
- Linux之tcpdump使用详解
1.1 三种关键字 关于类型的关键字 第一种是关于类型的关键字,主要包括host,net,port, 例如 host 210.27.48.2,指明 210.27.48.2是一台主机,net 202. ...
- a标签空的情况下 IE6 IE7下点击无效
如果给空a标签定义了宽度和高度且使用了absolute,则在IE6和IE7中点击无效. 两种解决方法(主要是针对a标签不能设置背景情况): 1.给a标签添加样式:background: ...