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个叶子节点,满足 ...
随机推荐
- WebStorm开发React项目,修代码之后运行的项目不更新
昨天遇到一个恶心的问题,我新建了一个React.js项目,想做一点关于Pc端的开发,习惯性的用了WebStorm,可是发现一个问题,就是代码修改之后,浏览器上根本不会刷新.然后,我把方向定位在没有正确 ...
- codeforces263B
Squares CodeForces - 263B Vasya has found a piece of paper with a coordinate system written on it. T ...
- 使用poi将Excel文件转换为data数据
pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:// ...
- Go语言变量和常量
一.变量相关 1.变量声明 C# : int a; Go : var a int; 需要在前面加一个var关键字,后面定义类型 可以使用 var( a int; b string;)减少var 2.变 ...
- Luogu5289 十二省联考2019字符串问题(后缀数组+拓扑排序+线段树/主席树/KDTree)
先考虑80分做法,即满足A串长度均不小于B串,容易发现每个B串对应的所有A串在后缀数组上都是一段连续区间,线段树优化连边然后判环求最长链即可.场上就写了这个. 100分也没有什么本质区别,没有A串长度 ...
- jwt实现
<?phpnamespace app\admin\controller;use think\Config;use think\Controller;use think\Request;use t ...
- Hdoj 2191.悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 题解
Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你准备自己采购一些粮食支援灾区,现在假设你一共有资金n元,而市场有m种大米,每种大米都是袋装产品 ...
- 【BZOJ2817】[ZJOI2012]波浪(动态规划)
[BZOJ2817][ZJOI2012]波浪(动态规划) 题面 BZOJ 洛谷 题解 首先这个差值最大也就是\(n^2\)级别的. 那么这样子就可以压进状态啦. 我们把这个操作看成一个个加数的操作,按 ...
- [ZJOI2019]游记之我的第一次省选--自闭记
2019/3/23 day -1 今天是体育中考....(祝我好运) 实心球再次投出测量范围,虽然成绩是10.5,但是目测有15米. 立定跳远2.70,好近,我爸叫我跳2.8的QwQ. 1000米最后 ...
- JS基本类型-引用类型-深浅拷贝
在JavaScript中变量包含两种类型的值:一种是基本类型,一种是引用类型. 基本类型包括:数值.字符串.null.undefined.布尔值引用类型包括:对象.数组.函数.正则… 补充: null ...