QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#382320 | #21529. 动态树(简单版) | Isrothy | Compile Error | / | / | C++23 | 3.6kb | 2024-04-08 12:20:54 | 2024-04-08 12:20:56 |
Judging History
answer
#include <cstdio>
#include <stack>
struct Lct {
unsigned val, sum;
bool rev;
Lct *ch[2], *fa;
Lct() = default;
explicit Lct(unsigned val)
: val(val), sum(val), rev(false), ch{nullptr, nullptr}, fa(nullptr) {}
Lct *push_up() {
sum = val;
if (ch[0] != nullptr) {
sum ^= ch[0]->sum;
}
if (ch[1] != nullptr) {
sum ^= ch[1]->sum;
}
return this;
}
bool dir() {
return fa->ch[1] == this;
}
bool is_root() {
return !fa || (fa->ch[0] != this && fa->ch[1] != this);
}
Lct *update_rev() {
rev ^= 1;
std::swap(ch[0], ch[1]);
return this;
}
Lct *push_down() {
if (rev) {
if (ch[0]) {
ch[0]->update_rev();
}
if (ch[1]) {
ch[1]->update_rev();
}
rev = false;
}
return this;
}
Lct *rotate(bool f) {
auto q = ch[f];
ch[f] = q->ch[!f];
if (ch[f]) {
ch[f]->fa = this;
}
q->fa = fa;
if (!is_root()) {
fa->ch[dir()] = q;
}
q->ch[!f] = this;
fa = q;
push_up();
return q;
}
Lct *splay() {
std::stack<Lct *> stk;
for (auto q = this; !q->is_root(); q = q->fa) {
stk.push(q->fa);
}
while (!stk.empty()) {
stk.top()->push_down();
stk.pop();
}
push_down();
while (!is_root()) {
bool f = dir();
if (!fa->is_root()) {
(fa->dir() == f ? fa->fa : fa)->rotate(f);
}
fa->rotate(dir());
}
return push_up();
}
auto access() {
Lct *p = nullptr;
for (Lct *q = this; q != nullptr; q = q->fa) {
q->splay();
q->ch[1] = p;
q->push_up();
p = q;
}
return this;
}
auto make_root() {
return access()->splay()->update_rev();
}
auto find_root() {
access()->splay();
auto q = this;
while (q->ch[0]) {
q = q->push_down()->ch[0];
}
return q->splay();
}
};
void link(Lct *p, Lct *q) {
if (p->find_root() != q->find_root()) {
p->make_root()->fa = q;
}
}
void cut(Lct *p, Lct *q) {
p->make_root();
q->access()->splay();
auto r = q->ch[0];
if (!r) {
return;
}
while (r->ch[1]) {
r = r->push_down()->ch[1];
}
if (r == p) {
q->ch[0]->fa = nullptr;
q->ch[0] = nullptr;
}
r->splay();
}
std::optional<unsigned> query(Lct *p, Lct *q) {
if (p->find_root() != q->find_root()) {
return std::nullopt;
}
p->make_root();
q->access()->splay();
return q->sum;
}
void update(Lct *p, unsigned val) {
p->make_root();
p->val = val;
p->push_up();
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
std::vector<Lct> v(n + 1);
for (int i = 1; i <= n; i++) {
scanf("%u", &v[i].val);
}
while (m--) {
int op, x, y;
scanf("%d%d%d", &op, &x, &y);
if (op == 0) {
printf("%d\n", (int) query(&v[x], &v[y]).value_or(-1));
} else if (op == 1) {
link(&v[x], &v[y]);
} else if (op == 2) {
cut(&v[x], &v[y]);
} else if (op == 3) {
update(&v[x], y);
}
}
return 0;
}
详细
answer.code:121:6: error: ‘optional’ in namespace ‘std’ does not name a template type 121 | std::optional<unsigned> query(Lct *p, Lct *q) { | ^~~~~~~~ answer.code:3:1: note: ‘std::optional’ is defined in header ‘<optional>’; did you forget to ‘#include <optional>’? 2 | #include <stack> +++ |+#include <optional> 3 | answer.code: In function ‘int main()’: answer.code:139:10: error: ‘vector’ is not a member of ‘std’ 139 | std::vector<Lct> v(n + 1); | ^~~~~~ answer.code:3:1: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’? 2 | #include <stack> +++ |+#include <vector> 3 | answer.code:139:20: error: expected primary-expression before ‘>’ token 139 | std::vector<Lct> v(n + 1); | ^ answer.code:139:22: error: ‘v’ was not declared in this scope 139 | std::vector<Lct> v(n + 1); | ^ answer.code:147:34: error: ‘query’ was not declared in this scope 147 | printf("%d\n", (int) query(&v[x], &v[y]).value_or(-1)); | ^~~~~ answer.code:138:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 138 | scanf("%d%d", &n, &m); | ~~~~~^~~~~~~~~~~~~~~~ answer.code:145:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 145 | scanf("%d%d%d", &op, &x, &y); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~