思路:很裸的康拓展开。。 我的平衡树居然跑的比树状数组+二分还慢。。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg
using namespace std; const int N = 2e5 + ;
const int M = 1e7 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ; int n, a[N], b[N], c1[N], c2[N]; struct node {
node* ch[];
int key, fix, sz, cnt;
void update() {
sz = ch[]->sz + ch[]->sz + cnt;
}
}; typedef node* P_node; struct Treap {
node base[N], nil;
P_node root, null, len; Treap() {
root = null = &nil;
null->key = null->fix = 1e9;
null->sz = null->cnt = ;
null->ch[] = null->ch[] = null;
len = base;
} P_node newnode(int tkey) {
len->key = tkey;
len->fix = rand();
len->ch[] = len->ch[] = null;
len->sz = len->cnt = ;
return len++;
} void rot(P_node &p, int d) {
P_node k = p->ch[d ^ ];
p->ch[d ^ ] = k->ch[d];
k->ch[d] = p;
p->update();
k->update();
p = k;
} void _Insert(P_node &p, int tkey) {
if(p == null) {
p = newnode(tkey);
} else if(p->key == tkey) {
p->cnt++;
} else {
int d = tkey > p->key;
_Insert(p->ch[d], tkey);
if(p->ch[d]->fix > p->fix) {
rot(p, d ^ );
}
}
p->update();
} void _Delete(P_node &p, int tkey) {
if(p == null) return;
if(p->key == tkey) {
if(p->cnt > ) p->cnt--;
else if(p->ch[] == null) p = p->ch[];
else if(p->ch[] == null) p = p->ch[];
else {
int d = p->ch[]->fix > p->ch[]->fix;
rot(p, d);
_Delete(p->ch[d], tkey);
}
} else {
_Delete(p->ch[tkey > p->key], tkey);
}
p->update();
} int _Kth(P_node p, int k) {
if(p == null || k < || k > p->sz) return ;
if(k < p->ch[]->sz + ) return _Kth(p->ch[], k);
if(k > p->ch[]->sz + p->cnt) return _Kth(p->ch[], k - p->ch[]->sz - p->cnt);
return p->key;
} int _Rank(P_node p, int tkey, int res) {
if(p == null) return -;
if(p->key == tkey) return p->ch[]->sz + res + ;
if(tkey < p->key) return _Rank(p->ch[], tkey, res);
return _Rank(p->ch[], tkey, res + p->ch[]->sz + p->cnt);
} int _Pred(P_node p, int tkey){
if(p == null) return -1e9;
if(tkey <= p->key) return _Pred(p->ch[], tkey);
return max(p->key, _Pred(p->ch[], tkey));
} int _Succ(P_node p, int tkey){
if(p == null) return 1e9;
if(tkey >= p->key) return _Succ(p->ch[], tkey);
return min(p->key, _Succ(p->ch[], tkey));
} void Insert(int tkey){ _Insert(root,tkey); }
void Delete(int tkey){ _Delete(root,tkey); }
int Kth(int k){ return _Kth(root,k); }
int Rank(int tkey){ return _Rank(root,tkey,); }
int Pred(int tkey){ return _Pred(root,tkey); }
int Succ(int tkey){ return _Succ(root,tkey); }
}tp; void getOrder(int *a, int *c, int n) {
tp.len = tp.base;
for(int i = ; i < n; i++) tp.Insert(i);
for(int i = ; i < n; i++) {
c[n - i - ] = tp.Rank(a[i]) - ;
tp.Delete(a[i]);
}
} void getPerm(int *c, int n) {
tp.len = tp.base;
for(int i = ; i < n; i++) tp.Insert(i);
for(int i = n - ; i >= ; i--) {
int val = tp.Kth(c[i] + );
tp.Delete(val);
printf("%d ", val);
}
puts("");
}
int main() {
scanf("%d", &n);
for(int i = ; i < n; i++) scanf("%d", &a[i]);
for(int i = ; i < n; i++) scanf("%d", &b[i]);
getOrder(a, c1, n);
getOrder(b, c2, n); for(int i = ; i < n; i++) {
c1[i] += c2[i];
int cnt = c1[i] / (i + );
c1[i] -= cnt * (i + );
c1[i + ] += cnt;
} c1[n - ] %= n;
getPerm(c1, n);
return ;
} /*
*/

Codeforces Round #285 (Div. 1) B - Misha and Permutations Summation 康拓展开+平衡树的更多相关文章

  1. 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  2. 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

    题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...

  3. 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  4. Codeforces Round #285 (Div. 1) A. Misha and Forest 拓扑排序

    题目链接: 题目 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes 问题描述 L ...

  5. Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)

    传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...

  6. Codeforces Round #485 (Div. 2) E. Petr and Permutations

    Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...

  7. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  8. Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  9. Codeforces Round #285 (Div.1 B & Div.2 D) Misha and Permutations Summation --二分+树状数组

    题意:给出两个排列,求出每个排列在全排列的排行,相加,模上n!(全排列个数)得出一个数k,求出排行为k的排列. 解法:首先要得出定位方法,即知道某个排列是第几个排列.比如 (0, 1, 2), (0, ...

随机推荐

  1. Tensorboard教程:显示计算图中节点信息

    Tensorboard显示计算图节点信息 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 强烈推荐Tensorflow实战Google深度学习框架 实验平台: Tensorflow1 ...

  2. github访问很慢的问题

    公司一直用着svn, 之前也的确用过github的版本管理,但是一直都是可视化的操作 这几天面试了几名前端,问了一下发现他们在之前的公司里都是用git的, 于是今天好好温故了一下怎么用命令行进行一下g ...

  3. JS中的匿名函数自执行、函数声明与函数表达式

    先看一段jQuery源码中匿名函数自执行的例子: (function( window, undefined ) { // jquery code })(window); 另外一种常见的写法: +fun ...

  4. JS-DOM-随时更新

    DOM里有三种节点:元素节点.文本节点和属性节点 getElmentById(); //id选择器  在JS中用此方法来查找获取  建议大小写 以免不兼容 有时候查找不到  DOM操作必须等到HTML ...

  5. 51nod 1171 大灾变

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1757 二分答案mid 避难所拆为mid个点 每个避难所的第一个点向第二个 ...

  6. LightOJ1214 Large Division 基础数论+同余定理

    Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...

  7. Python学习笔记(二十九)ThreadLocal

    import threading #创建全局ThreadLocal对象: local_school = threading.local() def process_student(): #获取当前线程 ...

  8. Python学习笔记(十八)@property

    # 请利用@property给一个Screen对象加上width和height属性, # 以及一个只读属性resolution: # -*- coding: utf-8 -*- class Scree ...

  9. CodeForces - 1003D

    Polycarp has nn coins, the value of the ii-th coin is aiai. It is guaranteed that all the values are ...

  10. C.Fountains(Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)+线段树+RMQ)

    题目链接:http://codeforces.com/contest/799/problem/C 题目: 题意: 给你n种喷泉的价格和漂亮值,这n种喷泉题目指定用钻石或现金支付(分别用D和C表示),C ...