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

10 5
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

4
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(线段树)的更多相关文章

  1. bzoj3212 Pku3468 A Simple Problem with Integers 线段树

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2046  So ...

  2. bzoj 3212 Pku3468 A Simple Problem with Integers 线段树基本操作

    Pku3468 A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 2173  Solved:  ...

  3. 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...

  4. 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 ...

  5. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  6. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

  7. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

    A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...

  8. 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 ...

  9. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

随机推荐

  1. 基于struts2--实现文件上传下载

    1. 文件的上传: 1). 表单需要注意的 3 点 ①. method="post"     ②. enctype="mulitipart/form-data" ...

  2. git相关使用技巧和问题

    本地有修改和提交,如何强制用远程的库更新更新.我尝试过用git pull -f,总是提示 You have not concluded your merge. (MERGE_HEAD exists). ...

  3. 2017ACM/ICPC Guangxi Invitational Solution

    A: A Math Problem 题意:给出一个n,找出有多少个k满足kk  <= n 思路: kk的增长很快,当k == 16 的时候就已经超过1e18 了,对于每一次询问,暴力一下就可以 ...

  4. 2017-2018-2 20165207 实验三《敏捷开发与XP实践》实验报告

    java 实验三 实验报告 实验内容 代码规范 不规范的代码可能妨碍阅读,在粘贴下来老师在云班课中设置的提交点一的代码之后,我首先使用了IDEA中Code选项卡的Reformat Code功能规范代码 ...

  5. 20145329 《网络对抗技术》PC平台逆向破解

    shellcode注入 实践是在非常简单的一个预设条件下完成的: (1)关闭堆栈保护 (2)关闭堆栈执行保护 (3)关闭地址随机化 (4)在x32环境下 (5)在Linux实践环境 shellcode ...

  6. C++ 为什么要使用#ifdef __cplusplus extern "C" { #endif

    转载:http://www.cnblogs.com/ayanmw/archive/2012/03/15/2398593.html 转载:http://blog.csdn.net/zkl99999/ar ...

  7. 如何在编辑框中使用IAutoComplete接口(转载)

    出自:http://www.vckbase.com/index.php/wv/27.html 如果可能我想用打包类来实现.唉!,就叫我封装先生吧. 你算是找对地方了.但是我要先声明我的解决办法不是你所 ...

  8. 《EMCAScript6入门》读书笔记——2.let和const命令

  9. LA 3268 号码簿分组(最大流+二分)

    https://vjudge.net/problem/UVALive-3268 题意: 有n个人和m个组.一个人可能属于很多组.现在请你从某些组中去掉几个人,使得每个人只属于一个组,并使得人数最多的组 ...

  10. 智课雅思词汇---二十七、形容词后缀-ant/-ent

    智课雅思词汇---二十七.形容词后缀-ant/-ent 一.总结 一句话总结: ...的 后缀:-ant ①[形容词后缀] 大部分与-ance或-ancy,相对应,表示属于...的.具有...性质的 ...