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 [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
随机推荐
- 怎样查看当前QQ的版本号是多少
怎样查看当前QQ的版本号是多少.. -------------- -------------
- VMware Workstation 12 Pro 之安装Windows10 EP系统
VMware Workstation 12 Pro 之安装Windows10 EP系统... --------------- 先准备好要用的Win10的镜像文件ISO ---------------- ...
- 流畅python学习笔记:第十九章:动态属性和特性
首先来看一个json文件的读取.书中给出了一个json样例.该json文件有700多K,数据量充足,适合本章的例子.文件的具体内容可以在http://www.oreilly.com/pub/sc/os ...
- java归并排序详解
归并排序 /** * 归并排序 * 简介:将两个(或两个以上)有序表合并成一个新的有序表 即把待排序序列分为若干个子序列,每个子序列是有序的.然后再把有序子序列合并为整体有序序列 * 时间 ...
- [2016-03-15]rabbitmq notes
安装 系统:CentOS 6 环境: rabbitmq 依赖的Erlang环境包 wget https://www.rabbitmq.com/releases/erlang/erlang-18.1-1 ...
- 七字真言解读TCP三次握手
三次握手所谓的"三次握手"即对每次发送的数据量是怎样跟踪进行协商使的发送和接收同步,根据所接收到的数据量而确定的数据确认数及数据发送.接收完毕后何时撤消联系,并建立虚连接. 一.七 ...
- MySQL(六)之MySQL常用操作符
前言 在前面的MySQL学习中,我们学习了MySQL的安装,管理以及配置,还有是它的DDL.今天给大家分享一下,MySQL的操作符和它的函数这部分. 千里之行始于足下,做什么事情都要脚踏实地的去做才能 ...
- Spring 事务管理笔记
本文为 Spring 框架的事务管理学习笔记,官网文档地址为:Transaction Management,隔离级别及传播属性解释来自 org.springframework.transaction. ...
- 如何部署 Calico 网络?- 每天5分钟玩转 Docker 容器技术(67)
Calico 是一个纯三层的虚拟网络方案,Calico 为每个容器分配一个 IP,每个 host 都是 router,把不同 host 的容器连接起来.与 VxLAN 不同的是,Calico 不对数据 ...
- 6.分析request_irq和free_irq函数如何注册注销中断
上一节讲了如何实现运行中断,这些都是系统给做好的,当我们想自己写个中断处理程序,去执行自己的代码,就需要写irq_desc->action->handler,然后通过request_irq ...