A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 67511   Accepted: 20818
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 Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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 <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm>
using namespace std;
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define lr rt<<1
#define rr rt<<1|1
typedef long long LL;
const int oo = 1e9+;
const double PI = acos(-1.0);
const double eps = 1e- ;
const int N = ;
const int mod = ;
int n , m ;
LL sum[N<<] , lazy[N<<] , e[N] , tot ; void Up( int rt ) {
sum[rt] = sum[lr] + sum[rr];
}
void Down( int l , int r , int rt ) {
if( lazy[rt] != ) {
int mid = (l+r)>>;
sum[lr] += lazy[rt]*(mid-l+) , sum[rr] += lazy[rt]*(r-mid);
lazy[lr] += lazy[rt] , lazy[rr] += lazy[rt];
lazy[rt] = ;
}
}
void build( int l , int r , int rt ){
lazy[rt] = ;
if( l == r ) {
sum[rt] = e[tot++];
return ;
}
int mid = (l+r)>>;
build(lson),build(rson);
Up(rt);
}
void update( int l , int r , int rt , int L , int R , LL val ) {
if( L == l && r == R ) {
sum[rt] += val*(r-l+) ;
lazy[rt] += val;
return ;
}
Down( l , r , rt );
int mid = (l+r)>>;
if( R <= mid ) update(lson,L,R,val);
else if( L > mid ) update(rson,L,R,val);
else update(lson,L,mid,val) , update(rson,mid+,R,val);
Up(rt);
}
LL query( int l , int r , int rt , int L , int R ) {
if( L == l && r == R ) {
return sum[rt];
}
Down(l,r,rt);
int mid = (l+r)>>;
if( R <= mid ) return query(lson,L,R);
else if( L > mid ) return query(rson,L,R);
else return query(lson,L,mid) + query(rson,mid+,R);
} int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
#endif // LOCAL
char s[]; int x , y ; LL c ;
while( ~scanf("%d%d",&n,&m ) ) {
tot = ;
for( int i = ; i < n ; ++i ) {
scanf("%I64d",&e[i]);
}
build(root);
while(m--) {
scanf("%s",s);
if( s[] == 'Q' ) {
scanf("%d%d",&x,&y);
printf("%I64d\n",query(root,x,y));
}
else {
scanf("%d%d%I64d",&x,&y,&c);
update(root,x,y,c);
}
}
}
}

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. 【学习总结】认识MVC

    参考链接: 菜鸟教程-MVC模式 CSDN:浅谈MVC架构-你到底有什么本事 目录: 一.什么是MVC 1.概念 MVC全名是Model View Controller,是模型(model)-视图(v ...

  2. Gradle中的GroupID和ArtifactID指的是什么?

    GroupId和ArtifactId被统称为“坐标”是为了保证项目唯一性而提出的,如果你要把你项目弄到maven本地仓库去,你想要找到你的项目就必须根据这两个id去查找. GroupId一般分为多个段 ...

  3. ubuntu vim8.1编译安装

    sudo apt-get install libncurses5-dev python-dev python3-dev libgtk-3-dev libatk1.0-dev libbonoboui2- ...

  4. 后缀自动机(SAM) 学习笔记

    最近学了SAM已经SAM的比较简单的应用,SAM确实不好理解呀,记录一下. 这里提一下后缀自动机比较重要的性质: 1,SAM的点数和边数都是O(n)级别的,但是空间开两倍. 2,SAM每个结点代表一个 ...

  5. 开源Futter项目

    前段时间Flutter很火,所以在闲暇之余做了一个助学通的Flutter移动端应用,现在开源出来,希望对想要学习Flutter的朋友有所帮助. 我大致做个项目介绍: 学生签到系统:分java服务端提供 ...

  6. .net core webapi添加swagger

    依赖项——右键——管理NuGet程序包——浏览——输入以下内容 Install-Package Swashbuckle.AspNetCore -Pre 双击Properties——点击生成——勾选XM ...

  7. Java实现线程的两种方式?Thread类实现了Runnable接口吗?

    Thread类实现了Runnable接口吗? 我们看看源码中对与Thread类的部分声明 public class Thread implements Runnable { /* Make sure ...

  8. hive基础知识or基本操作命令

    MySQL的密码是:123456 1.hive创建标准表(以后均可以按照这样创建): create [external] table [if not exists] records (year STR ...

  9. cookie记住账户密码

    //cookie保存路径 String autologin=request.getParameter("autologin"); String path = request.get ...

  10. html中 的method=post和method=get的区别

    1. get是从服务器上获取数据,post是向服务器传送数据. 2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过 ...