A Simple Problem with Integers
 

Description

You have N integers, A1A2, ... , 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 A1A2, ... , 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.

Source

 

线段树功能:update:成段增减 query:区间求和

 #include<cstdio>
#include<algorithm> #define clr(x,y) memset(x,y,sizeof(x))
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 const int maxn=1e5+;
using namespace std; LL sum[maxn<<],Lazy[maxn<<]; void PushUp(int rt)
{
sum[rt]=sum[rt<<]+sum[rt<<|];
} void PushDown(int rt,int m)
{
if(Lazy[rt]) {
Lazy[rt<<]+=Lazy[rt];
Lazy[rt<<|]+=Lazy[rt];
sum[rt<<]+=(m-(m>>))*Lazy[rt];
sum[rt<<|]+=(m>>)*Lazy[rt];
Lazy[rt]=;
}
} void build(int l,int r,int rt)
{
int m;
Lazy[rt]=;
if(l==r) {
scanf("%lld",&sum[rt]);
return;
} m=(l+r)>>;
build(lson);
build(rson);
PushUp(rt);
} void Updata(int L,int R,int c,int l,int r,int rt)
{
int m;
if(L<=l && r<=R) {
Lazy[rt]+=c;
sum[rt]+=(LL)c*(r-l+);
return;
} PushDown(rt,r-l+);
m=(l+r)>>;
if(L<=m) Updata(L,R,c,lson);
if(R>m) Updata(L,R,c,rson);
PushUp(rt); } LL query(int L,int R,int l,int r,int rt)
{
int m;
LL ret=;
if(L<=l && r<=R) {
return sum[rt];
} PushDown(rt,r-l+);
m=(l+r)>>;
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson); return ret;
} int main()
{
int Q,n,a,b,c;
char st[]; scanf("%d%d",&n,&Q);
build(,n,); while(Q--) {
scanf("%s",st);
if(st[]=='C') {
scanf("%d%d%d",&a,&b,&c);
Updata(a,b,c,,n,);
} else {
scanf("%d%d",&a,&b);
printf("%lld\n",query(a,b,,n,));
} } return ;
}

[POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  8. POJ 3468 A Simple Problem with Integers (伸展树区间更新求和操作 , 模板)

    伸展数最基本操作的模板,区间求和,区间更新.为了方便理解,特定附上一自己搞的搓图 这是样例中的数据输入后建成的树,其中的1,2是加入的边界顶点,数字代表节点编号,我们如果要对一段区间[l, r]进行操 ...

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

随机推荐

  1. hadoop集群的故障概率估算

    hadoop集群的机器数业界(国内)最大的在5000左右,是什么限制了集群的规模呢?有好几个原因. 1. namenode的内存大小限制 2. 机器故障概率随着机器数目增大而增大,通常一份数据存储在h ...

  2. 桌面上嵌入窗口(桌面日历)原理探索(将该窗口的Owner设置成桌面的Shell 窗口,可使用SetWindowLong更改窗口的GWL_HWNDPARENT,还要使用SetWindowPos设置Z-Order)

    今天在QQ群里有人问怎样实现将自己的窗口嵌入桌面,让它和桌面融为一体,就像很多桌面日历软件那样. 我当时想到的就是建立一个Child  Window,将他的父窗口设置成桌面Shell窗口就可以了.但是 ...

  3. 64位调试器花费的时间比预期的要长(A 64-bit debugging operation is taking longer than expected)

    在stackoverflow上找到解决方案的: http://stackoverflow.com/questions/21329899/vs2013-professional-local-64-bit ...

  4. CH Round #53 -GCD Path

    描述 给定一张N个点的有向图,点i到点j有一条长度为 i/(gcd(i,j))的边.有Q个询问,每个询问包含两个数x和y,求x到y的最短距离. 输入格式 第一行包含两个用空格隔开的整数,N和Q. 接下 ...

  5. 黑马程序员_Java面向对象2_继承

    4.面向对象_继承 4.1继承的概述 提高了代码的复用性. 让类与类之间产生了关系,有了这个关系,才有多态的特性. 注意:千万不要为了获取其他类的功能而去继承,简化代码而继承.必须是类与类之间有所属关 ...

  6. poj1305:概念水题

    了解一下毕达哥拉斯三元组概念= = 暴力求出所有的本源三元组即可 代码: #include <iostream> #include <stdio.h> #include< ...

  7. Ugly Number II 解答

    Question Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime ...

  8. Oracle百问百答(四)

    Oracle百问百答(四) 31.怎样查看某用户下的表? select table_name from all_tables where owner=upper('jhemr'); 32.怎样查看某用 ...

  9. c语言通过时间种子产生随机数并选出最大值以及下标

    1 #include <stdio.h> #include <stdlib.h> #include <time.h> //2016 10 10 void main( ...

  10. 代理delegate、NSNotification、KVO在开发中的抉择

    在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信.在IOS应用不断的出现三种模式来实现这种通信: 1.委托delegation: 2.通 ...