BZOJ4129: Haruna’s Breakfast
Description
Haruna每天都会给提督做早餐! 这天她发现早饭的食材被调皮的 Shimakaze放到了一棵
Input
第一行包括两个整数n,m,代表树上的结点数(标号为1~n)和操作数。
Output
对于每次询问,输出该链的mex值。
Sample Input
1 0 1 0 2 4 4 0 1 0
1 2
2 3
2 4
2 5
1 6
6 7
2 8
3 9
9 10
0 7 14
1 6 6
0 4 9
1 2 2
1 1 8
1 8 3
0 10 9
1 3 5
0 10 0
0 7 7
Sample Output
1
2
2
3
HINT
1<=n<=5*10^4
1.加入一个元素
#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=<<;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=,f=;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-;
for(;isdigit(c);c=Getchar()) x=x*+c-'';
return x*f;
}
const int maxn=;
int n,m,SIZE,val[maxn],first[maxn],next[maxn<<],to[maxn<<],e;
void AddEdge(int u,int v) {
to[++e]=v;next[e]=first[u];first[u]=e;
to[++e]=u;next[e]=first[v];first[v]=e;
}
int anc[maxn][],dep[maxn],S[maxn],blo[maxn],nu[maxn],top,ToT;
void dfs(int x,int fa) {
dep[x]=dep[anc[x][]=fa]+;nu[x]=top;
rep(i,,) anc[x][i]=anc[anc[x][i-]][i-];
ren if(to[i]!=fa) {
dfs(to[i],x);
if(top-nu[x]>SIZE) {
ToT++;while(top>nu[x]) blo[S[top--]]=ToT;
}
}
S[++top]=x;
}
int lca(int x,int y) {
if(dep[x]<dep[y]) swap(x,y);
dwn(i,,) if(<<i<=dep[x]-dep[y]) x=anc[x][i];
dwn(i,,) if(anc[x][i]!=anc[y][i]) x=anc[x][i],y=anc[y][i];
return x==y?x:anc[x][];
}
struct Query {
int x,y,t,id;
bool operator < (const Query& ths) const {
if(blo[x]!=blo[ths.x]) return blo[x]<blo[ths.x];
if(blo[y]!=blo[ths.y]) return blo[y]<blo[ths.y];
return t<ths.t;
}
}A[maxn];
int ans[maxn],cnt[maxn],vis[maxn],X[maxn],Y[maxn],Z[maxn],M,N;
int bel[maxn],st[maxn],en[maxn],bans[maxn];
void U(int x) {
int v=val[x];
if(!vis[x]) {
if(!cnt[v]) bans[bel[v]]++;
cnt[v]++;
}
else {
cnt[v]--;
if(!cnt[v]) bans[bel[v]]--;
}
vis[x]^=;
}
void update(int x,int v) {
v=min(v,n+);
if(vis[x]) U(x),val[x]=v,U(x);
else val[x]=v;
}
void move(int x,int y) {
if(dep[x]<dep[y]) swap(x,y);
while(dep[x]!=dep[y]) U(x),x=anc[x][];
while(x!=y) U(x),U(y),x=anc[x][],y=anc[y][];
}
int query() {
rep(i,,bel[n+]) if(bans[i]!=en[i]-st[i]+) {
rep(j,st[i],en[i]) if(!cnt[j]) return j;
return -;
}
}
void init() {
int SIZE2=(int)sqrt(n);
rep(i,,n+) {
bel[i]=i/SIZE2+;
en[bel[i]]=i;
if(!i||bel[i]!=bel[i-]) st[bel[i]]=i;
}
}
int main() {
n=read();m=read();SIZE=(int)pow(n,0.666)*0.7;
rep(i,,n) val[i]=min(n+,read());
rep(i,,n) AddEdge(read(),read());
dfs(,);ToT++;rep(i,,top) blo[S[i]]=ToT;
memset(ans,-,sizeof(ans));init();
rep(i,,m) {
int t=read(),x=read(),y=read();
if(!t) X[++M]=x,Y[M]=val[x],val[x]=Z[M]=y;
else {
if(blo[x]>blo[y]) swap(x,y);
A[++N]=(Query){x,y,M,i};
}
}
sort(A+,A+N+);
int cu=,cv=,t=M;
rep(i,,N) {
int x=A[i].x,y=A[i].y,z=lca(x,y);
while(t<A[i].t) t++,update(X[t],Z[t]);
while(t>A[i].t) update(X[t],Y[t]),t--;
move(cu,x);move(cv,y);cu=x;cv=y;
U(z);ans[A[i].id]=query();U(z);
}
rep(i,,m) if(ans[i]>=) printf("%d\n",ans[i]);
return ;
}
BZOJ4129: Haruna’s Breakfast的更多相关文章
- [BZOJ4129]Haruna’s Breakfast(树上带修改莫队)
BZOJ3585,BZOJ2120,BZOJ3757三合一. 对于树上路径问题,树链剖分难以处理的时候,就用树上带修改莫队. 这里的MEX问题,使用BZOJ3585的分块方法,平衡了时间复杂度. 剩下 ...
- 【树上莫队】【带修莫队】【权值分块】bzoj4129 Haruna’s Breakfast
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using ...
- bzoj4129 Haruna’s Breakfast 莫队
这个思想不难理解了前面几个就能懂 但是代码比较复杂,大概会和之前几次碰到难题的时候一样,一步步思考下去,然后把难点分成好几个板块讲下qwq 首先读入这颗树,预处理下lca,然后就分块,这个时候就会碰到 ...
- bzoj4129 Haruna’s Breakfast 树上带修莫队+分块
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4129 题解 考虑没有修改的序列上的版本应该怎么做: 弱化的题目应该是这样的: 给定一个序列,每 ...
- 【BZOJ4129】Haruna’s Breakfast(树上莫队)
[BZOJ4129]Haruna's Breakfast(树上莫队) 题面 BZOJ Description Haruna每天都会给提督做早餐! 这天她发现早饭的食材被调皮的 Shimakaze放到了 ...
- BZOJ 4129 Haruna’s Breakfast (分块 + 带修莫队)
4129: Haruna’s Breakfast Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 835 Solved: 409[Submit][St ...
- 【bzoj4129】Haruna’s Breakfast 带修改树上莫队+分块
题目描述 给出一棵树,点有点权.支持两种操作:修改一个点的点权,查询链上mex. 输入 第一行包括两个整数n,m,代表树上的结点数(标号为1~n)和操作数.第二行包括n个整数a1...an,代表每个结 ...
- 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 ...
随机推荐
- set_include_path详细解释
zendframework的示例index.php里有这样一句 set_include_path('.' . PATH_SEPARATOR . '../library/'. PATH_SEPARATO ...
- JetBrains WebStorm 7.0 Build 131.202 Win/Mac/Liniux
JetBrains WebStorm 7.0 Build 131.202 (Win/Mac/Liniux) | 121.6/106/133 Mb WebStorm 7 — Everything you ...
- MYSQL索引失效的各种情形总结
1) 没有查询条件,或者查询条件没有建立索引 2) 在查询条件上没有使用引导列 3) 查询的数量是大表的大部分,应该是30%以上. 4) 索引本身失效 5) 查询条件使用函数在索引列上,或者对索 ...
- Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. Example Given 1-> ...
- Light OJ 1393 Crazy Calendar (尼姆博弈)
C - Crazy Calendar Time Limit:4000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Su ...
- Markdown 语法速查表
Markdown 语法速查表 1 标题与文字格式 标题 # 这是 H1 <一级标题> ## 这是 H2 <二级标题> ###### 这是 H6 <六级标题> 文 ...
- 家族(codevs 1073)
题目描述 Description 若某个家族人员过于庞大,要判断两个是否是亲戚,确实还很不容易,现在给出某个亲戚关系图,求任意给出的两个人是否具有亲戚关系. 规定:x和y是亲戚,y和z是亲戚,那么x和 ...
- Mac相关命令
1,查询端口占用与Kill相应进程 lsof -i:端口,查询端口的占用情况 kill PID,关闭指定PID的进程. 如: localhost:~ tianjingcheng$ kill 729 l ...
- 玩转Chrome workplace
简介 相信大家都知道Chrome Develop Tools中的workplace,这使得我们能够让本地的文件夹在Source面板下可以编辑.没错,这使得chrome同样可以成为我们可选的编辑器之一, ...
- 【读书笔记】读《JavaScript设计模式》之代理模式
一.定义 代理是一个对象,它可以用来控制对另一个对象的访问.它与另外那个对象实现了同样的接口,并且会把任何方法调用传递给那个对象.另外那个对象通常称为本体.代理可以代替其实体被实例化,并使其可被远程访 ...