BZOJ 3720: Gty的妹子树 [树上size分块]
题意: 一棵树,询问子树中权值大于$k$的节点个数,修改点权值,插入新点;强制在线
一开始以为询问多少种不同的权值,那道CF的强制在线带修改版,直接吓哭
然后发现看错了这不一道树上分块水题...
用王室联邦分块的话需要维护每一个块$dfs$序最小值和最大值,并且插入操作会破坏原来的性质
不如直接按$size$分块,根节点$size<block$就加入根,否则新建块
$size$分块不能保证块的数量,可以被菊花图卡掉,然而本题没有所以就可以安心的写了
每个块维护排序后的值
查询操作,不完整的块(因为是$size$分块所以只有子树根所在块不完整)暴力,直接把块建一个图,每个整块二分
修改维护有序,插入也维护有序;当然修改和插入后重新排序也可以
复杂度 修改插入$O(S)$ 查询$O(S+\frac{N}{S}logS)$
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int N=6e4+, M=1e4+, S=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int n,Q,a[N],op,u,x;
struct edge{int v,ne;} e[N<<];
int cnt,h[N];
inline void ins(int u,int v) {
e[++cnt]=(edge){v,h[u]}; h[u]=cnt;
e[++cnt]=(edge){u,h[v]}; h[v]=cnt;
} struct meow{
int a[S], size;
inline void set() {sort(a+, a++size);}
inline int count(int v) {return size - (upper_bound(a+, a++size, v) - a) + ;}
inline void push(int v) {a[++size]=v;}
inline void replace(int x,int v) {
if(x==v) return;
for(int i=;i<=size;i++) if(a[i]==x) {
if(v>x) while(i<size && v>a[i+]) a[i]=a[i+], i++;
else while(i> && v<a[i-]) a[i]=a[i-], i--;
a[i]=v; break;
}
}
inline void insert(int v){
int i;
for(i=; i<=size && a[i]<v; i++) ;
for(int j=size; j>=i; j--) a[j+]=a[j];
a[i]=v; size++;
}
}b[M];
int m, pos[N], block; struct Graph4Block{
struct edge{int v,ne;} e[M];
int cnt,h[M];
inline void ins(int u,int v) {
e[++cnt]=(edge){v,h[u]}; h[u]=cnt;
}
int dfs(int u,int k) {
int ans= b[u].count(k);
for(int i=h[u];i;i=e[i].ne) ans+=dfs(e[i].v, k);
return ans;
}
}G; int fa[N];
void dfs(int u) {
int p=pos[u];
b[p].push(a[u]);
for(int i=h[u];i;i=e[i].ne)
if(e[i].v!=fa[u]) {
fa[e[i].v]=u;
if(b[p].size < block) pos[e[i].v]=p;
else pos[e[i].v]=++m, G.ins(p, m);
dfs(e[i].v);
}
} struct Block{
int dfs(int u,int k) {
int ans= a[u]>k;
for(int i=h[u];i;i=e[i].ne)
if(e[i].v!=fa[u]) {
if(pos[e[i].v] == pos[u]) ans+= dfs(e[i].v, k);
else ans+= G.dfs(pos[e[i].v], k);
}
return ans;
}
int Que(int u, int k) {return dfs(u, k);} void Cha(int u, int d) {b[pos[u]].replace(a[u], d); a[u]=d;} void Ins(int u, int d){
a[++n]=d; ins(u, n); fa[n]=u;
int p=pos[u];
if(b[p].size < block) pos[n]=p, b[p].insert(a[n]);
else pos[n]=++m, b[m].push(a[n]), G.ins(p, m);
}
}B;
int main() {
freopen("in", "r", stdin);
n=read();
for(int i=;i<n;i++) ins(read(), read() );
for(int i=;i<=n;i++) a[i]=read();
block=pow(n, 0.6);
pos[]=++m; dfs();
for(int i=;i<=m;i++) b[i].set(); Q=read(); int lastans=;
for(int i=;i<=Q;i++) {
op=read();
u=read()^lastans; x=read()^lastans;
if(op==) lastans=B.Que(u, x), printf("%d\n",lastans);
else if(op==) B.Cha(u, x);
else B.Ins(u, x);
}
}
BZOJ 3720: Gty的妹子树 [树上size分块]的更多相关文章
- [bzoj 3720] Gty的妹子树 (树上分块)
树上分块(块状树) Description 我曾在弦歌之中听过你, 檀板声碎,半出折子戏. 舞榭歌台被风吹去, 岁月深处尚有余音一缕-- Gty神(xian)犇(chong)从来不缺妹子-- 他来到了 ...
- bzoj 3720: Gty的妹子树 块状树
3720: Gty的妹子树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 412 Solved: 153[Submit][Status] Descr ...
- BZOJ 3731 3731: Gty的超级妹子树 [树上size分块 !]
传送门 题意:一棵树,询问子树中权值大于k的节点个数,修改点权值,插入新点,断开边:强制在线 该死该死该死!!!!!! MD我想早睡觉你知不知道 该死该死沙比提 断开边只会影响一个块,重构这个块就行了 ...
- bzoj 3720 Gty的妹子树 树分块?瞎搞
Gty的妹子树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2149 Solved: 781[Submit][Status][Discuss] D ...
- BZOJ.3720.Gty的妹子树(树分块)
题目链接 洛谷上惨遭爆零是为什么.. 另外这个树分块算法是假的. /* 插入删除只涉及一个数,故每次可以枚举一遍,而不是重构完后sort */ #include<cmath> #inclu ...
- 【BZOJ3720】Gty的妹子树 块状树
[BZOJ3720]Gty的妹子树 我曾在弦歌之中听过你,檀板声碎,半出折子戏.舞榭歌台被风吹去,岁月深处尚有余音一缕……Gty神(xian)犇(chong)从来不缺妹子……他来到了一棵妹子树下,发现 ...
- bzoj 3744: Gty的妹子序列 主席树+分块
3744: Gty的妹子序列 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 101 Solved: 34[Submit][Status] Descr ...
- BZOJ 3744: Gty的妹子序列 【分块 + 树状数组 + 主席树】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3744 3744: Gty的妹子序列 Time Limit: 20 Sec Memory ...
- BZOJ 3744 Gty的妹子序列
Description 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见-- 某天,蒟蒻Autumn发现了从 Gty的妹子树上掉落下来了许多妹子,他发现 她们排成了一个序 ...
随机推荐
- use ambiguous的错误——编译错误
出现这样的问题是因为namespace std里面已经有一个count了,而 using namespace std;语句把该namespace 打开了,这导致了后面的引用不明确: 不过这里也可以把u ...
- .26-浅析webpack源码之事件流make(1)
compilation事件流中,依然只是针对细节步骤做事件流注入,代码流程如图: // apply => this-compilation // apply => compilation ...
- surging 微服务框架使用系列之surging介绍
首先,感谢surging的作者fanliang11为.net开源做出的贡献 其次, surging 的git地址:https://github.com/dotnetcore/surging surgi ...
- Spark算子--map和flatMap
map和flatMap--Transformation类算子 代码示例 result
- mysql中使用show table status 查看表信息
学习标签: mysql 本文导读:在使用mysql数据库时,经常需要对mysql进行维护,查询每个库.每个表的具体使用情况,Mysql数据库可以通过执行SHOW TABLE STATUS命令来获取每个 ...
- PHP和Python如何选择?或许可以考虑这三个问题
撤稿纠错 文/黄小天.李亚洲 (选自Hackernoon 机器之心编译) 2017 年可谓是网页应用与 API 之年,开发者不用每次重新发明轮子,而是利用脚手架和第三方库就能确保项目在几天内实时部署. ...
- 关于VC++中virtual ~的含义
我知道virtual 的虚函数定义,~CMainFrame( )是析构函数,用来释放内存.C++的继承和派生内容.所有可以被用作基类的类一般都用虚析构函数当基类对象的指针或引用调用派生类对象时,如果基 ...
- Java中 equals() 和 == 的区别
1)对于==,如果作用于基本数据类型的变量,则直接比较其存储的 "值"是否相等: 如果作用于引用类型的变量,则比较的是所指向的对象的地址 2)对于equals方法,注意:equal ...
- pyspider解析
https://www.cnblogs.com/microman/p/6111711.html #!/usr/bin/env python # -*- encoding: utf-8 -*- # Cr ...
- AF_INET 和PF_INET区别;AF_LOCAL PF_LOCAL 区别.
从字面理解: AF_INET = Address Format, Internet = IP Addresses PF_INET = Packet Format, Internet = IP, TCP ...