【块状树】bzoj3720 Gty的妹子树
块状树。
教程见: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的妹子树的更多相关文章
- BZOJ3720 Gty的妹子树
Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...
- BZOJ 3720 gty的妹子树
块状树裸题 块状树: 首先对树进行分块,分出的每一块都是一个连通块 通常的分块的方式如下: 1.父亲所在块不满,分到父亲所在块中 2.父亲所在块满,自己单独开一个块 (貌似有更为优越的分块方式? 注意 ...
- 3720: Gty的妹子树
3720: Gty的妹子树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1440 Solved: 482[Submit][Status][Disc ...
- Gty的妹子树(bzoj 3720)
Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...
- bzoj 3720: Gty的妹子树 块状树
3720: Gty的妹子树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 412 Solved: 153[Submit][Status] Descr ...
- 【块状树】bzoj3731 Gty的超级妹子树
带 加点 删边的块状树. 加点在 bzoj3720 说过. 删边其实就是块顶打标记,记录其属于哪棵树,防止在dfs搜集答案时跑到别的树上. 然后暴力把所在块拆开. 好像用邻接表存图,直接在vector ...
- 【BZOJ3720】Gty的妹子树 块状树
[BZOJ3720]Gty的妹子树 我曾在弦歌之中听过你,檀板声碎,半出折子戏.舞榭歌台被风吹去,岁月深处尚有余音一缕……Gty神(xian)犇(chong)从来不缺妹子……他来到了一棵妹子树下,发现 ...
- BZOJ 3731: Gty的超级妹子树
3731: Gty的超级妹子树 Time Limit: 7 Sec Memory Limit: 32 MBSubmit: 346 Solved: 96[Submit][Status][Discus ...
- bzoj Gty的超级妹子树 块状树
Gty的超级妹子树 Time Limit: 7 Sec Memory Limit: 32 MBSubmit: 500 Solved: 122[Submit][Status][Discuss] De ...
随机推荐
- WCF分布式开发步步为赢(13):WCF服务离线操作与消息队列MSMQ
之前曾经写过一个关于MSMQ消息队列的文章:WCF分布式开发必备知识(1):MSMQ消息队列 ,当时的目的也是用它来作为学习WCF 消息队列MSMQ编程的基础文章.在那篇文章里,我们详细介绍了MSMQ ...
- HDU 1203 01背包
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Codeforces Round #525 (Div. 2)D. Ehab and another another xor problem
D. Ehab and another another xor problem 题目链接:https://codeforces.com/contest/1088/problem/D Descripti ...
- 解决mysql的日志文件过大的问题
https://www.2cto.com/database/201203/122984.html
- php getimagesize()函数获取图片宽度高度
//php自带函数 getimagesize() $img_info = getimagesize('tomener.jpg'); echo '<pre>'; print_r($img_i ...
- es6+最佳入门实践(10)
10.Generator 10.1.Generator是什么? Generator函数是ES6提供的一种异步编程解决方案.在它的内部封装了多个状态,因此,又可以理解为一种状态机,执行Generator ...
- Python基础(4)_集合、布尔类型
一.集合 集合的作用一:关系运算集合的作用二:去重 定义集合:集合内的元素必须是唯一的:集合内的元素必须是可hash的,也是就不可变类型:集合是无序的 s={'egon',123,'egon','1' ...
- Nios II 中的缓存和内存数据的读写
nios 使用地址中31bit来表示访问是否bypass cache.如果bit 31=0 表示不bypass cache,即使用cache里的数据:如果bit 31=1表示bypass cache, ...
- R的农场
R的农场 题目描述 最近,R 终于获得了一片他梦寐以求的农场,但如此大的一片农场,想要做好防卫工作可不是一件容易的事.所以 R 购买了 N 个守卫,分别让他们站在一定的位置上(守卫不可移动,同一位置上 ...
- 哪些情况会导致OOM
1. 堆溢出 java堆用于存储对象实例,只要不断地创建对象,并且这些对象不会被回收(什么情况对象不会被回收呢?如:由于GC Root到对象之间有可达路径,所以垃圾回收机制不会清除这些对象),那么,当 ...