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

    public class Solution { public string IntToRoman(int num) { string[] M = { "", "M&quo ...

  2. ubuntu12上部署Django1.8.4+uwsgi+nginx超级详细流程配置到云服务器

    环境: 系统:ubuntu12,系统自带默认有python2.7 框架:Django1.8.4,需要python2.7以上才能支持   前言: 用户浏览器发送http请求->nginx(静态文件 ...

  3. 微信公众号php从0开发,包括功能(自定义菜单,分享)

    之前写的一篇微信公众号文章. 工作需要,进行此次调研,并记录开发过程. 开发目的,页面授权,页面获取用户头像,用户昵称 微信id, 分享页面. 微信订阅号 无法获取用户个人信息 写在记录前,公众号也是 ...

  4. Mysql慢查询定位和优化实践分享

    调优目标:提高io的利用率,减少无谓的io能力浪费. 1.打开慢查询日志定位慢sql: my.cnf: slow_query_log slow_query_log_file=mysql.slow lo ...

  5. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial—5.4 绘制数据图

    5.4 绘制数据图 参考视频: 5 - 4 - Plotting Data (10 min) 5.4.1 绘制曲线 1.画一个sin曲线 >> t = [:0.01:0.98]; > ...

  6. day63-webservice 03.解析cxf提供的例子

    Path配置: C:\Program Files (x86)\ScanSign;E:\app\zhongzh\product\11.2.0\dbhome_1\bin;D:\app\zhongzh\pr ...

  7. Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity Framework Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 38 Entity F ...

  8. 基于jquery 的插件,让IE支持placeholder属性

    开发一个项目的时候为了美观和用户体验用到了input标签的placeholder属性,但是这个属性是html5中的,所以低版本的IE浏览器不支持.于是在百度找了一些解决方法,找了好几个都不是那么完美, ...

  9. commons-X系列

    1 commons-lang 1.1 ReflectionToStringBuilder 将对象进行字符串拼接 /* * Licensed to the Apache Software Foundat ...

  10. 50. Pow(x, n) 幂次方

    [抄题]: mplement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, ...