》》点击进入原题测试《《

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 加农炮(不水的线段树)的更多相关文章

  1. [51nod 1681]公共祖先(dfs序+线段树合并)

    [51nod 1681]公共祖先(dfs序+线段树合并) 题面 给出两棵n(n<=100000)个点的树,对于所有点对求它们在两棵树中公共的公共祖先数量之和. 如图,对于点对(2,4),它们在第 ...

  2. 51Nod 1287 加农炮 (线段树)

    1287 加农炮  题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 一个长度为M的正整数数组A,表示从左向右的地形高度 ...

  3. 51nod 1287加农炮

    1287 加农炮  题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个长度为M的正整数数组A,表示从左向右的地形高度.测试一种加农炮 ...

  4. 51nod 1287: 加农炮 好题啊好题

    1287 加农炮 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 一个长度为M的正整数数组A,表示从左向右的地形高度. ...

  5. 分块试水--CODEVS5037 线段树练习4加强版

    感觉这才算入门题吧..前面那些线段树练习,改几个字符就过了一定要搞成几道题.. n<=2e5的数列,给常数K<=2e5,m<=2e5个操作,区间加,问一个区间里K的倍数. 这题空间? ...

  6. 分块试水--CODEVS4927 线段树练习5

    模板 #include<stdio.h> #include<algorithm> #include<string.h> #include<stdlib.h&g ...

  7. 【wikioi】1191 数轴染色(线段树+水题)

    http://wikioi.com/problem/1191/ 太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉. #include <cstdio> #inc ...

  8. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  9. 【线段树】Bzoj1230 [Usaco2008 Nov]lites 开关灯

    Description Farmer John尝试通过和奶牛们玩益智玩具来保持他的奶牛们思维敏捷. 其中一个大型玩具是牛栏中的灯. N (2 <= N <= 100,000) 头奶牛中的每 ...

随机推荐

  1. URAL1553 Caves and Tunnels 树链剖分 动态树

    URAL1553 维护一棵树,随时修改某个节点的权值,询问(x,y)路径上权值最大的点. 树是静态的,不过套动态树也能过,时限卡的严就得上树链剖分了. 还是那句话 splay的核心是splay(x) ...

  2. openstack dnsmasq调试

  3. Eclipse 配置 Python 环境

    1.将下载好的Pydev4.5.2(http://sourceforge.net/projects/pydev/files/pydev/  里面有很多版本) 文件夹里的两个文件夹(features+p ...

  4. Java实现二叉树遍历

    参考资料: http://blog.csdn.net/wuwenxiang91322/article/details/12231657 环境: Java: jdk1.8.0_91 import jav ...

  5. 10.17NOIP模拟赛

    #include<iostream> #include<cstdio> #include<cstring> #define N 1001 using namespa ...

  6. [App Store Connect帮助]六、测试 Beta 版本(4.4) 管理 Beta 版构建版本:停止测试构建版本

    在首页上,点按“我的 App”,选择您的 App,然后在工具栏中点按“TestFlight”. 在左列中的“构建版本”下,点按您 App 的平台(iOS 或 Apple TVOS). 在右表中,点按该 ...

  7. 使用Oracle的DBMS_SQL包执行动态SQL语句

    引用自:http://blog.csdn.net/ggjjzhzz/archive/2005/10/17/507880.aspx 在某些场合下,存储过程或触发器里的SQL语句需要动态生成.Oracle ...

  8. Sql2008事务日志已满处理

    处理方式: USE [master] GO ALTER DATABASE gzl SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE gzl SET ...

  9. arp学习笔记(linux高性能服务编程)

    先看看arp的定义吧 现在linux运行这条命令 tcpdump -i eth0:1 -ent '(dst 192.168.5.190 and src 192.168.5.109)or( dst 19 ...

  10. sp_Msforeachtable与sp_Msforeachdb详解

      一.简要介绍: 系统存储过程sp_MSforeachtable和sp_MSforeachdb,是微软提供的两个不公开的存储过程.从mssql6.5开始,存放在SQL Server的MASTER数据 ...