bzoj3720 Gty的妹子树 树分块

16 篇文章 0 订阅

Description


我曾在弦歌之中听过你,
檀板声碎,半出折子戏。
舞榭歌台被风吹去,
岁月深处尚有余音一缕……

Gty神(xian)犇(chong)从来不缺妹子……
他来到了一棵妹子树下,发现每个妹子有一个美丽度……
由于Gty很哲♂学,他只对美丽度大于某个值的妹子感兴趣。
他想知道某个子树中美丽度大于k的妹子个数。
某个妹子的美丽度可能发生变化……
树上可能会出现一只新的妹子……

维护一棵初始有n个节点的有根树(根节点为1),树上节点编号为1-n,每个点有一个权值wi。
支持以下操作:
0 u x 询问以u为根的子树中,严格大于x的值的个数。(u^=lastans,x^=lastans)
1 u x 把u节点的权值改成x。(u^=lastans,x^=lastans)
2 u x 添加一个编号为”当前树中节点数+1”的节点,其父节点为u,其权值为x。(u^=lastans,x^=lastans)

最开始时lastans=0

1<=n<=30000
1<=m<=30000

Solution


xc不给上洛谷bzoj什么鬼Σ(っ °Д °;)っ

首先这看起来非常像LCT维护子树信息,但是我不会
考虑用树分块。我们记 n=size n = s i z e ,每dfs到一个新点x就分类讨论
若x的父亲所在的块没满,将x加入父亲所在的块
否则就新开一个块
可以发现我们就能非常常规地对于整块排序、散块暴力了,插入其实操作同理

但是树分块似乎在某种情况下会被卡,例如菊花图。因此我们的块的数量要开得比size大一点,实际操作就是大许多

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

const int N=120005;
const int E=600005;

struct Graph {
    struct edge {int y,next;} e[E];
    int ls[N],edCnt;
    void add_edge(int x,int y) {
        e[++edCnt]=(edge) {y,ls[x]}; ls[x]=edCnt;
    }
} G,T;

struct SCC {
    int a[210],size;
    void Insert(int x)
    {
        int i;
        ++size;
        for(i=size;i>1&&a[i-1]>x;i--)
            a[i]=a[i-1];
        a[i]=x;
    }
    void Modify(int x,int y)
    {
        int temp=std:: lower_bound(a+1,a+size+1,x)-a;
        for(;temp<size&&a[temp+1]<y;temp++)
            a[temp]=a[temp+1];
        for(;temp>1&&a[temp-1]>y;temp--)
            a[temp]=a[temp-1];
        a[temp]=y;
    }
    int Query(int x)
    {
        int temp=std:: upper_bound(a+1,a+size+1,x)-a;
        return size-temp+1;
    }
} vec[10005];

int bel[N],w[N],fa[N],tot,B,ans;

int read() {
    int x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

void dfs(int now) {
    if (vec[bel[fa[now]]].size>=B) {
        bel[now]=++tot;
        T.add_edge(bel[fa[now]],bel[now]);
    } else bel[now]=bel[fa[now]];
    vec[bel[now]].Insert(w[now]);
    for (int i=G.ls[now];i;i=G.e[i].next) {
        if (G.e[i].y==fa[now]) continue;
        fa[G.e[i].y]=now;
        dfs(G.e[i].y);
    }
}

void dfs2(int now,int x,int y) {
    ans+=vec[now].Query(y);
    for (int i=T.ls[now];i;i=T.e[i].next) {
        dfs2(T.e[i].y,x,y);
    }
}

void dfs1(int now,int x,int y) {
    if (w[now]>y) ans++;
    for (int i=G.ls[now];i;i=G.e[i].next) {
        if (G.e[i].y==fa[now]) continue;
        if (bel[G.e[i].y]==bel[now]) dfs1(G.e[i].y,x,y);
        else dfs2(bel[G.e[i].y],x,y);
    }
}

int main(void) {
    int n=read(); B=sqrt(n);
    rep(i,2,n) {
        int x=read(),y=read();
        G.add_edge(x,y);
        G.add_edge(y,x);
    }
    rep(i,1,n) w[i]=read();
    bel[0]=tot=1; dfs(1);
    for (int Q=read(),lastans=0;Q--;) {
        int opt=read(),x=read()^lastans,y=read()^lastans;
        if (opt==0) {
            ans=0;
            dfs1(x,x,y);
            printf("%d\n", ans);
            lastans=ans;
        } else if (opt==1) {
            vec[bel[x]].Modify(w[x],y);
            w[x]=y;
        } else {
            w[++n]=y;
            G.add_edge(x,n); fa[n]=x;
            if (vec[bel[x]].size<B) bel[n]=bel[x];
            else {
                bel[n]=++tot;
                T.add_edge(bel[x],bel[n]);
            }
            vec[bel[n]].Insert(y);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值