제출 #543031

#제출 시각아이디문제언어결과실행 시간메모리
543031Danilo21Birthday gift (IZhO18_treearray)C++14
56 / 100
4034 ms43820 KiB
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define en '\n'
#define sp ' '
#define tb '\t'
#define ri(n) int n; cin >> n
#define rl(n) ll n; cin >> n
#define rs(s) string s; cin >> s
#define rc(c) char c; cin >> c
#define rv(v) for (auto &x : v) cin >> x
#define pven(v) for (auto x : v) cout << x << en
#define pv(v) for (auto x : v) cout << x << sp; cout << en
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define yes cout << "YES" << en
#define no cout << "NO" << en
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define ssort(a, b) if (a < b) swap(a, b)
#define bitcnt(a) __builtin_popcountll(a)
#define bithigh(a) 63-__builtin_clzll(a)
#define lg bithigh
ll highpow(ll a) { return 1LL << (ll)lg(a); }

using namespace std;

class Lca{
private:
    int n;
    vector<vector<int> > g, par;
    vector<int> in, out, depth;

    int dfs(int s, int p = -1, int d = 0, int t = 0){
        par[s][0] = p;
        in[s] = t;
        depth[s] = d;
        for (int u : g[s])
            if (u^p)
                t = dfs(u, s, d+1, t+1);
        return out[s] = t;
    }

public:
    Lca(int n = 0){
        this->n = n;
        g = vector<vector<int> >(n);
        par = vector<vector<int> >(n, vector<int>(lg(n)+1, -1));
        in = out = depth = vector<int>(n);
    }

    void edge(int u, int v){
        g[u].pb(v);
        g[v].pb(u);
    }

    void build(int root = 0){
        dfs(root);
        for (int d = 1; (1<<d) <= n; d++)
            for (int s = 0; s < n; s++)
                if (~par[s][d-1])
                    par[s][d] = par[par[s][d-1]][d-1];
    }

    int Par(int s, int d) const {
        if (!d) return s;
        return Par(par[s][lg(d)], d - highpow(d));
    }

    bool Ancestor(int s, int p) const {
        return in[s] >= in[p] && in[s] <= out[p];
    }

    int lca(int u, int v){
        if (depth[u] > depth[v]) swap(u, v);
        if (Ancestor(v, u)) return u;
        v = Par(v, depth[v] - depth[u]);
        for (int d = lg(n); ~d; d--){
            if (par[u][d]^par[v][d]){
                u = par[u][d];
                v = par[v][d];
            }
        }
        return par[u][0];
    }
};

Lca* lc;

struct node{

    int s;
    node(){ s = -1; }
    node(int u, int v){ s = lc->lca(u, v); }

    static node op(const node& a, const node& b){
        if (!~a.s) return b;
        if (!~b.s) return a;
        return node(a.s, b.s);
    }
};

struct seg{
    int s, l, r;
    seg(){ s = l = r = -1; }
    seg(int _s, int _l, int _r): s(_s), l(_l), r(_r){}
    static seg concate(const seg& a, const seg& b, bool f){
        if (!~a.s){
            if (!~b.s || !f) return seg();
            return b;
        }
        if (!~b.s){
            if (f) return seg();
            return a;
        }
        if (a.r+1 != b.l){
            if (f) return b;
            return a;
        }
        return seg(lc->lca(a.s, b.s), a.l, b.r);
    }
};

struct response{
    seg left, right, sol;
    bool solved = 0;
    response(){}
    response(int u, int l, int r, int s){
        left = right = seg(u, l, r);
        if (u==s){ sol = seg(u, l, r); solved = 1; }
    }
    response(const response& a, const response& b, int s){
        left = seg::concate(a.left, b.left, 0);
        right = seg::concate(a.right, b.right, 1);
        if (a.solved) sol = a.sol;
        if (b.solved) sol = b.sol;
        if (left.s == s) sol = left;
        if (right.s == s) sol = right;
        seg t = seg::concate(a.right, b.left, 0);
        if (t.s == s) sol = t;
        if (~sol.s) solved = 1;
    }
};

class segtree{
private:
    int n;
    vector<node> tree;

    void init(int n){
        this->n = highpow(n);
        if (bitcnt(n)>1) this->n <<= 1;
        tree = vector<node>(2*this->n);
    }

    node build(int s, int l, int r, int* arr){
        if (l==r) return tree[s] = node(arr[l], arr[l]);
        int m = (l + r)>>1;
        node a = build(2*s, l, m, arr);
        node b = build(2*s+1, m+1, r, arr);
        return tree[s] = node::op(a, b);
    }

    node update(int s, int l, int r, int pos, int u){
        if (pos < l || pos > r) return tree[s];
        if (l==r) return tree[s] = node(u, u);
        int m = (l + r)>>1;
        node a = update(2*s, l, m, pos, u);
        node b = update(2*s+1, m+1, r, pos, u);
        return tree[s] = node::op(a, b);
    }

    response query(int s, int l, int r, int ql, int qr, int u) const {
        if (ql > r || qr < l) return response();
        if (!lc->Ancestor(u, tree[s].s) && !lc->Ancestor(tree[s].s, u)) return response();
        if (l >= ql && r <= qr && lc->Ancestor(tree[s].s, u))
            return response(tree[s].s, l, r, u);
        if (l == r) return response();
        int m = (l + r)>>1;
        auto a = query(2*s, l, m, ql, qr, u);
        auto b = query(2*s+1, m+1, r, ql, qr, u);
        return response(a, b, u);
    }
public:
    segtree(int n, int* arr){ init(n); build(1, 0, this->n-1, arr); }
    void update(int pos, int u){ update(1, 0, n-1, pos, u); }
    array<int, 2> query(int l, int r, int u){
        auto q = query(1, 0, n-1, l, r, u);
        if (!q.solved) return {-2, -2};
        return {q.sol.l, q.sol.r};
    }
};

const ll LINF = 4e18;
const int mxN = 2e5+10, INF = 2e9, mod = (1 ? 1e9+7 : 998244353);
int n, m, q, a[2*mxN];
segtree* st;

void Solve(){

    cin >> n >> m >> q;
    lc = new Lca(n);
    for (int i = 1; i < n; i++){
        ri(u); ri(v);
        u--; v--;
        lc->edge(u, v);
    }
    lc->build();
    memset(a, -1, sizeof(a));
    for (int i = 0; i < m; i++)
        cin >> a[i], a[i]--;
    st = new segtree(m, a);
    while (q--){
        ri(t); t--;
        if (t){
            ri(l); ri(r); ri(s);
            l--; r--; s--;
            auto ans = st->query(l, r, s);
            cout << ans[0]+1 << sp << ans[1]+1 << en;
        }
        else{
            ri(i); ri(s);
            i--; s--;
            st->update(i, s);
        }
    }
}

int main(){

    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    cout << setprecision(12) << fixed;
    cerr << setprecision(12) << fixed;
    cerr << "Started!" << endl;

    int t = 1;
    //cin >> t;
    while (t--)
        Solve();

    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

treearray.cpp: In function 'long long int highpow(long long int)':
treearray.cpp:26:22: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
   26 | #define bithigh(a) 63-__builtin_clzll(a)
      |                      ^
treearray.cpp:27:12: note: in expansion of macro 'bithigh'
   27 | #define lg bithigh
      |            ^~~~~~~
treearray.cpp:28:38: note: in expansion of macro 'lg'
   28 | ll highpow(ll a) { return 1LL << (ll)lg(a); }
      |                                      ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...