传送门

同样是树上莫队

只不过要求一个集合的mex,这里可以使用分块,可以在根号时间内得出解

/**************************************************
Problem: 4129
User: star_magic_young
Language: C++
Result: Accepted
Time:748 ms
Memory:8712 kb
****************************************************/ #include<bits/stdc++.h>
#define LL long long
#define il inline
#define re register using namespace std;
const int N=50000+10;
il int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*w;
}
int n,m,q,szm,mm[N],ti,mdf[N][3],a[N];
int na,an[N];
int to[N<<1],nt[N<<1],hd[N],tot=1,st[N],tp,fa[N],sz[N],de[N],son[N],top[N];
bool v[N];
il void add(int x,int y)
{
++tot,to[tot]=y,nt[tot]=hd[x],hd[x]=tot;
++tot,to[tot]=x,nt[tot]=hd[y],hd[y]=tot;
}
void dfs1(int x)
{
sz[x]=1;
int la=tp;
for(int i=hd[x];i;i=nt[i])
{
int y=to[i];
if(y==fa[x]) continue;
fa[y]=x,de[y]=de[x]+1,dfs1(y),sz[x]+=sz[y];
if(sz[son[x]]<sz[y]) son[x]=y;
if(tp-la>=szm){++m;while(tp!=la) mm[st[tp--]]=m;}
}
st[++tp]=x;
}
void dfs2(int x,int ntp)
{
top[x]=ntp;
if(son[x]) dfs2(son[x],ntp);
for(int i=hd[x];i;i=nt[i])
{
int y=to[i];
if(y==fa[x]||y==son[x]) continue;
dfs2(y,y);
}
}
il int glca(int x,int y)
{
while(top[x]!=top[y])
{
if(de[top[x]]<de[top[y]]) swap(x,y);
x=fa[top[x]];
}
return de[x]<de[y]?x:y;
} int b[N],sm[400],ll[400],rr[400],sq,tt=-1;
il void upd(int x)
{
v[x]^=1;
if(a[x]<=n)
{
if(v[x]) sm[a[x]/sq]+=(++b[a[x]]==1);
else sm[a[x]/sq]-=(--b[a[x]]==0);
}
}
il int gans()
{
for(int i=0;i<=tt;++i)
if(sm[i]!=rr[i]-ll[i]+1)
{
for(int j=ll[i];j<=rr[i];++j)
if(!b[j]) return j;
}
return 1******7;
}
il void mv(int x,int y)
{
if(x==y) return;
if(de[x]<de[y]) swap(x,y);
while(de[x]>de[y])
{
upd(x),x=fa[x];
}
while(x!=y)
{
upd(x),x=fa[x];
upd(y),y=fa[y];
}
//upd(x);
}
struct qu
{
int l,r,t,id;
bool operator < (const qu &bb) const {return mm[l]!=mm[bb.l]?mm[l]<mm[bb.l]:(mm[r]!=mm[bb.r]?mm[r]<mm[bb.r]:t<bb.t);}
}qq[N]; int main()
{
n=rd(),q=rd();
szm=(int)pow(n,0.45);
sq=sqrt(n);
for(int i=0;i<=n;i+=sq) ll[++tt]=i,rr[tt]=i+sq-1;
rr[tt]=min(rr[tt],n);
for(int i=1;i<=n;++i) a[i]=rd();
for(int i=1;i<n;++i) add(rd(),rd());
dfs1(1);
++m;while(tp) mm[st[tp--]]=m;
dfs2(1,1);
for(int i=1;i<=q;++i)
{
int op=rd();
if(op)
{
qq[i-ti].l=rd(),qq[i-ti].r=rd(),qq[i-ti].t=ti,qq[i-ti].id=i-ti;
if(mm[qq[i-ti].l]>mm[qq[i-ti].r]) swap(qq[i-ti].l,qq[i-ti].r);
}
else mdf[++ti][0]=rd(),mdf[ti][1]=rd();
}
sort(qq+1,qq+q-ti+1);
for(int i=1,l=1,r=1,t=0;i<=q-ti;l=qq[i].l,r=qq[i].r,++i)
{
while(t<qq[i].t)
{
++t;
int xx=mdf[t][0],yy=mdf[t][1];mdf[t][2]=a[xx];
if(v[xx])
{
if(a[xx]<=n) sm[a[xx]/sq]-=(--b[a[xx]]==0);
if(yy<=n) sm[yy/sq]+=(++b[yy]==1);
}
a[xx]=yy;
}
while(t>qq[i].t)
{
int xx=mdf[t][0],yy=mdf[t][2];
if(v[xx])
{
if(a[xx]<=n) sm[a[xx]/sq]-=(--b[a[xx]]==0);
if(yy<=n) sm[yy/sq]+=(++b[yy]==1);
}
a[xx]=yy;
--t;
}
mv(l,qq[i].l),mv(r,qq[i].r);
int lca=glca(qq[i].l,qq[i].r);
upd(lca),an[qq[i].id]=gans(),upd(lca);
}
for(int i=1;i<=q-ti;++i) printf("%d\n",an[i]);
return 0;
}

BZOJ 4129 Haruna’s Breakfast的更多相关文章

  1. BZOJ 4129 Haruna’s Breakfast (分块 + 带修莫队)

    4129: Haruna’s Breakfast Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 835  Solved: 409[Submit][St ...

  2. BZOJ 4129: Haruna’s Breakfast [树上莫队 分块]

    传送门 题意: 单点修改,求一条链的mex 分块维护权值,$O(1)$修改$O(S)$求mex...... 带修改树上莫队 #include <iostream> #include < ...

  3. bzoj 4129 Haruna’s Breakfast 树上莫队

    按照dfs序分块,莫队乱搞 再套个权值分块 #include<cstdio> #include<iostream> #include<cstring> #inclu ...

  4. BZOJ 4129 Haruna’s Breakfast ( 树上带修莫队 )

    题面 求树上某路径上最小的没出现过的权值,有单点修改 添加链接描述 分析 树上带修莫队板题,问题是怎么求最小的没出现过的权值. 因为只有nnn个点,所以没出现过的最小值一定在[0,n][0,n][0, ...

  5. 【BZOJ】4129: Haruna’s Breakfast 树分块+带修改莫队算法

    [题意]给定n个节点的树,每个节点有一个数字ai,m次操作:修改一个节点的数字,或询问一条树链的数字集合的mex值.n,m<=5*10^4,0<=ai<=10^9. [算法]树分块+ ...

  6. 【BZOJ4129】Haruna’s Breakfast(树上莫队)

    [BZOJ4129]Haruna's Breakfast(树上莫队) 题面 BZOJ Description Haruna每天都会给提督做早餐! 这天她发现早饭的食材被调皮的 Shimakaze放到了 ...

  7. BZOJ4129: Haruna’s Breakfast

    Description Haruna每天都会给提督做早餐! 这天她发现早饭的食材被调皮的 Shimakaze放到了一棵 树上,每个结点都有一样食材,Shimakaze要考验一下她. 每个食材都有一个美 ...

  8. bzoj4129 Haruna’s Breakfast 树上带修莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4129 题解 考虑没有修改的序列上的版本应该怎么做: 弱化的题目应该是这样的: 给定一个序列,每 ...

  9. 【树上莫队】【带修莫队】【权值分块】bzoj4129 Haruna’s Breakfast

    #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...

随机推荐

  1. 对delphi中的数据敏感控件的一点探索

    一直对delphi数据敏感控件很好奇,感觉很神奇.只要简单设置一下,就显示和编辑数据,不用写一行代码. 如果不用数据敏感控件,编辑一个表字段数据并保存,我相信应用如下代码. Table1.edit, ...

  2. C#中的DateTime

    一.DateTime是值类型还是引用类型的探索 二.了解DateTime结构体 三.DateTime.Now和DateTime.UtcNow是怎么计算出来的 一.DateTime是值类型还是引用类型的 ...

  3. arctan

    ArcTanWhen the ArcTan functional configuration is selected, the input vector (X_IN,Y_IN) is rotated( ...

  4. Link-Cut Tree(LCT)&TopTree讲解

    前言: Link-Cut Tree简称LCT是解决动态树问题的一种数据结构,可以说是我见过功能最强大的一种树上数据结构了.在此与大家分享一下LCT的学习笔记.提示:前置知识点需要树链剖分和splay. ...

  5. log4net 单独项目

    首先参考:http://blog.csdn.net/feiying008/article/details/45440547 有时,我们需要将日志功能作为单独模块,用来以后嫁接到其他项目. 今天就来看看 ...

  6. Minimum Cost POJ - 2516(模板题。。没啥好说的。。)

    题意: 从发货地到商家 送货 求送货花费的最小费用... 有m个发货地,,,n个商家,,每个商家所需要的物品和物品的个数都不一样,,,每个发货地有的物品和物品的个数也不一样,,, 从不同的发货地到不同 ...

  7. windows上搭建nginx

    命令 相关命令(需定位到所在目录):安装:start nginx.exe 停止运行:nginx.exe -s stop 或在进程找到nginx.exe结束重启:nginx.exe -s reload ...

  8. MT【207】|ax^2+bx+c|中判别式$\Delta$的含义

    已知$a,b\in R^+,a+b=2$且对任意的$x\in R$,均有$|2x^2+ax-b|\ge|x^2+cx+d|$则$\dfrac{d-4c}{cd}$的最小值______ 提示:注意到$\ ...

  9. 【刷题】BZOJ 2407 探险

    Description 探险家小T好高兴!X国要举办一次溶洞探险比赛,获奖者将得到丰厚奖品哦!小T虽然对奖品不感兴趣,但是这个大振名声的机会当然不能错过! 比赛即将开始,工作人员说明了这次比赛的规则: ...

  10. VB: 再次使用的体会

    放下VB已经有7.8年的时候了. 记得在上学的时候,一直迷恋着它,学了三年的VB,写了不少小软件. 到了工作之后,转到JAVA后,就一直没用VB. 这次的项目由于与系统的相关性高以及安装文件的大小有限 ...