HDU 6162 Ch’s gift

树状数组加lca离线瞎搞了下。
并不会什么树链剖分和主席树。。

于是学了下树链剖分和主席树。。。

树链剖分加主席树的代码:

#include<bits/stdc++.h>
using namespace std;

#define pb push_back
typedef long long LL;
const int N = 1e5+5;

struct Node {
    int lc, rc, num;
    LL sum;
}T[N*20];

int dfs_clock, node_cnt, ql, qr, qv, m;
int a[N], b[N], root[N];
int sz[N], son[N], p[N], dep[N], dfn[N], top[N];
vector<int> G[N];

void init(int n) {
    p[1] = 1, dep[1] = 0, top[1] = 1;
    dfs_clock = node_cnt = 0;
    for(int i = 1; i <= n; i++) G[i].clear();
}

void build(int &o, int L, int R, int pre) {
    o = ++node_cnt;
    T[o] = T[pre];
    T[o].sum += 1LL*a[qv];
    if(L == R) return;
    int M = (L+R)>>1;
    if(qv <= M) build(T[o].lc, L, M, T[pre].lc);
    else build(T[o].rc, M+1, R, T[pre].rc);
}

int dfs_0(int u, int fa) {
    sz[u] = 1; son[u] = 0;
    for(int v:G[u]) if(v != fa) {
        p[v] = u;
        dep[v] = dep[u]+1;
        dfs_0(v, u);
        sz[u] += sz[v];
        if(sz[v] > sz[son[u]]) son[u] = v;
    }
}

int dfs_1(int u, int fa) {
    dfn[u] = ++dfs_clock;
    qv = b[u];
    build(root[dfn[u]], 1, m, root[dfn[u]-1]);
    if(son[u]) {
        top[son[u]] = top[u];
        dfs_1(son[u], u);
    }
    for(int v:G[u]) if(v!=fa && v!=son[u]) {
        top[v] = v;
        dfs_1(v, u);
    }
}

LL query(int o, int L, int R, int pre) {
    if(ql<=L && qr>=R) return T[o].sum-T[pre].sum;
    int M = (L+R)>>1;
    LL ret = 0;
    if(ql <= M) ret += query(T[o].lc, L, M, T[pre].lc);
    if(qr > M) ret += query(T[o].rc, M+1, R, T[pre].rc);
    return ret;
}

void solve(int u, int v) {
    LL ret = 0;
    while(top[u] != top[v]) {
        if(dep[top[u]] < dep[top[v]]) swap(u, v);
        ret += query(root[dfn[u]], 1, m, root[dfn[top[u]]-1]);
        u = p[top[u]];
    }
    if(dep[u] > dep[v]) swap(u, v);
    ret += query(root[dfn[v]], 1, m, root[dfn[u]-1]);
    printf("%lld", ret);
}

int main() {
    int n, q;
    while(~scanf("%d%d", &n, &q)) {
        init(n);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            b[i] = a[i];
        }
        sort(a+1, a+1+n);
        m = unique(a+1, a+1+n)-a-1;
        for(int i = 1; i <= n; i++) {
            b[i] = lower_bound(a+1, a+1+m, b[i])-a;
        }
        for(int i = 1; i < n; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].pb(v), G[v].pb(u);
        }
        dfs_0(1, -1); dfs_1(1, -1);
        while(q--) {
            int s, t;
            scanf("%d%d%d%d", &s, &t, &ql, &qr);
            ql = lower_bound(a+1, a+1+m, ql)-a;
            qr = upper_bound(a+1, a+1+m, qr)-a-1;
            solve(s, t);
            printf("%c", q?' ':'\n');
        }
    }
    return 0;
}

水数据的代码:

#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define CLR(A, X) memset(A, X, sizeof(A))
#define bitcount(X) __builtin_popcountll(X)
//#pragma comment(linker, "/STACK:102400000, 102400000")
const double eps = 1e-10;
int dcmp(double x) { if(fabs(x) < eps) return 0; return x<0?-1:1; }
typedef long long LL;
typedef pair<int, int> PII;

const int N = 1e5+5;

struct BIT{
    LL c[N];
    LL sum(int x) { LL res=0; while(x > 0) res+=c[x], x-=(x&-x); return res; }
    void add(int x, int d) { while(x<N) c[x]+=d, x+=(x&-x); }
    void reset() { CLR(c,0); }
}A;

int a[N], b[N], p[N][35], deep[N], W;
LL d[N];
vector<int> G[N];
vector<PII> H[N];
struct Fuck {
    int u, v, l, r, x;
}c[N];
map<pair<int, PII>, LL> ms;

int lca(int a, int b) {
    if(deep[a] < deep[b]) swap(a, b);
    int t = deep[a] - deep[b];
    for(int i = 0; (1<<i) <= t; i++) if((1<<i)&t) a = p[a][i];
    for(int i = 22; i >= 0; i--) {
        if(p[a][i] != p[b][i]) a = p[a][i], b = p[b][i];
    }
    return a==b?a:p[a][0];
}

void dfs(int u, int fa) {
    for(int i = 1; i <= 22; i++) p[u][i] = p[p[u][i-1]][i-1];
    A.add(a[u], d[a[u]]);
    for(PII x:H[u]) ms[{u, {x.fi, x.se}}] = A.sum(x.se)-A.sum(x.fi-1);
    for(int v:G[u]) if(v != fa) {
        deep[v] = deep[u]+1;
        p[v][0] = u;
        dfs(v, u);
    }
    A.add(a[u], -d[a[u]]);
}

void dfs2(int u, int fa) {
    for(int i = 1; i <= 22; i++) p[u][i] = p[p[u][i-1]][i-1];
    for(int v:G[u]) if(v != fa) {
        deep[v] = deep[u]+1;
        p[v][0] = u;
        dfs2(v, u);
    }
}

int main() {
    int n, m;
    while(~scanf("%d%d", &n, &m)) {
        A.reset(); ms.clear();
        for(int i = 1; i <= n; i++) G[i].clear(), H[i].clear();
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
        sort(b+1, b+1+n);
        W = unique(b+1, b+1+n)-b;
        for(int i = 1; i <= n; i++) {
            int x = a[i];
            a[i] = lower_bound(b+1, b+W, a[i])-b;
            d[a[i]] = x;
        }
        int u, v, l, r, x;
        for(int i = 0; i < n-1; i++) {
            scanf("%d%d", &u, &v);
            G[u].pb(v); G[v].pb(u);
        }
        dfs2(1, -1);
        for(int i = 1; i <= m; i++) {
            scanf("%d%d%d%d", &u, &v, &l, &r);
            int id1 = lower_bound(b+1, b+W, l)-b;
            int id2 = upper_bound(b+1, b+W, r)-b-1;
            if(id2 == 0) id2 = 1;
            H[u].pb({id1, id2}); H[v].pb({id1, id2});
            x = lca(u, v);
            H[x].pb({id1, id2}); H[p[x][0]].pb({id1, id2});
            c[i] = {u, v, id1, id2, x};
        }
        p[1][0] = 0;
        dfs(1, -1);
        for(int i = 1; i <= m; i++) {
            u = c[i].u, v = c[i].v, l = c[i].l, r = c[i].r, x = c[i].x;
            LL ret = ms[{u, {l, r}}]+ms[{v, {l, r}}]-ms[{p[x][0], {l, r}}]-ms[{x, {l, r}}];
            printf("%lld%c", ret, i==m?'\n':' ');
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值