A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 89818   Accepted: 27967
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int MAXN=;
struct Node{
ll sum,lazy;
int l,r;
};
struct SegmentTree{
private:
Node a[MAXN*];
public:
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].lazy=;
if(l==r)
{
scanf("%lld",&a[rt].sum);
return ;
}
int mid=(a[rt].l+a[rt].r)>>;
build(rt<<,l,mid);
build(rt*+,mid+,r);
a[rt].sum=a[rt<<].sum+a[rt*+].sum;
}
void pushDown(int rt)
{
int mid=(a[rt].l+a[rt].r)>>;
a[rt<<].sum+=(mid-a[rt].l+)*a[rt].lazy;
a[rt*+].sum+=(a[rt].r-mid)*a[rt].lazy;
a[rt<<].lazy+=a[rt].lazy;
a[rt*+].lazy+=a[rt].lazy;
a[rt].lazy=;
}
void change(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].sum+=(r-l+)*val;
a[rt].lazy+=val;
return ;
}
if(a[rt].lazy!=)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
{
change(rt<<,l,r,val);
}
else if(mid<l)
{
change(rt*+,l,r,val);
}
else
{
change(rt<<,l,mid,val);
change(rt*+,mid+,r,val);
}
a[rt].sum=a[rt<<].sum+a[rt*+].sum;
}
ll query(int rt,int l,int r)
{
if(a[rt].l==l&&a[rt].r==r)
{
return a[rt].sum;
}
if(a[rt].lazy!=)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
return query(rt<<,l,r);
else if(mid<l)
return query(rt*+,l,r);
else
{
return query(rt<<,l,mid)+query(rt*+,mid+,r);
}
}
};
SegmentTree solver;
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
solver.build(,,n);
for(int i=;i<m;i++)
{
scanf("%*c");
char op;
scanf("%c",&op);
if(op=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",solver.query(,l,r));
}
else
{
int l,r,val;
scanf("%d%d%d",&l,&r,&val);
solver.change(,l,r,val);
}
}
} return ;
}

POJ3468(树状数组区间维护)的更多相关文章

  1. poj3468树状数组的区间更新,区间求和

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 47174   ...

  2. POJ 3468 A Simple Problem with Integers(树状数组区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 97217   ...

  3. 树状数组区间加法&区间求和操作

    树状数组区间加法&区间求和操作 一般的树状数组解决区间加&单点询问并不复杂 但是要解决区间求和... 我们假设原数组是\(\{a_i\}\),差分数组\(\{d_i=a_i-a_{i- ...

  4. 1082 线段树练习 3 && 树状数组区间修改区间查询

    1082 线段树练习 3 题意: 给定序列初值, 要求支持区间修改, 区间查询 Solution 用树状数组, 代码量小, 空间占用小 巧用增量数组, 修改时在 \(l\) 处 $ + val$ , ...

  5. 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

    题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...

  6. HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数

    xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...

  7. 【bzoj3132】上帝造题的七分钟 二维树状数组区间修改区间查询

    题目描述 “第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a,b),右下角为(c,d)的一个矩形区域内的全部数字加上一个值的操作. ...

  8. 【bzoj4540】[Hnoi2016]序列 单调栈+离线+扫描线+树状数组区间修改区间查询

    题目描述 给出一个序列,多次询问一个区间的所有子区间最小值之和. 输入 输入文件的第一行包含两个整数n和q,分别代表序列长度和询问数.接下来一行,包含n个整数,以空格隔开,第i个整数为ai,即序列第i ...

  9. Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】

    任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...

随机推荐

  1. UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-3: ordinal not in range(256)

    今天使用MySQLdb往MySQL插入中文数据遇到一个异常: UnicodeEncodeError: 'latin-1' codec can't encode characters in positi ...

  2. Python菜鸟之路:Python基础

    一.Python版本升级至3.0的必然性 In November 2014, it was announced that Python 2.7 would be supported until 202 ...

  3. react-native 使用 antd-mobile-rn UI进行开发app

    1.创建 react-native 项目 react-native init app03 2.安装组件 npm install antd-mobile-rn --save 3.配置按需加载 npm i ...

  4. Meeting-in-the-Middle (LA 2965)

    Meeting-in-the-Middle,又称“中途相遇法”.准确地说,它只是一种策略. 顺便说一下,这个算法真的很冷门! 结合这道题来讨论一下吧:LA 2965.ε(┬┬﹏┬┬)3 因为博主的英文 ...

  5. Symfony 如何使用ckeditor

    首先: 1)加载以下两个bundle "egeloen/ckeditor-bundle": "^4.0","helios-ag/fm-elfinder ...

  6. Symfony 没有找到数据库驱动An exception occured in driver: could not find driver

    如果一直报这个错误, 第一,你本地没有相关的数据库驱动(mysql:-->pdo_myql,postgresql-->pdo_pgsql等); 需要执行 php -m|grep -i pd ...

  7. 016-Spring Boot JDBC

    一.数据源装配 通过查看代码可知,默认已装配了数据源和JdbcTemplate System.out.println(context.getBean(DataSource.class)); Syste ...

  8. ABAP 发邮件(三)

    [转自http://blog.sina.com.cn/s/blog_7c7b16000101bnxk.html]SAP ABAP 发邮件方法三(OO) *&------------------ ...

  9. ansible copy文件比较慢, 使用Synchronize模块

    Ansible中的同步模块(rsync) Synchronize模块 1 2 3 4 5 6 7   This is a wrapper around rsync. Of course you cou ...

  10. FlashFXP上传下载

    正常情况我们的生产环境我们本地是不能直接访问的,因为网段不通,且生产环境不允许我们随便访问,但是对于我们自运维的集群我们有时候需要上去做一些操作,通过堡垒机跳转到生产机器上即可,但是我们不能通过xsh ...