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<< ...
随机推荐
- Window7系统安装Ubuntu16双系统
在电脑上插入ubuntu系统启动盘,之前做好的u盘启动盘,重启计算机,进入BIOS设置界面,设置系统启动为u盘启动,保存后退出.之后进入ubuntu系统安装界面. 在安装界面中选择系统语言,选择安装u ...
- Python 列表反转显示方法
第一种,使用reversed 函数,reversed返回的结果是一个反转的迭代器,我们需要对其进行 list 转换 listNode = [1,2,3,4,5] newList = list(reve ...
- 清北学堂2019NOIP提高储备营DAY4
今天只有一上午,讲的东西不多,这里就整理一下高精的东西,数论部分请见my blog 高精度: 先讲一讲进制问题:十进制的二进制表示:以10为例, 10的二进制表示为1010 10的三进制表示为101 ...
- COUNT(*) vs COUNT(col)
w https://www.percona.com/blog/2007/04/10/count-vs-countcol/
- 启动tomcat报错One or more listeners failed to start,却找不到任何错误日志的临时解决方案
在整合spring和quartz时,启动tomcat,服务台报以上错误,却找不到任何错误日志…… 参考了https://www.cnblogs.com/sxdcgaq8080/p/8005886.ht ...
- 子页面中ifram高度自使用
HTML: <iframe id="mainframe" name="mainframe" style="width: 100%; border ...
- vue --》watch 深度监听的优化。
话不多说,直接上代码,注释很清楚 <template> <div> <input type="text" v-model="value&qu ...
- C++学习笔记(六)--结构体
1.一种自定义的类型--结构体定义: struct 结构体名称 { //成员表列也称作域 还可以包括函数,即函数成员,不过一般结构体类型中不包含,而是放在类中. 类型名 成员名; };这种结构体类型类 ...
- Vue 基础 day06 webpack 3.x 结合vue
在普通页面使用 render 函数渲染组件 var login = { template: '<h3>login</h3>' } var vm = new Vue({ // c ...
- Java数据结构之链表(Linked List)
1.链表(Linked List)介绍 链表是有序的列表,但是它在内存存储结构如下: 2.特点: 链表是以节点的方式来存储,是链式存储 每个节点包含 data 域, next 域:指向下一个节点. 链 ...