跳到正文

剪贴板

剪贴板 adp6bllm

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
using namespace std;
const int N = 6e5 + 7, md = 1e9 + 7;
vector<int> e[N], up[N], dn[N];
int cnt, lg[N * 4];
inline int mylg(int x)
{
    return lg[x];
}
int fa[N][21], dep[N], que[N * 2], pot, st[N * 2][22], dfn[N], son[N], top[N], d[N];
inline void dfs(int u, int pa)
{
    d[u] = dep[u] = dep[pa] + 1;
    fa[u][0] = pa; que[dfn[u] = ++pot] = u;
    for (register int i = 1; i <= 20; ++i)
        fa[u][i] = fa[fa[u][i - 1]][i - 1];
    for (register int v : e[u])
    {
        if (v == pa)
            continue;
        dfs(v, u);
        if (d[v] > d[u])
            son[u] = v, d[u] = d[v];
        que[++pot] = u;
    }
}
inline void dfs2(int u, int tp)
{
    top[u] = tp;
    if (u == tp)
    {
        for (register int i = 0, o = u; i <= d[u] - dep[u]; i++)
            up[u].push_back(o), o = fa[o][0];
        for (register int i = 0, o = u; i <= d[u] - dep[u]; i++)
            dn[u].push_back(o), o = son[o];
    }
    if (son[u])
        dfs2(son[u], tp);
    for (register int v : e[u])
        if (v != fa[u][0] && v != son[u])
            dfs2(v, v);
}
inline void build()
{
    for (register int i = 1; i <= pot; ++i)
        st[i][0] = que[i];
    for (register int i = 1; i <= 21; ++i)
        for (register int j = 1; j + (1 << i) - 1 <= pot; ++j)
            st[j][i] = (dep[st[j][i - 1]] < dep[st[j + (1 << (i - 1))][i - 1]] ? st[j][i - 1] : st[j + (1 << (i - 1))][i - 1]);
}
int nlc;
inline int lca(int u, int v)
{
    u = dfn[u], v = dfn[v];
    if (u > v)
        swap(u, v);
    int x = mylg(v - u + 1);
    int a = st[u][x], b = st[v - (1 << x) + 1][x];
    return (dep[a] < dep[b] ? a : b);
}
inline int kth(int u, int dis)
{
    if (!dis)
        return u;
    u = fa[u][mylg(dis)]; dis -= (1 << mylg(dis)); dis -= dep[u] - dep[top[u]]; u = top[u];
    if (u == 0)
        return 0;
    int ret = ((dis >= 0) ? up[u][dis] : dn[u][-dis]);
    return ret;
}
inline int dis(int u, int v)
{
    return dep[u] + dep[v] - 2 * dep[nlc = lca(u, v)];
}
inline int find_dis(int u, int v, int dis)
{
    int w = nlc;
    int d1 = dep[u] - dep[w], d2 = dep[v] - dep[w];
    if (d1 > dis)
        return kth(u, dis);
    else
        return kth(v, d1 + d2 - dis);
}
struct circle
{
    int u, r, u1;
    long long step;
    inline circle operator+ (const circle& b) const
    {
        circle c;
        if (!u1 && !b.u1)
        {
            int x = dis(u, b.u);
            if (x > r + b.r)
                c.u = find_dis(u, b.u, r), c.u1 = find_dis(b.u, u, b.r), c.r = 0, c.step = b.step + step + x - r - b.r;
            else
            {
                if (x + r <= b.r)
                    c = *this, c.step += b.step;
                else if (x + b.r <= r)
                    c = b, c.step += step;
                else
                {
                    int d = (r + b.r - x);
                    c.r = d / 2;
                    c.u = find_dis(u, b.u, r - c.r);
                    c.step = step + b.step;
                }
            }
        }
        else if (!b.u1)
        {
            int x = dis(u1, b.u);
            if (x > b.r)
                c.u = u, c.u1 = find_dis(b.u, u1, b.r), c.r = 0, c.step = b.step + step + x - b.r;
            else
                c = *this, c.step += b.step;
        }
        else if (!u1)
        {
            int x = dis(b.u, u);
            if (x > r)
                c.u1 = b.u1, c.u = find_dis(u, b.u, r), c.r = 0, c.step = b.step + step + x - r;
            else
                c = b, c.step += step;
        }
        else
        {
            c.u = u; c.u1 = b.u1;
            c.r = 0;
            c.step = step + b.step + dis(u1, b.u); 
        }
        return c;
    }
};
inline void add(int u, int v)
{
    ++cnt;
    e[u].push_back(cnt);
    e[cnt].push_back(u);
    e[v].push_back(cnt);
    e[cnt].push_back(v);
}
circle nw[N];
struct cat_tree
{
    circle tr[(1 << 20) * 20];
    int st[(1 << 20) * 2], ed[(1 << 20) * 2], pr[(1 << 20) * 2], pl[(1 << 20) * 2], sz, tot, rg[(1 << 20)];
    inline void build(int i, int l, int r)
    {
        pl[i] = l, pr[i] = r;
        st[i] = tot + 1;
        for (register int j = ((l + r) >> 1); j >= l; --j)
            ++tot, tr[tot] = (j == ((l + r) >> 1)) ? nw[j] : (nw[j] + tr[tot - 1]);
        for (register int j = ((l + r) >> 1) + 1; j <= r; ++j)
            ++tot, tr[tot] = (j == ((l + r) >> 1) + 1) ? nw[j] : (tr[tot - 1] + nw[j]);
        if (l != r)
            build((i << 1), l, ((l + r) >> 1)), build((i << 1 | 1), ((l + r) >> 1) + 1, r);
        else
            rg[r] = i;
    }
    inline void Build(int n)
    {
        tot = 0;
        int cnt = 1;
        while (cnt < n)
            cnt *= 2;
        sz = cnt;
        build(1, 1, sz);
    }
    inline circle query(circle u, int l, int r)
    {
        if (l == r)
            return u + nw[l];
        int yl = l, yr = r;
        (l = rg[l]), (r = rg[r]);
        int x = (l >> (mylg(l ^ r) + 1));
        return u + tr[st[x] + (((pl[x] + pr[x]) >> 1) - yl)] + tr[st[x] + ((pr[x] - pl[x] + 1) >> 1) + (yr - ((pl[x] + pr[x]) >> 1)) - 1];
    }
}t;
void gems(int c, int n, int m, std::vector<int> u, std::vector<int> v, std::vector<int> a, std::vector<int> d)
{
    lg[0] = -1;
    for (int i = 1; i < N * 4; i++)
        lg[i] = lg[i >> 1] + 1;
    cnt = n;
    for (register int i = 0; i < n - 1; ++i)
        add(u[i], v[i]);
    dfs(1, 0); dfs2(1, 1); build();
    for (register int i = 0; i < m; ++i)
        nw[i + 1] = {a[i], d[i] * 2, 0, 0};
    t.Build(m);
}
long long query(int x, int l, int r)
{
    circle I10v3cky = t.query({x, 0, 0, 0}, l, r);
    return I10v3cky.step / 2;
}