A Simple Problem With Integers

POJ-3468

  • 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用。
  • 代码中需要注意的点我都已经标注出来了,容易搞混的就是update函数里面还需要计算sum数组。因为这里查询的时候是直接用sum查询结点。
//区间更新,区间查询
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=100005;
long long num[maxn];
long long sum[maxn<<2];
long long lazy[maxn<<2];
int n,m;
void pushup(int id,int l,int r){
int lc=id<<1;
int rc=id<<1|1;
sum[id]=sum[lc]+sum[rc];
}
void pushdown(int id,int l,int r){
int lc=id<<1;
int rc=id<<1|1;
int mid=(l+r)>>1;
lazy[lc]+=lazy[id];
lazy[rc]+=lazy[id];
sum[lc]+=lazy[id]*(mid-l+1);
sum[rc]+=lazy[id]*(r-mid-1+1);
lazy[id]=0;
}
void build(int id,int l,int r){
if(l==r){
sum[id]=num[l];
return;
}
int lc=id<<1;
int rc=id<<1|1;
int mid=(l+r)>>1;
build(lc,l,mid);
build(rc,mid+1,r);
pushup(id,l,r);//向上维护
}
void update(int id,int l,int r,int p,int q,int v){
if(p<=l&&q>=r){//完全包含区间
lazy[id]+=v;
sum[id]+=v*(r-l+1);//-----------------------这一步容易忘记
return;
}
pushdown(id,l,r);//---------------------这一步也容易忘记
int mid=(l+r)>>1;
int lc=id<<1,rc=id<<1|1;
if(p<=mid){
update(lc,l,mid,p,q,v);
}
if(q>mid){
update(rc,mid+1,r,p,q,v);
}
pushup(id,l,r);
}
long long query(int id,int l,int r,int p,int q){
long long sums=0;
if(p<=l&&q>=r){
//return sums=sum[id]+lazy[id]*(r-l+1);
return sums=sum[id];
}
pushdown(id,l,r);
int mid=(l+r)>>1;
if(p<=mid){
sums+=query(id<<1,l,mid,p,q);
}
if(q>mid){
sums+=query(id<<1|1,mid+1,r,p,q);
}
//pushup(id,l,r);//----------因为这里是查询函数,所以可以省略。这里在pushdown函数里面做过类似的。
return sums;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>m){
memset(lazy,0,sizeof(lazy));
for(int i=1;i<=n;i++){
cin>>num[i];
}
build(1,1,n);
for(int i=0;i<m;i++){
char c;
cin>>c;
if(c=='C'){//add,更新
int a,b,c;
cin>>a>>b>>c;
update(1,1,n,a,b,c);
}else{//查询
int a,b;
cin>>a>>b;
cout<<query(1,1,n,a,b)<<endl;
}
}
}
return 0;
}

POJ-3468(线段树+区间更新+区间查询)的更多相关文章

  1. poj 3468 线段树区间更新/查询

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  2. hdu 1698+poj 3468 (线段树 区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注 ...

  3. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

  4. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  5. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  6. codevs 1690 开关灯 线段树区间更新 区间查询Lazy

    题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这些路灯全是关着的,六点之后,会有M(2<=m<=100000)个人 ...

  7. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)

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

  9. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  10. POJ 3468 (线段树 区间增减) A Simple Problem with Integers

    这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和 ...

随机推荐

  1. hdu2818 Building Block

    Problem Description John are playing with blocks. There are N blocks (1 <= N <= 30000) numbere ...

  2. Educational Codeforces Round 94 (Rated for Div. 2) D. Zigzags (枚举,前缀和)

    题意:有一长度为\(n(4\le n\le 3000)\)的数组,选择四个位置\((i,j,k,l)\ (1\le i<j<k\le n)\),使得\(a_i=a_k\)并且\(a_j=a ...

  3. 再记一次 应用服务器 CPU 暴高事故分析

    一:背景 1. 前言 大概有2个月没写博客了,不是不想写哈

  4. Hexo之更换背景及透明度

    Hexo之更换背景及透明度 引入方式 首先,介绍一下引入方式,外部导入css文件,不影响内部配置. 1.创建css文件 创建一个css文件移动到\themes\butterfly\source\css ...

  5. git merge bug

    git merge bug 本地分支 dev commit 后, 直接 pull 远程 dev 分支, 导致远程 dev 分支 merge 到本地 dev 分支了, 取消本次 merge 操作? Re ...

  6. React Native hot reloading & Android & iOS

    React Native hot reloading & Android & iOS https://facebook.github.io/react-native/docs/debu ...

  7. js & h5 & app & object storage

    js & h5 & app & object storage API https://developer.mozilla.org/en-US/docs/Web/API Stor ...

  8. Bootstrap5 多级dropdown

    <div class="dropdown"> <a class="btn dropdown-toggle"> Dropdown link ...

  9. js & disabled right click & disabled right menu

    js & disabled right click (() => { const log = console.log; log(`disabled copy`); document.bo ...

  10. python中yaml模块的使用

    1.yaml库的导入 经过尝试,发现在python2 和python3语言环境下,安装yaml库的命令行语句不一样. python2: pip install yaml python3:pip ins ...