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来很费劲 ...
随机推荐
- CentOS6编译装载nbd模块
今天突然发现CentOS系统没有nbd模块,只能重新装下,下面记录下整个编译过程: 系统:CentOS6.5 内核:2.6.32-431.el6.x86_64 [root@localhost ~]# ...
- Customize R initiallization in Mac
First, we need to find a "Rprofile" document. locate Rprofile Then we find Rprofile docume ...
- Python编程小记
发现这种结构很实用: while True: expression .... if condition: expression break 好吧,我承认我是菜鸟-
- 解决SimpleCursorAdapter不能自动更新的问题
假设场景是这样的:你使用SimpleCursorAdapter显示数据,并监听数据的变化:在数据发生变化的时候,调用cursor的requery,期待UI显示也跟着变化. 但是,你可能会发现,UI并没 ...
- 98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- safari穿越到chrome
tell application "Safari" set theURL to URL of front document set the clipboard to theURL ...
- Codeforces Round #116 (Div. 2, ACM-ICPC Rules)
Codeforces Round #116 (Div. 2, ACM-ICPC Rules) 代码 Codeforces Round #116 (Div. 2, ACM-ICPC Rules) A. ...
- SVN代码提交冲突解决方案
SVN代码提交冲突解决方案 1.若你的代码被其他人修改并提交过了,期间你自己也修改过该文件,UPDATE的时候自己代码被覆盖. 右键——>显示日志 查看该文件的更新记录 找到需恢复的版本 右键— ...
- codeForce-19D Points (点更新+离散化)
题目大意:在二维坐标系的x正半轴,y正半轴和第一象限内,有三种操作: 1.add x,y (添加点<x,y>): 2.remove x,y(移除点<x,y>): 3.find ...
- hdu1811 并查集+拓扑序
题意:现在有一个排名系统,有一系列信息,分别是 > < = 的比较,而如果最终相等,就会将这些相等的按照序号从小到大排,问给出的信息是否可以确定完整的排序. 由于如果很多点相等,他们肯定能 ...