个人整理的数组splay板子,指针的写的太丑了就不放了。。

splay的板子。。

由于被LCT榨干了。。所以昨天去学了数组版的splay,现在整理一下板子。。

以BZOJ3224和3223为例题。。暂时只有这些,序列的话等有时间把维修序列给弄上来!!

BZOJ 3224 平衡树的操作

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
} const int N = 300005;
int ch[N][2], val[N], size[N], num[N], fa[N], root, tot; int newNode(int p, int v){
val[++tot] = v, fa[tot] = p;
size[tot] = num[tot] = 1, ch[tot][0] = ch[tot][1] = 0;
return tot;
} void push_up(int x){
size[x] = size[ch[x][0]] + size[ch[x][1]] + num[x];
} void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
fa[x] = z, ch[z][ch[z][1] == y] = x, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
} void splay(int x, int goal){
if(x == goal) return;
while(fa[x] != goal){
int y = fa[x], z = fa[y];
if(z != goal){
if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
push_up(x);
if(goal == 0) root = x;
} void insert(int x){
if(!root){
root = newNode(0, x);
return;
}
int cur = root;
while(ch[cur][x > val[cur]]){
if(val[cur] == x) break;
cur = ch[cur][x > val[cur]];
}
if(val[cur] == x) size[cur] ++, num[cur] ++, splay(cur, 0);
else ch[cur][x > val[cur]] = newNode(cur, x), splay(ch[cur][x > val[cur]], 0);
} void find(int x){
if(!root) return;
int cur = root;
while(val[cur] != x && ch[cur][x > val[cur]])
cur = ch[cur][x > val[cur]];
splay(cur, 0);
} int precursor(int x){
find(x);
if(val[root] < x) return root;
int cur = ch[root][0];
while(ch[cur][1]) cur = ch[cur][1];
return cur;
} int successor(int x){
find(x);
if(val[root] > x) return root;
int cur = ch[root][1];
while(ch[cur][0]) cur = ch[cur][0];
return cur;
} void del(int x){
int pre = precursor(x), suc = successor(x);
splay(pre, 0), splay(suc, root);
int key = ch[suc][0];
if(num[key] > 1) num[key] --, size[key] --, splay(key, 0);
else ch[suc][0] = 0;
push_up(suc), push_up(root);
} int ranks(int x){
find(x);
return size[ch[root][0]] + 1;
} int select(int k){
int cur = root, m = size[ch[cur][0]];
while(1){
if(k > m + num[cur]){
k -= m + num[cur];
cur = ch[cur][1];
}
else{
//cout << m << endl;
if(m >= k) cur = ch[cur][0];
else return val[cur];
}
m = size[ch[cur][0]];
}
} void inOrder(int x){
if(!x) return;
inOrder(ch[x][0]);
cout << val[x] << " ";
inOrder(ch[x][1]);
} int main(){ int n = read();
insert(-INF), insert(INF);
for(int i = 0; i < n; i ++){
int opt = read(), x = read();
if(opt == 1) insert(x);
else if(opt == 2) del(x);
else if(opt == 3) printf("%d\n", ranks(x) - 1);
else if(opt == 4) printf("%d\n", select(x + 1));
else if(opt == 5) printf("%d\n", val[precursor(x)]);
else if(opt == 6) printf("%d\n", val[successor(x)]);
//inOrder(root);puts("");
}
return 0;
}

BZOJ 3223 文艺平衡树,虽然只有一个反转。。

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
} const int N = 100005;
int ch[N][2], val[N], fa[N], size[N], rev[N], tot, root; int newNode(int p, int v){
val[++tot] = v, fa[tot] = p, size[tot] = 1;
ch[tot][0] = ch[tot][1] = rev[tot] = 0;
return tot;
} void reverse(int x){
rev[x] ^= 1;
swap(ch[x][0], ch[x][1]);
} void push_up(int x){
if(!x) return;
size[x] = size[ch[x][0]] + size[ch[x][1]] + 1;
} void push_down(int x){
if(rev[x]){
reverse(ch[x][0]), reverse(ch[x][1]);
rev[x] ^= 1;
}
} int buildTree(int p, int l, int r){
if(l > r) return 0;
int mid = (l + r) >> 1;
int cur = newNode(p, mid);
ch[cur][0] = buildTree(cur, l, mid - 1);
ch[cur][1] = buildTree(cur, mid + 1, r);
push_up(cur);
return cur;
} void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
push_down(y), push_down(x);
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
fa[x] = z, ch[z][ch[z][1] == y] = x, fa[y] = x, ch[x][p] = y;
push_up(y), push_up(x);
} void splay(int x, int goal){
if(x == goal) return;
while(fa[x] != goal){
int y = fa[x], z = fa[y];
if(z != goal){
if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
rotate(y);
}
rotate(x);
}
push_up(x);
if(goal == 0) root = x;
} int find(int k){
if(!root) return 0;
int cur = root, m = size[ch[cur][0]];
while(1){
push_down(cur);
if(m < k){
k -= m + 1;
cur = ch[cur][1];
}
else{
if(m >= k + 1) cur = ch[cur][0];
else return cur;
}
m = size[ch[cur][0]];
}
} void re(int l, int r){
int x = find(l - 1), y = find(r + 1);
splay(x, 0), splay(y, root);
reverse(ch[ch[root][1]][0]);
} void inOrder(int x){
if(!x) return;
push_down(x);
inOrder(ch[x][0]);
if(val[x]) printf("%d ", val[x]);
inOrder(ch[x][1]);
} int main(){ int n = read(), m = read();
root = newNode(0, 0), ch[root][1] = newNode(root, 0);
ch[ch[root][1]][0] = buildTree(ch[root][1], 1, n);
while(m --){
int l = read(), r = read();
re(l, r);
}
inOrder(root);
return 0;
}
上一篇:SpringBoot+Mybatis+Freemark 最简单的例子


下一篇:Centos中安装code blocks