BZOJ2589 Spoj 10707 Count on a tree II


Solution

吐槽:这道题目简直...丧心病狂

如果没有强制在线不就是树上莫队入门题?

如果加了强制在线怎么做?

考虑分块(莫队与分块真是基友)

我们按照深度为\(\sqrt{n}\)的子树分块,那么这一棵树最多不超过\(\sqrt{n}\)个块.

维护每一个块的根节点到树上每一个节点的答案,暴力即可.然后用可持久化块状数组维护一下遍历时出现的最深的颜色的深度.

查询答案的做法:

  1. 在一个块内,直接暴力查.
  2. 不在一个块里面,预处理的答案再加上深度大的节点到他所在块的根节点的答案即可,去重可以用块状数组里面的东西.

代码实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi()
{
    int f=1,sum=0;char ch=getchar();
    while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    return f*sum;
}
const int N=60010;
int Bl[N],B,P[N],ans[310][N],a[N],b[N],bl[N],num,p[N][310],Anum,rt[310],F[N];
struct array
{
    int num[210];
    int operator[](int x){return p[num[Bl[x]]][P[x]];};
    void insert(const array &pre,int x,int dep)
    {
        int block=Bl[x],t=P[x];
        memcpy(num,pre.num,sizeof(num));
        memcpy(p[++Anum],p[num[block]],sizeof(p[0]));
        p[Anum][t]=dep;num[block]=Anum;
    }
}s[N];
int to[N<<1],nxt[N<<1],front[N],cnt,dep[N],f[N][22],st[N],sta,kind;
inline void Add(int u,int v)
{
    to[++cnt]=v;nxt[cnt]=front[u];front[u]=cnt;
}
inline int dfs(int u,int fa)
{
    dep[u]=dep[fa]+1;
    f[u][0]=fa;
    s[u].insert(s[fa],a[u],dep[u]);
    st[++sta]=u;int mx=dep[u],now=sta;
    for(re int i=front[u];i;i=nxt[i])
    {
        int v=to[i];
        if(v==fa)continue;
        mx=max(mx,dfs(v,u));
    }
    if(mx-dep[u]>=B || now==1)
    {
        rt[++num]=u;
        for(re int i=now;i<=sta;i++)bl[st[i]]=num;
        sta=now-1;return dep[u]-1;
    }
    return mx;
}
int lca(int u,int v)
{
    if(dep[u]<dep[v])swap(u,v);
    for(re int i=20;~i;i--)
        if(dep[u]-(1<<i)>=dep[v])u=f[u][i];
    if(u==v)return u;
    for(re int i=20;~i;i--)
        if(f[u][i]!=f[v][i])
            u=f[u][i],v=f[v][i];
    return f[u][0];
}
inline void getans(int u,int fa,int BL)
{
    if(++F[a[u]]==1)kind++;
    ans[BL][u]=kind;
    for(re int i=front[u];i;i=nxt[i])
    {
        int v=to[i];
        if(v==fa)continue;
        getans(v,u,BL);
    }
    if(--F[a[u]]==0)kind--;
}
int solve_same(int x,int y)
{
    sta=0;
    for(kind=0;x!=y;x=f[x][0])
    {
        if(dep[x]<dep[y])swap(x,y);
        if(!F[a[x]]++)++kind,st[++sta]=a[x];
    }
    int QAQ=kind+(!F[a[x]]);
    for(;sta;sta--)F[st[sta]]=0;
    return QAQ;
}
int solve_diff(int x,int y)
{
    if(dep[rt[bl[x]]]<dep[rt[bl[y]]])swap(x,y);
    int sum=ans[bl[x]][y];
    int z=rt[bl[x]],d=dep[lca(x,y)];
    sta=0;
    for(;x!=z;x=f[x][0])
    {
        if(!F[a[x]] && s[z][a[x]]<d && s[y][a[x]]<d)
            F[st[++sta]=a[x]]=1,sum++;
    }
    for(;sta;sta--)F[st[sta]]=0;
    return sum;
}
int n,m;
void print(int x)
{
    if(x>=10)print(x/10);
    putchar(x%10+'0');
}
int main()
{
    n=gi();m=gi();B=sqrt(n);
    for(int i=1;i<=n;i++)Bl[i]=(i-1)/B+1,P[i]=i%B;
    for(re int i=1;i<=n;i++)a[i]=b[i]=gi();
    sort(b+1,b+n+1);int N=unique(b+1,b+n+1)-b-1;
    for(re int i=1;i<=n;i++)
        a[i]=lower_bound(b+1,b+N+1,a[i])-b;
    for(re int i=1;i<n;i++)
    {
        int u=gi(),v=gi();
        Add(u,v);Add(v,u);
    }
    dfs(1,1);
    for(re int i=1;i<=num;i++)getans(rt[i],rt[i],i);
    for(re int j=1;j<=20;j++)
        for(re int i=1;i<=n;i++)
            f[i][j]=f[f[i][j-1]][j-1];
    int lastans=0;
    while(m--)
    {
        int u=gi()^lastans,v=gi();
        if(bl[u]==bl[v])lastans=solve_same(u,v);
        else lastans=solve_diff(u,v);
        print(lastans);putchar('\n');
    }
    return 0;
}

【BZOJ2589】 Spoj 10707 Count on a tree II的更多相关文章

  1. 【BZOJ2588】Spoj 10628. Count on a tree 主席树+LCA

    [BZOJ2588]Spoj 10628. Count on a tree Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lasta ...

  2. bzoj2589: Spoj 10707 Count on a tree II

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v),你需要回答u xor lastans和v这两个节点间有多少种不同的点权.其中lastans是上一个询问的答案,初 ...

  3. 【bzoj2588】Spoj 10628. Count on a tree 离散化+主席树

    题目描述 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始为0,即第一个 ...

  4. [BZOJ]2589: Spoj 10707 Count on a tree II

    Time Limit: 20 Sec  Memory Limit: 400 MB Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v),你需要回答u xor last ...

  5. BZOJ2539 Spoj 10707 Count on a tree II

    题面 题解 因为这道题目我也不太会做,所以借鉴了一下大佬heyujun的博客 如果不强制在线,这道题目是树上莫队练手题 我们知道莫队是离线的,但是万一强制在线就凉凉了 于是我们就需要一些操作:树分块 ...

  6. 【BZOJ】【2588】COT(Count On a Tree)

    可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...

  7. spoj COT2 - Count on a tree II

    COT2 - Count on a tree II http://www.spoj.com/problems/COT2/ #tree You are given a tree with N nodes ...

  8. SPOJ COT2 - Count on a tree II(LCA+离散化+树上莫队)

    COT2 - Count on a tree II #tree You are given a tree with N nodes. The tree nodes are numbered from  ...

  9. 【bzoj 2588】Spoj 10628. Count on a tree

    Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答案,初始 ...

随机推荐

  1. java调用第三方包的例子

    第三方包路径 D:\jp\log4j\log4j-1.2.16.jar 代码D:\jp\log4j\Log4jDemo.java import org.apache.log4j.*; public c ...

  2. 【C#】解析C#程序集的加载和反射

    目录结构: contents structure [+] 程序集 程序集的加载 发现程序集中的类型 反射对类型成员的常规操作 发现类型的成员 创建类型的实例 绑定句柄减少进程的内存消耗 解析自定义特性 ...

  3. 前端html的简单认识

    一.html 超文本标记语言 hypertext markup language 二.html的结构 三.html标签格式 1.标签由<>把关键字括起来 2.标签通常是成对出现的 , eg ...

  4. Python-类-dict

    class dict(object): """ dict() -> new empty dictionary dict(mapping) -> new dic ...

  5. IntelliJ IDEA 2017版 spring-boot搭建拦截器

    1.建立一个springboot-web项目 https://www.cnblogs.com/liuyangfirst/p/8298588.html 2.加入过滤接口 public class Log ...

  6. 怎样导入现有的NiosII工程

    查找路径

  7. AngularJS实战之filter的使用二

    博文一中的filter是angular自带的filter,一般不会满足我们的使用.我们可以自定义filter. 一.自定义filter实现反转字符串 <div>{{ceshi|revers ...

  8. 动态创建控件 #Create(...)

    在类中创建一个控件对象;例:CButton m_btn; 用Create创建一个对象(#其实已经与其绑定)m_btn.Create(.....); #注意Create()函数的参数 问题一:点击一个b ...

  9. P750 内存插槽

    查看p750内存插槽占用情况 lscfg -vp | grep -p DIMM Memory DIMM: Record Name.................VINI Flag Field.... ...

  10. JS高程研读记录一【事件流】

    事件流主要有冒泡事件.事件捕获及DOM事件流.现浏览器除了IE8及更早版外,基本支持DOM事件流. 冒泡事件由IE提出,而事件捕获则由Netscape提出.但两者却是截然相反的方案. 以DIV点击为例 ...