题解:P11195 [COTS 2021] 疫苗接种 Cijepise
莫名其妙的题。
分析
首先这题不需要求没有修改时
但是这题需要我们让
定义状态函数
如果
如果
把上面的情况取交集,发现我们可以改变第一个满足
那么现在需要实现:快速加点或删点,快速查询第一个比
我说的是不是莫名奇妙的。
代码
const int N=1e6+10;
int n,q;
int val[N],f[N];
vector<int> e[N];
multiset<int> st;
il void dfs(int u,int fa){
for(auto v:e[u])
if(v!=fa) st.insert(val[v]);
for(auto v:e[u])
if(v!=fa){
st.erase(st.find(val[v]));
auto it=st.lower_bound(val[v]);
int x=(it==st.end()?-1:(*it));
if(it!=st.end()) f[v]=f[u]+1,st.erase(st.find(x));
else f[v]=f[u];
dfs(v,u);
if(~x) st.insert(x);
st.insert(val[v]);
}
for(auto v:e[u])
if(v!=fa) st.erase(st.find(val[v]));
}
il void solve(){
n=rd;
for(re int i=1;i<=n;++i) val[i]=rd;
for(re int i=1;i< n;++i){
int u=rd,v=rd;
e[u].push_back(v),
e[v].push_back(u);
}
dfs(1,0);
q=rd;
while(q--) cout<<f[rd]<<"\n";
return ;
}