3720: Gty的妹子树
3720: Gty的妹子树
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1440 Solved: 482
[Submit][Status][Discuss]
Description
我曾在弦歌之中听过你,
檀板声碎,半出折子戏。
舞榭歌台被风吹去,
岁月深处尚有余音一缕……
Gty神(xian)犇(chong)从来不缺妹子……
他来到了一棵妹子树下,发现每个妹子有一个美丽度……
由于Gty很哲♂学,他只对美丽度大于某个值的妹子感兴趣。
他想知道某个子树中美丽度大于k的妹子个数。
某个妹子的美丽度可能发生变化……
树上可能会出现一只新的妹子……
维护一棵初始有n个节点的有根树(根节点为1),树上节点编号为1-n,每个点有一个权值wi。
支持以下操作:
0 u x 询问以u为根的子树中,严格大于x的值的个数。(u^=lastans,x^=lastans)
1 u x 把u节点的权值改成x。(u^=lastans,x^=lastans)
2 u x 添加一个编号为"当前树中节点数+1"的节点,其父节点为u,其权值为x。(u^=lastans,x^=lastans)
最开始时lastans=0。
Input
输入第一行包括一个正整数n(1<=n<=30000),代表树上的初始节点数。
接下来n-1行,每行2个整数u,v,为树上的一条无向边。
任何时刻,树上的任何权值大于等于0,且两两不同。
接下来1行,包括n个整数wi,表示初始时每个节点的权值。
接下来1行,包括1个整数m(1<=m<=30000),表示操作总数。
接下来m行,每行包括三个整数 op,u,v:
op,u,v的含义见题目描述。
保证题目涉及的所有数在int内。
Output
对每个op=0,输出一行,包括一个整数,意义见题目描述。
Sample Input
1 2
10 20
1
0 1 5
Sample Output
HINT
Source
分析:
这个SIZE值要注意,由于有二分操作,所以大小定为2.0*sqrt(n)*log2(n)比较好,而不是sqrt(n),证明略。
加点的时候,分两种情况讨论,
1.如果x节点所在块的数量还没有达到最大值,那就把y节点加进去,然后对整个序列快排。
2.如果达到了最大的值,就新建一个块。
最后询问的时候,由于每一次操作之后块里存的数组都是有序的,因此查找只需要二分。
写两个递归的query函数,在不整的块中暴力查找,在整的块中二分查找
g:存储树的形态
block:储存属于root的子树,且超过SIZE,另行分块的节点
g.del[i]:i边是否已重复,保证块外往块内转移不会出错
然后看代码
luogu's #1&&cogs's #1&&bzoj's #3 (id:bbsh)
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
const int N=6e4+,M=N<<;
int n,m,last,size[N],sum,SIZE,w[N],top[N];
struct node{
int v[M],next[M],head[M],tot;
bool del[M];
inline void add(int x,int y){
v[++tot]=y;next[tot]=head[x];head[x]=tot;
}
}g,block,linked;
vector<int>list[N];
inline void init(int u,int f){
int root=top[u];
list[root].push_back(w[u]);
for(int i=g.head[u];i;i=g.next[i]){
if(g.v[i]==f){g.del[i]=;continue;}
if(size[root]<SIZE)
size[root]++,top[g.v[i]]=root;
else
block.add(root,g.v[i]);
init(g.v[i],u);
}
}
inline void query_block(int u,int x){
sum+=list[u].end()-upper_bound(list[u].begin(),list[u].end(),x);
for(int i=block.head[u];i;i=block.next[i])
query_block(block.v[i],x);
}
inline void query_out_board(int u,int x){
if(w[u]>x) sum++;
for(int i=g.head[u];i;i=g.next[i]){
if(g.del[i]) continue;
if(top[u]==top[g.v[i]])
query_out_board(g.v[i],x);
else
query_block(g.v[i],x);
}
}
int main(){
#ifndef online_judge
freopen("gtygirltree.in","r",stdin);
freopen("gtygirltree.out","w",stdout);
#endif
n=read();
SIZE=(int)ceil(2.0*sqrt(n)*log2(n));
for(int i=,x,y;i<n;i++){
x=read();y=read();
g.add(x,y);g.add(y,x);
}
for(int i=;i<=n;i++) w[i]=read(),top[i]=i,size[i]=;
init(,);
for(int i=;i<=n;i++) if(top[i]==i) sort(list[i].begin(),list[i].end());
m=read(),last=;
for(int opt,u,x,tp;m--;){
opt=read();u=read()^last;x=read()^last;
if(!opt){
sum=;
if(u==top[u])
query_block(u,x);
else
query_out_board(u,x);
printf("%d\n",last=sum);
}
else if(opt==){
tp=top[u];
list[tp].erase(lower_bound(list[tp].begin(),list[tp].end(),w[u]));
list[tp].insert(lower_bound(list[tp].begin(),list[tp].end(),x),x);
w[u]=x;
}
else{
w[++n]=x;
tp=top[u];
g.add(u,n);
if(size[tp]<SIZE){
top[n]=tp;
size[tp]++;
list[tp].insert(lower_bound(list[tp].begin(),list[tp].end(),x),x);
}
else{
top[n]=n;
size[u]=;
list[n].push_back(x);
block.add(tp,n);
}
}
}
return ;
}
3720: Gty的妹子树的更多相关文章
- BZOJ 3720 gty的妹子树
块状树裸题 块状树: 首先对树进行分块,分出的每一块都是一个连通块 通常的分块的方式如下: 1.父亲所在块不满,分到父亲所在块中 2.父亲所在块满,自己单独开一个块 (貌似有更为优越的分块方式? 注意 ...
- Gty的妹子树(bzoj 3720)
Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...
- BZOJ3720 Gty的妹子树
Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕…… Gty神(xian)犇(chong)从来不缺妹子…… 他来到了一棵妹子树下,发现每 ...
- 【块状树】bzoj3720 Gty的妹子树
块状树.教程见:http://z55250825.blog.163.com/blog/static/1502308092014163413858/将树按一定大小分块,分成许多子树,在每个子树的根节点记 ...
- bzoj 3720: Gty的妹子树 块状树
3720: Gty的妹子树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 412 Solved: 153[Submit][Status] Descr ...
- bzoj 3720 Gty的妹子树 树分块?瞎搞
Gty的妹子树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2149 Solved: 781[Submit][Status][Discuss] D ...
- BZOJ 3731: Gty的超级妹子树
3731: Gty的超级妹子树 Time Limit: 7 Sec Memory Limit: 32 MBSubmit: 346 Solved: 96[Submit][Status][Discus ...
- [bzoj 3720] Gty的妹子树 (树上分块)
树上分块(块状树) Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕-- Gty神(xian)犇(chong)从来不缺妹子-- 他来到了 ...
- bzoj3731: Gty的超级妹子树
一代神题啊orz(至少是以前年代的神题吧) 块状树 复杂度nsqrtnlogn 真是exciting 还没有卡时限 话不多说直接上代码 (最近解锁了记事本写代码的技能...感觉越来越依赖OJ调试了.. ...
随机推荐
- Topcoder SRM 666 DIV 1
WalkOverATree 题意:给你一棵树,有个人在节点0,现在问你,这个人走L步,最多能访问多少个不同的节点,一个节点可以被走多次,但只算一次. 题解:这个问题的关键在于,每个点最多走两次,这是因 ...
- jquery_final
第一章 jquery入门 1,jquery的引入 <script type="text/javascript" src="js/jquery-3.3.1.min.j ...
- nginx 配置静态目录 访问bootstrap
location /static/ { alias /Users/wangziqiang/djangoprojects/bpmTest/static/; } 注意 /static/ 中 /的完整 ...
- MFC中 编辑框输入换行功能
首先修改编辑框的属性: Multiline 设为true , Auto HScroll 设为true , Auto VScroll 设为 true . 然后响应PreTranslateMessage( ...
- 用循环将三个DIV变成红色
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- struts2实现文件查看、下载
CreateTime--2017年9月7日10:25:33 Author:Marydon struts2实现文件查看.下载 1.界面展示 <a style="color: #199 ...
- unity 切圆角矩形 --shader编程
先上个效果图 制作思路 如上图我们要渲染的就是上图带颜色的部分 步骤: 先获取黄色和蓝绿部分 例如以下图 算法 |U|<(0.5-r)或|V|<(0.5-r) 注意的是模型贴图最大值是1. ...
- RF --系统关键字开发
需求: 接收一个目录路径,自动遍历目录下以及子目录下的所有批处理(.bat) 文件并执行. 首先在..\Python27\Lib\site-packages 目录下创建 CustomLibrary 目 ...
- linux - console/terminal/virtual console/pseudo terminal ...
http://en.wikipedia.org/wiki/System_console System console Knoppix system console showing the boot p ...
- UML类图简明教程
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...