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为操作次数). 想法:嗯...学了这么长 ...
随机推荐
- Vue指令学习
# new Vue({ vue所有的数据都是放到data里面的 # data:{ vue对象的数据 # a:1,对象 # b:[] , # } # methods:{vue对象的方法 # dosomt ...
- Linux 正文处理命令及tar vi 编辑器 homework
作业一: 1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group >/1.txt 2) 将用户信息数据库文件和用 ...
- jQuery的AJax异步加载
主要用到load()方法以及getScript()方法,具体以一个例子说明: 在现有html文件中加载一个拟好的片段,以及在片段加载完成之前阻止用户进一步操作的弹出框. 首先是现有html代码,无任何 ...
- Velocity根据模版生成静态html
新公司的一个CMS项目要用到,这里记录下 一.项目文件图 二.springmvc-servlet.xml 添加 <!-- 定义环境变量文件 --> <bean id="pr ...
- apache2不识别php
sudo apt-get install libapache2-mod-php7.0 sudo a2enmod php7.0 sudo service apache2 restart 注意:Apach ...
- HTML5的头部、拨号、短信、邮件(转)
HTML5[语法要点] 一.头部设置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <!--页面窗口自动调整到设备 ...
- ROS探索总结(五)——创建简单的机器人模型smartcar
前面我们使用的是已有的机器人模型进行仿真,这一节我们将建立一个简单的智能车机器人smartcar,为后面建立复杂机器人打下基础. 一.创建硬件描述包 roscreat-pkg smartcar_de ...
- Blender 软件 四视图布局的创建方法
Blender 软件 四视图布局的创建方法 我使用的Blender版本:Blender V 2.77 Q: 为什么要创建一个:四视图布局场景? A: 原因1:四视图布局最适合模型建模.原因2:在 Bl ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)
一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...
- JavaPersistenceWithHibernate第二版笔记-第七章-003Mapping an identifier bag(@OrderColumn、@ElementCollection、@CollectionTable、、)
一.结构 二.代码 1. package org.jpwh.model.collections.listofstrings; import org.jpwh.model.Constants; impo ...