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. 在.jsp中非表单请求action的几种方式总结

    转自:https://www.jb51.net/article/35621.htm 1 一: 复制代码 代码如下: <a href="userAction.do?flag=user_r ...

  2. latex中如何引用公式

    在使用latex编辑文章时,经常会需要引用公式.图表等等. 如果我们人为地对这些公式.图表进行编号1-2-3-4,然后在文章中使用Eq(1)-Eq(2)-Eq(3)-Eq(4)去引用这些公式,固然是可 ...

  3. web页面在ios下不支持fixed可用absolute替代的方案

    本文引用出处:http://www.cnblogs.com/PeunZhang/p/3553020.html. 对于 ios4 和 android2.2 以下不支持 position:fixed 的问 ...

  4. POJ1161(并查集)

    1.题目链接地址 http://poj.org/problem?id=1161 2.源代码 #include <iostream> using namespace std; ]; ]; i ...

  5. 安装oracle xe一些注意点

    主要是web管理数据的的端口8080端口的问题, 会和tomcat冲突 安装时把开启8080端口的tomcat启动了 占用8080端口就行了, 然后安装oracle xe就会让你输入 web管理数据的 ...

  6. 求正整数n的所有因子

    因子的概念:假如整数n除以m,结果是无余数的整数,那么我们称m就是n的因子. 需要注意的是,唯有被除数,除数,商皆为整数,余数为零时,此关系才成立.反过来说,我们称n为m的倍数. 求一个正整数n的所有 ...

  7. Weblogic的安装、配置与应用部署

    1. Weblogic安装 1.1 Linux下安装过程 安装环境: 操作系统: redhat-release-5Server-5.4.0.3 Weblogic版本: Weblogic 9.24 1) ...

  8. Mybatis简化sql书写,别名的使用

    之前,我们在sql映射xml文件中的引用实体类时,需要写上实体类的全类名(包名+类名),如下: <!-- 创建用户(Create) --> <insert id="addU ...

  9. dubbo参数调优

    dubbo中配置优先级规律:方法级配置优先级高于接口级,consumer的优先级高于provider. 详细: consumer的method配置  >  provider的method配置 c ...

  10. suse配置dhcp服务器

    Suse  dhcp服务器安装在安装系统时勾选 Suse dhcp 默认配置文件 /etc/dhcpd.conf Suse dhcp 启动程序 /etc/init.d/dhcpd restart 配置 ...