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 ...
随机推荐
- [C#]解决程序Vista/Win7下因UAC导致的读写错误
在微软的操作系统中,vista和win7加入了UAC的功能,UAC(User Account Control,用户帐户控制)是微软为提高系统安全而在Windows Vista中引入的新技术,它要求用户 ...
- Django 模型(数据库)
Django 模型(数据库) ) email = models.EmailField() memo = models.TextField() def __unico ...
- Sql Server查询同一ID 时间较大的一条数据
- C语言的 32个关键之和9个控制语言之关键字
auto break case char const continue default do double else enum extern float for goto ...
- EditPlus 4.3.2487 中文版已经发布(11月12日更新)
新的版本修复了粘贴多重选择文本的问题,以及增加了横向扩展列选模式选择范围的快捷键(Ctrl+Alt+→/←).
- BZOJ2221: [Jsoi2009]面试的考验
传送门 一句话题意,给定一个序列,询问区间内差值的绝对值的最小值. 这道题之前见过一次,似乎是在一次UER上,那一道题当时是用了近似算法才能过. 数据保证数列随机. 这道题显然非常适合离线的做法,考虑 ...
- double、float等多字节数据处理
一.常规的多字节: 有2,4,8字节 float和double是具有自身算法的数据类型,和其他整型不一样[整型数据,可以直接通过移位来进行计算值的大小,float和double不行] 值 = 尾数x ...
- 在Github上搭建博客
貌似还是这个链接最靠谱呀 http://my.oschina.net/nark/blog/116299 如何利用github建立个人博客:之一 在线编辑器http://markable.in/ed ...
- Gym - 100712D Alternating Strings
http://codeforces.com/gym/100712/attachments 题意: 给出一个01串,现在要切割这个01串,使得每个子串长度都不大于k,并且每个子串不能01交替出现,单个字 ...
- UVa 10635 王子和公主(LCS转LIS)
https://vjudge.net/problem/UVA-10635 题意: 有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数.两个序列的第一个元素均 ...