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

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.

Source

 
Tips:
  非常裸的线段树的区间操作;
 
Code:
#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的更多相关文章

  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. 3212: Pku3468 A Simple Problem with Integers

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

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

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

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

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

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

  6. bzoj3212 pku3468 A Simple Problem with Integers

    一个有初值的数列.区间加.区间查 用线段树直接水过 然而并没有1A,主要是做题太快没看规模结果没注意线段树要用longlong建 卧槽怎么可以这么坑爹,害得我看见wa心慌了,还以为连线段树都要跪 一开 ...

  7. BZOJ3212 Pku3468 A Simple Problem with Integers 题解

    题目大意: 一个数列,有两个操作:1.修改操作,将一段区间内的数加上c:2.查询操作,查询一段区间内的数的和. 思路: 线段树裸题,区间修改.区间查询,维护和以及加上的数,由于无序,不需要向下推标记, ...

  8. 【分块】【线段树】bzoj3212 Pku3468 A Simple Problem with Integers

    线段树入门题…… 因为poj原来的代码莫名RE,所以丧病地写了区间修改的分块…… 其实就是块上打标记,没有上传下传之类. #include<cstdio> #include<cmat ...

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

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

随机推荐

  1. [js高手之路]设计模式系列课程-发布者,订阅者重构购物车

    发布者订阅者模式,是一种很常见的模式,比如: 一.买卖房子 生活中的买房,卖房,中介就构成了一个发布订阅者模式,买房的人,一般需要的是房源,价格,使用面积等信息,他充当了订阅者的角色 中介拿到卖主的房 ...

  2. ajax提交汉字到后台保存时乱码问题!

    前端(转换): var param = XXXXXXXXXXXXXXXXXX; param = decodeURIComponent(param,true); param = encodeURI(pa ...

  3. 第一次用上 Android Studio 2.3 过程及错误解决

    因为要开发Android5.0的缘故,抛弃了eclipse转到了Android Studio,第一次使用就是遇到了许多问题,终于是解决问题了,特意写一篇博文给各位要准备从eclipse转到Androi ...

  4. C语言运算符运算顺序判断实例1

    程序1 #include <stdio.h> int main(void) { , j = , k = ; printf("%d\n", --j > i & ...

  5. echarts柱状图修改背景线为网格线、去掉刻度标签、鼠标悬停在柱条上时变色、柱条圆角弧度、

    option = { color: ['red'],//修改柱条颜色 tooltip : { triggerOn:'mousemove' }, grid: { left: '3%', right: ' ...

  6. webStrom支持Vue

    找到webstorm-->preferences-->fileTypes-->html-->添加+-->*.vue

  7. spring事务详解

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt122 Spring事务机制主要包括声明式事务和编程式事务,此处侧重讲解声明式 ...

  8. Java设计模式之-------->"代理模式"

    01.什么是代理模式? 解析:代理(Proxy):代理模式的主要作用是为其他对象提供一种代理以控制对这个对象的访问.在某些情况下,一个对象不想或者不能直接引用另一个对象, 而代理对象可以在客户端和目标 ...

  9. Mac Intellij Debug 模式

    前言:本文的核心是记录debug的使用,与具体的操作系统无关. 1.什么是Debug Debug是程序的一种运行模式.用来发掘程序的走向,以及程序在运行过程中参数的变化. 2.Debug的作用 deb ...

  10. 团队作业8——第二次项目冲刺(Beta阶段)博客汇总

    一.冲刺计划安排 http://www.cnblogs.com/teamworkers/p/6875742.html 二.七天冲刺汇总 http://www.cnblogs.com/teamworke ...