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. C#使用Xamarin开发可移植移动应用进阶篇(9.混淆代码,防止反编译)

    前言 系列目录 C#使用Xamarin开发可移植移动应用目录 源码地址:https://github.com/l2999019/DemoApp 可以Star一下,随意 - - 说点什么.. 今天讲讲如 ...

  2. 手把手封装数据层之DButil数据库连接的封装

    最近这段时间一直在用SSM框架做增删改查,突然想把以前还不会用框架的时候,综合百度和各种资料结合API文档抄袭而来的数据层的封装分享给大家.这边先封装一个DButil. 我这个封装就是烂大街的那种,没 ...

  3. 利用WHID为隔离主机建立隐秘通道

    0 引言 从2014年BADUSB出现以后,USB-HID攻击就这一直被关注,且具争议.争议的焦点是USB-HID的实战效果过于“鸡肋”,无论从早期的BADUSB,还是到后来的各种USB-HID设备, ...

  4. v-cloak 实现vue实例未编译完前不显示

    前言: 由于网速原因,(ps:之前同事无意间在网速很差的情况下测出的)在使用vue开发时,会由于vue实例还没编译成功的时候数据绑定的"Mustache"标签会闪现一下,造成不好的 ...

  5. 怎样通过js 取消input域的hidden属性使其变的可见

    document.getElementById(ID).setAttribute("hidden",false);厉害了 我的哥!

  6. 学习python的*args和 **kwargs

    *args表示任何多个无名参数,它是一个tuple(元组):**kwargs表示关键字参数,它是一个dict(字典) def foo(*args, **kwargs): print 'args = ' ...

  7. Express + Session 实现登录验证

    1. 写在前面 当我们登录了一个网站,在没有退出登录的情况下,我们关闭了这个网站 ,过一段时间,再次打开这个网站,依然还会是登录状态.这是因为,当我们登录了一个网站,服务器会保存我们的登录状态,直到我 ...

  8. SQL Server 常用操作XML

    --修改FunctionNo节点值,@OperateFunctionNo为参数 set @DataXml.modify('replace value of (/CrudData/FunctionNo/ ...

  9. [转载]C header files matching your running 

    原文地址:C header files matching your running kernel were not found.作者:[Opser]小默 c header files matching ...

  10. 封装bootstrap-treegrid组件

    封装bootstrap-treegrid组件   阅读目录 一.开源的treegrid 1.组件效果预览 2.组件开源地址 二.封装treegrid 1.组件封装的必要性 2.组件封装代码示例 3.封 ...