SPOJ10707 COT2 Count on a tree II


Solution

我会强制在线版本! Solution戳这里

代码实现


#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(),v=gi();
        if(bl[u]==bl[v])lastans=solve_same(u,v);
        else lastans=solve_diff(u,v);
        print(lastans);putchar('\n');
    }
    return 0;
}

【SPOJ10707】 COT2 Count on a tree II的更多相关文章

  1. 【SPOJ10707】COT2 - Count on a tree II

    题目大意:给定一棵 N 个节点的无根树,每个节点有一个颜色.现有 M 个询问,每次询问一条树链上的不同颜色数. 题解:学会了树上莫队. 树上莫队是将节点按照欧拉序进行排序,将树上问题转化成序列上的问题 ...

  2. 【树上莫队】【SP10707】 COT2 - Count on a tree II

    Description 给定一棵 \(n\) 个点的树,每个节点有一个权值,\(m\) 次询问,每次查询两点间路径上有多少不同的权值 Input 第一行是 \(n\) 和 \(m\) 第二行是 \(n ...

  3. 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 ...

  4. 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  ...

  5. COT2 - Count on a tree II(树上莫队)

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

  6. SPOJ10707 COT2 - Count on a tree II 【树上莫队】

    题目分析: 考虑欧拉序,这里的欧拉序与ETT欧拉序的定义相同而与倍增LCA不同.然后不妨对于询问$u$与$v$让$dfsin[u] \leq dfsin[v]$,这样对于u和v不在一条路径上,它们可以 ...

  7. 【SPOJ】10628. Count on a tree(lca+主席树+dfs序)

    http://www.spoj.com/problems/COT/ (速度很快,排到了rank6) 这题让我明白了人生T_T 我知道我为什么那么sb了. 调试一早上都在想人生. 唉. 太弱. 太弱. ...

  8. SPOJ COT2 Count on a tree II(树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ You are given a tree with N nodes.The tree nodes are numbere ...

  9. SPOJ COT2 Count on a tree II (树上莫队)

    题目链接:http://www.spoj.com/problems/COT2/ 参考博客:http://www.cnblogs.com/xcw0754/p/4763804.html上面这个人推导部分写 ...

随机推荐

  1. ajax之Content-Type决定form-data方式提交还是request-payloud等

    1.post请求的Content-Type为application/x-www-form-urlencoded,参数是在请求体中,即上面请求中的Form Data. 在servlet中,可以通过req ...

  2. SVN基本操作 (zz)

    SVN基本操作 分类: LINUX 原文地址:SVN基本操作 作者:tuyer 文章摘要:SVN 基本操作:SVN是什么 Svn是一个离线的代码管理,可以多个人一起修改,然后再将修改的内容提交到Svn ...

  3. 2017/2/16:自己ajax+json习惯性写法 代码拼接的写法 +json用post提交乱码的原因

    1.先导入jquery的包 2.ajax的写法跟注意点 返回一个list的写法 代码拼接写法: html层: 2.script处 4:在你前面传递参数的时候没有遇到乱码问题的情况下,你使用json并且 ...

  4. 5. Longest Palindromic Substring - Unsolved

    https://leetcode.com/problems/longest-palindromic-substring/#/description Given a string s, find the ...

  5. linux_域名映射

    vi /etc/hosts在最后加上ip及映射的域名 192.168.229.111 node001 192.168.229.112 node002 192.168.229.113 node003

  6. 求一个数的n次幂

    1.当这个数是2的多少次幂: 求(2^m)^n  =  2^(m*n) = 1<<m*n; 2.快速幂(要考虑底数a,和m的正负) int quick_mod(int a,int m){ ...

  7. Sprign中常用注解

    1.@Component 创建类对象,相当于配置<bean/> 2.@Service 与 @Component功能相同 2.1写在ServiceImpl类上 (建议在ServiceImpl ...

  8. 219.01.19 bzoj3252: 攻略(长链剖分+贪心)

    传送门 长链剖分好题. 题意:给一棵带点权的树,可以从根节点到任一叶节点走kkk次,走过的点只能计算一次,问kkk次走过的点点权值和最大值. 思路: 考虑将整棵树带权长链剖分,这样链与链之间是不会重复 ...

  9. 2018.06.26 NOIP模拟 号码(数位dp)

    题目背景 SOURCE:NOIP2015-GDZSJNZX(难) 题目描述 Mike 正在在忙碌地发着各种各样的的短信.旁边的同学 Tom 注意到,Mike 发出短信的接收方手机号码似乎都满足着特别的 ...

  10. java学习路线图(2018年最新版)

    最近有些网友问我如何自学 Java 后端,还有些是想从别的方向想转过来,但都不太了解 Java 后端究竟需要学什么,究竟要从哪里学起,哪些是主流的 Java 后端技术等等,导致想学,但又很迷茫,不知从 ...