POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)
A Simple Problem with Integers
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 139191 | Accepted: 43086 | |
| 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 <algorithm>
#include <queue>
#include <stack>
#define oo 0x3f3f3f3f
using namespace std;
struct node
{
long long int lazy;
long long int data;
int l, r;
};
struct node tree[10000000];
long long int Begin[10000000];
void Buildtree( int root, int l, int r )
{
tree[root].l = l;
tree[root].r = r;
tree[root].lazy = 0;
if( l == r )
tree[root].data = Begin[l];
else
{
int mid = ( l + r ) >> 1;
Buildtree( root<<1, l, mid );
Buildtree( root<<1|1, mid+1, r);
tree[root].data = tree[root<<1].data + tree[root<<1|1].data;
}
}
void Pushdown( int root )
{
if( tree[root].lazy != 0 )
{
tree[root<<1].lazy += tree[root].lazy;
tree[root<<1|1].lazy += tree[root].lazy;
tree[root<<1].data += ( tree[root<<1].r - tree[root<<1].l + 1 ) * tree[root].lazy;
tree[root<<1|1].data += ( tree[root<<1|1].r - tree[root<<1|1].l + 1 ) * tree[root].lazy;
tree[root].lazy = 0;
}
}
void Updata( int root, int l, int r, int z )
{
int i = tree[root].l, j = tree[root].r;
if( i > r || l > j )
return;
if( i >= l && j <= r )
{
tree[root].data += (j - i + 1) * z;
tree[root].lazy += z;
return;
}
Pushdown( root );
Updata( root<<1, l, r, z );
Updata( root<<1|1, l, r, z );
tree[root].data = tree[root<<1].data + tree[root<<1|1].data;
}
long long int Query ( int root, int l, int r )
{
int i = tree[root].l, j = tree[root].r;
if( i > r || l > j )
return 0;
if( l <= i && r >= j )
return tree[root].data;
Pushdown( root );
return Query(root<<1, l, r) + Query(root<<1|1, l, r);
}
int main()
{
int i, n, q;
scanf("%d %d", &n, &q);
for( i=1; i<=n; i++ )
scanf("%lld", &Begin[i]);
Buildtree( 1, 1, n );
while( q-- )
{
char order;
int a, b, c;
getchar();
scanf("%c", &order);
if( order == 'C' )
{
scanf("%d %d %d", &a, &b, &c);
Updata( 1, a, b, c);
}
else if( order == 'Q' )
{
scanf("%d %d", &a, &b);
printf("%lld\n", Query( 1, a, b ));
}
}
return 0;
}
POJ-3468-A Simple Problem with Integers(线段树 区间更新 区间和)的更多相关文章
- POJ 3468 A Simple Problem with Integers (线段树多点更新模板)
题意: 给定一个区间, 每个区间有一个初值, 然后给出Q个操作, C a b c是给[a,b]中每个数加上c, Q a b 是查询[a,b]的和 代码: #include <cstdio> ...
- 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 ...
- 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 ...
- 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 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 75541 ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- poj 3468 A Simple Problem with Integers 线段树加延迟标记
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
- poj 3468 A Simple Problem with Integers 线段树区间更新
id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072 ...
随机推荐
- 把jpg文件读取到内存char* 再转换成CImage
网络上找到大神写的转换方法,不过就记下来,学习学习: 当然转成CImage之后就可以从CImage转换成HBITMAP 了 void DrawPic(CDC *pDC,char *buf,int le ...
- oracle语言基础
一.语言分类 1.DML(Data Manipulation Language,数据操作语言):用于对数据的操作. DML包括:(1)SELECT:查询数据 select * from temp; ...
- quartz在web.xml的配置
第一步:下载所需的Jar包 commons-beanutils.ja.commons-collections.jar.commons-logging.jar.commons-digester.jar. ...
- laravel策略类,实现当前登陆的用户是否具有删除,修改文章的权限
策略类依赖月门脸类Auth 首先创建一个门脸类 make:auth 然后再创建一个策略 php artisan make:policy PostPolicy 定义Auth的登陆类,用的是哪个模型登陆 ...
- window7 和ubuntu 双系统时 ubuntu不能引导怎么办?
假如你的Ubuntu的 / 分区是sda9,又假如 /boot分区是 sda6,在终端下输入sudo -imount /dev/sda7 /mntmount /dev/sda6 /mnt/boot ( ...
- 532. K-diff Pairs in an Array绝对值差为k的数组对
[抄题]: Given an array of integers and an integer k, you need to find the number of unique k-diff pair ...
- CentOS双网卡双IP设置
CentOS双网卡双IP设置 系统环境:CentOS Linux 网络环境: 两个IP地址,192.168.0.10和10.10.30.2,掩码是255.255.255.0,这两个子网的网关地址分别是 ...
- linux下配置eclipse环境
注明:本文为博主原创文章,转载请注明出处 前期准备 (此文使用的是非安装版jdk1.8,你也可以下载版本更低的,而且建议使用1.6版本,66大顺嘛,嘻嘻) 1.点击下载jdk 2.点击下载eclips ...
- SP1557 GSS2 - Can you answer these queries II
一开始看不懂题解,看懂了题解之后觉得还是挺妙的. 好多题解里都提到了HH的项链,但是我觉得关系并不大啊…… 先把所有询问离线下来按照右端点排序,按照询问的要求一个一个加入数字,怎么加入数字,我们设计一 ...
- npm 升级
当我们运行某个项目是 会提示 > my-first-vue-project@1.0.0 dev C:\Users\ASUS\my-project > node build/dev-serv ...