답안 #857222

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
857222 2023-10-05T15:29:30 Z dknguyen176 Birthday gift (IZhO18_treearray) C++14
0 / 100
79 ms 165836 KB
#include <bits/stdc++.h>

#define mp make_pair

using namespace std;

const int N = 2e5+5;

int n, m, q;
vector<int> adj[N];
int a[N];

int cnt = 0;
int be[N], en[N], h[N], par[N];
pair<int, int> b[N*2];

pair<int, int> rmq[20][N*2];

struct LCANode {
    int all, left, right, lpos, rpos;
    multiset<int> pos;
};
LCANode it[N*4];

int ans_x, ans_y;

void dfs(int u) {
    b[++cnt] = mp(h[u], u);
    be[u] = en[u] = cnt;

    for (int &v: adj[u]) if (v != par[u]) {
        par[v] = u;
        h[v] = h[u] + 1;
        dfs(v);

        b[++cnt] = mp(h[u], u);
        en[u] = cnt;
    }
}
void build_rmq() {
    for (int i = 1; i <= cnt; ++i) {
        rmq[0][i] = b[i];
    }
    for (int j = 1; (1 << j) <= cnt; ++j) {
        for (int i = 1; i + (1 << j-1) <= cnt; ++i) {
            rmq[j][i] = min(rmq[j-1][i], rmq[j-1][i + (1 << j-1)]);
        }
    }
}
int get_lca(int u, int v) {
    int l = be[u], r = be[v];
    if (l > r) swap(l, r);
    int k = log2(r-l+1);
    auto mi = min(rmq[k][l], rmq[k][r - (1<<k) +1]);
    return mi.second;
}
void build(int k, int l, int r) {
    if (l == r) {
        it[k].all = a[r];
        it[k].pos.insert(be[a[r]]);
        return;
    }

    int m = (l + r) >> 1;
    build(k*2, l, m);
    build(k*2+1, m+1, r);
    it[k].all = get_lca(it[k*2].all, it[k*2+1].all);
    for (int i = l; i <= r; ++i) {
        it[k].pos.insert(be[a[i]]);
    }
//    cout << "build " << l << ' ' << r << ' ' << it[k].all << endl;
}
void update(int k, int l, int r, int i, int u, int old_u) {
    if (l == r) {
        it[k].all = u;
        it[k].pos.erase(it[k].pos.find(be[old_u]));
        it[k].pos.insert(be[u]);
        return;
    }

    int m = (l + r) >> 1;
    if (i <= m) update(k*2, l, m, i, u, old_u);
    else update(k*2+1, m+1, r, i, u, old_u);

    it[k].all = get_lca(it[k*2].all, it[k*2+1].all);
    it[k].pos.erase(it[k].pos.find(be[old_u]));
    it[k].pos.insert(be[u]);
}
LCANode merge_node(LCANode lnode, LCANode rnode, int u) {
    if (lnode.all && rnode.all) {
        int lca = get_lca(lnode.all, rnode.all);
        return LCANode{lca, lca, lca, rnode.lpos, lnode.rpos};
    }

    LCANode node;
    node.all = 0;
    node.left = lnode.left;
    node.right = rnode.right;
    node.lpos = lnode.lpos;
    node.rpos = rnode.rpos;
    if (lnode.all && rnode.left) {
        node.left = get_lca(lnode.all, rnode.left);
        node.lpos = rnode.lpos;
    }
    if (rnode.all && lnode.right) {
        node.right = get_lca(rnode.all, lnode.right);
        node.rpos = lnode.rpos;
    }
    if (lnode.right && rnode.left) {
        int lca = get_lca(lnode.all, rnode.left);
        if (lca == u) {
            ans_x = lnode.rpos;
            ans_y = rnode.lpos;
        }
    }
    return node;
}
LCANode get(int k, int l, int r, int ql, int qr, int u) {
    if (qr < l || r < ql || *it[k].pos.lower_bound(be[u]) > en[u]) {
        return LCANode{0, 0, 0, l-1, r+1};
    }
    if (ql <= l && r <= qr && get_lca(it[k].all, u) == u) {
//        cout << "a " << l << ' ' << r << ' ' << it[k].all << endl;
        int lca = it[k].all;
        if (lca == u) {
            ans_x = l;
            ans_y = r;
        }
        return LCANode{lca, lca, lca, r, l};
    }

    int m = (l + r) >> 1;
    LCANode lnode = get(k*2, l, m, ql, qr, u);
    if (ans_x) return LCANode{};
    LCANode rnode = get(k*2+1, m+1, r, ql, qr, u);
    if (ans_x) return LCANode{};
    LCANode node = merge_node(lnode, rnode, u);
//    cout << l << ' ' << r << ' ' << node.all << ' ' << node.left << ' ' << node.right << endl;
//    cout << lnode.all << ' ' << lnode.left << ' ' << lnode.right << endl;
//    cout << rnode.all << ' ' << rnode.left << ' ' << rnode.right << endl;
    return node;
}
int main()
{
//    freopen("a.inp", "r", stdin);

    cin >> n >> m >> q;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= m; ++i) {
        cin >> a[i];
    }

    dfs(1);
    build_rmq();

//    for (int i = 1; i <= cnt; ++i) cout << b[i].second << ' ';
//    cout << endl;
//    cout << "lca " << get_lca(4, 5) << endl;

    build(1, 1, m);

    for (int i = 1; i <= q; ++i) {
        int t;
        cin >> t;
        if (t == 1) {
            int pos, u;
            cin >> pos >> u;
            update(1, 1, m, pos, u, a[pos]);
            a[pos] = u;
        } else {
            int l, r, u;
            cin >> l >> r >> u;
            ans_x = ans_y = 0;
            get(1, 1, m, l, r, u);
            if (ans_x) cout << ans_x << ' ' << ans_y << '\n';
            else cout << -1 << ' ' << -1 << '\n';
        }
    }

    return 0;
}

Compilation message

treearray.cpp: In function 'void build_rmq()':
treearray.cpp:45:36: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   45 |         for (int i = 1; i + (1 << j-1) <= cnt; ++i) {
      |                                   ~^~
treearray.cpp:46:62: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   46 |             rmq[j][i] = min(rmq[j-1][i], rmq[j-1][i + (1 << j-1)]);
      |                                                             ~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 73564 KB n=5
2 Runtime error 79 ms 165836 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 73564 KB n=5
2 Runtime error 79 ms 165836 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 73564 KB n=5
2 Runtime error 79 ms 165836 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 73564 KB n=5
2 Runtime error 79 ms 165836 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -