poj 3468 : A Simple Problem with Integers 【线段树 区间修改】
题目是对一个数组,支持两种操作
操作C:对下标从a到b的每个元素,值增加c;
操作Q:对求下标从a到b的元素值之和。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
typedef long long LL; const int maxn=1e5+;
LL lazy[maxn<<],sum[maxn<<]; void push_up(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void push_down(int rt,int len)
{
if(lazy[rt]==) return ;
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
sum[rt<<]+=lazy[rt]*(len-(len>>));
sum[rt<<|]+=lazy[rt]*(len>>);
lazy[rt]=;
} void build(int rt,int l,int r)
{
if(l==r)
{
lazy[rt]=;
scanf("%lld",&sum[rt]);
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
push_up(rt);
} void update(int rt,int l,int r,int ul,int ur,int v)
{
if(ul<=l&&r<=ur)
{
lazy[rt]+=v;
sum[rt]+=(r-l+)*v;
return ;
}
push_down(rt,r-l+);
int mid=(l+r)>>;
if(ul<=mid) update(rt<<,l,mid,ul,ur,v);
if(ur>mid) update(rt<<|,mid+,r,ul,ur,v);
push_up(rt);
} LL query(int rt,int l,int r,int ql,int qr)
{
if(ql<=l&&r<=qr) return sum[rt];
int mid=(l+r)>>;
push_down(rt,r-l+);
LL ret=;
if(ql<=mid) ret+=query(rt<<,l,mid,ql,qr);
if(qr>mid) ret+=query(rt<<|,mid+,r,ql,qr);
return ret;
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
build(,,n);
while(m--)
{
char op[];
scanf("%s",op);
if(op[]=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",query(,,n,l,r));
}
else
{
int l,r,c;
scanf("%d%d%d",&l,&r,&c);
update(,,n,l,r,c);
}
}
}
}
poj 3468 : A Simple Problem with Integers 【线段树 区间修改】的更多相关文章
- POJ 3468 A Simple Problem with Integers 线段树区间修改
http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和 C A B ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)
A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
- POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 67511 ...
- (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新)
题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
随机推荐
- TreeSet 源码分析
TreeSet 1)底层由 TreeMap 支持的 Set 接口实现,Set 中的元素按照自然顺序或指定的比较器排序. 创建实例 /** * 支持此 Set 的底层的 TreeMap 对象 */ pr ...
- Java 基础-类与面向对象
类 Object 类(java.lang.Object)是所有 Java 类的直接或间接父类. 成员方法及变长参数 方法定义: [public | protected | private] [stat ...
- Java基础/发起http和https请求
Java中发起http和https请求 一般调用外部接口会需要用到http和https请求. 本案例为:前后端完全分离,前端框架(React+Mobx+Nornj),后端(Go语言). 面临问题:跨域 ...
- “希希敬敬对”队软件工程第九次作业-beta冲刺第二次随笔
队名: “希希敬敬对” 龙江腾(队长) 201810775001 杨希 201810812008 何敬上 201810812004 今日讨论会议照片一张: 每个人 ...
- 使用Vsftpd服务(匿名访问模式、本地用户模式)
FTP协议占用两个端口号: 21端口:命令控制,用于接收客户端执行的FTP命令. 20端口:数据传输,用于上传.下载文件数据.. FTP数据传输的类型: 主动模式:FTP服务端主动向FTP客户端发起连 ...
- 解决MySQL报错:Access denied for user ‘root’@‘localhost’(using password: YES)
Windows 10(mysql5.1) 修改配置文件 找到MySQL安装目录下配置文件my.ini(在我的win10环境下,其路径为C:\ProgramData\MySQL\MySQL Server ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- Linux安装Python3以及虚拟环境
python3的linux环境编译安装 1.linux下安装软件的方式 选则yum工具,方便,自行解决软件之间的依赖关系,自动下载且安装 1.配置yum源 可以选择阿里云源,清华源等 配置第一个仓库, ...
- PhotonServer新增应用的配置
一,如下代码配置 <MyGameInstance MaxMessageSize=" MaxQueuedDataPerPeer=" PerPeerMaxReliableData ...
- NodeJS、npm安装步骤和配置(windows版本)
https://jingyan.baidu.com/article/48b37f8dd141b41a646488bc.html 上面这个链接很详细了,怕它没了自己记一遍.我的简洁一点. 1. 打开no ...