【bzoj4154】[Ipsc2015]Generating Synergy KD-tree
题目描述
输入
输出
样例输入
1
4 3 7
1 2 2
3 0 0
2 1 3
3 0 0
1 0 2
2 0 0
4 1 1
4 0 0
样例输出
32
题解
KD-tree
“子树内”是dfs序限制,“距离不超过l”是深度限制。对满足两种限制的点的修改,可以将其看作平面上的点,修改相当于矩形修改,使用lazy标记+pushdown即可。
时间复杂度$O(n\sqrt n)$
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 100010
using namespace std;
int head[N] , to[N] , next[N] , cnt , deep[N] , pos[N] , last[N] , tot , d , root;
struct data
{
int p[2] , mx[2] , mn[2] , c[2] , w , tag;
bool operator<(const data &a)const {return p[d] == a.p[d] ? p[d ^ 1] < a.p[d ^ 1] : p[d] < a.p[d];}
}a[N];
inline void add(int x , int y)
{
to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt;
}
void dfs(int x)
{
int i;
pos[x] = ++tot , a[x].p[0] = pos[x] , a[x].p[1] = deep[x];
for(i = head[x] ; i ; i = next[i])
deep[to[i]] = deep[x] + 1 , dfs(to[i]);
last[x] = tot;
}
inline void pushup(int x)
{
int l = a[x].c[0] , r = a[x].c[1];
a[x].mx[0] = max(a[x].p[0] , max(a[l].mx[0] , a[r].mx[0]));
a[x].mx[1] = max(a[x].p[1] , max(a[l].mx[1] , a[r].mx[1]));
a[x].mn[0] = min(a[x].p[0] , min(a[l].mn[0] , a[r].mn[0]));
a[x].mn[1] = min(a[x].p[1] , min(a[l].mn[1] , a[r].mn[1]));
}
int build(int l , int r , int now)
{
if(l > r) return 0;
int mid = (l + r) >> 1;
d = now , nth_element(a + l , a + mid , a + r + 1);
a[mid].w = 1 , a[mid].tag = 0;
a[mid].c[0] = build(l , mid - 1 , now ^ 1);
a[mid].c[1] = build(mid + 1 , r , now ^ 1);
pushup(mid);
return mid;
}
inline void pushdown(int x)
{
if(a[x].tag)
{
int l = a[x].c[0] , r = a[x].c[1];
a[l].w = a[l].tag = a[r].w = a[r].tag = a[x].tag;
a[x].tag = 0;
}
}
void update(int bx , int ex , int by , int ey , int v , int x)
{
if(!x || a[x].mx[0] < bx || a[x].mn[0] > ex || a[x].mx[1] < by || a[x].mn[1] > ey) return;
if(a[x].mn[0] >= bx && a[x].mx[0] <= ex && a[x].mn[1] >= by && a[x].mx[1] <= ey)
{
a[x].w = a[x].tag = v;
return;
}
pushdown(x);
if(a[x].p[0] >= bx && a[x].p[0] <= ex && a[x].p[1] >= by && a[x].p[1] <= ey) a[x].w = v;
update(bx , ex , by , ey , v , a[x].c[0]) , update(bx , ex , by , ey , v , a[x].c[1]);
}
int query(int px , int py , int x)
{
d ^= 1;
if(a[x].p[0] == px && a[x].p[1] == py) return a[x].w;
pushdown(x);
if(d)
{
if(py < a[x].p[1] || (py == a[x].p[1] && px < a[x].p[0])) return query(px , py , a[x].c[0]);
else return query(px , py , a[x].c[1]);
}
else
{
if(px < a[x].p[0] || (px == a[x].p[0] && py < a[x].p[1])) return query(px , py , a[x].c[0]);
else return query(px , py , a[x].c[1]);
}
}
int main()
{
int T;
scanf("%d" , &T);
while(T -- )
{
memset(head , 0 , sizeof(head)) , cnt = 1;
a[0].mx[0] = a[0].mx[1] = -1 << 30 , a[0].mn[0] = a[0].mn[1] = 1 << 30;
int n , m , i , x , y , z , ans = 0;
scanf("%d%*d%d" , &n , &m);
for(i = 2 ; i <= n ; i ++ ) scanf("%d" , &x) , add(x , i);
dfs(1);
root = build(1 , n , 0);
for(i = 1 ; i <= m ; i ++ )
{
scanf("%d%d%d" , &x , &y , &z);
if(z) update(pos[x] , last[x] , deep[x] , deep[x] + y , z , root);
else d = 1 , ans = (ans + (long long)query(pos[x] , deep[x] , root) * i) % 1000000007;
}
printf("%d\n" , ans);
}
return 0;
}
【bzoj4154】[Ipsc2015]Generating Synergy KD-tree的更多相关文章
- 【BZOJ4154】[Ipsc2015]Generating Synergy KDtree
[BZOJ4154][Ipsc2015]Generating Synergy Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问 ...
- BZOJ4154:[Ipsc2015]Generating Synergy(K-D Tree)
Description 给定一棵以1为根的有根树,初始所有节点颜色为1,每次将距离节点a不超过l的a的子节点染成c,或询问点a的颜色 Input 第一行一个数T,表示数据组数 接下来每组数据的第一行三 ...
- BZOJ4154:[IPSC2015]Generating Synergy
浅谈\(K-D\) \(Tree\):https://www.cnblogs.com/AKMer/p/10387266.html 题目传送门:https://lydsy.com/JudgeOnline ...
- 【bzoj 4154】[Ipsc2015]Generating Synergy
题目 大概已经掌握熟练码出\(kdt\)的技能了 发现距离子树根节点\(x\)不超过\(l\)的点可以用两种方式来限制,首先\(dfs\)序在\([dfn_x,dfn_x+sum_x)\)中,深度自然 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【BZOJ2843】极地旅行社(Link-Cut Tree)
[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...
- 【BZOJ4530】大融合(Link-Cut Tree)
[BZOJ4530]大融合(Link-Cut Tree) 题面 讨厌权限题!!! Loj链接 题目描述 小强要在 N个孤立的星球上建立起一套通信系统.这套通信系统就是连接 N个点的一个树.这个树的边是 ...
- 【BZOJ1969】航线规划(Link-Cut Tree)
[BZOJ1969]航线规划(Link-Cut Tree) 题面 BZOJ 题解 删边操作 套路呀 离线读入倒过来做 变成加边操作 现在考虑怎么确定两点直接的关键路径条数 如果是一棵树,那么每条边都是 ...
- 【BZOJ4825】【HNOI2017】单旋(Link-Cut Tree)
[BZOJ4825][HNOI2017]单旋(Link-Cut Tree) 题面 题面太长,懒得粘过来 题解 既然题目让你写Spaly 那就肯定不是正解 这道题目,让你求的是最大/最小值的深度 如果有 ...
随机推荐
- 你可能不知道的 new.target
new 是构造函数生成实例的命令, ES6为 new 命令引入了 new.target属性.这个属性用于确定构造函数是怎么调用的. 在构造函数中, 如果一个构造函数不是通过 new操作符调用的, ne ...
- react的 react-router使用
官方API:https://reacttraining.com/react-router/web/api/BrowserRouter; React Router 安装命令如下. 使用时,路由器Rout ...
- JNDI整理
JNDI 什么是JNDI JNDI全称为Java Naming and Directory Interface,命名及目录查找接口,是java平台的一种标准扩展,它提供了一系列接口.类和命名空间的概念 ...
- Python——字典
字典是一种key-value 的 数据类型,使用就想我们上学用的字典.可以通过笔画,字母来查对应页的详细内容. 特性:1. 字典是无须的.(如果光打印字典里的字符串,那么排序不会按照顺序排,因为字典是 ...
- Spring Cloud 入门Eureka -Consumer服务消费(声明式Feign)(三)
Spring Cloud Feign是一套基于Netflix Feign实现的声明式服务调用客户端.它使得编写Web服务客户端变得更加简单.我们只需要通过创建接口并用注解来配置它既可完成对Web服务接 ...
- lvs+ipvsadm负载均衡
使用LVS实现负载均衡原理及安装配置详解 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均 ...
- 【Effective C++ 读书笔记】导读 Introduction
学习程序语言根本大法是一回事,学习如何以某种语言设计并实现高效程序则是另一回事. 一组明智选择并精心设计的classes.functions.templates可使程序编写容易.直观.高效.并且远离错 ...
- PHP表单安全过滤和防注入 htmlspecialchars() 和test_input()
什么是 htmlspecialchars() 函数? htmlspecialchars() 函数把特殊字符转换为 HTML 实体.这意味着 < 和 > 之类的 HTML 字符会被替换为 & ...
- C语言进阶——变量属性05
C语言变量属性: C语言的变量可以有自己的属性 在定义变量的时候加上“属性”关键字 “属性”关键字指明变量的特有意义 语法:property type value_name; auto关键字: aut ...
- ### Cause: java.lang.reflect.UndeclaredThrowableException
### Cause: java.lang.reflect.UndeclaredThrowableException Caused by: org.apache.ibatis.exceptions.Pe ...