您的位置:首页 > 其它

求LCA练习+部分算法复习 2017.1.22

2017-01-22 21:57 495 查看
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#ifndef    WIN32
#define    AUTO "%I64d"
#else
#define AUTO "%lld"
#endif
using namespace std;
typedef bool boolean;
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = 1;
while(!isdigit((x = getchar())) && x != '-');
if(x == '-'){
aFlag = -1;
x = getchar();
}
for(u = x - '0'; isdigit((x = getchar())); u = u * 10 + x - '0');
ungetc(x, stdin);
u *= aFlag;
}

template<typename T>
class ScapegoatTreeNode {
public:
T val;
int count;
int size;
ScapegoatTreeNode* next[2];
ScapegoatTreeNode():count(1), size(1){
memset(next, 0, sizeof(next));
}
ScapegoatTreeNode(T val):val(val), count(1), size(1){
memset(next, 0, sizeof(next));
}

inline void maintain() {
size = count;
for(int i = 0; i < 2; i++)
if(next[i] != NULL)
size += next[i]->size;
}

inline void addCount(int c){
if(count == 0 && c < 0)    return;
size += c, count += c;
}

inline int cmp(T x){
if(val == x)    return -1;
return (x < val) ? (0) : (1);
}
};

template<typename T>
class ScapegoatTree {
protected:
static void insert(ScapegoatTreeNode<T>*& node, T val){
if(node == NULL){
node = new ScapegoatTreeNode<T>(val);
return;
}
int d = node->cmp(val);
if(d == -1){
node->addCount(1);
return;
}
insert(node->next[d], val);
node->maintain();
}

static boolean remove(ScapegoatTreeNode<T>*& node, T val){
if(node == NULL)    return false;
int d = node->cmp(val);
if(d == -1){
node->addCount(-1);
return true;
}
boolean res = remove(node->next[d], val);
if(res)    node->maintain();
return res;
}

static ScapegoatTreeNode<T>* findKth(ScapegoatTreeNode<T>*& node, int k){
int ls = (node->next[0] == NULL) ? (0) : (node->next[0]->size);
if(k >= ls + 1 && k <= ls + node->count)    return node;
if(k <= ls)    return findKth(node->next[0], k);
return findKth(node->next[1], k - ls - node->count);
}
public:
ScapegoatTreeNode<T>* root;
vector<ScapegoatTreeNode<T>*> lis;

ScapegoatTree():root(NULL){        }

ScapegoatTreeNode<T>* rebuild(int l, int r){
if(l > r)    return NULL;
int mid = (l + r) >> 1;
ScapegoatTreeNode<T>*& node = lis[mid];
node->next[0] = rebuild(l, mid - 1);
node->next[1] = rebuild(mid + 1, r);
node->maintain();
return node;
}

void rebuild(ScapegoatTreeNode<T>*& node, ScapegoatTreeNode<T>*& f){
lis.clear();
travel(node);
int d = -1;
if(f != NULL)    d = f->cmp(node->val);
ScapegoatTreeNode<T>* res = rebuild(0, lis.size() - 1);
if(d != -1)        f->next[d] = res;
else root = res;
}

void insert(T val){
insert(root, val);
}

void remove(T val){
remove(root, val);
}

ScapegoatTreeNode<T>* findKth(int k){
return findKth(root, k);
}
};

int n;
int lb, rb;
ScapegoatTree<int> s;

inline void init() {
readInteger(lb);
readInteger(rb);
for(int i = lb; i <= rb; i++){
ScapegoatTreeNode<int>* node = new ScapegoatTreeNode<int>(i);
readInteger(node->count);
node->maintain();
s.lis.push_back(node);
}
s.root = s.rebuild(0, s.lis.size() - 1);
}

inline void solve() {
readInteger(n);
char cmd[10];
int a;
while(n--) {
scanf("%s", cmd);
readInteger(a);
if(cmd[0] == 'a'){
s.insert(a);
}else if(cmd[0] == 'd'){
s.remove(a);
}else{
ScapegoatTreeNode<int>* node = s.findKth(a);
printf("%d\n", node->val);
}
}
}

int main(){
freopen("kth.in", "r", stdin);
freopen("kth.out", "w", stdout);
init();
solve();
return 0;
}


替罪羊树
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: