BZOJ原题链接

洛谷原题链接

线段树合并裸题。

因为交换子树只会对子树内部的逆序对产生影响,所以我们计算交换前的逆序对个数和交换后的个数,取\(\min\)即可。

对每个叶子节点建一棵动态开点线段树,然后向上合并并更新答案。

而逆序对可以在线段树合并的过程中算出来,因为是权值线段树,根据\(mid\)计算贡献即可。

这题洛谷和\(BZOJ\)不卡常,而\(LOJ\)丧心病狂地把每个点开到\(160ms\),我这份代码需要改成超级快读才能卡过。。

#include<cstdio>
using namespace std;
typedef long long ll;
const int N = 8e6 + 10;
int L[N], R[N], S[N], BT, SEG, n;
ll s, s_1, s_2;
inline int re()
{
int x = 0;
char c = getchar();
bool p = 0;
for (; c < '0' || c > '9'; c = getchar())
p |= c == '-';
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
return p ? -x : x;
}
inline ll minn(ll x, ll y){ return x < y ? x : y; }
void bu(int &r, int x, int y, int k)
{
r = ++SEG;
S[r] = 1;
if (!(x ^ y))
return;
int mid = (x + y) >> 1;
k <= mid ? bu(L[r], x, mid, k) : bu(R[r], mid + 1, y, k);
}
int merge(int x, int y)
{
if (!x)
return y;
if (!y || !(x ^ y))
return x;
s_1 += 1LL * S[R[x]] * S[L[y]];
s_2 += 1LL * S[L[x]] * S[R[y]];
L[x] = merge(L[x], L[y]);
R[x] = merge(R[x], R[y]);
S[x] = S[L[x]] + S[R[x]];
return x;
}
int dfs()
{
int r, x, y, ro = 0;
r = re();
if (r)
{
bu(ro, 1, n, r);
return ro;
}
x = dfs();
y = dfs();
s_1 = s_2 = 0;
ro = merge(x, y);
s += minn(s_1, s_2);
return ro;
}
int main()
{
n = re();
dfs();
printf("%lld", s);
return 0;
}

BZOJ2212或洛谷3521 [POI2011]ROT-Tree Rotations的更多相关文章

  1. 洛谷P3513 [POI2011]KON-Conspiracy

    洛谷P3513 [POI2011]KON-Conspiracy 题目描述 Byteotia的领土被占领了,国王Byteasar正在打算组织秘密抵抗运动. 国王需要选一些人来进行这场运动,而这些人被分为 ...

  2. [洛谷P3527] [POI2011]MET-Meteors

    洛谷题目链接:[POI2011]MET-Meteors 题意翻译 Byteotian Interstellar Union有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1 ...

  3. 洛谷P1501 [国家集训队]Tree II(LCT,Splay)

    洛谷题目传送门 关于LCT的其它问题可以参考一下我的LCT总结 一道LCT很好的练习放懒标记技巧的题目. 一开始看到又做加法又做乘法的时候我是有点mengbi的. 然后我想起了模板线段树2...... ...

  4. 洛谷P3515 [POI2011]Lightning Conductor(动态规划,决策单调性,单调队列)

    洛谷题目传送门 疯狂%%%几个月前就秒了此题的Tyher巨佬 借着这题总结一下决策单调性优化DP吧.蒟蒻觉得用数形结合的思想能够轻松地理解它. 首先,题目要我们求所有的\(p_i\),那么把式子变一下 ...

  5. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

  6. 洛谷P3521 [POI2011]ROT-Tree Rotation [线段树合并]

    题目传送门 Tree Rotation 题目描述 Byteasar the gardener is growing a rare tree called Rotatus Informatikus. I ...

  7. 洛谷 P1501 [国家集训队]Tree II 解题报告

    P1501 [国家集训队]Tree II 题目描述 一棵\(n\)个点的树,每个点的初始权值为\(1\).对于这棵树有\(q\)个操作,每个操作为以下四种操作之一: + u v c:将\(u\)到\( ...

  8. 洛谷 P3527 [POI2011]MET-Meteors 解题报告

    P3527 [POI2011]MET-Meteors 题意翻译 \(\tt{Byteotian \ Interstellar \ Union}\)有\(N\)个成员国.现在它发现了一颗新的星球,这颗星 ...

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

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

随机推荐

  1. VUE中如何优雅的动态绑定长按事件

    答案没有: 图片是从后端传过来, 加到imgTarget属性,实现长按点击删除该图片 let img = document.createElement('img'); img.src = " ...

  2. Python递归解压缩多级.zip压缩包

    参考如下代码(from:https://stackoverflow.com/questions/36285502/how-to-extract-zip-file-recursively-in-pyth ...

  3. DHCP的搭建

    挂载光盘 yum –y install dhcp cat /etc/dhcp/dhcpd.conf 配置文件到 /usr/share/doc/dhcp*/dhcpd.conf.sample 这是dhc ...

  4. 一键精简Windows不常用的字体.cmd

    一键精简Windows不常用的字体.cmd @Echo offset path=%path%;%~dp0setlocal EnableDelayedExpansionset /P dv=请输入要精简W ...

  5. oracle user_tables没有新创建的表的问题

    oracle 新创建表后,在user_tables没有,在user_tab_columns也没有,暂时未找到办法

  6. 算法实践--最小生成树(Kruskal算法)

    什么是最小生成树(Minimum Spanning Tree) 每两个端点之间的边都有一个权重值,最小生成树是这些边的一个子集.这些边可以将所有端点连到一起,且总的权重最小 下图所示的例子,最小生成树 ...

  7. springmvc的简单使用以及ssm框架的整合

    Spring web mvc是基于servlet的一个表现层框架 首先创建一个简单的web工程了解它的使用 web.xml的配置 <?xml version="1.0" en ...

  8. delete content on the right of cursor, Mac

    delete content on the right of cursor, Mac It's not convenient to press Fn+delete to delete content ...

  9. HBuilder ,及自用主题

    字体:Consolas http://bbs.csdn.net/topics/390858585  让代码更美:你最爱的编程字体 http://www.dcloud.io HBuilder下载 htt ...

  10. 63.(原65)纯 CSS 创作一个摇摇晃晃的 loader

    原文地址:https://segmentfault.com/a/1190000015424389 修改后地址:https://scrimba.com/c/cqKv4VCR HTML code: < ...