51NOD 1287 加农炮(不水的线段树)

Input示例 Output示例
思路:刚开始以为结点存最大值就行了,然后大于左子树的最大值就能进入右子树;然后发现样例都过不了;后面发现,并不是这个样子,假如这个数小于等于右孩子最左边那个数的话,也不能进入有孩子,所以结点还得保存右孩子最左边的那个值;同时更新一个最大值,当输入值咸鱼等于a[0]或者大于最大值时跳过。
#include<climits>
#include<iostream>
#include<algorithm>
using namespace std; #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
const int maxn = 5e4 + ;
ll tree[maxn << ], mtree[maxn << ], a[maxn];
ll ma = INT_MIN; void build(int l, int r, int rt)
{
if (l == r){
tree[rt] = a[l];
mtree[rt] = a[l];
return;
}
int m = (l + r) >> ;
build(lson);
build(rson);
tree[rt] = max(tree[rt << ], tree[rt << | ]);
mtree[rt] = mtree[rt << ];
}
void update(int x, int l, int r, int rt)
{
if (l == r){
a[l] += ;
tree[rt] += ;
mtree[rt] += ;
if (a[l] > ma)ma = a[l];
return;
}
int m = (l + r) >> ;
if (tree[rt << ] < x&&x>mtree[rt << | ]) update(x, rson);
else update(x, lson);
tree[rt] = max(tree[rt << ], tree[rt << | ]);
mtree[rt] = mtree[rt << ];
}
void Print(int l, int r, int rt)
{
if (l == r){
cout << rt << " = " << tree[rt] << endl;
return;
}
cout << rt << " = " << tree[rt] << endl;
int m = (r + l) >> ;
if (l <= m)Print(lson);
if (r > m)Print(rson);
}
int main()
{
std::ios::sync_with_stdio(false);
int m, n; cin >> m >> n; for (int i = ; i <= m; i++){
cin >> a[i];
if (ma < a[i])ma = a[i];
}
build(, m, ); int temp;
while (n--){
cin >> temp;
if (temp <= a[] || temp>ma)
continue;
update(temp, , m, );
//Print(1, m, 1);
}
for (int i = ; i <= m; i++){
if (i != )cout << endl;
cout << a[i];
} }
51NOD 1287 加农炮(不水的线段树)的更多相关文章
- [51nod 1681]公共祖先(dfs序+线段树合并)
[51nod 1681]公共祖先(dfs序+线段树合并) 题面 给出两棵n(n<=100000)个点的树,对于所有点对求它们在两棵树中公共的公共祖先数量之和. 如图,对于点对(2,4),它们在第 ...
- 51Nod 1287 加农炮 (线段树)
1287 加农炮 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 一个长度为M的正整数数组A,表示从左向右的地形高度 ...
- 51nod 1287加农炮
1287 加农炮 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个长度为M的正整数数组A,表示从左向右的地形高度.测试一种加农炮 ...
- 51nod 1287: 加农炮 好题啊好题
1287 加农炮 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 一个长度为M的正整数数组A,表示从左向右的地形高度. ...
- 分块试水--CODEVS5037 线段树练习4加强版
感觉这才算入门题吧..前面那些线段树练习,改几个字符就过了一定要搞成几道题.. n<=2e5的数列,给常数K<=2e5,m<=2e5个操作,区间加,问一个区间里K的倍数. 这题空间? ...
- 分块试水--CODEVS4927 线段树练习5
模板 #include<stdio.h> #include<algorithm> #include<string.h> #include<stdlib.h&g ...
- 【wikioi】1191 数轴染色(线段树+水题)
http://wikioi.com/problem/1191/ 太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉. #include <cstdio> #inc ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- 【线段树】Bzoj1230 [Usaco2008 Nov]lites 开关灯
Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每 ...
随机推荐
- 洛谷 P2312 & bzoj 3751 解方程 —— 取模
题目:https://www.luogu.org/problemnew/show/P2312 https://www.lydsy.com/JudgeOnline/problem.php?id=3751 ...
- ODB——基于c++的ORM映射框架尝试(使用)
摘要: 2.使用 首先,需要定义一个对象,用来和数据库字段对应: [cce lang=”cpp”] #ifndef VOLUME_H #define VOLUME_H #include #includ ...
- 计算属性 computed
计算属性 computed 计算缓存 vs Methods <div id="example"> <p>Original message: "{{ ...
- EasyUI Form表单提交
转自:https://www.cnblogs.com/net5x/articles/4576926.html Form(表单) 使用$.fn.form.defaults重写默认值对象 form提供了各 ...
- 【146】ArcObjects类库索引
ArcObjects 类库(一) ----------------------------------------------------------------------------------- ...
- 【转载】7条便利的ViewState技巧
32.Seven handy ViewState tips 32.7条便利的ViewState技巧 Every time I have to deal with a classic ASP.NET W ...
- SQL 存储过程语句拼接愁人的时间类型
http://www.cnblogs.com/yrup/p/4201329.html 解决方法: Select CONVERT(varchar(100), GETDATE(), 25): 2006-0 ...
- Classic BADI总结
这里对sap Classic Badi 做一下总结,虽然已经是过时的技术了. Classic BADI的创建 Classic BADI的实施 Classic BADI的调用及运行原理 New BADI ...
- c语言小项目-使用mysql数据库的图书管理系统
VS2013通过MySQL方式连接到MySQL MySQL官网上C++的API有两个.一个是很成熟的mysql++,另一个是MySQL Connector/C++,近两年才出的,模仿JDBC做的,封装 ...
- 百度地图API显示多个标注点带提示的代码 / 单个标注点带提示代码
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...