分析

树上莫队裸题。
好博客

树剖的时候不能再次dfs重儿子。(好像是废话,但我因为这个问题调了三小时)

代码

#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x){
    T data=0;
    int w=1;
    char ch=getchar();
    while(!isdigit(ch))
    {
        if(ch=='-')
            w=-1;
        ch=getchar();
    }
    while(isdigit(ch))
        data=10*data+ch-'0',ch=getchar();
    return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff;

const int MAXN=1e5+7;
int n,q;
map<int,int>M;
int a[MAXN],id;
int belong[MAXN],block;

struct Edge
{
    int nx,to;
}E[MAXN<<2];
int head[MAXN],ecnt;

void addedge(int x,int y)
{
    E[++ecnt].to=y;
    E[ecnt].nx=head[x],head[x]=ecnt;
}

int fa[MAXN],dep[MAXN],sz[MAXN],son[MAXN],top[MAXN];
int st[MAXN],ed[MAXN],pot[MAXN],clk;

void dfs1(int x,int f)
{
    fa[x]=f,sz[x]=1;
    st[x]=++clk,pot[clk]=x;
    dep[x]=dep[f]+1;
    for(int i=head[x];i;i=E[i].nx)
    {
        int y=E[i].to;
        if(y==f)
            continue;
        dfs1(y,x);
        sz[x]+=sz[y];
        if(sz[y]>sz[son[x]])
            son[x]=y;
    }
    ed[x]=++clk,pot[clk]=x;
}

void dfs2(int x,int topf)
{
    top[x]=topf;
    if(!son[x])
        return;
    dfs2(son[x],topf);
    for(int i=head[x];i;i=E[i].nx)
    {
        int y=E[i].to;
        if(y==fa[x]||y==son[x]) // edit 1:cannot dfs son[x] twice and wrongly
            continue;
        dfs2(y,y);
    }
}

int lca(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
            swap(x,y);
        x=fa[top[x]];
    }
    return dep[x]<dep[y]?x:y;
}

struct Quiz
{
    int l,r,f,id;
    bool operator<(const Quiz&x)
    {
        if(belong[l]^belong[x.l])
            return l<x.l;
        else
            return r<x.r;
    }
}Q[MAXN];

int ql,qr,res;
bool used[MAXN];
int cnt[MAXN],ans[MAXN];

void add(int x)
{
    if(++cnt[x]==1)
        ++res;
}

void del(int x)
{
    if(--cnt[x]==0)
        --res;
}

void change(int x)
{
    used[x]?del(a[x]):add(a[x]);
    used[x]^=1;
}

int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    read(n);read(q);
    block=sqrt(n);
    for(int i=1;i<=n;++i)
    {
        read(a[i]);
        if(!M[a[i]])
            M[a[i]]=++id;
        a[i]=M[a[i]];
    }
    for(int i=1;i<=2*n;++i) // edit 1:belong should be inited to 2n
        belong[i]=(i-1)/block+1;
    for(int i=1;i<n;++i)
    {
        int x,y;
        read(x);read(y);
        addedge(x,y);
        addedge(y,x);
    }
    dfs1(1,0);
    dfs2(1,1);
    for(int i=1;i<=q;++i)
    {
        int x,y;
        read(x);read(y);
        if(st[x]>st[y])
            swap(x,y);
        int f=lca(x,y);
        if(f==x)
            Q[i].l=st[x],Q[i].r=st[y];
        else
            Q[i].l=ed[x],Q[i].r=st[y],Q[i].f=f;
        Q[i].id=i;
    }
    sort(Q+1,Q+q+1);
    ql=1,qr=0,res=0;
    for(int i=1;i<=q;++i)
    {
        while(ql>Q[i].l)
            change(pot[--ql]);
        while(qr<Q[i].r)
            change(pot[++qr]);
        while(ql<Q[i].l)
            change(pot[ql++]);
        while(qr>Q[i].r)
            change(pot[qr--]);
        if(Q[i].f)
            change(Q[i].f);
        ans[Q[i].id]=res;
        if(Q[i].f)
            change(Q[i].f);
    }
    for(int i=1;i<=q;++i)
        printf("%d\n",ans[i]);
//  fclose(stdin);
//  fclose(stdout);
    return 0;
}

SPOJCOT2 Count on a tree II的更多相关文章

  1. SPOJcot2 Count on a tree II (树上莫队)

    You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer ...

  2. 【SPOJ10707】 COT2 Count on a tree II

    SPOJ10707 COT2 Count on a tree II Solution 我会强制在线版本! Solution戳这里 代码实现 #include<stdio.h> #inclu ...

  3. 【BZOJ2589】 Spoj 10707 Count on a tree II

    BZOJ2589 Spoj 10707 Count on a tree II Solution 吐槽:这道题目简直...丧心病狂 如果没有强制在线不就是树上莫队入门题? 如果加了强制在线怎么做? 考虑 ...

  4. 【BZOJ2589】[SPOJ10707]Count on a tree II

    [BZOJ2589][SPOJ10707]Count on a tree II 题面 bzoj 题解 这题如果不强制在线就是一个很\(sb\)的莫队了,但是它强制在线啊\(qaq\) 所以我们就用到了 ...

  5. 【SPOJ】Count On A Tree II(树上莫队)

    [SPOJ]Count On A Tree II(树上莫队) 题面 洛谷 Vjudge 洛谷上有翻译啦 题解 如果不在树上就是一个很裸很裸的莫队 现在在树上,就是一个很裸很裸的树上莫队啦. #incl ...

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

  7. AC日记——Count on a tree II spoj

    Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #inc ...

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

随机推荐

  1. php json josn_decode()返回的是对像,如何把对像转成数组

    php json josn_decode()返回的是对像,如何把对像转成数组 a.php传值页面,使用 json_encode($array)对数组进行加密码. b.php页面在接收a.php传过来的 ...

  2. UltraDropDown

    private void FruitInit() { //Create some fruit fruits.Add(-1,"apple"); fruits.Add(-2," ...

  3. Silverlight自定义控件系列 – TreeView (1)

      原文路径:http://blog.csdn.net/wlanye/article/details/7265457 很多人都对MS自带的控件不太满意(虽然MS走的是简约风格),都会试图去修改或创建让 ...

  4. RMQ板子

    对于RMQ这种静态最值询问, 用线段树的话查询过慢, 一般用ST表预处理后O(1)查询, 下以最大值查询为例, 这里假定$n$不超过5e5 void init() { Log[0] = -1; REP ...

  5. UVA-1579 Matryoshka (区间DP)

    题目大意:n个俄罗斯套娃,都有相应的编号,每次可将两个相邻的套娃组合成一组,每次合成只能小的放到大的里面,并且是逐层嵌套.问将这n个套娃分成若干个组,并且每组都是编号从1开始的连续序列,最少需要几步. ...

  6. 数据库故障诊断(Troubleshooting)之性能问题导致的数据库严重故障案例之一

    好久不来这里写东西,今天有点时间,来这里写点最近遇到的事情.前段时间,某电信业务用户因某核心生产库最近多次宕机重启,多方人员介入无果后,给我发来了邮件,大概意思就是现在该问题已经造成了比较严重的后果, ...

  7. mrh支付宝玩转福

    支付宝扫福 都会玩了 2017

  8. linux processes identifiers

    Linux, like all Unix uses user and group identifiers to check for access rights to files and images ...

  9. (C#基础)反射理解

    这个知识点很基础. 代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; n ...

  10. sgu106.The equation 拓展欧几里得 难度:0

    106. The equation time limit per test: 0.25 sec. memory limit per test: 4096 KB There is an equation ...