原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890

如下:

 #include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using std::sort;
using std::swap;
const int Max_N = ;
struct node{
int val, pos;
}rec[];
inline bool cmp(const node &a, const node &b){
if (a.val == b.val) return a.pos < b.pos;
return a.val < b.val;
}
struct Node{
int v, s, rsz, rev;
Node *pre, *ch[];
inline void set(int _v = -, int _s = , Node *p = NULL){
v = _v, s = _s, rev = rsz = ;
pre = ch[] = ch[] = p;
}
inline void update(){
rev ^= ;
swap(ch[], ch[]);
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
rsz = ch[]->rsz + ch[]->rsz;
if (v != -) rsz++;
}
inline void push_down(){
if (rev != ){
rev ^= ;
ch[]->update();
ch[]->update();
}
}
};
struct SplayTree{
int top = ;
Node *tail, *root, *null;
Node stack[Max_N], *store[Max_N], *ptr[Max_N];
void init(int n){
top = ;
tail = &stack[];
null = tail++;
null->set();
root = newNode(-);
root->ch[] = newNode(-);
root->ch[]->pre = root;
Node *x = built(, n);
root->ch[]->ch[] = x;
x->pre = root->ch[];
root->ch[]->push_up();
root->push_up();
}
inline Node *built(int l, int r){
Node *p = null;
if (l > r) return null;
int mid = (l + r) >> ;
p = newNode(rec[mid].val), ptr[mid] = p;
p->ch[] = built(l, mid - );
if (p->ch[] != null) p->ch[]->pre = p;
p->ch[] = built(mid + , r);
if (p->ch[] != null) p->ch[]->pre = p;
p->push_up();
return p;
}
inline Node *newNode(int v){
Node *p = null;
if (!top) p = tail++;
else p = store[--top];
p->set(v, , null);
return p;
}
inline void rotate(Node *x, int d){
Node *y = x->pre;
y->push_down(), x->push_down();
y->ch[!d] = x->ch[d];
if (x->ch[d] != null) x->ch[d]->pre = y;
x->pre = y->pre;
if (y->pre != null) y->pre->ch[y->pre->ch[] != y] = x;
x->ch[d] = y;
y->pre = x;
y->push_up();
if (y == root) root = x;
}
inline void splay(Node *x, Node *f){
for (; x->pre != f; x->push_down()){
if (x->pre->pre == f){
rotate(x, x->pre->ch[] == x);
} else {
Node *y = x->pre, *z = y->pre;
if (z->ch[] == y){
if (y->ch[] == x) rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
} else {
if (y->ch[] == x) rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
}
}
}
x->push_up();
}
inline Node *select(Node *x, int k){
int t = ;
for (;;){
x->push_down();
t = x->ch[]->s;
if (k == t + ) break;
else if (k < t + ) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x;
}
inline void del(){
Node *x = root;
store[top++] = x;
root = root->ch[];
root->pre = null;
splay(select(root, ), null);
root->ch[] = x->ch[];
root->ch[]->pre = root;
root->push_up();
}
inline void solve(int n) {
const int N = n + ;
for (int i = ; i <= n; ++i) {
scanf("%d", &rec[i].val);
rec[i].pos = i;
}
init(n), sort(rec + , rec + n + , cmp);
splay(root->ch[], null);
for (int i = ; i <= n; i++){
splay(ptr[rec[i].pos], null);
root->ch[]->update();
printf("%d", root->ch[]->rsz + i);
if (i != n) printf(" ");
del();
}
printf("\n");
}
}spt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while (~scanf("%d", &n) && n) {
spt.solve(n);
}
return ;
}

最新文章

  1. Linux 杂记
  2. publish_subscribe
  3. 技术文集:万能WINDOWS XP封装
  4. 将UIImage保存成JPG或PNG格式存储在本地
  5. Jquery 在动态元素上绑定事件
  6. android 在fragment中获取界面的UI组件
  7. AutoLayout(转)
  8. javascript代码放置位置对程序的影响
  9. 用git提交代码步骤
  10. 利用代码改变世界 #AzureDev
  11. 【Machine Learning in Action --4】朴素贝叶斯从个人广告中获取区域倾向
  12. elementUI vue 页面加载的时候页面出现了黑字 页面优化处理 按钮弹出框文字
  13. React-菜鸟学习笔记(二)
  14. golang 对结构体进行格式化输出
  15. error C4996: &#39;sprintf&#39;: This function or variable may be unsafe.
  16. 2.4 UML类图
  17. 连通性问题--Algorithms IN C读书笔记
  18. 【PHP】快递鸟 物流查询接口实现
  19. request-2高级用法
  20. BZOJ 1040 ZJOI 2008 骑士 基环树林+树形DP

热门文章

  1. Error querying database. Cause: java.sql.SQLException: ORA-01745: 无效的主机/绑定变量名
  2. button的type属性
  3. 学习总结 java Iterator迭代器练习
  4. [drp 6]接口和抽象类的区别,及其应用场景
  5. leetcode022. Generate Parentheses
  6. jquery selector 使用方法
  7. javascript 实现HashTable(哈希表)
  8. 分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。
  9. 【风马一族_Android】通过菜单的点击,跳转到不同界面
  10. 首页banner特效