传送门

分块树,代码参考了Manchery的

具体细节还是看代码好了

这题卡常……注意常数写好点……

 //minamoto
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define getc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[<<],*p1=buf,*p2=buf;
inline int read(){
#define num ch-'0'
char ch;bool flag=;int res;
while(!isdigit(ch=getc()))
(ch=='-')&&(flag=true);
for(res=num;isdigit(ch=getc());res=res*+num);
(flag)&&(res=-res);
#undef num
return res;
}
const int N=;
struct Block{
vector<int> a;
inline void insert(int x){
a.insert(lower_bound(a.begin(),a.end(),x+),x);
}
inline void erase(int x){
a.erase(lower_bound(a.begin(),a.end(),x));
}
inline void modify(int x,int y){
erase(x),insert(y);
}
inline int query(int x){
return a.end()-upper_bound(a.begin(),a.end(),x);
}
inline int size(){return a.size();}
}Blo[N];
int tot,ver[N<<],head[N],Next[N<<],first[N];
inline void add(int u,int v){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot;
}
inline void addb(int u,int v){
ver[++tot]=v,Next[tot]=first[u],first[u]=tot;
}
int n,B,a[N],fa[N],belong[N],bat[N],cnt;
void dfs(int u,int f){
fa[u]=f;
if(u==||Blo[belong[f]].size()==B)
Blo[belong[u]=++cnt].insert(a[u]),addb(belong[f],belong[u]),bat[cnt]=belong[f];
else Blo[belong[u]=belong[f]].insert(a[u]);
for(int i=head[u];i;i=Next[i])
if(ver[i]!=f) dfs(ver[i],u);
}
int Y,ans=;
void block_dfs(int u){
ans+=Blo[u].query(Y);
for(int i=first[u];i;i=Next[i])
if(bat[ver[i]]==u) block_dfs(ver[i]);
}
void find(int u,int f){
if(a[u]>Y) ++ans;
for(int i=head[u];i;i=Next[i])
if(ver[i]!=f&&fa[ver[i]]==u)
if(belong[ver[i]]==belong[u]) find(ver[i],u);
else block_dfs(belong[ver[i]]);
}
int c[N],d[N];
void cont(int u,int f){
c[++*c]=u;
for(int i=head[u];i;i=Next[i])
if(ver[i]!=f&&fa[ver[i]]==u)
if(belong[ver[i]]==belong[u]) cont(ver[i],u);
else d[++*d]=belong[ver[i]];
}
int main(){
int q,op,u,v,lastans=;
//freopen("testdata.in","r",stdin);
n=read(),B=sqrt(n);
for(int i=;i<n;++i)
u=read(),v=read(),add(u,v),add(v,u);
for(int i=;i<=n;++i) a[i]=read();
dfs(,);q=read();
while(q--){
op=read();
switch(op){
case :{
u=read(),v=read();
u^=lastans,v^=lastans;
Y=v,ans=;
find(u,fa[u]);
printf("%d\n",lastans=ans);
break;
}
case :{
u=read(),v=read();
u^=lastans,v^=lastans;
Blo[belong[u]].modify(a[u],v);
a[u]=v;
break;
}
case :{
u=read(),v=read();
u^=lastans,v^=lastans;
a[++n]=v;
add(u,n),add(n,u);
fa[n]=u;
if(Blo[belong[u]].size()==B)
Blo[belong[n]=++cnt].insert(a[n]),addb(belong[u],belong[n]),bat[cnt]=belong[u];
else Blo[belong[n]=belong[u]].insert(a[n]);
break;
}
case :{
u=read(),u^=lastans;
if(belong[u]!=belong[fa[u]]){
fa[u]=,bat[belong[u]]=;break;
}
cont(u,fa[u]);
belong[u]=++cnt;
for(int i=;i<=*c;++i){
Blo[belong[fa[u]]].erase(a[c[i]]);
Blo[cnt].insert(a[c[i]]);
belong[c[i]]=cnt;
}
for(int i=;i<=*d;++i)
addb(cnt,d[i]),bat[d[i]]=cnt;
fa[u]=,*c=*d=;
break;
}
}
}
return ;
}

bzoj3731: Gty的超级妹子树(树分块)的更多相关文章

  1. [BZOJ 3731] Gty的超级妹子树 (树分块)

    [BZOJ 3731] Gty的超级妹子树 (树分块) 题面 给出一棵树(或森林),每个点都有一个值.现在有四种操作 1.查询x子树里>y的值有多少个 2.把点x的值改成y 3.添加一个新节点, ...

  2. bzoj Gty的超级妹子树 块状树

    Gty的超级妹子树 Time Limit: 7 Sec  Memory Limit: 32 MBSubmit: 500  Solved: 122[Submit][Status][Discuss] De ...

  3. BZOJ 3731 3731: Gty的超级妹子树 [树上size分块 !]

    传送门 题意:一棵树,询问子树中权值大于k的节点个数,修改点权值,插入新点,断开边:强制在线 该死该死该死!!!!!! MD我想早睡觉你知不知道 该死该死沙比提 断开边只会影响一个块,重构这个块就行了 ...

  4. 【块状树】bzoj3731 Gty的超级妹子树

    带 加点 删边的块状树. 加点在 bzoj3720 说过. 删边其实就是块顶打标记,记录其属于哪棵树,防止在dfs搜集答案时跑到别的树上. 然后暴力把所在块拆开. 好像用邻接表存图,直接在vector ...

  5. bzoj3731: Gty的超级妹子树

    一代神题啊orz(至少是以前年代的神题吧) 块状树 复杂度nsqrtnlogn 真是exciting 还没有卡时限 话不多说直接上代码 (最近解锁了记事本写代码的技能...感觉越来越依赖OJ调试了.. ...

  6. BZOJ 1086 & 类树的分块

    题意: “余”人国的国王想重新编制他的 国家.他想把他的国家划分成若干个省,每个省都由他们王室联邦的一个成员来管理.他的国家有n个城市,编号为1..n.一些城市之间有道路相连,任意两个 不同的城市之间 ...

  7. 「日常训练&知识学习」树的分块(王室联邦,HYSBZ-1086)

    题意与分析 这题的题意就是树分块,更具体的看题目(中文题). 学习这一题是为了树的分块,为树上莫队做铺垫. 参考1:https://blog.csdn.net/LJH_KOQI/article/det ...

  8. Educational Codeforces Round 22 E. Army Creation 主席树 或 分块

    http://codeforces.com/contest/813/problem/E 题目大意: 给出长度为n的数组和k,  大小是1e5级别. 要求在线询问区间[l, r]权值,  权值定义为对于 ...

  9. HYSBZ 1086 王室联邦 (树的分块)

    题意:国王想把他的国家划分成若干个省.他的国家有n个城市,是一棵树,即n-1条边,编号为1..n.为了防止管理太过分散,每个省至少要有B个城市,为了能有效的管理,每个省最多只有3B个城市.每个省必须有 ...

随机推荐

  1. Python基础-内置函数总结

    内置函数 int('123') float() string() tuple() set() dict(name='zdd',age=18) type()#查看类型 len()#看长度,其实是元素的个 ...

  2. php 代码中的箭头“ ->”与“=>”是什么意思?

    类是一个复杂数据类型,这个类型的数据主要有属性.方法两种东西. 属性其实是一些变量,可以存放数据,存放的数据可以是整数.字符串,也可以是数组,甚至是类. 方法实际上是一些函数,用来完成某些功能. 引用 ...

  3. 几个常用的url生成二维码的接口

    找到了几个URL生成的接口,速度上可能会有差别,可试验后选用,我用过第一个,分享: <!doctype html> <html lang="en"> < ...

  4. luogu1901 发射站

    单调栈 正着插一遍反着插一遍 记录每个点左边右边第一个比他高的... yyc太强辣 #include<iostream> #include<cstdlib> #include& ...

  5. P1607 [USACO09FEB]庙会班车Fair Shuttle

    题目描述 Although Farmer John has no problems walking around the fair to collect prizes or see the shows ...

  6. 【C++】标准库sort函数的自定义排序

    自定义排序需要单独写一个compare函数 例1 LeetCode 056. Merge Intervals Given a collection of intervals, merge all ov ...

  7. mouseout与mouseleave的区别

    1 mouseout:当鼠标指针从元素上移开时,发生 mouseout 事件.该事件大多数时候会与 mouseover 事件一起使用. 2 mouseout与 mouseleave 事件不同,不论鼠标 ...

  8. 解析CmdLine参数

    UBOOL ParseParam( const TCHAR* Stream, const TCHAR* Param, UBOOL bAllowQuoted ) { const TCHAR* Start ...

  9. angular项目线上地址跳转或刷新报错的解决

    引用地址:https://blog.csdn.net/qq_35415307/article/details/80707463 本地ng项目没问题,到了线上跳转刷新都会报404错误,相信这个问题每个做 ...

  10. HBase 官方文档

    HBase 官方文档 Copyright © 2010 Apache Software Foundation, 盛大游戏-数据仓库团队-颜开(译) Revision History Revision ...