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 ...
随机推荐
- Apache Derby数据库系统使用方法
Apache Derby数据库系统使用方法 最近由于项目要求,试用了一下Apache Derby数据库,这里对了解到的内容做一个记录. Apache Derby是一个开源的关系型数据库管理系统,用Ja ...
- redis实现发布订阅
订阅者 #!/usr/bin/env python # -*- coding:utf-8 -*- import redis r = redis.Redis(host='192.168.11.119') ...
- Texture Filter
[Texture Filter] 我们的纹理是要贴到三维图形表面的,而三维图形上的pixel中心和纹理上的texel中心并不一至(pixel不一定对应texture上的采样中心texel),大小也不一 ...
- OpenGL顶点缓冲区对象
[OpenGL顶点缓冲区对象] 显示列表可以快速简单地优化立即模式(glBegin/glEnd)的代码.在最坏的情况下,显示列表的命令被预编译存到命令缓冲区中,然后发送给图形硬件.在最好的情况下,是编 ...
- SpringMVC总结三:请求Controller返回视图类型以及请求方式、参数介绍
视图解析,请求Controller返回的视图类型: @Controller @RequestMapping("/test") public class TestController ...
- Cunit编译安装
1. Examples/Makefile.am:26: to 'configure.ac' and run 'autoconf' again. configure.ac:211: error: re ...
- SpringMVC——数据转换 & 数据格式化 & 数据校验
一.数据绑定流程 1. Spring MVC 主框架将 ServletRequest 对象及目标方 法的入参实例传递给 WebDataBinderFactory 实例,以创 建 DataBinder ...
- 互联网+大数据解决方案(ppt)
from: 互联网+大数据解决方案(ppt) 导读:大数据(bigdata),或称巨量资料,指的是所涉及的资料量规模巨大到无法透过目前主流软件工具,在合理时间内达到撷取.管理.处理.并整理成为帮助企业 ...
- 高级软件测试技术(测试管理工具实践day4)
今天是截止日期,有胡俊辉的指导下小组成员都了解使用了Bugzilla的基本使用.大家都在晚上之前把各自的文档汇总给汪鸿,由他撰写了操作手册.并且在下午杨瑞丰完成了视频的录制工作.但是在转化为MP4 格 ...
- cgroup初步分析(1)
cgroup的功能和作用不废话,直说一下cgroup的几条设计准则,有了几条设计准则的约束,就比较容易理解其中的数据结构和函数,至于源代码cgroup.c,无非是两个内容,一是task_struct. ...