左偏树(BZOJ4003)

左偏树打个标记,没了。

#include <cstdio>
#include <vector>
using namespace std;

typedef long long ll;
const int N = 300005;
int n,m,e,x,tt,c[N],d[N],a[N],hd[N],nxt[N],to[N],rt[N],f[N],g[N];
ll h[N],v[N],s[N];
vector<int> vc[N];
struct nd {int l,r,d,id; ll w,ad,mu;}t[N];
void add(int x, int y) {to[++e] = y, nxt[e] = hd[x], hd[x] = e;}

void pd(int x) {
    t[t[x].l].mu *= t[x].mu, t[t[x].l].ad *= t[x].mu, t[t[x].l].w *= t[x].mu;
    t[t[x].r].mu *= t[x].mu, t[t[x].r].ad *= t[x].mu, t[t[x].r].w *= t[x].mu;
    t[t[x].l].ad += t[x].ad, t[t[x].r].ad += t[x].ad, t[t[x].l].w += t[x].ad, t[t[x].r].w += t[x].ad;
    t[x].mu = 1, t[x].ad = 0;
}
int mrg(int x, int y) {
    if(!x || !y) return x+y;
    if(t[x].w > t[y].w) swap(x, y);
    pd(x);
    t[x].r = mrg(t[x].r, y);
    if(t[t[x].l].d < t[t[x].r].d) swap(t[x].l, t[x].r);
    t[x].d = t[t[x].r].d+1;
    return x;
}

void dfs(int x) {
    for(int i = 0; i < vc[x].size(); i++)
        t[++tt].id = vc[x][i], t[tt].w = s[vc[x][i]], t[tt].mu = 1, rt[x] = mrg(rt[x], tt);
    for(int i = hd[x]; i; i = nxt[i]) d[to[i]] = d[x]+1, dfs(to[i]), rt[x] = mrg(rt[x], rt[to[i]]);
    while(rt[x] && t[rt[x]].w < h[x])
        f[x]++, g[t[rt[x]].id] = x, pd(rt[x]), rt[x] = mrg(t[rt[x]].l, t[rt[x]].r);
    if(a[x]) t[rt[x]].mu *= v[x], t[rt[x]].ad *= v[x], t[rt[x]].w *= v[x];
    else t[rt[x]].ad += v[x], t[rt[x]].w += v[x];
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%lld", &h[i]);
    for(int i = 2; i <= n; i++) scanf("%d%d%lld", &x, &a[i], &v[i]), add(x, i);
    for(int i = 1; i <= m; i++) scanf("%lld%d", &s[i], &c[i]), vc[c[i]].push_back(i);
    d[1] = 1, dfs(1);
    for(int i = 1; i <= n; i++) printf("%d\n", f[i]);
    for(int i = 1; i <= m; i++) printf("%d\n", d[c[i]]-d[g[i]]);
    return 0;
}
posted @ 2016-12-21 11:10  Monster_Yi  阅读(305)  评论(0编辑  收藏  举报