SGU 271 水题。。。。
期末来了,总是淡淡的忧伤,没办法,只能找水题做了
[cpp]
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
typedef long long lld;
#define L x->c[0]
#define R x->c[1]
#define KT root->c[1]->c[0]
const int maxn = 111111;
struct node {
struct node *c[2], *fa;
int id;
int sz;
bool flip;
char name[5];
inline bool d() {
return fa->c[0] == this;
}
inline void setc(int d, node *s) {
c[d] = s;
s->fa = this;
}
inline void up() {
sz = c[0]->sz + c[1]->sz + 1;
}
inline void down() {
if(flip) {
c[0]->flip ^= 1;
c[1]->flip ^= 1;
swap(c[0],c[1]);
flip = false;
}
}
inline void clear(node *null) {
c[0] = c[1] = null;
sz = 1;
}
} NODE[2 * maxn], *null = &NODE[0] ;
int top;
node* ID[maxn];
struct SplayTree {
node* root;
void Rotate(node *x,int f) {
node *y = x->fa;
y->down(); x->down();
y->setc(!f,x->c[f]);
x->fa = y->fa;
if (y->fa != null) y->fa->setc(!y->d(),x);
x->setc(f,y);
y->up();
}
void Splay(node *x, node *goal) {
x->down();
while (x->fa != goal) {
x->fa->fa->down(); x->fa->down(); x->down();
if (x->fa->fa == goal)
Rotate(x, x->d());
else {
int f = x->fa->d();
x->d() == f ? Rotate(x->fa, f) : Rotate(x, !f);
Rotate(x, f);
}
}
x->up();
if (goal == null) root = x;
}
void RTO(int k, node *goal) {
node *x = root;
x->down();
while (L->sz + 1 != k) {
if(k < L->sz + 1) x = L;
else {
k -= L->sz + 1;
x = R;
}
x->down();
}
Splay(x, goal);
}
void build(node* &x,int l,int r,node *fa) {
if(l > r) return ;
int m = (l + r) >>1;
x = new_node(fa,num[m]);
build(x->c[0],l,m-1,x);
build(x->c[1],m+1,r,x);
x->up();
}
node *new_node(node *fa,char *s) {
node *x = &NODE[++top];
x->id = top;
x->c[0] = x->c[1] = null;
x->sz = 1;
x->flip = false;
strcpy(x->name,s);
x->fa = fa;
return x;
}
void ADD(char *s) {
RTO(1,null);
RTO(2,root);
node *tmp = new_node(root->c[1],s);
root->c[1]->setc(0,tmp);
root->c[1]->up();
root->up();
// // debug();
// puts("");
// print(root);
// puts("");
}
void ROTATE(int K) {
if(root->sz-2 <= K) {
RTO(1,null);
RTO(root-
补充:软件开发 , C++ ,