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. Codeforces Round #547 (Div. 3) F1/2. Same Sum Blocks (Easy/Hard) (贪心,模拟)

    题意:有一长度为\(n\)的数组,求最多的区间和相同的不相交的区间的个数. 题解:我们可以先求一个前缀和,然后第一层循环遍历区间的右端点,第二层循环枚举左端点,用前缀和来\(O(1)\)求出区间和,\ ...

  2. Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths (思维)

    题意:有一个\(n\)x\(m\)的矩阵,从\((1,1)\)出发走到\((n,m)\),问最少修改多少个数,使得所有路径上的数对应相等(e.g:\((1,2)\)和\((n-1,m)\)或\((2, ...

  3. Vulkan与DX11交互

    Demo演示地址07_wintest 有什么用 在android平台主流是用opengl es,android下vulkan与opengles纹理互通. 而在win平台,主流游戏还用的是DX11,如果 ...

  4. 二、Jmeter 后置处理器(BeanShell PostProcessor)

    1.新建JDBC Request,如下图所示: 重要的参数说明: Variable Name:数据库连接池的名字,需要与JDBC Connection Configuration的Variable N ...

  5. Excel添加超链接

    using Microsoft.Office.Interop.Excel; Worksheet sheet; public void AddLink(int row, int col, string ...

  6. Leetcode(22)-括号生成

    给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())& ...

  7. sscanf的最基础用法(非原创)

    1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main(){ 6 ch ...

  8. CodeForces 1047C Enlarge GCD(数论)题解

    题意:n个数的gcd是k,要你删掉最少的数使得删完后的数组的gcd > k 思路:先求出k,然后每个数除以k.然后找出出现次数最多的质因数即可. 代码: #include<cmath> ...

  9. cobaltstrike的使用

    0x01 介绍 Cobalt Strike是一款渗透测试神器,常被业界人称为CS神器.Cobalt Strike已经不再使用MSF而是作为单独的平台使用,它分为客户端与服务端,服务端是一个,客户端可以 ...

  10. vuepress config favicon

    vuepress config favicon .vuepress/public favicons https://vuepress.vuejs.org/guide/assets.html#publi ...