A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 58269   Accepted: 17753
Case Time Limit: 2000MS

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

 
  线段树,区间修改求和
  题意
  
  思路
  
 
  代码
 #include <iostream>
#include <stdio.h>
using namespace std; #define MAXN 100010 struct Node{
long long L,R;
long long sum; //当前区间的所有数的和
long long inc; //累加量
}a[MAXN*]; void Build(long long d,long long l,long long r) //建立线段树
{ //初始化当前节点的信息
a[d].L = l;
a[d].R = r;
a[d].inc = ; if(l==r){ //找到叶子节点
scanf("%I64d",&a[d].sum);
return ;
} //建立线段树
long long mid = (l+r)>>;
Build(d<<,l,mid);
Build(d<<|,mid+,r); //更新当前节点的信息
a[d].sum = a[d<<].sum + a[d<<|].sum;
} void Updata(long long d,long long l,long long r,long long v) //更新区间[l,r]的累加量为v
{
if(a[d].L==l && a[d].R==r){ //找到终止节点
a[d].inc += v;
return ;
} long long mid = (a[d].L+a[d].R)/;
a[d].sum += a[d].inc*(a[d].R - a[d].L + ); if(mid>=r){ //左孩子找
Updata(d<<,l,r,v);
}
else if(mid<l){ //右孩子找
Updata(d<<|,l,r,v);
}
else{ //左孩子、右孩子都找
Updata(d<<,l,mid,v);
Updata(d<<|,mid+,r,v);
} a[d].sum = a[d<<].sum + a[d<<|].sum
+ a[d<<].inc*(a[d<<].R - a[d<<].L + )
+ a[d<<|].inc*(a[d<<|].R - a[d<<|].L + );
} long long Query(long long d,long long l,long long r) //查询区间[l,r]的所有数的和
{
if(a[d].L==l && a[d].R==r){ //找到终止节点
return a[d].sum + a[d].inc * (r-l+);
} long long mid = (a[d].L+a[d].R)/;
//更新每个节点的sum
a[d].sum += a[d].inc * (a[d].R - a[d].L + );
a[d<<].inc += a[d].inc;
a[d<<|].inc += a[d].inc;
a[d].inc = ; //Updata(d<<1,a[d<<1].L,a[d<<1].R,a[d].inc);
//Updata(d<<1|1,a[d<<1|1].L,a[d<<1|1].R,a[d].inc); if(mid>=r){ //左孩子找
return Query(d<<,l,r);
}
else if(mid<l){ //右孩子找
return Query(d<<|,l,r);
}
else{ //左孩子、右孩子都找
return Query(d<<,l,mid) + Query(d<<|,mid+,r);
}
a[d].sum = a[d<<].sum + a[d<<|].sum
+ a[d<<].inc*(a[d<<].R - a[d<<].L + )
+ a[d<<|].inc*(a[d<<|].R - a[d<<|].L + );
} int main()
{
long long n,q,A,B;
long long v;
scanf("%I64d%I64d",&n,&q);
Build(,,n);
while(q--){ //q次询问
char c[];
scanf("%s",&c);
switch(c[]){
case 'Q':
scanf("%I64d%I64d",&A,&B);
printf("%I64d\n",Query(,A,B)); //输出区间[A,B]所有数的和
break;
case 'C':
scanf("%I64d%I64d%I64d",&A,&B,&v);
Updata(,A,B,v);
break;
default:break;
}
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 3468:A Simple Problem with Integers(线段树,区间修改求和)的更多相关文章

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

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

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

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

  3. POJ 3468 A Simple Problem with Integers 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

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

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

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

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

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

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

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

  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. HDU 1159 裸最长公共子串

    试着拍了一道模板题 dp开了500,开100会超时..... string类型中间有空格会判为结束 #include<algorithm> -->min,max函数的头文件 #inc ...

  2. IOS路线图

    存档,存档...

  3. 浏览器js自动查表脚本

    javascript: void((function() {$.get("", {wen: "880350384879600241",action: " ...

  4. c++数据类型和定义

    我们都知道,刚开始学习数学的时候.乘法口诀.99乘法口诀.这个是大家都需要背的.背熟了这个,大家才能知道遇到算术题如何计算.这个99乘法口诀就是一种定义. 同样任何的语言都会有很多的定义.比如语文:各 ...

  5. UIScrollView 的 delaysContentTouches

    UIScrollView 的一段说明: Because a scroll view has no scroll bars, it must know whether a touch signals a ...

  6. Oracle备份之RMAN

    1.备份:物理备份时文件层次的备份,逻辑备份时数据层次的备份,物理备份为主,逻辑备份作为补充.物理备份分为用户管理备份和RMAN备份,前者使用SQL命令和OS的cp命令进行文件备份,后者使用RMAN工 ...

  7. Python~win32com~Excel

    import win32com.client #w=win32com.client.Dispatch("Word.Application") #w.Visible=1 o=win3 ...

  8. 【leetcode】Largest Number ★

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. [Java 基础] 使用java.util.zip包压缩和解压缩文件

    reference :  http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...

  10. C#中的变量及命名规则

    变量: 1.作用 :可以让我们在计算机中存储数据 2.语法:变量类型    变量名=赋值: 3.常用的数据类型:  int   整数类型  取值范围:最大2147483647;最小-214748364 ...