传送门

分块树,代码参考了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. pow,sqrt使用时需注意

    使用时注意类型,可见两者皆不可以用int 1.pow 函数声明: double pow (double base , double exponent); float pow (float base , ...

  2. Python中进度条如何实现

    print源码,参数end默认值为换行符,需要置成空,就会实现打印一行的效果 import time for i in range(100): time.sleep(1)#sleep一秒再输出 # 需 ...

  3. 将jsp页面内容保存到excel(转)

    在此,强调一下搜索时关键词的重要性,这样一下子可以定位到文章,否则处于盲人摸象,毫无目的尴尬境地.本篇就是通过export jsp to excel找到的. 原文地址:How to Export We ...

  4. 使用jquery执行ajax

    $.ajax():返回其创建的XMLHttpRequest对象 回调函数:如果要处理$.ajax()得到的数据,则应该使用回调函数!beforeSend:在发送请求之后调用,需要一个XMLHttpRe ...

  5. ajax stream 一边下载二进制数据一边处理

    最近有在做 stream 下载,并且边下载 stream 边处理功能.解析二进制的功能.最初参考了 flv.js 的 flv stream 下载处理功能,发现他并没有使用的 XMLHttpReques ...

  6. linux命令学习笔记(48):watch命令

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测 一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下 ...

  7. [Codeforces 1139D] Steps to One

    [题目链接] https://codeforces.com/contest/1139/problem/D [算法] 考虑dp 设fi表示现在gcd为i , 期望多少次gcd变为1 显然 , fi = ...

  8. a标签中href="javacript:;" href="javacript:void(0);" href="#"区别

    在使用<a>标签时,经常会绑定其他事件比如onclick,这时候我们会给<a>标签的href属性赋值为“#”,“javacript:;”,“javacript:void(0); ...

  9. jquery鼠标悬停事件hover()

    在JQuery中提供了.hover()事件,hover的第一个参数(匿名方法)表示mouseenter,第二个参数表示mouseleave,即表示可以为hover传递两个参数.如下代码所示: $( & ...

  10. Design:目录

    ylbtech-Design:目录 1.返回顶部 1. http://idesign.qq.com/#!index/feed 2. https://www.behance.net/ 3. 2.返回顶部 ...