BZOJ 4129 Haruna’s Breakfast
同样是树上莫队
只不过要求一个集合的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的更多相关文章
- BZOJ 4129 Haruna’s Breakfast (分块 + 带修莫队)
4129: Haruna’s Breakfast Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 835 Solved: 409[Submit][St ...
- BZOJ 4129: Haruna’s Breakfast [树上莫队 分块]
传送门 题意: 单点修改,求一条链的mex 分块维护权值,$O(1)$修改$O(S)$求mex...... 带修改树上莫队 #include <iostream> #include < ...
- bzoj 4129 Haruna’s Breakfast 树上莫队
按照dfs序分块,莫队乱搞 再套个权值分块 #include<cstdio> #include<iostream> #include<cstring> #inclu ...
- BZOJ 4129 Haruna’s Breakfast ( 树上带修莫队 )
题面 求树上某路径上最小的没出现过的权值,有单点修改 添加链接描述 分析 树上带修莫队板题,问题是怎么求最小的没出现过的权值. 因为只有nnn个点,所以没出现过的最小值一定在[0,n][0,n][0, ...
- 【BZOJ】4129: Haruna’s Breakfast 树分块+带修改莫队算法
[题意]给定n个节点的树,每个节点有一个数字ai,m次操作:修改一个节点的数字,或询问一条树链的数字集合的mex值.n,m<=5*10^4,0<=ai<=10^9. [算法]树分块+ ...
- 【BZOJ4129】Haruna’s Breakfast(树上莫队)
[BZOJ4129]Haruna's Breakfast(树上莫队) 题面 BZOJ Description Haruna每天都会给提督做早餐! 这天她发现早饭的食材被调皮的 Shimakaze放到了 ...
- BZOJ4129: Haruna’s Breakfast
Description Haruna每天都会给提督做早餐! 这天她发现早饭的食材被调皮的 Shimakaze放到了一棵 树上,每个结点都有一样食材,Shimakaze要考验一下她. 每个食材都有一个美 ...
- bzoj4129 Haruna’s Breakfast 树上带修莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4129 题解 考虑没有修改的序列上的版本应该怎么做: 弱化的题目应该是这样的: 给定一个序列,每 ...
- 【树上莫队】【带修莫队】【权值分块】bzoj4129 Haruna’s Breakfast
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
随机推荐
- Security Testing Test Scenarios
1 check for sql injection attacks2 secure pages should use https protocol3 page crash should not rev ...
- selenium之测试角色管理页面举例
# 测试角色权限管理页面功能 # author:xr # date:2017-07-20 from selenium import webdriver from login_page import L ...
- SQL连接:内连接、外连接、交叉连接。
SQL连接可以分为内连接.外连接.交叉连接. 数据库数据: book表 stu表 1.内连接 ...
- codeforces701C
They Are Everywhere CodeForces - 701C 大B,年轻的口袋妖怪训练师,找到了一个由 n 间从左向右的房间组成的大房子.你可以从街道里走入任何一间房子,也可以从任何一间 ...
- python+django+uwsgi 搭建环境
第一步: 搭建python环境 最好使用 pyenv可以很好的管理多版本下的python环境 第二步:搭建django环境 使用 pip install django==1.12.1 来安 ...
- MT【49】四次函数求最值
已知$f(x)=(1-x^2)(x^2+ax+b)$的图像关于x=3对称,求$f(x)$的最大值. 解答:显然$-1,7;1,5$是$f(x)=0$的根.故$(x^2+ax+b)=(x-5)(x-7) ...
- 删除linux下的指定文件
要求:删除linux下2天前的指定文件 find 文件问题:在 tmp 目录下有大量包含 picture_* 的临时文件,每天晚上 2:00 对一天前的文件进行清理.之前在 crontab 下跑如下脚 ...
- 06 Zabbix分布式监控和主被动模式
06 Zabbix分布式监控和主被动模式 zabbix proxy设置 使用zabbix代理的好处 监控拥有不可靠的远程区域 当监控项目数以万计的时候使用代理分担zabbix-proxy压力 简化分布 ...
- JOISC 2017 自然公园
吐槽 YMD的课件是真的毒,YYB的也很毒. 题目链接 LOJ sol 我是一个一个Subtask做的... Subtask 1 \(O(n^2)\)枚举每两个点有没有边即可. Subtask 2 链 ...
- 洛谷P3230 比赛
emmmmmm,这个之前讲课的原题居然出到比赛里了. 我怒肝2h+然后A了此题,结果还是被某高一巨佬吊打...... 题意:n个球队两两比赛,胜得3分,败得0分,平得1分. 现有一个总分表,求问可能的 ...