Luogu P3521 [POI2011]ROT-Tree Rotations
题目链接 \(Click\) \(Here\)
线段树合并,没想到学起来意外的很简单,一般合并权值线段树。
建树方法和主席树一致,即动态开点。合并方法类似于\(FHQ\)的合并,就是把两棵树的信息整合到一个里面。暂时没写过定义域不同的线段树合并,具体方法也想象不出来,写到了再详细讲吧。
算法复杂度:均摊\(O(NlogN)\),实际空间时间复杂度都不够稳定,需要谨慎使用。
#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
#define ll long long
#define mid ((l + r) >> 1)
int n, pos;
ll ANS = 0, ans1 = 0, ans2 = 0;
struct node{
int sumn, ls, rs;
}t[N << 5];
int cnt = 0; //
void update (int &nown, int l, int r) {
if (nown == 0) nown = ++cnt;
t[nown].sumn++;
if (l != r) {
if (pos <= mid) {
update (t[nown].ls, l, mid);
} else {
update (t[nown].rs, mid + 1, r);
}
}
}
void merge (int &lx, int rx) {
if (lx * rx == 0) {
lx = lx + rx;
return;
}
t[lx].sumn += t[rx].sumn;
ans1 += 1LL * t[t[lx].rs].sumn * t[t[rx].ls].sumn;
ans2 += 1LL * t[t[lx].ls].sumn * t[t[rx].rs].sumn;
merge (t[lx].ls, t[rx].ls);
merge (t[lx].rs, t[rx].rs);
}
void solve (int &x) {
int t, ls, rs; x = 0;
cin >> t;
if(t == 0) {
solve (ls);
solve (rs);
ans1 = ans2 = 0;
merge (x = ls, rs);
ANS += min (ans1, ans2);
} else {
pos = t;
update (x, 1, n);
}
}
int main () {
cin >> n;
int t = 0;
solve (t);
cout << ANS << endl;
return 0;
}
Luogu P3521 [POI2011]ROT-Tree Rotations的更多相关文章
- P3521 [POI2011]ROT-Tree Rotations (线段树合并)
P3521 [POI2011]ROT-Tree Rotations 题意: 给你一颗树,只有叶子节点有权值,你可以交换一个点的左右子树,问你最小的逆序对数 题解: 线段树维护权值个个数即可 然后左右子 ...
- BZOJ2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 391 Solved: 127[Submi ...
- BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )
线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...
- loj2163 / bzoj2212 / P3521 [POI2011]ROT-Tree Rotations(线段树合并)
P3521 [POI2011]ROT-Tree Rotations loj2163 [POI2011]ROT-Tree Rotations(数据加强) (loj的数据套了个fread优化才过...) ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- 洛谷 P3521 [POI2011]ROT-Tree Rotations 解题报告
P3521 [POI2011]ROT-Tree Rotations 题意:递归给出给一棵\(n(1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 大体 ...
- 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并
[BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...
- POI2011 Tree Rotations
POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...
- [bzoj3702/2212][Poi2011]二叉树/Tree Rotations_线段树
二叉树 Tree Rotations bzoj-3702 bzoj-2212 Poi-2011 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节点,满足 ...
随机推荐
- vue-cli: 渲染过程理解(vue create demo01方式创建)
1.根目录配置 vue.config.js, 设置入口文件: index.js module.exports = { pages:{ index: { entry: 'src/pages/home/i ...
- Web API 2 使用Entity Framework Part 1.
创建项目 打开Visual Studio,选择ASP.NET Web Application,项目名称BookService并点击OK. 选择Web API 模板 如果你想使用Azure App Se ...
- cordova window error
npm install -g cordova --save ; ------------------------- error: deprecated node-uuid@1.4.7: use uui ...
- 【NOI2002】
A 银河英雄传说 带权并查集 B 贪吃的九头龙 树形dp f[x][j][0] = min(f[x][j][0], min(f[vv][t][0] + tmp[j - t][0] + ww * (m ...
- Linux 通过Shell 查找问题进程 [转]
背景介绍: 最近公司服务器不太稳定,总是在凌晨某个时段突发高负载情况,因为客观环境比较复杂,所以很难猜测出到底是哪个进程出现了问题,加之故障发生时,通常我在睡觉,等我被报警短信吵醒,通过公司 VPN ...
- module_loader.py
# few functions that make it possible to import functions # from jupyter notebooks as from modules; ...
- emwin 存在多个窗口时,如何获取当前所在窗口
@2019-02-20 [小记] emwin存在多个窗口时,如何获取当前所在窗口 > emwin 之获取当前窗口的一种方法 [需求] 用于在代码中获知当前呈现的是哪个窗口 [方法] 进入新窗口将 ...
- Crash 的文明世界
题目描述 给一棵树,求以每个点为根时下列式子的值. 题解 当k=1时这就是一个经典的换根dp问题. 所以这道题还是要用换根dp解决. 部分分做法: 考虑转移时是这样的一个形式(图是抄的). 用二项式定 ...
- centos7搭建ELK Cluster集群日志分析平台(一):Elasticsearch
应用场景: ELK实际上是三个工具的集合,ElasticSearch + Logstash + Kibana,这三个工具组合形成了一套实用.易用的监控架构, 很多公司利用它来搭建可视化的海量日志分析平 ...
- 20165223 week2学习查漏补缺
标识符.字符集.关键字 基本数据类型 逻辑类型:boolean 常量:true.false 变量:boolean赋值 整数类型:byte.short.long.int 注意long型后缀L Java没 ...