HackerRank "Array and simple queries" !
The most interesting, flexible and juicy binary tree problem I have ever seen.
I learnt it from here: https://codepair.hackerrank.com/paper/5fIoGg74?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6IkJsdWVCaXJkMjI0IiwiZW1haWwiOiJoZWFsdGh5dG9ueUBnbWFpbC5jb20ifQ%3D%3D
Everything is so generalized.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; struct node {
long value, size;
int prev;
node *left, *right;
}; const long MAX_N = 100000l;
node nodes[MAX_N];
long values[MAX_N]; // Get Tree Size
long size(node *p)
{
return p ? p->size : ;
} // Tree Operations
//
node *merge(node *l, node *r)
{
if (!l) return r;
if (!r) return l; if (l->prev > r->prev)
{
l->right = merge(l->right, r);
l->size = size(l->left) + size(l->right) + ;
return l;
} // l is on left of r: append l to r's left
r->left = merge(l, r->left);
r->size = size(r->left) + size(r->right) + ;
return r;
} // split tree p by inx, to l and r
void split(node *p, long inx, node *&l, node *&r)
{
if (!p)
{
l = r = nullptr;
return;
} long cur = size(p->left) + ;
if (cur <= inx)
{
split(p->right, inx - cur, p->right, r);
l = p;
}
else
{
split(p->left, inx, l, p->left);
r = p;
} p->size = size(p->left) + size(p->right) + ;
} //
node *extract(node *&p, long from, long to)
{
node *l, *m, *r; split(p, from, l, m);
split(m, to - from, m, r);
p = merge(l, r);
return m;
} // Traversal
long preorder(node *p, long i/*from inx*/) // return new start inx
{
if (!p) return i; i = preorder(p->left, i);
values[i++] = p->value;
return preorder(p->right, i);
} int main()
{
for (long i = ; i != MAX_N; ++i) {
nodes[i].prev = rand();
nodes[i].size = ;
} long n, m;
cin >> n >> m; node *tree = nullptr;
for (long i = ; i != n; ++i) {
cin >> nodes[i].value;
tree = merge(tree, nodes + i);
} for (long i = ; i != m; ++i) {
long l, r;
int type;
cin >> type >> l >> r; node *sub = extract(tree, l - , r);
if (type == )
tree = merge(sub, tree);
else
tree = merge(tree, sub);
} // Serialize tree
preorder(tree, );
cout << abs(values[] - values[n-]) << endl;
for (long i = ; i < n; ++i)
cout << values[i] << " ";
cout << endl; return ;
}
HackerRank "Array and simple queries" !的更多相关文章
- [Codeforces 863D]Yet Another Array Queries Problem
Description You are given an array a of size n, and q queries to it. There are queries of two types: ...
- Yet Another Array Queries Problem CodeForces - 863D (暴力/思维)
You are given an array a of size n, and q queries to it. There are queries of two types: 1 li ri — p ...
- Elastic Search 5.x Nest Multiple Queries C#
I'm using C# with those nuget packeges; <package id="Elasticsearch.Net" version="5 ...
- 转://Oracle PL/SQL 优化与调整 -- Bulk 说明
一. Bulk 概述 本来只想测试一下Bulk Collect 和update性能的,但发现Bulk 的东西还是很多的,在OTN上搜了一些,整理如下. 1.1 Bulk Binding 和 Bulk ...
- Sphinx 2.2.11-release reference manual
1. Introduction 1.1. About 1.2. Sphinx features 1.3. Where to get Sphinx 1.4. License 1.5. Credits 1 ...
- 零售行业下MongoDB在产品目录系统、库存系统、个性推荐系统中的应用【转载】
Retail Reference Architecture Part 1: Building a Flexible, Searchable, Low-Latency Product Catalog P ...
- Docker 搭建 ELK 集群步骤
前言 本篇文章主要介绍在两台机器上使用 Docker 搭建 ELK. 正文 环境 CentOS 7.7 系统 Docker version 19.03.8 docker-compose version ...
- Objective-C Memory Management
Objective-C Memory Management Using Reference Counting 每一个从NSObject派生的对象都继承了对应的内存管理的行为.这些类的内部存在一个称为r ...
- 前端自动化测试工具doh学习总结(一)
前言 项目中需要用到前端自动化测试,自己被当作一个探针研究了下目前用的比较多的web自动化测试工具.一开始研究的是的selenium,但由于项目使用了大量的dijit控件,写起testCase来很费劲 ...
随机推荐
- 《JS高程》基本类型和引用类型的值学习笔记
ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值. 创建方式类似:创建一个变量并为其赋值. (1)基本类型值和引用类型值比较 基本类型值 引用类型值 简单的数据段 可能由 ...
- SpringCloud学习后获取的地址
关于SpringCloud + Docker 学习地址: (1) https://yq.aliyun.com/articles/57265 (2) https://yq.aliyun.com/team ...
- Activity Lifecycle
Activity Lifecycle Activities in the system are managed as an activity stack(activity栈?). When a new ...
- Eclipse 手机开发不能运行
ADB server didn't ACK问题 The connection to adb is down, and a severe error has occured. 1.先把eclipse关闭 ...
- Java 抓取网页内容
前两天想写一段自动提取微博状态的代码.据我所知,实现这个功能即可以用PHP写,也可以用Java写.我认为用Java写调试方便一点,PHP的脚本还要上传到服务器什么的. 代码很简单的,新建一个java. ...
- 《灰帽Python-黑客和逆向工程师的Python编程》学习记录
ctypes是Python语言的一个外部库,提供和C语言兼容的数据类型,可以很方便的调用C DLL中的函数. 操作环境:CentOS6.5 Python版本:2.66 ctypes是强大的,强大到本书 ...
- [POI 2008][BZOJ 1132]Tro
这题我真是无能为力了 这题的做法还是挺简单的 枚举左下角的点做为原点,把其余点按极角排序 PS.是作为原点,如枚举到 k 时,对于所有 p[i] (包括p[k]) p[i]-=p[k] (此处为 ...
- javascrip中的confirm小技巧
jsp页面中的一个标签: <a href="javascript:void(0)" onclick="confirmDelete('<%=request.ge ...
- python3基础语法
一.编码 默认情况下, python3源码文件以UTF-8编码,所有字符串都是unicode字符串.当然你也可以为源码文件指定不同的编码: # -*- coding: gbk -*- 二.标识符 1. ...
- JS页面间传值
一:JavaScript静态页面值传递之URL篇 能过URL进行传值.把要传递的信息接在URL上. 例子: 参数传出页面Post.htm—> <input type="tex ...