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. Android 开发之深入理解安卓调试桥各种错误解决办法

    摘要: Android开发调试项目使用到安卓调试桥工具,Android Debug Bridge(ADB)位于sdk路径platform-tools文件夹,使用Android Studio或Eclip ...

  2. struts自定义拦截器实现

    示例 添加新功能:只有是登录的状态访问hello_loginSuccess才会显示登录成功. index.jsp登录成功页面 test.jsp登录页面 一.修改原代码实现 1.登录后将登录信息添加到S ...

  3. Function- 几个转换函数

    几个转换函数 SJIS_DBC_TO_SBC 全角转半角SJIS_SBC_TO_DBC 半角转全角 CLPB_IMPORT :从剪贴板导入internal tableCLPB_EXPORT : 从in ...

  4. linux 7- - watch,free,mpstat,vmstat,iostat,pidstat,df,du

    十八.  和系统运行状况相关的Shell命令:     1.  Linux的实时监测命令(watch):     watch 是一个非常实用的命令,可以帮你实时监测一个命令的运行结果,省得一遍又一遍的 ...

  5. c的详细学习(8)指针学习(二)

    (1)指针与二维数组 一个数组的名字代表该数组的的首地址,是地址常量(作为形式参数的数组名除外),这一规定对二维数组或更高维数组同样适用. 在c语言中定义的任何一个二维数组实际上都可以看做是一个一维数 ...

  6. 《DevExpress》记录之TreeList

    如这两幅图所示:如果要显示左边的竖线,需要设置 感谢 DoomGuards本节Dome下载地址:http://pan.baidu.com/s/1wBOJk 密码:vz4d

  7. 【leetcode刷题笔记】Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  8. 剑指offer——翻转单词顺序VS左旋转字符串

    字符串的交换等,注意判断字符串的是否为NULL,以及判断边界等. #include <iostream> #include <string> using namespace s ...

  9. Spring与web.xml

    出处:http://blog.csdn.net/u010796790 在web.xml配置监听器 ContextLoaderListener (listener-class) ContextLoade ...

  10. 剑指offer之 数组中出现次数超过一半的数字

    public class Solution { public int MoreThanHalfNum_Solution(int [] array) { if(array==null||array.le ...