POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468
题目大意很简单,有两个操作,一个
Q a, b 查询区间[a, b]的和
C a, b, c让区间[a, b] 的每一个数+c
第一次线段树的延时标记,花了好大的功夫才写好==!
很容易看出来使用使用线段树记录区间的和,但是难点在于每次修改的是一个区间而不是一个点
所以采用的方法就是每次做修改操作时,只将区间[a,b]的标记+c,而不是真正意义上的将区间[a, b] 的每一个值+c。
而当我们做查询操作时,就只需要将区间[a, b]在从[1, N]开始查找到查找到时所经过的区间的标记往下传递就可以了(同时记得更新当前节点的值)

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R typedef long long LL;
const double eps = 1e-;
const int MAXN = ;
const int MAXM = ; struct Node { LL sum, add; } tree[MAXN<<];
int a, b, N, Q;
LL c; void updataChild(int k, int L, int R)//将编号为k的标记传到他的子节点去
{
tree[k].sum += (R-L+) * tree[k].add;//先更新它自己的sum值
tree[k<<].add += tree[k].add;//将左右子节点的add值更新
tree[k<<|].add += tree[k].add;
tree[k].add = ;//去掉标记
} void update(int k, int L, int R)//更新区间的值
{
if(R<a || b<L) return ;//不再区间内 if(a<=L && R<=b) { tree[k].add += c; return; }//实际上只更新这个区间的add值 int mid = (L+R)>>; update(lson); update(rson); //往左和往右更新 tree[k].sum = tree[k<<].sum + tree[(k<<)+].sum//更新当前节点的sum
+ (mid-L+)*tree[k<<].add + (R-mid)*tree[k<<|].add;
} LL query(int k, int L, int R)
{
if(R<a || b<L) return ; if(a<=L && R<=b) return tree[k].sum + (R-L+)*tree[k].add;//注意加上当前节点的标记 int mid = (L+R)>>; updataChild(k, L, R);//吧标记传到子节点 return query(lson) + query(rson);
} int main()
{
while(~scanf("%d %d", &N, &Q))
{
mem0(tree); char ch;
for(a=;a<=N;a++)
{
scanf("%lld%*c", &c); b = a;//区间是[a, a]
update(, , N);
}
for(int i=;i<Q;i++)
{
scanf("%c", &ch);
if(ch == 'Q')
{
scanf("%d %d%*c", &a, &b);
printf("%lld\n", query(,,N));
}
else
{
scanf("%d %d %lld%*c", &a, &b, &c);
update(,,N);
}
}
}
return ;
}
POJ3468 A Simple Problem with Integers(线段树延时标记)的更多相关文章
- 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(线段树模板 功能:区间增减,区间求和)
转载请注明出处: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 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- 基于Flume的美团日志收集系统(二)改进和优化
在<基于Flume的美团日志收集系统(一)架构和设计>中,我们详述了基于Flume的美团日志收集系统的架构设计,以及为什么做这样的设计.在本节中,我们将会讲述在实际部署和使用过程中遇到的问 ...
- BZOJ 1406 密码箱
直接两层枚举就行了. 避免排序可以用set. #include<iostream> #include<cstdio> #include<cstring> #incl ...
- IOS中导航控制器的代理及隐藏控制器刚出现时的滚动条
一.导航控制器的代理 1.UINavigationController的delegate属性 2.代理方法 1> 即将显示新控制器时调用 /* navigationController : 导航 ...
- [ ] 字符组(Character Classes) (转)
[]能够匹配所包含的一系列字符中的任意一个.需要注意的是,[]虽然能匹配其中的任意一个字符,但匹配的结果只能是一个字符,不是多个. 例如[abc]表示字符“a”或“b”或“c”. []支持用连字符“- ...
- 支持向量机之Hinge Loss 解释
Hinge Loss 解释 SVM 求解使通过建立二次规划原始问题,引入拉格朗日乘子法,然后转换成对偶的形式去求解,这是一种理论非常充实的解法.这里换一种角度来思考,在机器学习领域,一般的做法是经验风 ...
- oracle----sqlldr用法
SQL*LOADER是ORACLE的数据加载工具,通常用来将操作系统文件迁移到ORACLE数据库中.SQL*LOADER是大型数据仓库选择使用的加载方法,因为它提供了最快速的途径(DIRECT,PAR ...
- 三 最简单的 AndEngine 程序框架
package com.example.AndEngineExample; import org.anddev.andengine.engine.Engine;import org.anddev.an ...
- Python 获得命令行参数的方法
如果想对python脚本传参数,python中对应的argc, argv(c语言的命令行参数)是什么呢?需要模块:sys参数个数:len(sys.argv)脚本名: sys.argv[0]参数1 ...
- [Duilib] 交替背景色设置失败的原因
用列表显示一列数据时,相邻数据常用不同背景色来达到区别的作用.但是设置了Duilib相应属性之后交替背景色效果并未出现.逐一排除之后发现是item的enable属性设置为"false&quo ...
- js String方法集合
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String//返回对应索引的字符 ...