bzoj3212 Pku3468 A Simple Problem with Integers 线段树
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 线段树的更多相关文章
- 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 ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- 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 ...
随机推荐
- Django 基本设置
建立django目录,为了独立区分app和主站的关系,需要把app完全和主站分离 app/views.py from django.shortcuts import render from djang ...
- socket_sever实现多客户端并发
#!/usr/bin/env python # -*- coding:utf-8 -*- import socketserver class mysever(socketserver.BaseRequ ...
- 使用Github pages+jekyll搭建自己的博客(windows版)
最近突发奇想,想试试GitHub pages来搭建博客.网上一搜一大堆,嗯...看来还是挺简单的...于是自己撸起袖子干...... 结果对于我这种GitHub注册过,git 没用过,ruby.jek ...
- appiun滑动的简单封装
import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.test ...
- vue-cli webpack3扩展多模块打包
场景 在实际的项目开发中会出现这样的场景,项目中需要多个模块(单页或者多页应用)配合使用的情况,而vue-cli默认只提供了单入口打包,所以就想到对vue-cli进行扩展 实现 首先得知道webpac ...
- kubernetes进阶(03)kubernetes的namespace
服务发现与负载均衡Kubernetes在设计之初就充分考虑了针对容器的服务发现与负载均衡机制,提供了Service资源,并通过kube-proxy配合cloud provider来适应不同的应用场景. ...
- Linux CentOS7.0 (04)systemctl vs chkconfig、service
CentOS 7.0中已经没有service命令,而是启用了systemctl服务器命令 systemctl 是系统服务管理器命令,它实际上将 service 和 chkconfig 这两个命令组合到 ...
- zuul入门(1)zuul 的概念和原理
一.zuul是什么 zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul 在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架. ...
- Nginx负载均衡(架构之路)
[前言] 在大型网站中,负载均衡是有想当必要的.尤其是在同一时间访问量比较大的大型网站,例如网上商城,新闻等CMS系统,为了减轻单个服务器的处理压力,我们引进了负载均衡这一个概念,将一个服务器的压力分 ...
- Linux实用的网站
ABCDOCKER网站 https://www.abcdocker.com/ 徐亮伟网站 http://www.xuliangwei.com/ 安装centos物理服务 ...