Luogu 3521 [POI2011]ROT-Tree Rotations
BZOJ 2212
从下到上线段树合并。
考虑到每一个子树内部产生的贡献不可能通过换儿子消除,所以一次更换只要看看把哪个儿子放在左边产生的逆序对数少就可以了。
逆序对数可以在线段树合并的时候顺便算出来。
由于只有叶子结点有权值 + 二叉树的特性,大大方便了这道题的代码和细节处理。
注意点数总共要开到$2 * n$。
时间复杂度$O(nlogn)$。
Code:
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll; const int N = 4e5 + ; int m, n = , rt = ;
ll ans = 0LL; struct Node {
int lc, rc, w;
} a[N]; inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline ll min(ll x, ll y) {
return x > y ? y : x;
} void build(int &now) {
now = ++n;
read(a[now].w);
if(a[now].w) return;
build(a[now].lc), build(a[now].rc);
} namespace SegT {
struct Node {
int lc, rc;
ll siz;
} s[N * ]; int sta[N * ], top = , root[N], nodeCnt = ;
ll res1, res2; inline void push(int now) {
sta[++top] = now;
} inline int newNode() {
if(top) return sta[top--];
else return ++nodeCnt;
} #define lc(p) s[p].lc
#define rc(p) s[p].rc
#define siz(p) s[p].siz
#define mid ((l + r) >> 1) inline void up(int p) {
if(!p) return;
siz(p) = siz(lc(p)) + siz(rc(p));
} void ins(int &p, int l, int r, int x) {
if(!p) p = newNode();
++siz(p);
if(l == r) return; if(x <= mid) ins(lc(p), l, mid, x);
else ins(rc(p), mid + , r, x);
} int merge(int u, int v, int l, int r) {
if(!u || !v) return u + v; res1 += siz(rc(u)) * siz(lc(v));
res2 += siz(rc(v)) * siz(lc(u)); int p = newNode();
if(l == r) siz(p) = siz(u) + siz(v);
else {
lc(p) = merge(lc(u), lc(v), l, mid);
rc(p) = merge(rc(u), rc(v), mid + , r);
up(p);
}
push(u), push(v);
return p;
} void print(int p, int l, int r) {
if(l == r) {
printf("%lld", siz(p));
return;
} print(lc(p), l, mid), print(rc(p), mid + , r);
} inline void deb(int x) {
print(root[x], , m);
} #undef lc
#undef rc
#undef mid
#undef siz } using namespace SegT; void solve(int now) {
if(a[now].w) return;
solve(a[now].lc), solve(a[now].rc); res1 = res2 = 0LL;
root[now] = merge(root[a[now].lc], root[a[now].rc], , m);
ans += min(res1, res2);
} int main() {
read(m);
build(rt); /* for(int i = 1; i <= n; i++)
printf("%d %d %d\n", a[i].lc, a[i].rc, a[i].w);
printf("\n"); */ for(int i = ; i <= n; i++)
if(a[i].w) ins(root[i], , m, a[i].w); /* for(int i = 1; i <= n; i++) {
if(!a[i].w) continue;
deb(i);
printf("\n");
} */ solve(rt); printf("%lld\n", ans);
return ;
}
Luogu 3521 [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 ...
- 2212: [Poi2011]Tree Rotations
2212: [Poi2011]Tree Rotations https://www.lydsy.com/JudgeOnline/problem.php?id=2212 分析: 线段树合并. 首先对每个 ...
- 【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个叶子节点,满足 ...
- bzoj 2212 Tree Rotations
bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...
- Luogu 3690 Link Cut Tree
Luogu 3690 Link Cut Tree \(LCT\) 模板题.可以参考讲解和这份码风(个人认为)良好的代码. 注意用 \(set\) 来维护实际图中两点是否有直接连边,否则无脑 \(Lin ...
- 线段树合并(【POI2011】ROT-Tree Rotations)
线段树合并([POI2011]ROT-Tree Rotations) 题意 现在有一棵二叉树,所有非叶子节点都有两个孩子.在每个叶子节点上有一个权值(有nn个叶子节点,满足这些权值为1-n1-n的一个 ...
随机推荐
- const指针和指向const的指针
int *const p=&a; 这是const指针,这种指针必须在定义时就给出它所指向的地址,否则会error:uninitialized const 'p'.const指针的指针本身是co ...
- Redis底层探秘(二):链表和跳跃表
链表简介 链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活地跳转链表的长度. 作为一种常用数据结构,链表内置在很多高级的编程语言里面,因为Redis使用C语言并没有内 ...
- LeetCode Maximum Length of Pair Chain
原题链接在这里:https://leetcode.com/problems/maximum-length-of-pair-chain/description/ 题目: You are given n ...
- php redis 操作手册
String 类型操作 string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象 1 $redis-&g ...
- shell split函数的使用
#!/bin/awk -f BEGIN{FS=","} {split($1,name," "); for (i in name) print name[i] }
- Cloudera API访问
多租户管理页面(admin)操作 cloudera 管理页面页面操作多租户是这样的: 进入到YARN的服务页面,点击Resource Pool,你将会看到已经存在的资源池,然后再点击资源池表格右上角的 ...
- 3、Selenium调用IEDriverServer打开IE浏览器
学习Selenium时若想调用IE浏览器,均需要以下步骤 (1).http://selenium-release.storage.googleapis.com/index.html 下载IEDrive ...
- 解决 No module named PyQt5.QtWebKitWidgets
原因:在 PyQt 5.6(+) 版本中, 新增 QtWebEngineWidgets 代替QtWebKitWidgets. 示例代码:#coding: utf-8 import sysfrom Py ...
- ODP.NET OracleBulkCopy
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Oracle.DataA ...
- Angular5学习笔记 - 创建服务(九)
一.创建服务 ng generate service service-name #简写 ng g s component-name ng g s services/userService 二.效果 三 ...