3212: Pku3468 A Simple Problem with Integers

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 2046  Solved: 892
[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. 

水题 区间加法 区间求和
mmp我又忘了update后pushup

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define ls u<<1
#define rs ls|1
#define ll long long
#define N 100050
using namespace std;
int a[N],n,m;ll sum[N<<2],lz[N<<2];
void pushup(int u){sum[u]=sum[ls]+sum[rs];}
void build(int u,int l,int r){
if(l==r){
sum[u]=a[l];
return;
}
int mid=l+r>>1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(u);
}
void pushdown(int u,int L,int R){
if(!lz[u])return;
int mid=L+R>>1;
lz[ls]+=lz[u];lz[rs]+=lz[u];
sum[ls]+=lz[u]*(mid-L+1);
sum[rs]+=lz[u]*(R-mid);
lz[u]=0;
}
ll query(int u,int L,int R,int l,int r){
if(l<=L&&R<=r)return sum[u];
pushdown(u,L,R);
int mid=L+R>>1;ll t=0;
if(l<=mid)t+=query(ls,L,mid,l,r);
if(r>mid)t+=query(rs,mid+1,R,l,r);
return t;
}
void update(int u,int L,int R,int l,int r,int val){
if(l<=L&&R<=r){
sum[u]+=val*(R-L+1);
lz[u]+=val;return;
}
pushdown(u,L,R);int mid=L+R>>1;
if(l<=mid)update(ls,L,mid,l,r,val);
if(r>mid)update(rs,mid+1,R,l,r,val);
pushup(u);
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,1,n);
char s[10];int l,r,v;
while(m--){
scanf("%s%d%d",s,&l,&r);
if(s[0]=='Q'){
printf("%lld",query(1,1,n,l,r));
if(m)putchar('\n');
}
else scanf("%d",&v),update(1,1,n,l,r,v);
}
return 0;
}

bzoj3212 Pku3468 A Simple Problem with Integers 线段树的更多相关文章

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

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

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

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

  3. BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)

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

  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. Clover3(可以让Windows Explorer像浏览器一样有标签页)

    这不是广告!!! 下载地址:http://cn.ejie.me/ 效果图:

  2. Mego开发文档 - 数据库建模

    数据库建模 我们还提供了一些其他的特性,用于定制化数据库对应的数据结构. 表映射 框架默认会使用CLR类型名称做为实际数据库的表名,当两者不一致时可以使用该特性强制表名称. [Table(" ...

  3. 算法题丨Remove Element

    描述 Given an array and a value, remove all instances of that value in-place and return the new length ...

  4. 新概念英语(1-95)Tickets,please!

    Lesson 95 Tickets, please. 请把车票拿出来. Listen to the tape then answer this question. Why did George and ...

  5. hadoop2.6.0实践:003 检查hadoop是否可用

    start-dfs.sh start-yarn.sh 1.检查hdfs hdfs dfs -ls / http://localhost:50070 2.运行例子程序 hdfs dfs -ls / hd ...

  6. iOS 封装.framework 以及使用

    .framework是什么? .framework是什么? 这个问题相信做iOS的都知道答案. 在我们的日常开发中,经常会用到各种已经封装好的库,比如支付宝.微信SDK等等中的库,这些库可以给我们的开 ...

  7. 相同域名不同端口的两个应用,cookie名字、路径都相同的情况下,会覆盖吗

    首先答案是: 会的. 本地测试流程: 两个相同的应用,代码完全相同:只是部署在两个不同的tomcat:域名都是localhost 应用A:部署在http://localhost:8087/ 应用B:部 ...

  8. 南京邮电大学java程序设计作业在线编程第四次作业

    王利国的的 "Java语言程序设计第4次作业(2018)" 详细 主页 我的作业列表 作业结果详细 总分:100 选择题得分:40  1.下列方法定义中,正确的是() A.doub ...

  9. 竞赛基础篇---部分和问题(DFS)

    问题链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1058 描述 给定整数a1.a2........an,判断是否可以从中选出若干数,使 ...

  10. sys.exc_info()可以捕获到任意异常

    import sys try: a = 3 assert a > 4 except: exc = sys.exc_info()#返回异常的元祖 print (exc)