A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 85502   Accepted: 26556
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 <cstdio>
using namespace std;
const int MAXN = ;
typedef long long LL;
struct Node{
int l, r;
LL sum, lazy;
}a[MAXN*];
int n, m;
void build(int rt, int l, int r)
{
a[rt].l = l;
a[rt].r = r;
a[rt].lazy = ;
if(l == r)
{
scanf("%I64d", &a[rt].sum);
return ;
}
int mid = (l + 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 += a[rt].lazy * (mid - a[rt].l + );
a[(rt<<)|].sum += a[rt].lazy * (a[rt].r - mid);
a[rt<<].lazy += a[rt].lazy;
a[(rt<<)|].lazy += a[rt].lazy;
a[rt].lazy = ;
}
void update(int rt, int l, int r, int val)
{
if(a[rt].l == l && a[rt].r == r)
{
a[rt].sum += (LL)val * (r - l + );
a[rt].lazy += (LL)val;
return ;
}
if(a[rt].lazy != )
{
pushDown(rt);
}
int mid = (a[rt].l + a[rt].r) >> ;
if(r <= mid)
{
update(rt << , l, r, val);
}
else if(mid < l)
{
update((rt << ) | , l, r, val);
}
else
{
update(rt << , l, mid, val);
update((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);
}
}
int main()
{
while(scanf("%d %d",&n, &m) != EOF)
{
build(, , n);
while(m--)
{
scanf("%*c");
char op;
scanf("%c", &op);
if(op == 'Q')
{
int l, r;
scanf("%d %d", &l, &r);
printf("%I64d\n", query(, l, r));
}
else
{
int l, r, val;
scanf("%d %d %d", &l, &r ,&val);
update(, l, r, val);
}
}
}
return ;
}

POJ3468(线段树区间维护)的更多相关文章

  1. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  2. poj3468(线段树区间更新&区间求和模板)

    题目链接: http://poj.org/problem?id=3468 题意: 输入 n, m表初始有 n 个数, 接下来 m 行输入, Q x y 表示询问区间 [x, y]的和: C x y z ...

  3. HDU 6315 Naive Operations(线段树+区间维护)多校题解

    题意:a数组初始全为0,b数组题目给你,有两种操作: 思路:dls的思路很妙啊,我们可以将a初始化为b,加一操作改为减一,然后我们维护一个最小值,一旦最小值为0,说明至少有一个ai > bi,那 ...

  4. POJ3468(线段树 区间修改 lazy-tag)

    我的线段树真的没救了......还是多练几道吧....... You have N integers, A1, A2, ... , AN. You need to deal with two kind ...

  5. POJ-3468(线段树+区间更新+区间查询)

    A Simple Problem With Integers POJ-3468 这题是区间更新的模板题,也只是区间更新和区间查询和的简单使用. 代码中需要注意的点我都已经标注出来了,容易搞混的就是up ...

  6. HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. 线段树(区间维护):HDU 3308 LCIS

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. poj3468 A Simple Problem with Integers(线段树区间更新)

    https://vjudge.net/problem/POJ-3468 线段树区间更新(lazy数组)模板题 #include<iostream> #include<cstdio&g ...

  9. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

随机推荐

  1. PAT 1054. 求平均值 (20)

    本题的基本要求非常简单:给定N个实数,计算它们的平均值.但复杂的是有些输入数据可能是非法的.一个“合法”的输入是[-1000,1000]区间内的实数,并且最多精确到小数点后2位.当你计算平均值的时候, ...

  2. hibernate 查询方式

    1.对象导航查询 2.OID查询 3.hql查询 4.QBC查询 5.本地sql查询 一.对象导航查询 示例: 查询id=6的user对象的所有角色: 二.OID查询 实例查询id=6的user对象 ...

  3. RZ11 系统配置参数

    SAP系统配置参数详解[转] SAP 系统参数设置 path: /usr/sap/PRD/SYS/profile profile: PRD_DVEBMGS00_sapapp 如果您想查看所有的参数及当 ...

  4. BAPI LIST

    [转自 http://blog.csdn.net/minsenwu/article/details/8432081] 库存管理BAPI 库存: 1. BAPI_MATERIAL_AVAILABILIT ...

  5. requests ip代理池单ip和多ip设置方式

    reqeusts库,在使用ip代理时,单ip代理和多ip代理的写法不同 (目前测试通过,如有错误,请评论指正) 单ip代理模式 省去headers等 import requests proxy = { ...

  6. html5 canvas做的图表插件

    用highchart的时候发现它是用svg来画图的,那么用canvas来做怎么样的. 以前做AS图表插件的时候,绘制图画主要用容器的Graphics对象来绘制,而canvas的context和Grap ...

  7. ZOJ - 1505 Solitaire 【双向BFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1505 题意 一个8 * 8 的棋盘上面有四个棋子 棋子可以上下左 ...

  8. Effective java -- 9 并发/序列化

    关于同步的问题,想弄明白java,同步不会是不行的.这不书弄完后还会从<java并发编程实战>和<java并发编程的艺术>选一本或者都看. 第六十六条:同步访问共享的可变数据说 ...

  9. Linux LVM管理

    创建和管理LVM 要创建一个LVM系统,一般需要经过以下步骤: 1. 创建分区 fdisk /dev/sdb 使用分区工具(如:fdisk等)创建LVM分区,方法和创建其他一般分区的方式是一样的,区别 ...

  10. 格式化namenode时 报错 No Route to Host from node1/192.168.1.111 to node3:8485 failed on socket timeout exception: java.net.NoRouteToHostException: No route to host

    // :: FATAL namenode.NameNode: Failed to start namenode. org.apache.hadoop.hdfs.qjournal.client.Quor ...