AddressSanitizer:DEADLYSIGNAL
=================================================================
==6820==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000010 (pc 0x55996232e378 bp 0x60300002b948 sp 0x7ffdcf18c7b0 T0)
==6820==The signal is caused by a READ memory access.
看不懂。
Code(79pts):
#include <cmath>
#include <cstdio>
#include <algorithm>
typedef struct AVLNode {
int data, height, cnt, size;
AVLNode *lson, *rson;
AVLNode():
data(0), height(1), cnt(1), size(1), lson(NULL), rson(NULL) {}
AVLNode(int val):
data(val), height(1), cnt(1), size(1), lson(NULL), rson(NULL) {}
}*AVLTree;
inline int Height(AVLTree T) {
if (T == NULL)
return 0;
return T -> height;
}
inline int Size(AVLTree T) {
if (T == NULL)
return 0;
return T -> size;
}
inline void Upd_Point(AVLTree &T) {
T -> size = Size(T -> lson) + Size(T -> rson) + T -> cnt;
T -> height = std::max(Height(T -> lson), Height(T -> rson)) + 1;
}
inline AVLTree LL_rotate(AVLTree &T) {
register AVLTree temp = T -> lson;
T -> lson = temp -> rson;
temp -> rson = T;
Upd_Point(T);
Upd_Point(temp);
return temp;
}
inline AVLTree RR_rotate(AVLTree &T) {
register AVLTree temp = T -> rson;
T -> rson = temp -> lson;
temp -> lson = T;
Upd_Point(T);
Upd_Point(temp);
return temp;
}
inline AVLTree LR_rotate(AVLTree &T) {
T -> lson = RR_rotate(T -> lson);
return LL_rotate(T);
}
inline AVLTree RL_rotate(AVLTree &T) {
T -> rson = LL_rotate(T -> rson);
return RR_rotate(T);
}
inline AVLTree Insert(AVLTree &T, int data) {
if (T == NULL) {
T = new AVLNode(data);
return T;
}
if (T -> data == data) {
T -> cnt ++;
Upd_Point(T);
return T;
}
if (T -> data > data) {
T -> lson = Insert(T -> lson, data);
if (Height(T -> lson) - Height(T -> rson) == 2){
if (data < T -> lson -> data) // 一路向左
T = LL_rotate(T);
else
T = LR_rotate(T);
}
} else {
T -> rson = Insert(T -> rson, data);
if (Height(T -> rson) - Height(T -> lson) == 2){
if (data > T -> rson -> data)
T = RR_rotate(T);
else
T = RL_rotate(T);
}
}
Upd_Point(T);
return T;
}
inline AVLTree Adjust(AVLTree &T) {
if (T == NULL)
return NULL;
if (Height(T -> lson) - Height(T -> rson) == 2) {
if (Height(T -> lson -> lson) >= Height(T -> lson -> rson))
T = LL_rotate(T);
else
T = LR_rotate(T);
} else if (Height(T -> rson) - Height(T -> lson) == 2) {
if (Height(T -> rson -> rson) >= Height(T -> rson -> lson))
T = RR_rotate(T);
else
T = RL_rotate(T);
}
Upd_Point(T);
return T;
}
inline AVLTree Delete(AVLTree &T, int data) {
if (T == NULL) // 值不存在
return NULL;
if (T -> data == data) { // 如果找到这个值
// 将这个值替换为这个点的直接后驱
// 实际上前驱也可以
if (T -> cnt > 1) {
T -> cnt --;
Upd_Point(T);
return T;
}
if (T -> rson == NULL) { // 这个节点没有子树上的后驱,把左子树拉上来,这个节点删掉或数量 -1
register AVLTree temp = T;
T = T -> lson;
delete temp;
} else { // 把子树上的后驱换上来,把后驱删了
register AVLTree temp;
temp = T -> rson;
while (temp -> lson)
temp = temp -> lson;
T -> data = temp -> data;
T -> cnt = temp -> cnt; // 此处有大量改动,检查。
temp -> cnt = 1;
T -> rson = Delete(T -> rson, T -> data); // 删后驱
Upd_Point(T);
}
return T;
} else if (T -> data > data)
T -> lson = Delete(T -> lson, data);
else if (T -> data < data)
T -> rson = Delete(T -> rson, data);
Adjust(T);
Upd_Point(T);
return T;
}
// 一下部分可能按需调整
inline int First_Rank(AVLTree T, int data) { // 查排名
if (T == NULL)
return 1;
if (T -> data == data) // 找到
return Size(T -> lson) + 1;
if (T -> data > data) // left tree
return First_Rank(T -> lson, data);
return First_Rank(T -> rson, data) + Size(T -> lson) + T -> cnt;
}
inline int kth(AVLTree T, int idx) {
if (T == NULL)
return 0;
if (idx <= Size(T -> lson))
return kth(T -> lson, idx);
if (idx <= Size(T -> lson) + T -> cnt)
return T -> data;
else
return kth(T -> rson, idx - Size(T -> lson) - T -> cnt);
}
inline int GetPrev(AVLTree Root, int data) {
AVLTree ans = new AVLNode(-10000005), T = Root;
while (T) {
if (T -> data == data) {
if (T -> lson) {
T = T -> lson;
while (T -> rson)
T = T -> rson;
ans = T;
}
break;
}
if (T -> data < data && T -> data > ans -> data)
ans = T;
T = T -> data > data ? T -> lson : T -> rson;
}
return ans -> data;
}
inline int GetNext(AVLTree Root, int data) {
AVLTree ans = new AVLNode(10000005), T = Root;
while (T) {
if (T -> data == data) {
if (T -> rson) {
T = T -> rson;
while (T -> lson)
T = T -> lson;
ans = T;
}
break;
}
if (T -> data > data && T -> data < ans -> data)
ans = T;
T = T -> data > data ? T -> lson : T -> rson;
}
return ans -> data;
}
inline void print(AVLTree now) {
if (now == NULL)
return;
print(now -> lson);
for (int i(1); i <= now -> cnt; i++)
printf("%d ", now -> data);
fflush(stdout);
print(now -> rson);
}
int T, opt, x;
int main() {
//freopen("input.txt", "r", stdin);
//freopen("ri.txt", "w", stdout);
AVLTree Root = NULL;
scanf("%d", &T);
while (T--) {
scanf("%d %d", &opt, &x);
if (opt == 1)
Insert(Root, x);
else if (opt == 2)
Delete(Root, x);
else if (opt == 3)
printf("%d\n", First_Rank(Root, x));
else if (opt == 4)
printf("%d\n", kth(Root, x));
else if (opt == 5)
printf("%d\n", GetPrev(Root, x));
else if (opt == 6)
printf("%d\n", GetNext(Root, x));
//print(Root);
//printf("\n");
}
return 0;
}