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. zjnu1726 STOGOVI (lca)

    Description Mirko is playing with stacks. In the beginning of the game, he has an empty stack denote ...

  2. P1020 导弹拦截(LIS)

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  3. Codeforces Round #643 (Div. 2) B. Young Explorers (思维,贪心)

    题意:给你一组人\(a\),现在要将这些人进行分组,对于\(i\),只有某一组的人数\(\ge a_{i}\)时,\(i\)才可以加入这个组,问最多能够有多少组,(不必将所有人都选用). 题解:我们将 ...

  4. linux下安装python3.7.2

    1.到python的官网去下载python3.7.2安装包,必须是Linux版本的 2.在/usr/tmp下下载python安装包 wget https://www.python.org/ftp/py ...

  5. Nginx基础 - HTTPS安全web服务

    1.HTTPS配置语法 Syntax: ssl on | off; Default: ssl off; Context: http, server Syntax: ssl_certificate fi ...

  6. Java中集合的有序问题

    Java中的容器主要包括两方面: Collection:List.Set.queue Map:HashMap.treeMap: 一. Collection 1. Set TreeSet:基于红黑树实现 ...

  7. How to using PyPI publish a Python package

    How to using PyPI publish a Python package PyPI & Python package https://pypi.org/ main make a f ...

  8. js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process

    js in depth: event loop & micro-task, macro-task & stack, queue, heap & thread, process ...

  9. 人物传记JULLIAN MURPHY:投资哪家强,区块链必然>股票+房地产

    今年上半年在金融股市出现巨大波动的时候,星盟的项目审核经理JULLIAN MURPHY发现了一个有趣的现象:各种熔断和暴跌的背后,特斯拉的股票却从去年年末开始至今已经暴涨了12倍,即便中途有所回落,但 ...

  10. C++算法代码——单词查找

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=2472 题目描述 给定 n 个长度不超过 50 的由小写英文字母组成的单词准备查询,以 ...