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个叶子节点,满足 ...
随机推荐
- SpringBoot之通过yaml绑定注入数据
依赖包: <!--配置文件注解提示包--> <dependency> <groupId>org.springframework.boot</groupId&g ...
- 了解C#中的HashSet与示例
在C#中引入HashSet 在.NET框架中,有几个类可用于执行这些操作.一些课程如下: 列表 字典 哈希集 队列 集合 在C#编程中,像ArrayList,List这样的集合,只需添加其中的值,而不 ...
- bzoj4152-[AMPPZ2014]The_Captain
Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用. Input 第一行包含一个正整数n(2 ...
- LAMP架构部署和动态网站环境的配置
实验环境: 操作系统:centos 7.5 服务器IP:192.168.10.5 运行用户:root 连接工具:xshell工具 web环境:Linux+apache+php+mariadb(LAMP ...
- Photoshop独立安装包下载页面
下载页面地址:https://helpx.adobe.com/cn/creative-cloud/kb/creative-cloud-apps-download.html
- ZOJ 1403 解密
参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6412212.htmlSafecracker Time Limit: 2 Seconds ...
- 大学jsp实验七--JavaBean在JSP中的应用
值bean的应用 (1)若有如下图所示的两个JSP页面,在第一个的页面的表单中填写相应内容,提交后再显示用户的提交信息.使用JavaBean的方式实现上述效果,请写出相应的代码. bean文件:Tes ...
- HDU1505 City Game(算竞进阶习题)
写了1506顺便写下1505.. 还是求矩形面积,不过要预处理一下以每一个F为底的高度,然后想左右扩展到最大长度即为矩形的长.. 计算方法有点绕,令l[i]表示i的左边界,那么初始化l[i] = i. ...
- atcoder NIKKEI Programming Contest 2019 E - Weights on Vertices and Edges
题目链接:Weights on Vertices and Edges 题目大意:有一个\(n\)个点\(m\)条边的无向图,点有点权,边有边权,问至少删去多少条边使得对于剩下的每一条边,它所在的联通块 ...
- 「POJ - 2318」TOYS (叉乘)
BUPT 2017 summer training (16) #2 A 题意 有一个玩具盒,被n个隔板分开成左到u右n+1个区域,然后给每个玩具的坐标,求每个区域有几个玩具. 题解 依次用叉积判断玩具 ...