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为操作次数). 想法:嗯...学了这么长 ...
随机推荐
- FAILED: Execution Error, return code 2 from org.apache.hadoop
错误遇到的情形: hive整合hbase,hive的数据表 load,select,insert一切正常 通过hive往hbase关联表插入数据的时候报错,错误内容如下: 2016-04-18 14: ...
- sql中IN的用法
1.和where配合使用 IN操作符允许我们在where的子句中规定多个值 SELECT * FROM Persons WHERE LastName IN ('Adams','Carter') 这句 ...
- js面试题知识点全解(一闭包)
闭包使用场景:1.函数作为返回值,如下场景 function F1(){ var a = 100 //自由变量 //返回一个函数(函数作为返回值) return function(){ console ...
- 基于GStreamer编写Mp3播放器
一.简介 作者系统为CentOS6,本文在此基础上对Mp3播放器进行开发,需要使用mp3解码库libmad和gstreamer0.10-plugins-ugly,详细步骤如下. 二.操作步骤 1) ...
- 数字图像处理实验(4):PROJECT 02-04 [Multiple Uses],Zooming and Shrinking Images by Bilinear Interpolation 标签: 图像处理MATLAB
实验要求: Zooming and Shrinking Images by Bilinear Interpolation Objective To manipulate another techniq ...
- ZROI2018提高day2t1
传送门 分析 考场上写了前20分和|a[i]|<=1的情况,但是因为没开long long爆零了.实际考场上差不多想到正解了,至少当时不会凸壳... 我们发现对于ax2+bx的大小关系我们可以将 ...
- 导出Excel解决方案之一NOPI
一.概要 导出Excel这个功能相信很多人都做过,但是实现这个功能解决方案有好几种,今天我未大家介绍一种比较新的,其实也不新了- -!它叫NPOI,可以完美操作EXCEl的导入和导出操作,让我们一起看 ...
- IntelliJ IDEA打可运行jar包时的错误
1.[ERROR] 'build.resources.resource.directory' 解决:需要在pom.xml的project->build->resources节点下,加入以 ...
- Java50道经典习题-程序32 左移右移
题目:取一个整数a从右端开始的4-7位.分析:比如取123456789从右端开始的4-7位即:3456(1)先使a右移4位.(2)设置一个低4位全为1,其余全为0的数.可用~(~0<<4) ...
- eclipse workspace 共享设置
总结一下,复制工作空间配置步骤如下: 1 使用eclipse新建workspace. 2 将新建的workspace下的.metadata\.plugins内容全部删除. 3 将需要拷贝的worksp ...