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<stdio.h>
using namespace std; const int MAXN = ;
int a[MAXN];
long long int tree[MAXN*];
long long int lazy[MAXN*];
int N,Q;
string s;
int x , y, z;
void push_down(int l ,int r ,int rt)
{
int m = (l+r)/; if(lazy[rt])
{
tree[rt*] += lazy[rt]*(m-l+);
tree[rt*+] += lazy[rt]*(r-m);
lazy[rt*] += lazy[rt];
lazy[rt*+] += lazy[rt];
lazy[rt] = ;
}
}
void bulid_tree(int l ,int r ,int rt)
{
if(l==r)
{
tree[rt] = a[l];
return ;
}
int m = (l+r)/; bulid_tree(l,m,rt*);
bulid_tree(m+,r,rt*+);
tree[rt] = tree[rt*]+tree[rt*+];
} long long int Query(int x ,int y ,int l ,int r ,int rt)
{
long long sum = ;
if(x<=l&&r<=y)
{
return tree[rt];
}
int m = (l+r)/;
push_down(l,r,rt);
if(x<=m)
{
sum += Query(x,y,l,m,rt*);
}
if(m<y)
{
sum += Query(x,y,m+,r,rt*+);
}
return sum;
}
void Update(int x ,int y ,int k ,int l ,int r ,int rt)
{
if(x<=l&&y>=r)
{
tree[rt] += k*(r-l+);
lazy[rt] += k;
return ;
}
push_down(l,r,rt);
int m = (l+r)/;
if(x<=m)
{
Update(x,y,k,l,m,rt*);
}
if(y>m)
{
Update(x,y,k,m+,r,rt*+);
}
tree[rt] = tree[rt*]+tree[rt*+];
}
int main()
{
scanf("%d%d",&N,&Q);
for(int i = ; i <= N;i++)
{
scanf("%d",&a[i]);
}
bulid_tree(,N,);
while(Q--)
{
cin>>s;
if(s[]=='Q')
{
scanf("%d%d",&x,&y);
printf("%lld\n",Query(x,y,,N,)); }
else
if(s[]=='C')
{
scanf("%d%d%d",&x,&y,&z);
Update(x,y,z,,N,);
}
}
return ;
}

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

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

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

  2. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

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

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

  4. (简单) 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 ...

  5. Poj 3468-A Simple Problem with Integers 线段树,树状数组

    题目:http://poj.org/problem?id=3468   A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. POJ1020(小正方形铺大正方形)

    Anniversary Cake Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16579   Accepted: 5403 ...

  2. mongoTemplate操作内嵌文档

    关系型数据库中,表与表的关联关系有1:1,也有1:n的.在java的面向对象的世界里就是主对象嵌子对象,与主对象嵌集合<子对象>的两种形式. 主对象嵌子对象操作: 新增.修改都直接用如下方 ...

  3. Java-API-Package:javax.annotation

    ylbtech-Java-API-Package:javax.annotation 1.返回顶部 1. Package javax.annotation Enum Summary Resource.A ...

  4. [cinder] volume type 使用简记

    cinder type-create sharecinder type-key share set volume_backend_name=GLUSTERFScinder type-create lo ...

  5. rails的respond to format

    Here are all the default Rails Mime Types: "*/*" => :all "text/plain" => : ...

  6. 如何同时打开两个UltraEdit

    高级—配置—应用程序布局—其它,勾上资源管理器选的文件将以独立的UltraEdit打开和允许多个UltraEdit

  7. 2016.5.30实现透明Panel及控件置顶的方法

    想放置一个透明Panel在某控件上端,实现效果是可透过此Panel看见下面控件,但鼠标点击却无任何反应. 1.新建置自定义Panel类 using System; using System.Colle ...

  8. pl/sql对excel数据的导入和导出

    本来这部分是在上篇pl/sql的,但笔者介于此篇的内容,就独立出来了, 1.导出查询结果到excel文件,在查询结果上右键,然后弹出选择框如下: 2.从excel向数据库中导入数据: a.创建要导入的 ...

  9. DAY17-Ajax

    Ajax准备知识:json 什么是json? 定义: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式. 它基于 ECMAScript ( ...

  10. 7-EasyNetQ之Request & Response

    EasyNetQ也支持Request/Response这种方式的消息模式.这种方式很容易在client/Server应用中执行,客户端发送一个请求到服务器,服务器然后处理请求后返回一个响应.和传统的R ...