块状树。
教程见:http://z55250825.blog.163.com/blog/static/1502308092014163413858/
将树按一定大小分块,分成许多子树,在每个子树的根节点记录一个数组,存储这个块中数据的有序表,查询时二分即可。

对于添加节点操作,若添加的节点的父节点所在块大小没有超过size,则添加到上面的块里,否则另起一块。

这个size值要注意,由于有二分操作,所以大小定为sqrt(n*log2(n))比较好,而不是sqrt(n),证明略。

时间复杂度O(n*sqrt(n)*log(sqrt(n)))。

 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 60001
typedef vector<int>::iterator ITER;
vector<int>List[maxn],Goto[maxn];
int res,CH[],Num;char c;
inline int Ge()
{
res=;c='*';
while(c<''||c>'')c=getchar();
while(c>=''&&c<=''){res=res*+(c-'');c=getchar();}
return res;
}
inline void P(int x)
{
if(!x){putchar('');puts("");return;}Num=;
while(x>){CH[++Num]=x%;x/=;}
while(Num)putchar(CH[Num--]+);
putchar('\n');
}
struct Graph
{
int v[maxn<<],first[maxn<<],next[maxn<<],en;
void AddEdge(const int &a,const int &b)
{v[++en]=b;next[en]=first[a];first[a]=en;}
};
Graph G,son;
int top[maxn],siz[maxn],sz,w[maxn];
bool vis[maxn];
int n,x,y,m,op;
int ans,val,U;
void makeblock(int cur)
{
vis[cur]=true;
for(int i=G.first[cur];i;i=G.next[i])
if(!vis[G.v[i]])
{
son.AddEdge(cur,G.v[i]);
if(siz[top[cur]]<sz)
{
List[top[cur]].push_back(w[G.v[i]]);
siz[top[cur]]++;
top[G.v[i]]=top[cur];
}
makeblock(G.v[i]);
}
}
void makeGoto(int cur)
{
for(int i=son.first[cur];i;i=son.next[i])
{
if(top[son.v[i]]!=top[cur])
Goto[top[cur]].push_back(son.v[i]);
makeGoto(son.v[i]);
}
}
void dfs(int cur)
{
ans+=( List[cur].end() - upper_bound( List[cur].begin() , List[cur].end() , val ) );
for(ITER it=Goto[cur].begin();it!=Goto[cur].end();it++)
dfs(*it);
}
void dfs_block(int cur)
{
if(w[cur]>val) ans++;
for(int i=son.first[cur];i;i=son.next[i])
if(top[son.v[i]]==top[cur]) dfs_block(son.v[i]);
else dfs(son.v[i]);
}
void query()
{
ans=;
if(U==top[U]) dfs(U);
else dfs_block(U);
P(ans);
}
void update()
{
List[top[U]].erase( lower_bound(List[top[U]].begin(),List[top[U]].end(),w[U]) );
w[U]=val;
List[top[U]].insert( lower_bound(List[top[U]].begin(),List[top[U]].end(),val+) , val );
}
void AddPoint()
{
n++;
if(siz[top[U]]<sz)
{
top[n]=top[U];
siz[top[n]]++;
}
else
{
top[n]=n;
siz[n]++;
Goto[top[U]].push_back(n);
}
son.AddEdge(U,n);
w[n]=val;
List[top[n]].insert( lower_bound(List[top[n]].begin(),List[top[n]].end(),val+) , val );
}
int main()
{
n=Ge();
for(int i=;i<n;i++)
{
x=Ge();y=Ge();
G.AddEdge(x,y);
G.AddEdge(y,x);
}
sz=sqrt((double)n*log2(n));
for(int i=;i<=n;i++)
{
w[i]=Ge();
top[i]=i;
siz[i]=;
}
makeblock();
for(int i=;i<=n;i++)
if(top[i]==i)
{
List[i].push_back(w[i]);
sort(List[i].begin(),List[i].end());
}
makeGoto();
m=Ge();
for(int i=;i<=m;i++)
{
op=Ge();U=Ge();val=Ge();U^=ans;val^=ans;
if(!op)query();
else if(op==)update();
else AddPoint();
}
return ;
}

【块状树】bzoj3720 Gty的妹子树的更多相关文章

  1. BZOJ3720 Gty的妹子树

    Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...

  2. BZOJ 3720 gty的妹子树

    块状树裸题 块状树: 首先对树进行分块,分出的每一块都是一个连通块 通常的分块的方式如下: 1.父亲所在块不满,分到父亲所在块中 2.父亲所在块满,自己单独开一个块 (貌似有更为优越的分块方式? 注意 ...

  3. 3720: Gty的妹子树

    3720: Gty的妹子树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1440  Solved: 482[Submit][Status][Disc ...

  4. Gty的妹子树(bzoj 3720)

    Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...

  5. bzoj 3720: Gty的妹子树 块状树

    3720: Gty的妹子树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 412  Solved: 153[Submit][Status] Descr ...

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

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

  7. 【BZOJ3720】Gty的妹子树 块状树

    [BZOJ3720]Gty的妹子树 我曾在弦歌之中听过你,檀板声碎,半出折子戏.舞榭歌台被风吹去,岁月深处尚有余音一缕……Gty神(xian)犇(chong)从来不缺妹子……他来到了一棵妹子树下,发现 ...

  8. BZOJ 3731: Gty的超级妹子树

    3731: Gty的超级妹子树 Time Limit: 7 Sec  Memory Limit: 32 MBSubmit: 346  Solved: 96[Submit][Status][Discus ...

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

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

随机推荐

  1. Educational Codeforces Round 54 (Rated for Div. 2) ABCD

    A. Minimizing the String time limit per test 1 second memory limit per test 256 megabytes Descriptio ...

  2. HDU 多校对抗赛 B Balanced Sequence

    Balanced Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. KVO-基本使用方法-底层原理探究-自定义KVO-对容器类的监听

    书读百变,其义自见! 将KVO形式以代码实现呈现,通俗易懂,更容易掌握 :GitHub   -链接如果失效请自动搜索:https://github.com/henusjj/KVO_base 代码中有详 ...

  4. supervisor提高nodejs调试效率

    1.NodeJS环境安装 2.安装supervisor npm install  supervisor -g (表示安装到全局路径下) 开发nodejs程序,调试的时候,无论你修改了代码的哪一部分,都 ...

  5. ssh中的相对路径与绝对路径的问题

    一:前言:自己在学习ssh的时候常常被路径给迷惑,就比如在刚刚学习jsp的servlet时,绝对路径和相对路径我就弄混了,所以专门写了一篇博客来记载.而现在自己是在学ssh的时候在此遇到路径问题,本来 ...

  6. SVG布局

    http://www.w3cplus.com/html5/nesting-svgs.html

  7. BroadCastRecieve

    首先介绍一下BroadCastRecieve有几种: 1.无序广播(普通广播):sendBroadcast()方式 2.有序广播:sendOrderedBroadcast()方式 3.粘性广播:sen ...

  8. 获取高德地图api

    先到高德开放平台首页按照关键字搜索地址,获取经纬度坐标: http://lbs.amap.com/console/show/picker 高德由坐标获取地址详细信息: http://restapi.a ...

  9. npm install 报node-sass错误

    Node Sass could not -bit with Node.js .x Found bindings for the following environments: - OS X -bit ...

  10. gcc 簡單操作

    gcc -c test.c 產出 test.o object file gcc -c test.c -o XXX 產出 XXX object file gcc test.c -o aaa 產出 aaa ...