BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 2530 Solved: 1096
[Submit][Status][Discuss]
Description
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+1, ... , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
55
9
15
HINT
The sums may exceed the range of 32-bit integers.
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const int maxn=;
int Lazy[maxn]; ll sum[maxn];
void pushup(int Now){ sum[Now]=sum[Now<<]+sum[Now<<|];}
void pushdown(int Now,int L,int R){
if(Lazy[Now]){
int Mid=(L+R)>>;
sum[Now<<]+=(ll)Lazy[Now]*(Mid-L+);
sum[Now<<|]+=(ll)Lazy[Now]*(R-Mid);
Lazy[Now<<]+=Lazy[Now];
Lazy[Now<<|]+=Lazy[Now];
Lazy[Now]=;
}
}
void build(int Now,int L,int R)
{
if(L==R) {
scanf("%lld",&sum[Now]);
return ;
}
int Mid=(L+R)>>;
build(Now<<,L,Mid); build(Now<<|,Mid+,R);
pushup(Now);
}
ll query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return sum[Now];
int Mid=(L+R)>>; ll res=;
pushdown(Now,L,R);
if(l<=Mid) res+=query(Now<<,L,Mid,l,r);
if(r>Mid) res+=query(Now<<|,Mid+,R,l,r);
pushup(Now);
return res;
}
void update(int Now,int L,int R,int l,int r,int c)
{
if(l<=L&&r>=R){ sum[Now]+=(ll)(R-L+)*c; Lazy[Now]+=c; return ;}
int Mid=(L+R)>>; pushdown(Now,L,R);
if(l<=Mid) update(Now<<,L,Mid,l,r,c);
if(r>Mid) update(Now<<|,Mid+,R,l,r,c);
pushup(Now);
}
int main()
{
int N,M,L,R,x; char opt[];
scanf("%d%d",&N,&M);
build(,,N);
rep(i,,M){
scanf("%s%d%d",opt,&L,&R);
if(opt[]=='Q') printf("%lld\n",query(,,N,L,R));
else scanf("%d",&x),update(,,N,L,R,x);
}
return ;
}
BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)的更多相关文章
- bzoj3212 Pku3468 A Simple Problem with Integers 线段树
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2046 So ...
- bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作
Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2173 Solved: ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
随机推荐
- 5.4 Components -- Wrapping Content in A Component(在组件中包裹内容)
1.有时候,你可能希望定义一个模板,它包裹其他模板提供的内容. 例如,假设我们创建一个blog-post模板,我们可以使用它来展现一个blog post: app/components/blog-po ...
- requirements.txt
在文件夹下 生成requirements.txt文件 pip freeze > requirements.txt 安装requirements.txt依赖 pip install -r requ ...
- Spring—Ioc
IoC容器,最主要的就是完成对象的创建以及维护对象的依赖关系等. 所谓控制反转,包括两部分:一是控制,二是反转,就是把传统方式需要由代码来实现对象的创建.维护对象的依赖关系,反转给容器来帮忙管理和实现 ...
- Python 以指定列宽格式化字符串
问题: 有一些长字符串,想以指定的列宽将他们重新格式化 解决方案: 使用textwrap模块来格式字符串的输出. textwrap 模块对于字符串打印时非常有用的,特别是当希望输出自动匹配终端大小的时 ...
- 经查-- git使用报错及解决办法
git push 错误 error: failed to push some refs to 'git@github.com:charblus/ ...' 本地和远程的文件应该合并后才能上传本地的新文 ...
- IntelliJ IDEA 中配置lombok插件,编写简略风格Java代码
1.打开IDEA的Settings面板,并选择Plugins选项,然后点击 “Browse repositories..” 2.开启注释处理 3.在pom.xml中添加lombox <!-- h ...
- Java8 Predicate
code: package com.qhong; import java.util.ArrayList; import java.util.Arrays; import java.util.List; ...
- Template Method(模板方法)
意图: 定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.TemplateMethod 使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤. 适用性: 一次性实现一个算法的不变的部分, ...
- 探索解析微服务下的RabbitMQ
概览 本文主要介绍如何使用RabbitMQ消息代理来实现分布式系统之间的通信,从而促进微服务的松耦合. RabbitMQ,也被称为开源消息代理,它支持多种消息协议,并且可以部署在分布式系统上.它轻量级 ...
- data模块
这个模块原本应该存放Excel文件,提供utils目录下的config模块调用: 这里公司内部无法使用Excel读取数据,顾使用了ddt,其实里面就是.xslx文件