A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 139191   Accepted: 43086
Case Time Limit: 2000MS

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 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.

线段树模板题

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#define oo 0x3f3f3f3f
using namespace std; struct node
{
long long int lazy;
long long int data;
int l, r;
}; struct node tree[10000000];
long long int Begin[10000000]; void Buildtree( int root, int l, int r )
{
tree[root].l = l;
tree[root].r = r;
tree[root].lazy = 0; if( l == r )
tree[root].data = Begin[l]; else
{
int mid = ( l + r ) >> 1;
Buildtree( root<<1, l, mid );
Buildtree( root<<1|1, mid+1, r); tree[root].data = tree[root<<1].data + tree[root<<1|1].data;
}
} void Pushdown( int root )
{
if( tree[root].lazy != 0 )
{
tree[root<<1].lazy += tree[root].lazy;
tree[root<<1|1].lazy += tree[root].lazy; tree[root<<1].data += ( tree[root<<1].r - tree[root<<1].l + 1 ) * tree[root].lazy;
tree[root<<1|1].data += ( tree[root<<1|1].r - tree[root<<1|1].l + 1 ) * tree[root].lazy; tree[root].lazy = 0;
}
} void Updata( int root, int l, int r, int z )
{
int i = tree[root].l, j = tree[root].r;
if( i > r || l > j )
return; if( i >= l && j <= r )
{
tree[root].data += (j - i + 1) * z;
tree[root].lazy += z;
return;
} Pushdown( root ); Updata( root<<1, l, r, z );
Updata( root<<1|1, l, r, z ); tree[root].data = tree[root<<1].data + tree[root<<1|1].data;
} long long int Query ( int root, int l, int r )
{
int i = tree[root].l, j = tree[root].r;
if( i > r || l > j )
return 0; if( l <= i && r >= j )
return tree[root].data; Pushdown( root ); return Query(root<<1, l, r) + Query(root<<1|1, l, r);
} int main()
{
int i, n, q;
scanf("%d %d", &n, &q);
for( i=1; i<=n; i++ )
scanf("%lld", &Begin[i]);
Buildtree( 1, 1, n ); while( q-- )
{
char order;
int a, b, c;
getchar();
scanf("%c", &order);
if( order == 'C' )
{
scanf("%d %d %d", &a, &b, &c);
Updata( 1, a, b, c);
}
else if( order == 'Q' )
{
scanf("%d %d", &a, &b);
printf("%lld\n", Query( 1, a, b ));
}
} return 0;
}

POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers (线段树多点更新模板)

    题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...

  2. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  3. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  4. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

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

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

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

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

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

  7. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  8. POJ 3468 A Simple Problem with Integers //线段树的成段更新

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

  9. poj 3468 A Simple Problem with Integers 线段树加延迟标记

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

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

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

随机推荐

  1. Directshow 采集音视频数据H264+AAC+rtmp效果还不错

    从usb摄像头或者采集卡中采集效果还是不错的.

  2. 常用的正则规则,直接copy就OK了

    import  re #用户名验证:(数字字母或下划线6到20位)re.match("/^\w{6,20}$/",匹配对象) #邮箱验证: re.match(" /^[a ...

  3. sql数据库各个版本清除日志

    SQL2005清空删除日志: 复制代码 代码如下: Backup Log DNName with no_log           --'这里的DNName是你要收缩的数据库名,自己注意修改下面的数据 ...

  4. interface vs abstract

    [interface vs abstract] 1.interface中的方法不能用public.abstract修饰,interface中的方法只包括signature. 2.一个类只能继承一个ab ...

  5. 【bzoj2947】[Poi2000]促销

    2947: [Poi2000]促销 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 181  Solved: 120[Submit][Status][D ...

  6. Linux awk&sed

    awk AWK是强大的文本处理工具,擅长对日志文件迚行快速分析. 它丌仅用亍 Linux ,也是任何环境中现有的功能最强大的数据处理引擎之一. 名称得自亍它的发明者 Alfred Aho .Pet ...

  7. c++原型模式(Prototype)

    原型模式是通过已经存在的对象的接口快速方便的创建新的对象. #include <iostream> #include <string> using namespace std; ...

  8. php+mysql网站无限级栏目分类-递归获取树形结构函数

    如果网站采用了无限级栏目结构,我们可以将网站所有栏目获取出来组成一个树形结构.数据库结构: 函数代码: //获得指定文章分类的子分类组成的树形结构 function cateTree($pid=0,$ ...

  9. 基于django rest framework做认证组件

    先导入要用到的类 from rest_framework.authentication import BaseAuthentication from rest_framework.exceptions ...

  10. Win7怎么进入安全模式 三种轻松进入Win7安全模式方法

    发布时间:2013-05-27 11:23 作者:电脑百事网原创 来源:www.pc841.com 13783次阅读 win7的安全模式和XP如出一辙,在安全模式里我们可以删除顽固文件.查杀病毒.解除 ...