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个叶子节点,满足 ...
随机推荐
- (转载)C#使用MemoryStream类读写内存
MemoryStream和BufferedStream都派生自基类Stream,因此它们有很多共同的属性和方法,但是每一个类都有自己独特的用法.这两个类都是实现对内存进行数据读写的功能,而不是对持久性 ...
- Lodop打印项对象类型属性区分
Lodop提供了一些打印项类型,默认是普通项,通过设置打印对象的类型,可以实现一些普通项不能实现的效果.例如:该博客另一篇博文 标题是Lodop打印控件 打印‘接下一页’‘以下空白’,就用了眉脚项. ...
- [oracle] to_date() 与 to_char() 日期和字符串转换
to_date("要转换的字符串","转换的格式") 两个参数的格式必须匹配,否则会报错. 即按照第二个参数的格式解释第一个参数. to_char(日期,& ...
- How to flash Havoc on enchilada
update fastboot and adb fastboot oem unlock adb debug enchilada reboot to fastboot fastboot devices ...
- Process 模块的方法
join from multiprocessing import Process import time, os def task(name): print('%s is running' % nam ...
- SQL 给视图赋权限
授予表权限 创建视图 授予视图权限 测试权限 复杂程度: 初级 数据要求: 使用自备的数据 您可以使用 SQL 在企业级地理数据库中创建表和要素类的视图. 本主题中的示例显示如何使用 Microsof ...
- PC平台的SIMD支持检测
如果我们希望在用SIMD来提升程序处理的性能,首先需要做的就是检测程序所运行的平台是否支持相应的SIMD扩展.平台对SIMD扩展分为两部分的支持: CPU对SIMD扩展的支持.SIMD扩展是随着CPU ...
- nginx POSTREAD阶段模块
L:50 Realip模块 需要将--with-http_realip_model 编译进Nginx 因为nginx有可能有反向代理 获取到的客户端ip就不是原用户IP了 X-Forwarded-Fo ...
- DRF 版本 认证
DRF的版本 版本控制是做什么用的, 我们为什么要用 首先我们要知道我们的版本是干嘛用的呢大家都知道我们开发项目是有多个版本的 当我们项目越来越更新~版本就越来越多我们不可能新的版本出了~以前旧的版本 ...
- java基础1之基本数据类型
java的数据类型 整数型(byte.short.int.long) 编程过程中,默认是int类型.long类型的字面值后面需要加上L或l PS:java底层,byte.short是按照32位计算的. ...