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. iOS9开发者测试版下载地址(系转载)

    将链接复制后,打开迅雷即可下载!请根据自己的手机型号下载.不需要udid绑定.开发者账号,下载后直接升级即可.请根据自己的手机型号将链接复制 打开迅雷 点击立即下载即可.不过好像Mac版的迅雷下载不了 ...

  2. Python判断当前用户是否是root

    import osif os.geteuid() != 0:print "This program must be run as root. Aborting."sys.exit( ...

  3. JQuery知识点链接

    1.深入理解jQuery插件开发                      http://learn.jquery.com/plugins/basic-plugin-creation/ 2.jQuer ...

  4. EOS单向N对1关联

    1. N端实体中用于关联的属性可以是主键也可以是非主键,1端的关联字段必须是主键(可以是单主键也可以是复合主键). 如下图关联字段:orgid 2.当在N端选择了用于关联的属性,那么这些属性在N端实体 ...

  5. 【leetcode】Rotate Image

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...

  6. POJ 2479

    ---恢复内容开始--- http://poj.org/problem?id=2479 #include <stdio.h> #include <iostream> using ...

  7. android气泡消息提醒布局

    无论是anroid还是ios,气泡消息提醒再正常不过了.然而要定义一个气泡消息提醒确要费一番周折.下面记录下气泡提醒布局. 定义气泡背景shape_unread_message_bg.xml < ...

  8. 华为 MATE7 调试 LOCAT 日志不输出问题

    [转]华为 MATE7 调试 LOCAT 日志不输出问题 http://www.cnblogs.com/glaivelee/p/4593221.html 用手机进行调试,在电脑上不显示logcat信息 ...

  9. Effective C++ -----条款23:宁以non-member、non-friend替换member函数

    宁可拿non-member non-friend函数替换member函数.这样做可以增加封装性.包裹弹性(packaging flexibility)和机能扩充性.

  10. ATS(App Transport Security)对HTTP协议屏蔽引起的问题

    一.问题描述 在学习网络处理的过程,发现代码都没错,运行时会收到如下错误提示: App Transport Security has blocked a cleartext HTTP (http:// ...