POJ3468 A Simple Problem with Integers 【段树】+【成段更新】
| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 57666 | Accepted: 17546 | |
| 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
2014-9-4 16:25:07更新
#include <stdio.h>
#include <string.h>
#define maxn 100002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll; struct Node{
ll lazy, sum;
} tree[maxn << 2]; void pushUp(int rt){
tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
} void pushDown(int l, int r, int rt)
{
int mid = (l + r) >> 1;
tree[rt << 1].sum += (mid - l + 1) * tree[rt].lazy;
tree[rt << 1 | 1].sum += (r - mid) * tree[rt].lazy;
tree[rt << 1].lazy += tree[rt].lazy;
tree[rt << 1 | 1].lazy += tree[rt].lazy;
tree[rt].lazy = 0;
} void build(int l, int r, int rt)
{
tree[rt].lazy = 0;
if(l == r){
scanf("%lld", &tree[rt].sum);
return;
}
int mid = (l + r) >> 1;
build(lson); build(rson);
pushUp(rt);
} void update(int left, int right, ll val, int l, int r, int rt)
{
if(left == l && right == r){
tree[rt].sum += (r - l + 1) * val;
tree[rt].lazy += val; return;
}
int mid = (l + r) >> 1;
if(tree[rt].lazy) pushDown(l, r, rt);
if(right <= mid) update(left, right, val, lson);
else if(left > mid) update(left, right, val, rson);
else {
update(left, mid, val, lson);
update(mid + 1, right, val, rson);
}
pushUp(rt);
} ll query(int left, int right, int l, int r, int rt)
{
if(left == l && right == r) return tree[rt].sum;
int mid = (l + r) >> 1;
if(tree[rt].lazy) pushDown(l, r, rt);
if(right <= mid) return query(left, right, lson);
else if(left > mid) return query(left, right, rson);
return query(left, mid, lson) + query(mid + 1, right, rson);
} int main()
{
int n, m, i, a, b;
char com[2];
ll c;
while(scanf("%d%d", &n, &m) == 2){
build(1, n, 1);
while(m--){
scanf("%s%d%d", com, &a, &b);
if(com[0] == 'Q')
printf("%lld\n", query(a, b, 1, n, 1));
else{
scanf("%lld", &c);
update(a, b, c, 1, n, 1);
}
}
}
return 0;
}
RE了三次,发现是query函数内的pushdown函数位置放错了。
。
改了下,最终A掉了。
//#define DEBUG
#include <stdio.h>
#define maxn 100002
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1 long long tree[maxn << 2], arr[maxn], lazy[maxn << 2]; void pushUp(int rt)
{
tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
} void pushDown(int l, int r, int rt)
{
int mid = (l + r) >> 1; tree[rt << 1] += (mid - l + 1) * lazy[rt];
tree[rt << 1 | 1] += (r - mid) * lazy[rt]; lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
} void build(int l, int r, int rt)
{
lazy[rt] = 0;
if(l == r){
tree[rt] = arr[r]; return;
} int mid = (l + r) >> 1;
build(lson);
build(rson); pushUp(rt);
} void update(int left, int right, long long val, int l, int r, int rt)
{
if(left == l && right == r){
lazy[rt] += val; tree[rt] += val * (r - l + 1); return;
} //include l == r if(lazy[rt]) pushDown(l, r, rt); int mid = (l + r) >> 1;
if(right <= mid) update(left, right, val, lson);
else if(left > mid) update(left, right, val, rson);
else{
update(left, mid, val, lson);
update(mid + 1, right, val, rson);
} pushUp(rt);
} long long query(int left, int right, int l, int r, int rt)
{
if(left == l && right == r)
return tree[rt]; if(lazy[rt]) pushDown(l, r, rt); int mid = (l + r) >> 1;
if(right <= mid){
return query(left, right, lson);
}else if(left > mid){
return query(left, right, rson);
}else{
return query(left, mid, lson) + query(mid + 1, right, rson);
}
} int main()
{
#ifdef DEBUG
freopen("../stdin.txt", "r", stdin);
freopen("../stdout.txt", "w", stdout);
#endif int n, q, i, a, b;
long long c;
char com[2];
while(scanf("%d%d", &n, &q) == 2){
for(i = 1; i <= n; ++i)
scanf("%lld", arr + i);
build(1, n, 1); while(q--){
scanf("%s%d%d", com, &a, &b);
if(com[0] == 'C'){
scanf("%lld", &c);
update(a, b, c, 1, n, 1);
}else printf("%lld\n", query(a, b, 1, n, 1));
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
POJ3468 A Simple Problem with Integers 【段树】+【成段更新】的更多相关文章
- 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 Case Time Limit:2000MS Descr ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- POJ 3468 A Simple Problem with Integers (线段树成段更新)
题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...
- POJ-3468 A Simple Problem with Integers (区间求和,成段加减)
You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- A Simple Problem with Integers(线段树,区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 83822 ...
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
转载请注明出处:http://blog.csdn.net/u012860063 Description You have N integers, A1, A2, ... , AN. You need ...
- POJ3468 A Simple Problem with Integers —— 线段树 区间修改
题目链接:https://vjudge.net/problem/POJ-3468 You have N integers, A1, A2, ... , AN. You need to deal wit ...
随机推荐
- 采用truelicense进行Java规划license控制 扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址
采用truelicense进行Java规划license控制 扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址. Truelicense 它是一个开源java license ...
- 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern)
原文:乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 适配器模式(Adapter Pattern) 作者:webabc ...
- memset功能的具体说明
1.void *memset(void *s,int c,size_t n)总的效果:内存空间开辟了 s 第一 n 字节的值设置为一个值 c. 2.样本#include void main(){cha ...
- 不用库(框架),自己写ajax
平常会使用ajax来请求数据,加载一个库(框架),或许仅仅maybe就使用了它的ajax部分. 写个ajax,一来可以经历一下处理问题的过程,提升技术能力,二来工作中有时真的用不着这么大的一个库(框架 ...
- Apache Commons Math3学习笔记(2) - 多项式曲线拟合(转)
多项式曲线拟合:org.apache.commons.math3.fitting.PolynomialCurveFitter类. 用法示例代码: // ... 创建并初始化输入数据: double[] ...
- 【iOS开发-图层】自己定义图层的两种方式
想要自己定义图层,仅仅须要构建一个类继承CALayer方法 假设让自己定义图层初始化上面就有画好的图形.有两种办法 重写drawInContext方法 自己定义的图层以下的方法.然后必须自己定义的图层 ...
- 终结者单身——setAccessible(true)
首先看一下"传说"Singleton模式 package go.derek; public class Singleton{ public static int times; pr ...
- 为什么OC语言很难
作为一个Objective-C的coder,我总能听到一部分人在这门语言上抱怨有很多问题.他们总在想快速学习这门语言来写一个App出来,但他们也总是联想到Objective-C看上去实在太难了或者在想 ...
- SQL Server高可用——日志传送(4-3)——使用
原文:SQL Server高可用--日志传送(4-3)--使用 顺接上一篇:SQL Server高可用--日志传送(4-2)--部署 本文为本系列最重要的一篇,讲述如何使用日志传送及一些注意事项.从上 ...
- T-SQL技巧收集——拆分字符串
原文:T-SQL技巧收集--拆分字符串 在开发中,很多时候都需要处理拆分字符串的操作.下面收集了几种方法供大家分享,其中的逗号可以改为多种有需要的符号,但是不能针对多种符号同时存在的例子.有待各位补充 ...