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. 2.PowerShell概述

    PowerShell PowerShell命令窗一般随系统带着,运行->输入:powershell,即可打开命令窗口. 命令 Powershell有诸多命令,兼容cmd命令 语法和命令 在此我推 ...

  2. C++ string (浅谈)

    浅谈string <string> typedef basic_string<char> string; 本篇主要内容是简单地介绍 string类 在竞赛方面较实用的一些功能, ...

  3. Navicat 快捷键 for Mysql

     常用快捷键: 1. ctrl + q: 打开新查询窗口 2. ctrl + r: 运行当前窗口内的所有语句 3. ctrl + w: 关闭当前窗口 4. F6: 打开一个MySQL命令行窗口 5. ...

  4. Python 往Excel写数据

    一.需求描述: 1.一张人员信息表中生成人员信息,某些列的字段要进行递增操作: 2.一个组织节点下存在1000人的限制要求: 3.一张Excel表格生成45000条数据: 二.Excel表格的表头如下 ...

  5. SPI/QSPI通信协议详解和应用

    SPi是高速全双工的串行总线,通常应用在通讯速率较高的场合. SS:从设备选择信号线,也称片选信号线 每个从设备都有一个独立的SS信号线,信号线独占主机的一个引脚,及有多少个从设备就有多少个片选信号线 ...

  6. VS2010下创建MVC4项目注意事项

    1.安装VS SP1. 2.安装NuGet Package Manager. (1)打开VS2010,进入"工具--扩展管理器". (2)点击"联机库",等待搜 ...

  7. ysoserial Commons Collections2反序列化研究

    Apache Commons Collections2反序列化研究 环境准备 JDK 1.7 Commons Collections 4.0 javassit 前置知识 PriorityQueue() ...

  8. 如何实现一个简易版的 Spring - 如何实现 @Component 注解

    前言 前面两篇文章(如何实现一个简易版的 Spring - 如何实现 Setter 注入.如何实现一个简易版的 Spring - 如何实现 Constructor 注入)介绍的都是基于 XML 配置文 ...

  9. 2016 最新的 树莓派3 Raspberry Pi 3 上手评测 图解教程 新手必看!(VNC 安装,启动,关闭)

    1.png . 官方教程: INSTALLING OPERATING SYSTEM IMAGES: https://www.raspberrypi.org/documentation/installa ...

  10. TypeScript & React & Jest

    TypeScript & React & Jest create-react-app Jest ``tsx import React from 'react'; import { re ...