题目链接 \(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的更多相关文章

  1. P3521 [POI2011]ROT-Tree Rotations (线段树合并)

    P3521 [POI2011]ROT-Tree Rotations 题意: 给你一颗树,只有叶子节点有权值,你可以交换一个点的左右子树,问你最小的逆序对数 题解: 线段树维护权值个个数即可 然后左右子 ...

  2. BZOJ2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 391  Solved: 127[Submi ...

  3. BZOJ 2212: [Poi2011]Tree Rotations( 线段树 )

    线段树的合并..对于一个点x, 我们只需考虑是否需要交换左右儿子, 递归处理左右儿子. #include<bits/stdc++.h> using namespace std; #defi ...

  4. loj2163 / bzoj2212 / P3521 [POI2011]ROT-Tree Rotations(线段树合并)

    P3521 [POI2011]ROT-Tree Rotations loj2163 [POI2011]ROT-Tree Rotations(数据加强) (loj的数据套了个fread优化才过...) ...

  5. 2212: [Poi2011]Tree Rotations

    2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...

  6. 洛谷 P3521 [POI2011]ROT-Tree Rotations 解题报告

    P3521 [POI2011]ROT-Tree Rotations 题意:递归给出给一棵\(n(1≤n≤200000)\)个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少. 大体 ...

  7. 【BZOJ2212】[Poi2011]Tree Rotations 线段树合并

    [BZOJ2212][Poi2011]Tree Rotations Description Byteasar the gardener is growing a rare tree called Ro ...

  8. POI2011 Tree Rotations

    POI2011 Tree Rotations 给定一个n<=2e5个叶子的二叉树,可以交换每个点的左右子树.要求前序遍历叶子的逆序对最少. 由于对于当前结点x,交换左右子树,对于范围之外的逆序对 ...

  9. [bzoj3702/2212][Poi2011]二叉树/Tree Rotations_线段树

    二叉树 Tree Rotations bzoj-3702 bzoj-2212 Poi-2011 题目大意:现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有n个叶子节点,满足 ...

随机推荐

  1. 4.namespace

    命名空间( namespace)是 Linux 内核的一个强大特性,为容器虚拟化的实现带来极大便 利. 利用这一特性,每个容器都可以拥有自己单独的命名空间,运行在其中的应用都像是在 独立的操作系统环境 ...

  2. redis两种持久化

    Redis 提供了不同级别的持久化方式: RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储. AOF持久化方式记录每次对服务器写的操作,当服务器重启的时候会重新执行这些命令来恢复原始的数据 ...

  3. Ftp、Ftps与Sftp之间的区别

    Ftp FTP 是File Transfer Protocol(文件传输协议)的英文简称,而中文简称为“文传协议”.用于Internet上的控制文件的双向传输.同时,它也是一个应用程序(Applica ...

  4. ASP.NET Core Building chat room using WebSocket

    Creating “Login form” We use here simple form where user can insert his or her preferred nick name f ...

  5. 动态sql and在前 逗号在后

  6. hdu-3294(最长回文子串)

    题意:给你一个字符和一个字符串让你求出最长回文子串并且输出来,答案需要根据给出的字符转换一下,就是将给出的字符认定为a,然后依次向后推: 解题思路:manacher模板+一些处理 代码: #inclu ...

  7. 【C/C++】c文件重点总结

    c文件重点知识总结 程序文件数据文件--->分文本文件(ASCII文件)和映像文件(二进制文件) .区分是用记事本打开后能否看懂. 用二进制文件读写花费时间少,因为用文本文件需要有一个转换的过程 ...

  8. POJ 2299 -Ultra-QuickSort-树状数组求逆序数

    POJ 2299Ultra-QuickSort 使用树状数组记录逆序对数. 把数组按照大小顺序插入,getsum(i)就是i前面的比他大的数. #include <cstdio> #inc ...

  9. codeforces 1051F The Shortest Statement

    题目链接:codeforces 1051F The Shortest Statement 题意:\(q\)组询问,求任意两点之间的最短路,图满足\(m-n\leq 20\) 分析:一开始看这道题:fl ...

  10. MT【303】估计

    (2016浙江填空压轴题)已知实数$a,b,c$则 (     )A.若$|a^2+b+c|+|a+b^2+c|\le1,$则$a^2+b^2+c^2<100$B.若$|a^2+b+c|+|a+ ...