Codeforces Round #285 (Div. 1) B - Misha and Permutations Summation 康拓展开+平衡树
思路:很裸的康拓展开。。 我的平衡树居然跑的比树状数组+二分还慢。。
#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 康拓展开+平衡树的更多相关文章
- 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles
题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...
- 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 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 ...
- 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 ...
- Codeforces Round #485 (Div. 2) E. Petr and Permutations
Codeforces Round #485 (Div. 2) E. Petr and Permutations 题目连接: http://codeforces.com/contest/987/prob ...
- 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 ...
- 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 ...
- Codeforces Round #285 (Div.1 B & Div.2 D) Misha and Permutations Summation --二分+树状数组
题意:给出两个排列,求出每个排列在全排列的排行,相加,模上n!(全排列个数)得出一个数k,求出排行为k的排列. 解法:首先要得出定位方法,即知道某个排列是第几个排列.比如 (0, 1, 2), (0, ...
随机推荐
- golang设置代理
http://note.youdao.com/noteshare?id=a8df0ec2d623f282a782dbe937bdae9f
- Codeforces 833B The Bakery dp线段树
B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...
- 微信小程序语音识别
语音识别现在已经发展的很成熟了,经过比对发现百度对开发者比较友好,提供很多种语言的SDK,对python来说直接安装 pip install baidu-aip 文档写的也不错 具体参考:http: ...
- Java设计模式の代理模式
目录 代理模式 1.1.静态代理 1.2.动态代理 1.3.Cglib代理 代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是 ...
- 目前最快速的多线程Kmeans算法,java实现
目前最快速Kmeans算法,并由java实现!面对很大的K值表现依然很好. 代码地址: https://github.com/Jethu1/fastKmeans #1.这是一个由java实现的的,多线 ...
- JavaScript Promises 实现框架一览及对比
In my previous post in Danish I looked at how to perform asynchronous calls by using promises. Now t ...
- oozie与mapreduce简单案例
准备工作 拷贝原来的模板 mkdir oozie-apps cd oozie-apps/ cp -r ../examples/apps/mar-reduce . mv map-reduce mr-w ...
- 2017ACM暑期多校联合训练 - Team 2 1003 HDU 6047 Maximum Sequence (线段树)
题目链接 Problem Description Steph is extremely obsessed with "sequence problems" that are usu ...
- js判断手机端(Android手机还是iPhone手机)
/** * [isMobile 判断平台] * @param test: 0:iPhone 1:Android */ function ismobile(test){ var u = navigato ...
- php-fpm性能优化
PHP-fpm PHP-FPM是一个PHPFastCGI管理器,是只用于php的. php-fpm 已经在 Linux.MacOSX.Solaris 和 FreeBSD 上测试通过. 确信 libxm ...