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 ...
随机推荐
- aop 记录用户操作(一)
转载: http://www.cnblogs.com/guokai870510826/p/5981015.html 使用标签来设置需要的记录 实例:@ISystemLog() @Controller ...
- (转)二分图匹配匈牙利算法与KM算法
匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...
- Codeforces Round #337 (Div. 2) B. Vika and Squares
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces Round #418 (Div. 2) B. An express train to reveries
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- mysql中存储过程
存储过程procedure 存储过程,其本质还是函数——但其规定:不能有返回值: 定义形式: 说明: 1,in:用于设定该变量是用来“接收实参数据”的,即“传入”:默认不写,就是in 2,out:用于 ...
- 洛谷 P2725 邮票 Stamps 解题报告
P2725 邮票 Stamps 题目背景 给一组 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K -- 表示信封上能够贴 K 张邮票.计算从 1 到 M 的最大连续可贴出的邮资. 题目描 ...
- bzoj1007/luogu3194 水平可见直线 (单调栈)
先按斜率从小到大排序,然后如果排在后面的点B和前面的点A的交点是P,那B会把A在P的右半段覆盖掉,A会把B在P的左半段覆盖掉. 然后如果我们现在又进来了一条线,它跟上一条的交点还在上一条和上上条的左边 ...
- 【POJ3090】Visible Lattice Points
题目大意:求 \[\sum\limits_{i=2}^n\phi(i)\] 题解:利用与埃筛类似的操作,可在 \(O(nlogn)\) 时间求出结果. 代码如下 #include <cstdio ...
- 【洛谷P1018】乘积最大 dp+高精度
题目大意:给定一个 N 个数组成的串,可以在串中插入 M 个乘号,求乘积最大是多少.N <= 40 阶段:前 i 个数用了 j 个乘号. 仅用阶段可以表示出一个状态,因此状态转移方程为 \(dp ...
- C# winform C/S WebBrowser 微信第三方登录
网上很多的资料都是B/S结构的,这里是基于C# C/S 结构的微信第三方授权登录 一.准备知识 1 http Get和Post方法.做第三方授权登录,获取信息基本上都是用get和post方法,做之前需 ...