CSES Path Queries I I
// compile: make data
// run: time ./data < big.in > big.out && diff big.out big.ans
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
#else
#define debug(x...)
#define Debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define NaN (int)(0x8b88e1d0595d51d1)
#define double long double
#define int long long
#define MAXN 200010
 
struct tree {
    int n;
    vector<vector<int>> e;
    struct node {
        vector<node*> ch;
        node *parent = nullptr;
        int size, depth, val, dfn;
        node *heavy = nullptr, *top = nullptr;
    };
    vector<node> vertex;
    node *root;
    tree(int V) {
        n = V;
        e.resize(n);
        vertex.resize(n);
    }
    void add_edge(int u, int v) {
        e[u].push_back(v), e[v].push_back(u);
    }
    int ind(node *x) {return x - &vertex[0]; }
    void build(int rt = 0) {
        function<void(int, int)> dfs = [&](int u, int pre) {
            node *x = &vertex[u];
            x->size = 1;
            x->depth = x==root ? 0 : x->parent->depth + 1;
            for (int v: e[u]) {
                if (v == pre) continue;
                node *y = &vertex[v];
                x->ch.push_back(y);
                y->parent = x;
                dfs(v, u);
                x->size += y->size;
            }
        };
        root = &vertex[rt];
        dfs(rt, -1);
    }
    vector<int> rank;
    void decomposition() {
        rank.resize(n);
        function<void(node*)> dfs1 = [&](node *x) {
            for (auto y: x->ch) {
                dfs1(y);
                if (!x->heavy || y->size > x->heavy->size) x->heavy = y;
            }
        };
        int total = 0;
        function<void(node*, node*)> dfs2 = [&](node *x, node *top) {
            vertex[ind(x)].dfn = total, rank[total++] = ind(x);
            x->top = top;
            if (x->heavy) dfs2(x->heavy, top);
            for (auto y: x->ch) {
                if (y == x->heavy) continue;
                dfs2(y, y);
                if (!x->heavy || y->size > x->heavy->size) x->heavy = y;
            }
        };
        dfs1(root);
        dfs2(root, root);
    }
    vector<pair<int, int>> path(int u, int v) {
        vector<pair<int, int>> res;
        node *x = &vertex[u], *y = &vertex[v];
        while (x->top != y->top) {
            if (x->top->depth < y->top->depth) swap(x, y);
            res.push_back({x->top->dfn, x->dfn});
            x = x->top->parent;
        }
        res.push_back({min(x->dfn, y->dfn), max(x->dfn, y->dfn)});
        return res;
    }
    int solve(int u, int v, auto func) {
        node *x = &vertex[u], *y = &vertex[v];
        int ans = -INF;
        while (x->top != y->top) {
            if (x->top->depth < y->top->depth) swap(x, y);
            cmax(ans, func(x->top->dfn, x->dfn));
            // res.push_back({x->top->dfn, x->dfn});
            x = x->top->parent;
        }
        cmax(ans, func(min(x->dfn, y->dfn), max(x->dfn, y->dfn)));
        return ans;
    }
    void graphviz_dump(int plus = 0, string filename = "graph.dot") {
        string vlabel = "", elabel = "";
        // vlabel = " color=\"red\"";
        elabel = " color=\"red\"";
        set<int> vset; set<pair<int, int>> eset;
        rep(i, 0, n) if (vertex[i].heavy) vset.insert(ind(vertex[i].heavy)), eset.insert({i, ind(vertex[i].heavy)});
        FILE *f = fopen(filename.c_str(), "w");
        fprintf(f, "digraph Tree {\n");
        rep(i, 0, n) {
            node *x = &vertex[i];
            fprintf(f, "    %lld;\n", vset.count(i) ? vlabel.c_str() : "");
        }
        rep(i, 0, n) {
            node *x = &vertex[i];
            for (auto y: x->ch) {
                fprintf(f, "    %ld -> %ld[arrowhead=\"none\"%s];\n", x - &vertex[0], y - &vertex[0], eset.count({i, ind(y)}) ? elabel.c_str() : "");
            }
        }
        fprintf(f, "}\n");
    }
};
 
template <class T> struct segtree {
    struct node {
        int l, r;
        T val;
        node *ch[2];
    };
    int cnt = 0;
    T DEFAULT_VALUE;
    node *newnode(int l, int r) {
        node *x = (l==r) ? &datalist[l] : &datalist[cnt++];
        // node *x = &datalist[cnt++];
        x->l = l, x->r = r;
        x->ch[0] = x->ch[1] = nullptr;
        return x;
    }
    vector<int> a;
    vector<node> datalist;
    node *root;
    segtree(vector<int> arr) {
        cnt = sz(arr);
        a = arr, build();
    }
    void pushup(node *x) {
        if (!x) return;
        if (!x->ch[0] && !x->ch[1]) return;
        if (x->ch[0] && x->ch[1]) x->val = x->ch[0]->val + x->ch[1]->val;
        else x->val = x->ch[!x->ch[0]]->val;
    }
    T query(int ql, int qr) {
        if (qr - ql < 10) {
            T res = DEFAULT_VALUE;
            rep(i, ql, qr+1) res = res + datalist[i].val;
            return res;
        }
        function<T(node*)> dfs = [&](node *x) -> T {
            if (ql > x->r || qr < x->l) return DEFAULT_VALUE;
            if (x->l >= ql && x->r <= qr) return x->val;
            return dfs(x->ch[0]) + dfs(x->ch[1]);
        };
        return dfs(root);
    }
    template <typename U> void modify(int q, U val) {
        function<void(node*)> dfs = [&](node *x) -> void {
            if (q > x->r || q < x->l) return;
            if (x->l >= q && x->r <= q) {
                x->val.update(val);
                return;
            }
            dfs(x->ch[0]), dfs(x->ch[1]);
            pushup(x);
        };
        dfs(root);
    }
    void build() {
        datalist.resize(sz(a) * 2);
        function<node*(int, int)> dfs = [&](int l, int r) {
            node *x = newnode(l, r);
            if (r > l) x->ch[0] = dfs(l, (l+r) >> 1), x->ch[1] = dfs(((l+r)>>1) + 1, r);
            else x->val.set(a[l]);
            pushup(x);
            return x;
        };
        root = dfs(0, sz(a)-1);
    }
    void graphviz_dump(string filename = "graph.dot") {
        FILE *f = fopen(filename.c_str(), "w");
        fprintf(f, "digraph {\n");
        rep(i, 0, cnt) {
            node *x = &datalist[i];
            fprintf(f, "    %lld;\n");
        }
        rep(i, 0, cnt) {
            node *x = &datalist[i];
            if (x->ch[0]) fprintf(f, "    %lld -> %ld;\n", i, x->ch[0] - &datalist[0]);
            if (x->ch[1]) fprintf(f, "    %lld -> %ld;\n", i, x->ch[1] - &datalist[0]);
        }
        fprintf(f, "}\n");
    }
};
 
int32_t main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
 
    struct value {
        int maxv = -INF;
        value operator+(const value &other) const {
            return {max(maxv, other.maxv)};
        }
        void set(int rhs) {
            maxv = rhs;
        }
        void update(int val) {
            maxv = val;
        }
    };
    int n, q; cin >> n >> q;
    tree g(n);
    rep(i, 0, n) {
        int x; cin >> x;
        g.vertex[i].val = x;
    }
    rep(i, 0, n-1) {
        int u, v; cin >> u >> v;
        g.add_edge(u-1, v-1);
    }
    g.build(0);
    g.decomposition();
    vector<int> a(n);
    rep(i, 0, n) a[i] = g.vertex[g.rank[i]].val;
    segtree<value> t(a);
    while (q--) {
        int op; cin >> op;
        if (op == 1) {
            int u, k; cin >> u >> k;
            t.modify(g.vertex[u-1].dfn, k);
        }
        else if (op == 2) {
            int u, v; cin >> u >> v;
            int ans = -INF;
            auto ps = g.path(u-1, v-1);
            for (auto [l, r]: ps) cmax(ans, t.query(l, r).maxv);
            printf("%lld ", ans);
        }
    }
    printf("\n");
 
    return 0;
}
No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next