被BZOJ坑了一下午,原以为是我程序有问题一直WA,结果是我数组小了。。。为啥不给我RE!!!

线段树合并,对于逆序对而言,只能通过交换左右子树来达到,那么我们就可以想到对于一个结点而言,我们当然要取左右子树不叫换产生的逆序对,及交换产生的逆序对,取最小的就好了,另外呢,因为要维护子树中包含的数,在往上递归时需要对该树合并。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<queue>
#include<list>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 2000005
#define ull unsigned long long
#define ll long long
#define hashmod 99999839
#define mod 9997
int n,x,y;
int ls[maxn],rs[maxn];//结点的左右儿子
int c[maxn],len;//结点代表的区间中有的个数
long long ans,s;
stack<int> st;
int newnode(){//返回一个新结点
int p;
if(st.empty()) p = len++;
else p = st.top(),st.pop();
ls[p] = rs[p] = ,c[p] = ;
return p;
}
int bulid(int x){//建立叶子结点对应的树并返回根
int root = newnode();//先申请一个新节点
int l = ,r = n,mid,p = root;
while(l < r){
mid = (l + r) >> ;
if(mid >= x) ls[p] = newnode(),p = ls[p],r = mid;
else rs[p] = newnode(),p = rs[p],l = mid + ;
}
return root;
}
int merge(int p,int q){//合并以p,q为根的树,并返回新的根
if(!p || !q) return p + q;
s += (long long)c[rs[p]] * c[ls[q]];
ls[p] = merge(ls[p],ls[q]);
rs[p] = merge(rs[p],rs[q]);
c[p] = c[ls[p]] + c[rs[p]];
st.push(q);//可将合并了的q结点放入st中
return p;
}
int solve(){
scanf("%d",&x);
if(x) return bulid(x);//对叶节点建立一颗线段树
int p = solve(),q = solve();//建立其左右儿子
ll t = c[p] * c[q];
s = ,p = merge(p,q);//逆序对由非叶子结点产生
if(s <= t / ) ans += s;
else ans += t - s;
return p;
}
void init(){
while(!st.empty()) st.pop();
len = ,ans = ;
}
int main(){
freopen("a.in","r",stdin);
freopen("b.out","w",stdout);
scanf("%d",&n);
init();
solve();
printf("%lld",ans);
return ;
}

bzoj2212 Tree Rotations的更多相关文章

  1. [bzoj2212]Tree Rotations(线段树合并)

    解题关键:线段树合并模板题.线段树合并的题目一般都是权值线段树,因为结构相同,求逆序对时,遍历权值线段树的过程就是遍历所有mid的过程,所有能求出所有逆序对. #include<iostream ...

  2. bzoj2212 Tree Rotations 线段树合并+动态开点

    题目传送门 思路: 区间合并线段树的题,第一次写,对于一颗子树,无论这个子树怎么交换,都不会对其他子树的逆序对造成影响,所以就直接算逆序对就好. 注意叶子节点是1到n的全排列,所以每个权值都只会出现1 ...

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

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

  4. BZOJ2212: [Poi2011]Tree Rotations

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

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

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

  6. 2018.07.07 BZOJ2212: Poi2011Tree Rotations(线段树合并)

    2212: [Poi2011]Tree Rotations Time Limit: 20 Sec Memory Limit: 259 MB Description Byteasar the garde ...

  7. bzoj 2212 Tree Rotations

    bzoj 2212 Tree Rotations 考虑一个子树 \(x\) 的左右儿子分别为 \(ls,rs\) .那么子树 \(x\) 内的逆序对数就是 \(ls\) 内的逆序对数,\(rs\) 内 ...

  8. 2212: [Poi2011]Tree Rotations

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

  9. POI2011 Tree Rotations

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

随机推荐

  1. 2017广东工业大学程序设计竞赛决赛 H tmk买礼物

    题意: Description 今天是校赛的日子,为了庆祝这么喜庆的日子,TMK打算买些礼物给女票LSH庆祝一下. TMK进入了雪梨超市,然后刚踏入的一瞬间,店主就对TMK说:“恭喜你成为了本店第21 ...

  2. hihocoder 神奇字符串

    思路: 暴力,模拟. 实现: #include <iostream> #include <algorithm> #include <cstdio> #include ...

  3. 【Python】高级函数

    1.Filter函数 def is_odd(x): return x % 2 == 1 #将列表中所有的奇数筛选出来 print(list(filter(is_odd,[1,2,3,4,5,6,7]) ...

  4. pthread Win32多线程编程的一些知识和感想

    研究遗传算法的一大诟病就是每次运行程序的结果并不是完全一样的,有时候能找到最优解有时候找不到最优解,这就是遗传算法的概率性导致的.那么怎么评价你的方法的好坏呐,这时候就要多次独立运行程序最后取结果的平 ...

  5. nginx php 配置模板

    server {     listen 80;     server_name    www.xxx.com;     #access_log     logs/www.xxx.com.access. ...

  6. iOS重签

    由于渠道推广需要,可能需要多个包做备份推广,区别是icon.游戏名称.登录logo.bundleid.签名证书.支付Consumables不同,其他游戏包体完全相同. 反复修改多次文件提交Jenkin ...

  7. Shell书籍推荐

    1.鸟哥私房菜 2.<Linux命令行与shell脚本编程大全>         第二本书买了,对于初学者来说一般.

  8. caffe和图像一些基础知识

    1.卷积层的参数放置在convoluytion_param{}中,pad默认是0,stride默认是1,如果在convoluytion_param中没有写pad = 什么,或者stride = 什么, ...

  9. Vue之x-template(1)

    今天,我们来讲一个比较有趣的一个功能吧 先来看一段代码示例: <html> <head> <meta charset="utf-8"> < ...

  10. webservice和一般处理程序

    一丶WebService 1.新建项目 2.选择Web窗体 3.添加新建项 二丶一般处理程序 前台访问: $.ajax({ type: "post", url: "Han ...