跳到正文

剪贴板

剪贴板 pg8oru9s

Eaux
原文
剪贴板信息

剪贴板信息

发布
收录
更新
C++
#include <bits/stdc++.h>
using namespace std;

namespace IO {
    const int __SIZE = (1 << 21) + 1;
    char ibuf[__SIZE], *iS, *iT, obuf[__SIZE],
        *oS = obuf, *oT = oS + __SIZE - 1, _c, qu[55];
    int __f, qr, _eof;
#define Gc()                                                                   \
    (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),             \
                 (iS == iT ? EOF : *iS++))                                     \
              : *iS++)
    void flush() {
        fwrite(obuf, 1, oS - obuf, stdout);
        oS = obuf;
    }
    void gc(char &x) {
        x = Gc();
    }
    void pc(char x) {
        *oS++ = x;
        if (oS == oT)
            flush();
    }
    void pstr(const char *s) {
        int __len = strlen(s);
        for (__f = 0; __f < __len; ++__f)
            pc(s[__f]);
    }
    void gstr(char *s) {
        for (_c = Gc(); _c < 32 || _c > 126 || _c == ' ';)
            _c = Gc();
        for (; _c > 31 && _c < 127 && _c != ' ' && _c != '\n' && _c != '\r';
             ++s, _c = Gc())
            *s = _c;
        *s = 0;
    }
    template <class I> bool read(I &x) {
        _eof = 0;
        for (__f = 1, _c = Gc(); (_c < '0' || _c > '9') && !_eof; _c = Gc()) {
            if (_c == '-')
                __f = -1;
            _eof |= _c == EOF;
        }
        for (x = 0; _c <= '9' && _c >= '0' && !_eof; _c = Gc()) {
            x = x * 10 + (_c & 15), _eof |= _c == EOF;
        }
        x *= __f;
        return !_eof;
    }
    template <class I> void print(I x) {
        if (!x)
            pc('0');
        if (x < 0) {
            pc('-');
            x = -x;
        }
        while (x) {
            qu[++qr] = x % 10 + '0', x /= 10;
        }
        while (qr)
            pc(qu[qr--]);
    }
    struct Flusher_ {
        ~Flusher_() { flush(); }
    } io_flusher_;
} // namespace IO
using IO::gc;
using IO::gstr;
using IO::pc;
using IO::print;
using IO::pstr;
using IO::read;

class SATT {
private: /* const */
    static constexpr const int MAX = 2e9;

public: /* Types */
    int rt;
    struct tag {
        int k, b;
        tag() { k = 1, b = 0; }
        tag(int k, int b) : k(k), b(b) {}
        bool empty() { return (k == 1 && b == 0); }
        friend tag operator*(const tag &x, const tag &y) {
            return {x.k * y.k, x.b * y.k + y.b};
        }
        friend int operator*(int x, const tag &y) { return x * y.k + y.b; }
    };
    struct dat {
        int sz, mn, mx, sm;
        dat() { sz = sm = 0, mx = -MAX, mn = MAX; }
        ~dat() = default;
        dat(int sz, int mn, int mx, int sm) : sz(sz), mx(mx), sm(sm), mn(mn) {};
        friend dat operator*(const dat &x, const dat &y) {
            return {x.sz + y.sz, min(x.mn, y.mn), max(x.mx, y.mx), x.sm + y.sm};
        }
        friend dat operator*(const dat &x, const tag &y) {
            return {x.sz, x.mn == MAX ? MAX : x.mn * y.k + y.b,
                    x.mx == -MAX ? -MAX : x.mx * y.k + y.b,
                    x.sm * y.k + x.sz * y.b};
        }
    };
    struct node {
        int son[3], fa, val;
        dat pth, str;
        tag tpth, tstr;
        int rev;
        node() {
            son[0] = son[1] = son[2] = fa = val = 0;
            pth = str = dat();
        }
        ~node() = default;
    };

public: /* Basic */
    vector<node> st;
    vector<int> trash;
    SATT() { st.push_back(node()); }
    SATT(int n) {
        st.reserve(4 * n + 1), trash.reserve(4 * n + 1), st.resize(n + 1);
    }
    ~SATT() = default;
    int newnode() {
        if (!trash.empty()) {
            int top = trash.back();
            trash.pop_back();
            return top;
        }
        st.push_back(node());
        return st.size() - 1;
    }
    void clear(int x) {
        st[x] = node();
        trash.push_back(x);
    }
    node &operator[](int x) { return st[x]; }

private: /* Aux */
    int &son(int x, int y) { return st[x].son[y]; }
    int &fa(int x) { return st[x].fa; }
    int &val(int x) { return st[x].val; }
    dat &pth(int x) { return st[x].pth; }
    dat &str(int x) { return st[x].str; }
    tag &tpth(int x) { return st[x].tpth; }
    tag &tstr(int x) { return st[x].tstr; }
    int &rev(int x) { return st[x].rev; }
    bool isrt(int x) { return (x != son(fa(x), 0)) && (x != son(fa(x), 1)); }
    bool dir(int x) { return x == son(fa(x), 1); }
    void setf(int x, int f, int typ) {
        if (x)
            fa(x) = f;
        son(f, typ) = x;
    }

private: /* Push */
    void trev(int x) {
        swap(son(x, 0), son(x, 1));
        rev(x) ^= 1;
    }
    void ptht(int x, tag t) {
        val(x) = val(x) * t;
        tpth(x) = tpth(x) * t;
        pth(x) = pth(x) * t;
    }
    void strt(int x, tag t) {
        tstr(x) = tstr(x) * t;
        str(x) = str(x) * t;
    }
    void pushup(int &x, int typ) {
        if (typ)
            str(x) = str(son(x, 0)) * str(son(x, 1)) * str(son(x, 2)) *
                     pth(son(x, 2));
        else {
            str(x) = str(son(x, 0)) * str(son(x, 1)) * str(son(x, 2));
            pth(x) = pth(son(x, 0)) * pth(son(x, 1)) *
                     dat(1, val(x), val(x), val(x));
        }
    }
    void pushdown(int &x, int typ) {
        if (typ) {
            if (!tstr(x).empty()) {
                strt(son(x, 0), tstr(x));
                strt(son(x, 1), tstr(x));
                strt(son(x, 2), tstr(x));
                ptht(son(x, 2), tstr(x));
                tstr(x) = tag();
            }
        } else {
            if (rev(x)) {
                trev(son(x, 0));
                trev(son(x, 1));
                rev(x) = 0;
            }
            if (!tpth(x).empty()) {
                ptht(son(x, 0), tpth(x));
                ptht(son(x, 1), tpth(x));
                tpth(x) = tag();
            }
            if (!tstr(x).empty()) {
                strt(son(x, 0), tstr(x));
                strt(son(x, 1), tstr(x));
                strt(son(x, 2), tstr(x));
                tstr(x) = tag();
            }
        }
    }
    void pdrt(int x, int typ) {
        if (!isrt(x))
            pdrt(fa(x), typ);
        pushdown(x, typ);
    }

private: /* Rot */
    void rotate(int x, int typ) {
        int f = fa(x), g = fa(f), lr = dir(x), y = son(x, lr ^ 1);
        if (g)
            son(g, son(g, 2) == f ? 2 : dir(f)) = x;
        son(x, lr ^ 1) = f;
        son(f, lr) = y;
        if (y)
            fa(y) = f;
        fa(f) = x;
        fa(x) = g;
        pushup(f, typ);
        pushup(x, typ);
    }
    void splay(int x, int typ, int goal = 0) {
        pdrt(x, typ);
        for (int i; i = fa(x), !isrt(x) && i != goal; rotate(x, typ))
            if (fa(i) != goal && !isrt(i))
                rotate(dir(x) ^ dir(i) ? x : i, typ);
    }

private: /* Access */
    void del(int x) {
        
        setf(son(x, 2), fa(x), 1);
        if (son(x, 0)) {
            int ls = son(x, 0);
            pushdown(ls, 1);
            while (son(ls, 1)) {
                ls = son(ls, 1);
                pushdown(ls, 1);
            }
            splay(ls, 1, x);
            setf(son(x, 1), ls, 1);
            setf(ls, fa(x), 2);
            pushup(ls, 1);
            pushup(fa(x), 0);
        } else
            setf(son(x, 1), fa(x), 2);
        clear(x);
    }
    void splice(int x) {
        splay(x, 1);
        int g = fa(x);
        splay(g, 0);
        pushdown(x, 1);
        if (son(g, 1)) {
            swap(fa(son(x, 2)), fa(son(g, 1)));
            swap(son(x, 2), son(g, 1));
        } else
            del(x);
        pushup(x, 1);
        pushup(g, 0);
    }
    void access(int x) {
        splay(x, 0);
        int y = x;
        if (son(x, 1)) {
            int nd = newnode();
            setf(son(x, 2), nd, 0);
            setf(son(x, 1), nd, 2);
            son(x, 1) = 0;
            setf(nd, x, 2);
            pushup(nd, 1);
            pushup(x, 0);
        }
        while (fa(x)) {
            splice(fa(x));
            splay(x, 0);
            pushup(x, 0);
        }
        // splay(y, 0);Config options are available globally in the config as userOptions
    }
    void makeroot(int x) {
        access(x);
        trev(x);
    }

    void expose(int x, int y) {
        makeroot(x);
        access(y);
    }
    int findrt(int x) {
        access(x);
        while (son(x, 0)) {
            pushdown(x, 0);
            x = son(x, 0);
        }
        splay(x, 0);
        return x;
    }

public: /* Link-Cut */
    void changeroot(int x) { makeroot(rt = x); }
    void link(int x, int y) {
        access(x);
        makeroot(y);
        setf(y, x, 1);
        pushup(x, 0);
    }
    void cut(int x, int y) {
        expose(x, y);
        clear(son(x, 1));
        fa(x) = son(y, 0) = 0;
        pushup(y, 0);
    }
    int cutfa(int x) {
        access(x);
        int y = son(x, 0);
        while (son(y, 1))
            pushdown(y, 0), y = son(y, 1);
        son(x, 0) = fa(son(x, 0)) = 0;
        pushup(x, 0);
        return y;
    }
    void changefa(int x, int y) {
        if (x == y || x == rt)
            return;
        int z = cutfa(x);
        if (findrt(x) == findrt(y))
            link(x, z);
        else
            link(x, y);
        makeroot(rt);
    }

public: /* Query */
    dat qpth(int x, int y) {
        expose(x, y);
        dat ans = pth(y);
        makeroot(rt);
        return ans;
    }
    dat qstr(int x) {
        access(x);
        dat ans = str(son(x, 2)) * dat(1, val(x), val(x), val(x));
        makeroot(rt);
        return ans;
    }

public: /* Update */
    void upth(int x, int y, tag t) {
        expose(x, y);
        ptht(y, t);
        makeroot(rt);
    }
    void ustr(int x, tag t) {
        access(x);
        strt(son(x, 2), t);
        val(x) = val(x) * t;
        pushup(x, 0);
        makeroot(rt);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int n, q;
    read(n), read(q);
    vector<pair<int, int>> edge(n);
    SATT tril(n);
    for (int i = 1; i < n; i++)
        read(edge[i].first), read(edge[i].second);
    for (int i = 1; i <= n; i++) {
        int x;
        read(x);
        tril[i].val = x;
    }
    for (int i = 1; i < n; i++)
        tril.link(edge[i].first, edge[i].second);
    int root;
    read(root);
    tril.changeroot(root);
    while (q--) {
        int op;
        read(op);
        if (op == 0 || op == 5) {
            int x, y;
            read(x), read(y);
            tril.ustr(x, SATT::tag(op != 0, y));
        } else if (op == 1) {
            int x;
            read(x);
            tril.changeroot(x);
        } else if (op == 3 || op == 4 || op == 11) {
            int x;
            read(x);
            SATT::dat ans = tril.qstr(x);
            if (op == 3)
                print(ans.mn), pc('\n');
            else if (op == 4)
                print(ans.mx), pc('\n');
            else
                print(ans.sm), pc('\n');
        } else if (op == 2 || op == 6) {
            int x, y, z;
            read(x), read(y), read(z);
            tril.upth(x, y, SATT::tag(op != 2, z));
        } else if (op == 7 || op == 8 || op == 10) {
            int x, y;
            read(x), read(y);
            SATT::dat ans = tril.qpth(x, y);
            if (op == 7)
                print(ans.mn), pc('\n');
            else if (op == 8)
                print(ans.mx), pc('\n');
            else
                print(ans.sm), pc('\n');
        } else if (op == 9) {
            int x, y;
            read(x), read(y);
            tril.changefa(x, y);
        }
    }
    return 0;
}
C++
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

namespace IO {
    const int __SIZE = (1 << 20) + 1;
    char ibuf[__SIZE], *iS, *iT, obuf[__SIZE],
        *oS = obuf, *oT = oS + __SIZE - 1, _c, qu[55];
    int __f, qr, _eof;
#define Gc()                                                                   \
    (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin),             \
                 (iS == iT ? EOF : *iS++))                                     \
              : *iS++)
    void flush() {
        fwrite(obuf, 1, oS - obuf, stdout);
        oS = obuf;
    }
    void gc(char &x) {
        x = Gc();
    }
    void pc(char x) {
        *oS++ = x;
        if (oS == oT)
            flush();
    }
    void pstr(const char *s) {
        int __len = strlen(s);
        for (__f = 0; __f < __len; ++__f)
            pc(s[__f]);
    }
    void gstr(char *s) {
        for (_c = Gc(); _c < 32 || _c > 126 || _c == ' ';)
            _c = Gc();
        for (; _c > 31 && _c < 127 && _c != ' ' && _c != '\n' && _c != '\r';
             ++s, _c = Gc())
            *s = _c;
        *s = 0;
    }
    template <class I> bool read(I &x) {
        _eof = 0;
        for (__f = 1, _c = Gc(); (_c < '0' || _c > '9') && !_eof; _c = Gc()) {
            if (_c == '-')
                __f = -1;
            _eof |= _c == EOF;
        }
        for (x = 0; _c <= '9' && _c >= '0' && !_eof; _c = Gc()) {
            x = x * 10 + (_c & 15), _eof |= _c == EOF;
        }
        x *= __f;
        return !_eof;
    }
    template <class I> void print(I x) {
        if (!x)
            pc('0');
        if (x < 0) {
            pc('-');
            x = -x;
        }
        while (x) {
            qu[++qr] = x % 10 + '0', x /= 10;
        }
        while (qr)
            pc(qu[qr--]);
    }
    struct Flusher_ {
        ~Flusher_() { flush(); }
    } io_flusher_;
} // namespace IO
using IO::gstr;
using IO::pc;
using IO::print;
using IO::pstr;
using IO::read;

class DynamicCactus {
private: /* const */
    static constexpr const int MAX = INT_MAX;

public: /* Types */
    struct tag {
        int tlaz, plaz;
        tag() { tlaz = plaz = 0; }
        tag(int tlaz, int plaz) : tlaz(tlaz), plaz(plaz) {}
        bool empty() { return (tlaz == 0 && plaz == 0); }
        tag tree() { return {tlaz, 0}; }
        tag move() { return {tlaz, tlaz}; }
        friend tag operator*(const tag &x, const tag &y) {
            return {x.tlaz + y.tlaz, x.plaz + y.plaz};
        }
    };
    struct dat {
        int u, v, dis;
        int tsiz, tmin, psiz;
        ll tsum, psum;
        int pmin;
        bool flag;
        dat() {
            u = v = dis = tsiz = psiz = 0, tmin = pmin = INT_MAX;
            tsum = psum = 0, flag = 0;
        }
        dat(int u, int v, int dis, int tsiz, int tmin, ll tsum, int psiz,
            int pmin, ll psum, bool flag)
            : u(u), v(v), dis(dis), tsiz(tsiz), tmin(tmin), psiz(psiz),
              tsum(tsum), psum(psum), pmin(pmin), flag(flag) {}
        void rev() { swap(u, v); }
        void insert(int w) { psiz++, pmin = min(pmin, w), psum += w; }
        dat move() const {
            return {u, v, dis, tsiz + psiz, min(tmin, pmin), tsum + psum,
                    0, INT_MAX, 0, flag};
        }
        friend dat operator*(const dat &x, const tag &y) {
            return {x.u, x.v, x.dis,
                    x.tsiz, x.tmin == INT_MAX ? INT_MAX : x.tmin + y.tlaz,
                    x.tsum + 1ll * x.tsiz * y.tlaz,
                    x.psiz, x.pmin == INT_MAX ? INT_MAX : x.pmin + y.plaz,
                    x.psum + 1ll * x.psiz * y.plaz, x.flag};
        }
    };
    struct node {
        char type, rev, cir;
        int son[3], fa, val;
        dat f;
        tag laz;
        node() { type = rev = cir = 0, son[0] = son[1] = son[2] = fa = val = 0; }
    };

public: /* Basic */
    vector<node> st;
    vector<int> trash;
    DynamicCactus(int n) {
        st.reserve(5 * n + 1024);
        trash.reserve(n + 64);
        st.resize(n + 1), st[0].f.tmin = st[0].f.pmin = MAX;
        for (int i = 1; i <= n; i++)
            type(i) = 'c';
    }
    int newnode(char c) {
        int id;
        if (!trash.empty())
            id = trash.back(), trash.pop_back();
        else
            id = st.size(), st.push_back(node());
        return type(id) = c, id;
    }
    void clear(int x) { st[x] = node(), trash.push_back(x); }
    node &operator[](int x) { return st[x]; }
    void setval(int x, int w) { val(x) = w, pushup(x); }

private: /* Aux */
    int &son(int x, int y) { return st[x].son[y]; }
    int &fa(int x) { return st[x].fa; }
    int &val(int x) { return st[x].val; }
    char &rev(int x) { return st[x].rev; }
    char &cir(int x) { return st[x].cir; }
    char &type(int x) { return st[x].type; }
    dat &f(int x) { return st[x].f; }
    tag &laz(int x) { return st[x].laz; }
    int &ls(int x) { return son(x, 0); }
    int &rs(int x) { return son(x, 1); }
    int &ms(int x) { return son(x, 2); }
    bool isrt(int x) { return type(x) != type(fa(x)); }
    bool dir(int x) { return son(fa(x), 1) == x; }
    void setf(int x, int f, int typ) {
        if (x)
            fa(x) = f;
        son(f, typ) = x;
    }

private: /* Push */
    dat compress(const dat &a, const dat &b, int w) {
        return {a.u, b.v, a.dis + b.dis,
                a.tsiz + b.tsiz, min(a.tmin, b.tmin), a.tsum + b.tsum,
                a.psiz + b.psiz + 1, min({a.pmin, b.pmin, w}),
                a.psum + b.psum + w, a.flag || b.flag};
    }
    dat twist(dat a, dat b) {
        if (a.dis > b.dis)
            swap(a, b);
        if (a.dis == b.dis)
            return {a.u, a.v, a.dis,
                    a.tsiz + b.tsiz, min(a.tmin, b.tmin), a.tsum + b.tsum,
                    a.psiz + b.psiz, min(a.pmin, b.pmin), a.psum + b.psum, 1};
        b = b.move();
        return {a.u, a.v, a.dis,
                a.tsiz + b.tsiz, min(a.tmin, b.tmin), a.tsum + b.tsum,
                a.psiz, a.pmin, a.psum, a.flag};
    }
    dat rake(const dat &a, const dat &b) {
        return {a.u, a.v, a.dis,
                a.tsiz + b.tsiz, min(a.tmin, b.tmin), a.tsum + b.tsum,
                a.psiz, a.pmin, a.psum, a.flag};
    }
    void pushup(int x) {
        if (type(x) == 'c') {
            if (ls(x) && rs(x)) {
                if (ms(x))
                    f(x) = compress(f(ls(x)), rake(f(rs(x)), f(ms(x))), val(x));
                else
                    f(x) = compress(f(ls(x)), f(rs(x)), val(x));
            } else {
                if (ls(x) || rs(x))
                    f(x) = f(ls(x) | rs(x));
                else
                    f(x) = {x, x, 0, 0, MAX, 0, 0, MAX, 0, 0};
                f(x).insert(val(x));
                if (ms(x))
                    f(x) = rake(f(x), f(ms(x)));
            }
            cir(x) = cir(ls(x)) | cir(rs(x));
        } else if (type(x) == 'r') {
            f(x) = f(ms(x)).move();
            if (ls(x))
                f(x) = rake(f(x), f(ls(x)));
            if (rs(x))
                f(x) = rake(f(x), f(rs(x)));
        } else if (type(x) == 't') {
            f(x) = twist(f(ls(x)), f(rs(x)));
            cir(x) = 1;
        }
    }
    void pushrev(int x) {
        if (!x)
            return;
        f(x).rev();
        swap(ls(x), rs(x)), rev(x) ^= 1;
    }
    void pushlaz(int x, tag y) {
        if (!x)
            return;
        laz(x) = laz(x) * y;
        f(x) = f(x) * y;
        if (type(x) == 'c')
            val(x) += y.plaz;
    }
    void pushdown(int x) {
        if (rev(x)) {
            pushrev(ls(x));
            pushrev(rs(x));
            rev(x) = 0;
        }
        if (laz(x).empty())
            return;
        if (type(x) == 'c') {
            pushlaz(ls(x), laz(x));
            pushlaz(rs(x), laz(x));
            pushlaz(ms(x), laz(x).tree());
        } else if (type(x) == 'r') {
            int w = laz(x).tlaz;
            for (int c = 0; c < 3; c++)
                pushlaz(son(x, c), {w, 0});
            pushlaz(ms(x), {0, w});
        } else if (type(x) == 't') {
            int w = f(ls(x)).dis - f(rs(x)).dis;
            pushlaz(ls(x), w <= 0 ? laz(x) : laz(x).move());
            pushlaz(rs(x), w >= 0 ? laz(x) : laz(x).move());
        }
        laz(x) = tag();
    }
    void pdrt(int x) {
        if (!isrt(x))
            pdrt(fa(x));
        pushdown(x);
    }

private: /* Rot */
    void rotate(int x) {
        int y = fa(x), z = fa(y), k = dir(x);
        if (z)
            *find(st[z].son, st[z].son + 3, y) = x;
        fa(x) = z;
        setf(son(x, !k), y, k), setf(y, x, !k);
        pushup(y);
    }
    void splay(int x) {
        pdrt(x);
        for (int y; y = fa(x), !isrt(x); rotate(x))
            if (!isrt(y))
                rotate(dir(x) ^ dir(y) ? x : y);
        pushup(x);
    }

private: /* Access */
    void del(int x) {
        int y = fa(x);
        pushdown(x);
        if (ls(x)) {
            int z = ls(x);
            fa(z) = 0;
            for (; rs(z); z = rs(z))
                pushdown(z);
            splay(z);
            setf(rs(x), z, 1);
            pushup(z);
            setf(z, y, 2);
        } else
            setf(rs(x), y, 2);
        pushup(y), clear(x);
    }
    void splice(int x) {
        splay(x);
        int y = fa(x);
        splay(y);
        pushdown(y), pushdown(x);
        if (fa(y) && type(fa(y)) == 't')
            return retwist(y, ms(x));
        int a = ms(x), b = rs(y);
        setf(a, y, 1);
        setf(b, x, 2);
        pushup(x);
        if (!b)
            del(x);
        else
            pushup(y);
    }
    void retwist(int x, int p = 0) {
        int y = fa(x);
        splay(fa(y));
        pushdown(y);
        int a = f(x).u, b = f(x).v;
        splay(b);
        if (ls(b) == y) {
            splice(fa(b));
            return retwist(x, p);
        }
        splay(a);
        fa(rs(a)) = 0;
        splay(b), setf(b, a, 1), pushup(b);
        pushdown(a), pushdown(b);
        pushdown(y), pushdown(x);
        if (p)
            pushdown(ms(x));
        int k = dir(x);
        setf(ls(x), y, k);
        if (rs(b)) {
            int z = newnode('r');
            setf(rs(b), z, 2), rs(b) = 0;
            setf(ms(b), z, 0);
            pushup(z), setf(z, b, 2);
        }
        setf(son(y, !k), b, 0);
        pushrev(rs(x));
        setf(rs(x), b, 1);
        pushup(b), setf(b, y, !k);
        pushup(y), setf(y, x, 0);
        if (p)
            del(fa(p));
        setf(p, x, 1);
        pushup(x), setf(x, a, 1);
        pushup(a);
    }
    void access(int x) {
        splay(x);
        if ((!fa(x) || type(fa(x)) != 't') && rs(x)) {
            int y = newnode('r');
            pushdown(x);
            setf(ms(x), y, 0);
            setf(rs(x), y, 2), rs(x) = 0;
            setf(y, x, 2);
            pushup(y), pushup(x);
        }
        for (; splay(x), fa(x);) {
            if (type(fa(x)) == 'r')
                splice(fa(x));
            else
                retwist(x);
        }
    }
    void makeroot(int x) { access(x), pushrev(x); }
    int findroot(int x) {
        access(x);
        for (; ls(x); x = ls(x))
            pushdown(x);
        return splay(x), x;
    }

public: /* Link-Cut */
    bool link(int x, int y, int w) {
        if (x == y)
            return 0;
        makeroot(x);
        if (findroot(y) == x) {
            if (cir(x))
                return 0;
            fa(rs(x)) = 0;
            splay(y), setf(y, x, 1);
            int z = newnode('t'), e = newnode('b');
            f(e) = {x, y, w, 0, MAX, 0, 0, MAX, 0, 0};
            pushdown(y);
            setf(ls(y), z, 0);
            setf(e, z, 1);
            pushup(z), setf(z, y, 0);
            pushup(y), pushup(x);
        } else {
            access(x), access(y);
            int e = newnode('b');
            f(e) = {y, x, w, 0, MAX, 0, 0, MAX, 0, 0};
            setf(e, x, 0);
            pushup(x), setf(x, y, 1);
            pushup(y);
        }
        return 1;
    }
    bool cut(int x, int y, int w) {
        if (x == y)
            return 0;
        makeroot(x);
        if (findroot(y) != x)
            return 0;
        access(y);
        fa(ls(y)) = 0;
        splay(x), setf(x, y, 0), pushup(y);
        if (type(rs(x)) == 'b') {
            if (f(rs(x)).dis != w)
                return 0;
            clear(rs(x)), rs(x) = 0;
            fa(x) = ls(y) = 0;
            pushup(x), pushup(y);
            return 1;
        } else if (type(rs(x)) == 't') {
            int z = rs(x), k = 0;
            pushdown(z);
            if (type(son(z, k)) != 'b' || f(son(z, k)).dis != w)
                k = 1;
            if (type(son(z, k)) != 'b' || f(son(z, k)).dis != w)
                return 0;
            setf(son(z, !k), x, 1), pushup(x), pushup(y);
            clear(z);
            return 1;
        } else
            return 0;
    }

public: /* Query */
    pair<int, ll> pquery(int x, int y) {
        makeroot(x);
        if (findroot(y) != x)
            return {-1, -1};
        if (f(x).flag)
            return {-2, -2};
        return {f(x).pmin, f(x).psum};
    }
    pair<int, ll> tquery(int x, int y) {
        makeroot(x);
        if (findroot(y) != x)
            return {-1, -1};
        splay(y);
        return {min(f(ms(y)).tmin, val(y)), f(ms(y)).tsum + val(y)};
    }

public: /* Update */
    bool pupdate(int x, int y, int w) {
        makeroot(x);
        if (findroot(y) != x)
            return 0;
        if (f(x).flag)
            return 0;
        return pushlaz(x, {0, w}), 1;
    }
    bool tupdate(int x, int y, int w) {
        makeroot(x);
        if (findroot(y) != x)
            return 0;
        splay(y), val(y) += w;
        return pushlaz(ms(y), {w, 0}), pushup(y), 1;
    }
};

int main() {
    int n, m;
    read(n), read(m);
    DynamicCactus tril(n);
    for (int i = 1, x; i <= n; i++)
        read(x), tril.setval(i, x);
    for (int x, y, z; m--;) {
        static ll w;
        static char op[99];
        gstr(op), read(x), read(y);
        if (*op == 'l')
            read(z), pstr(tril.link(x, y, z) ? "ok\n" : "failed\n");
        else if (*op == 'c')
            read(z), pstr(tril.cut(x, y, z) ? "ok\n" : "failed\n");
        else if (*op == 'q' && op[5] == '1')
            tie(x, w) = tril.pquery(x, y), print(x), pc(' '), print(w), pc('\n');
        else if (*op == 'q')
            tie(x, w) = tril.tquery(x, y), print(x), pc(' '), print(w), pc('\n');
        else if (*op == 'a' && op[3] == '1')
            read(z), pstr(tril.pupdate(x, y, z) ? "ok\n" : "failed\n");
        else
            read(z), pstr(tril.tupdate(x, y, z) ? "ok\n" : "failed\n");
    }
    return 0;
}