在第二个点(只有插入和查询排名)答案比期望值少 1
struct AVLNode {
i64 value;
i64 copies;
AVLNode *left, *right;
i64 height, heightDiff, size;
AVLNode() {}
AVLNode(i64 value, i64 _copies)
: value(value), copies(_copies), left(nullptr), right(nullptr), height(1), heightDiff(0), size(_copies) {}
};
AVLNode* root;
int delta = 0;
int removedCount = 0;
void pushup(AVLNode* u) {
int lh = u->left ? u->left->height : 0;
int rh = u->right ? u->right->height : 0;
u->height = max(lh, rh) + 1;
u->heightDiff = rh - lh;
int lsize = u->left ? u->left->size : 0;
int rsize = u->right ? u->right->size : 0;
u->size = lsize + rsize + u->copies;
}
AVLNode* rotateLeft(AVLNode* u) {
AVLNode* v = u->right;
u->right = v->left;
v->left = u;
pushup(u);
pushup(v);
return v;
}
AVLNode* rotateRight(AVLNode* u) {
AVLNode* v = u->left;
u->left = v->right;
v->right = u;
pushup(u);
pushup(v);
return v;
}
AVLNode* maintain(AVLNode* u) {
if (u->heightDiff < 0) {
if (u->left->heightDiff <= 0) {
u = rotateRight(u);
} else {
u->left = rotateLeft(u->left);
u = rotateRight(u);
}
} else if (u->heightDiff > 0) {
if (u->right->heightDiff >= 0) {
u = rotateLeft(u);
} else {
u->right = rotateRight(u->right);
u = rotateLeft(u);
}
}
return u;
}
AVLNode* insert(AVLNode* u, i64 val) {
if (u == nullptr) {
return new AVLNode(val, 1);
}
if (val == u->value) {
u->copies++;
} else if (val < u->value) {
u->left = insert(u->left, val);
} else {
u->right = insert(u->right, val);
}
pushup(u);
u = maintain(u);
return u;
}
AVLNode* remove(AVLNode* root, int val) {
if (root == nullptr) return nullptr;
int actualValue = root->value + delta;
if (val < actualValue) {
root->left = remove(root->left, val);
} else if (val > actualValue) {
root->right = remove(root->right, val);
} else {
if (root->copies > 1) {
root->copies--;
} else {
if (root->left == nullptr || root->right == nullptr) {
AVLNode* temp = root->left ? root->left : root->right;
delete root;
return temp;
} else {
AVLNode* successor = root->right;
while (successor->left != nullptr) {
successor = successor->left;
}
root->value = successor->value;
root->copies = successor->copies;
successor->copies = 1;
root->right = remove(root->right, successor->value + delta);
}
}
}
pushup(root);
root = maintain(root);
return root;
}
i64 getKth(AVLNode* root, i64 k) {
if (root == nullptr) return -1;
int leftSize = root->left ? root->left->size : 0;
if (k <= leftSize) {
return getKth(root->left, k);
} else if (k <= leftSize + root->copies) {
return root->value + delta;
} else {
return getKth(root->right, k - leftSize - root->copies);
}
}
i64 getRank(AVLNode* root, i64 v) {
if (root == nullptr) return 0;
if (root->value == v) return ((root->left ? root->left->size : 0) + 1);
if (root->value > v) return getRank(root->left, v);
return getRank(root->right, v) + (root->left ? root->left->size : 0) +
root->copies;
}
void Main() {
int n, m;
rd(n, m);
root = nullptr;
rep(i, n) {
i64 x;
rd(x);
root = insert(root, x);
}
i64 lastans = 0, ans = 0;
rep(i, m) {
i64 opt, x;
rd(opt, x);
x ^= lastans;
eprintf("Actual: %lld %lld\n", opt, x);
switch (opt) {
case 1:
root = insert(root, x);
break;
case 2:
root = remove(root, x);
break;
case 3:
lastans = getRank(root, x);
ans ^= lastans;
break;
case 4:
lastans = getKth(root, x);
ans ^= lastans;
break;
case 5:
lastans = getKth(root, getRank(root, x) - 1);
ans ^= lastans;
break;
case 6:
lastans = getKth(root, getRank(root, x) + 1);
ans ^= lastans;
break;
}
eprintf("ANS: %lld\n", lastans);
}
printf("%lld\n", ans);
}
int main() {
Main();
}