bzoj 3212 Pku3468 A Simple Problem with Integers
3212: Pku3468 A Simple Problem with Integers
Time Limit: 1 Sec Memory Limit: 128 MB
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.
Source
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int n,m,a[];
long long tree[],mark[];
char s[]; void build(int l,int r,int v){
if(l==r){
tree[v]=a[l];
return;
}
int mid=(l+r) >> ;
build(l,mid,v<<);
build(mid+,r,(v<<)+);
tree[v]=tree[v<<]+tree[(v<<)+];
} void add(int l,int r,int x,int y,int z,int v){
if(l==x&&r==y){
mark[v]+=z;
return;
}
int mid=(l+r)>>;
mark[v<<]+=mark[v];
mark[(v<<)+]+=mark[v];
mark[v]=;
if(mid>=y) add(l,mid,x,y,z,v<<);
if(mid<x) add(mid+,r,x,y,z,(v<<)+);
if(mid>=x&&mid<y){
add(l,mid,x,mid,z,v<<);
add(mid+,r,mid+,y,z,(v<<)+);
}
tree[v]=tree[v<<]+mark[v<<]*(mid-l+)+tree[(v<<)+]+mark[(v<<)+]*(r-mid);
} long long query(int l,int r,int x,int y,int v){
long long ans=;
if(l==x&&r==y){
ans=tree[v]+mark[v]*(r-l+);
return ans;
}
mark[v<<]+=mark[v];
mark[(v<<)+]+=mark[v];
mark[v]=;
int mid=(l+r)>>;
if(mid>=y) ans=query(l,mid,x,y,v<<);
if(mid<x) ans=query(mid+,r,x,y,(v<<)+);
if(mid>=x&&mid<y) ans=query(l,mid,x,mid,v<<)+query(mid+,r,mid+,y,(v<<)+);
tree[v]=tree[v<<]+mark[v<<]*(mid-l+)+tree[(v<<)+]+mark[(v<<)+]*(r-mid);
return ans;
} int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,n,);
for(int i=;i<=m;i++){
scanf("%s",s);
int x,y,z;
if(s[]=='C'){
scanf("%d%d%d",&x,&y,&z);
if(x>y) swap(x,y);
add(,n,x,y,z,);
}else{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
printf("%lld\n",query(,n,x,y,));
}
}
}
bzoj 3212 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: ...
- 3212: Pku3468 A Simple Problem with Integers
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1053 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 ...
- bzoj3212 Pku3468 A Simple Problem with Integers 线段树
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2046 So ...
- BZOJ3212: Pku3468 A Simple Problem with Integers(线段树)
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 2530 So ...
- bzoj3212 pku3468 A Simple Problem with Integers
一个有初值的数列.区间加.区间查 用线段树直接水过 然而并没有1A,主要是做题太快没看规模结果没注意线段树要用longlong建 卧槽怎么可以这么坑爹,害得我看见wa心慌了,还以为连线段树都要跪 一开 ...
- BZOJ3212 Pku3468 A Simple Problem with Integers 题解
题目大意: 一个数列,有两个操作:1.修改操作,将一段区间内的数加上c:2.查询操作,查询一段区间内的数的和. 思路: 线段树裸题,区间修改.区间查询,维护和以及加上的数,由于无序,不需要向下推标记, ...
- 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers
线段树入门题…… 因为poj原来的代码莫名RE,所以丧病地写了区间修改的分块…… 其实就是块上打标记,没有上传下传之类. #include<cstdio> #include<cmat ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
随机推荐
- 【可视化】Echarts3 在世界地图中绘制中国各省份的轮廓
要在世界地图展现出来的情况下绘制中国省份的轮廓,根据现有的echarts-api是不可行的. 但好在echarts也提供了自定义地图的方式,使用echarts.registerMap();来实现 第一 ...
- JS中的运算符 以及变量和输入输出
1.算术运算(单目运算符) + 加 .- 减.* 乘. / 除. % 取余.++ 自增.-- 自减. >>> +:有两种作用,链接字符串/加法运算,当+两边全为数字时,进行加法运算, ...
- 基于tgp协议的套接字的 粘包解决
客户端 import socket import struct import json phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM ...
- weblogic 启动问题
通过startWebLogic.cmd 手动启动weblogic可以成功启动 myeclipse配置后,runserver 就会报 Error occurred during initializati ...
- Java数组笔记
# 数组--------内存 # 2017/8/31 15:29:19 ## 数组(容器) ## - 为了存储同种数据类型的多个值 - 概念 - 数组是可以存储同一种数据类型多个元素的集合, ...
- latex插图续
LaTeX中一般只直接支持插入eps(Encapsulated PostScript)格式的图形文件, 因此在图片插入latex文档之前应先设法得到图片的eps格式的文件. UNIX下的各种应用软件 ...
- redis 安装及启动关闭
1.redis下载 方式1:直接去官网下载 https://redis.io/download 方式2:通过命令下载 wget http://download.redis.io/releases/re ...
- Nodejs进阶:服务端字符编解码&乱码处理
写在前面 在web服务端开发中,字符的编解码几乎每天都要打交道.编解码一旦处理不当,就会出现令人头疼的乱码问题. 不少从事node服务端开发的同学,由于对字符编码码相关知识了解不足,遇到问题时,经常会 ...
- 第一篇--认识Jmeter
Jmeter是Apache组织开发的基于Java的压力测试工具,它最初被设计用于Web应用测试,但后来扩展到其他测试领域. 它可以用于测试静态和动态资源,例如静态文件.Java 小服务程序.CGI 脚 ...
- bat调用带参数存储过程
@bat调用sql文件 sqlplus user/pass@orcl @F:\factory.sql @将所有的存储过程封装在sql中 factory.sql:exec pro_factory(&am ...