Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 115624   Accepted: 35897
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

Hint

The sums may exceed the range of 32-bit integers.

Source

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<deque>
#include<iomanip>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 100009
#define L 31
#define INF 1000000009
#define eps 0.00000001
/*
线段树 区间更新区间查询
*/
LL a[MAXN],pre[MAXN];
struct node
{
LL l, r;
LL data, sum, laz;
}T[MAXN*];
void build(LL p, LL l, LL r)
{
T[p].data = T[p].sum = T[p].laz = ;
T[p].l = l, T[p].r = r;
if (l == r) return;
LL mid = (l + r) / ;
build(p * , l, mid);
build(p * + , mid + , r);
}
void update(LL p, LL l, LL r, LL v)
{
//cout << p << ' ' << l << ' ' << r << ' ' << v << endl;
if (T[p].l >= l && T[p].r <= r)
{
T[p].data += v;
T[p].laz = ;
T[p].sum += (T[p].r - T[p].l + ) * v;
return;
}
LL mid = (T[p].l + T[p].r) / ;
if (T[p].laz)
{
T[p].laz = ;
update(p * , T[p].l, mid, T[p].data);
update(p * + , mid + , T[p].r, T[p].data);
T[p].data = ;
}
if (r <= mid)
update(p * , l, r, v);
else if (l > mid)
update(p * + , l, r, v);
else
{
update(p * , l, mid, v);
update(p * + , mid + , r, v);
}
T[p].sum = T[p * ].sum + T[p * + ].sum;
}
LL query(LL p, LL l, LL r)
{
if (l == T[p].l&&r == T[p].r)
return T[p].sum;
LL mid = (T[p].l + T[p].r) / ;
if (T[p].laz)
{
T[p].laz = ;
update(p * , T[p].l, mid, T[p].data);
update(p * + , mid + , T[p].r, T[p].data);
T[p].data = ;
}
if (r <= mid)
return query(p * , l, r);
else if (l > mid)
return query(p * + , l, r);
else
return query(p * , l, mid) + query(p * + , mid + , r);
}
LL n, q;
int main()
{
scanf("%lld%lld", &n, &q);
for (LL i = ; i <= n; i++)
scanf("%lld", &a[i]), pre[i] = pre[i - ] + a[i];
char c[];
LL a, b, d;
build(,,n);
while (q--)
{
scanf("%s", c);
if (c[] == 'Q')
scanf("%lld%lld", &a, &b), printf("%lld\n", query(, a, b) + pre[b] - pre[a-]);
else
scanf("%lld%lld%lld", &a, &b, &d), update(, a, b, d);
}
}

A Simple Problem with Integers 线段树 区间更新 区间查询的更多相关文章

  1. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

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

  2. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  4. POJ 3468A Simple Problem with Integers(线段树区间更新)

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

  5. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  6. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

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

  7. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  8. A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. ACM_01背包2

    背包4 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量不超过W的 ...

  2. 选择语言之switch case

    程序语言-选择语言之switch   case 多选一,类似if    else if  else if  else 模版: Switch(选择条件) { Case(条件一)//相当于if Conso ...

  3. LN : leetcode 53 Maximum Subarray

    lc 53 Maximum Subarray 53 Maximum Subarray Find the contiguous subarray within an array (containing ...

  4. 百度地图API在vue-cli中路径错误的问题

    在使用百度地图的时候,需要使用自定义的icon图片,百度的案例中使用的是线上地址,但当替换为本地图片路径的时候,错误出现了 这是本地图片地址 ) // 设置覆盖物大小 ); 这里有一点需要注意,这里路 ...

  5. android studio java.io.IOException:setDataSourse fail.

    这一次是针对Android开发中的一个小问题,权限获取的问题. 在写了一个一个小Android程序的时候,有时候普需要获取本机的文件(Audio&Video),这时候如果不加权限就会出现这种情 ...

  6. Shell输入/输出重定向

    输出重定向 重定向一般通过在命令间插入特定的符号来实现.特别的,这些符号的语法如下所示 command1 >file1 上面这个命令执行command1然后将输出的内容存入file1. 注意任何 ...

  7. axios方法封装

    axios方法封装 一般情况下,我们会用到的方法有:GET,POST,PUT,PATCH,封装方法如下:     五.封装后的方法的使用 1.在main.js文件里引用之前写好的文件,我的命名为htt ...

  8. ThinkPHP---thinkphp模型(M)

    (1)配置数据库连接 数据库的连接配置可以在系统配置文件ThinkPHP/Conf/convention.php中找到 /* 数据库设置 */ 'DB_TYPE' => '', // 数据库类型 ...

  9. A useful logger function in C project.

    #cat log.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...

  10. Bullet:关于ORACLE中的HASH JOIN的参数变化

    Oracle在7.3引入了hash join. 但是在Oracle 10g及其以后的Oracle数据库版本中,优化器,实际是CBO,也是因为HASH JOIN仅适用于CBO,在解析目标SQL时是否考虑 ...