一开始没思路 感觉像是一个树形dp 然而不会

然后看了一眼题解就明白了

一个点的子树 用dfs序表示肯定是一个连续的区间 并且由于有子树的距离限制 可以转化为一个深度的区间

于是每个点都会有一个在二维平面上的标号(x,y) == (编号,深度)

并且每次进行更新一个节点的子树的时候就可以得到一个x的区间和一个y的区间  (实际上这些x是独一无二的 y更像是一个限制的条件)

这样就可以转化成一个二维的平面 每次选择一个矩阵进行颜色的统一更新 询问单点的颜色 做一个延时标记

询问单点的时候可以每次找啊找直到找到这个点 也可以判断一下mark mark一定是最新的历史 如果这个点在这个区间里面并且这个区间有修改历史 那么ans就是mark

不过加了这个优化速度并没有提升多少

在这里kd-tree很像线段树 区别就是维度不同

找了一个小bug就直接a掉了 开心!

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<queue>
#include<string>
using namespace std;
#define L long long
const L INF = 999999999 ;
const L maxn = 100050 ;
const L mod = 1000000007;
/**
用dfs序设定每个点的子树的标号区间
存下每个点的deep
则每个点在二维平面的x为它的标号 y为它的深度 (如果仅要表示这个点主码x就可以了)
利用kdtree来进行区间修改单点查询
如果区间满足 Max[0] Min[0] 属于 x1 x2 && Max[1] Min[1] 属于 y11 y2 就直接修改这个区间的颜色 设置一个延时标记
x1 x2 == l[a] r[a] y11 y2 == deep[a] deep[a] + l
单点查询直接进行一个logn的查询
*/
L n , c , q ;
L deep[maxn] ;
vector<L >vec[maxn] ;
L l[maxn] , r[maxn] ;
L cnt ;
void dfs(L u , L dep) {
deep[u] = dep ;
l[u] = ++ cnt ;
for(L i = 0; i < vec[u].size(); i ++ ){
L v = vec[u][i] ;
dfs(v,dep + 1) ;
}
r[u] = cnt ;
}
L root , cmp_d ;
struct node {
L l , r ;
L d[2] , Max[2] , Min[2] ;
L c ;
L mark ;
}a[maxn];
bool cmp(node a,node b){return ((a.d[cmp_d] < b.d[cmp_d])||(a.d[cmp_d] == b.d[cmp_d] && a.d[!cmp_d] < b.d[!cmp_d])) ;} ;
void up(L p , L k ){
if(a[k].Max[0] > a[p].Max[0]) a[p].Max[0] = a[k].Max[0] ;
if(a[k].Max[1] > a[p].Max[1]) a[p].Max[1] = a[k].Max[1] ;
if(a[k].Min[0] < a[p].Min[0]) a[p].Min[0] = a[k].Min[0] ;
if(a[k].Min[1] < a[p].Min[1]) a[p].Min[1] = a[k].Min[1] ;
}
void down(L p) {
if(a[p].mark == 0)return ;
L newc = a[p].mark ;
a[p].mark = 0 ;
if(a[p].r) {
L rr = a[p].r ;
a[rr].c = newc ;
a[rr].mark = newc ;
}
if(a[p].l) {
L ll = a[p].l ;
a[ll].c = newc ;
a[ll].mark = newc ;
}
return ;
}
L build(L l, L r , L D) {
L mid = (l+r)/2 ;
cmp_d = D;
nth_element(a+1+l,a+1+mid,a+1+r,cmp) ;
a[mid].Max[0] = a[mid].Min[0] = a[mid].d[0] ;
a[mid].Max[1] = a[mid].Min[1] = a[mid].d[1] ;
a[mid].c = 1 ;
a[mid].mark = 0 ;
if(l != mid)a[mid].l = build(l,mid-1,D^1); else a[mid].l = 0;
if(r != mid)a[mid].r = build(mid+1,r,D^1); else a[mid].r = 0;
if(a[mid].l)up(mid,a[mid].l) ;
if(a[mid].r)up(mid,a[mid].r) ;
return mid ;
}
void upda(L p , L x1 , L y11 , L x2 , L y2 , L newc ) {
if(a[p].Max[0] < x1 || a[p].Min[0] > x2 || a[p].Max[1] < y11 || a[p].Min[1] > y2) {
return ;
}
if(a[p].Min[0] >= x1 && a[p].Max[0] <= x2 && a[p].Max[1] <= y2 && a[p].Min[1] >= y11) {
a[p].c = newc ;
a[p].mark = newc ;
return ;
}
down(p) ;
if(a[p].d[0] >= x1 && a[p].d[0] <= x2 && a[p].d[1] >= y11 && a[p].d[1] <= y2) {
a[p].c = newc ;
}
if(a[p].l) {
upda(a[p].l , x1, y11 , x2 , y2 , newc) ;
}
if(a[p].r) {
upda(a[p].r , x1, y11 , x2 , y2 , newc) ;
}
}
L ans ;
void ask(L p , L x, L y) {
if(a[p].d[0] == x && a[p].d[1] == y) {
ans = a[p].c ;
return ;
}
if(a[p].mark != 0) {
ans = a[p].mark ;
return ;
}
down(p) ;
if(a[p].l) {
L ll = a[p].l ;
if(x >= a[ll].Min[0] && x <= a[ll].Max[0] && y >= a[ll].Min[1] && y <= a[ll].Max[1]) {
ask(ll , x , y) ;
}
}
if(a[p].r) {
L ll = a[p].r ;
if(x >= a[ll].Min[0] && x <= a[ll].Max[0] && y >= a[ll].Min[1] && y <= a[ll].Max[1]) {
ask(ll , x , y) ;
}
}
}
int main(){
L t ;
scanf("%lld",&t);
while(t-- ){
scanf("%lld%lld%lld",&n,&c,&q) ;
for(L i = 1; i <= n ; i ++ )vec[i].clear() ;
for(L i = 2; i <= n ; i ++ ){
L fa ;
scanf("%lld",&fa) ;
vec[fa].push_back(i) ;
}
cnt = 0 ;
dfs(1,1) ;
for(L i = 1; i <= n; i ++ ){
a[i].l = a[i].r = 0;
a[i].d[0] = l[i] ;
a[i].d[1] = deep[i] ;
}
root = build(1,n,0) ;
L sum = 0;
for(L i = 1; i <= q; i ++ ){
L aa , ll , cc ;
scanf("%lld%lld%lld",&aa,&ll,&cc);
L x1 = l[aa] ;
L x2 = r[aa] ;
L y11 = deep[aa] ;
L y2 = deep[aa] + ll ;
if(cc != 0) {
upda(root , x1 , y11 , x2 , y2 , cc) ;
}
else {
ans = -1 ;
L x = l[aa] ;
L y = deep[aa] ;
ask(root, x , y) ;
sum += ans * i ;
sum %= mod ;
}
}
printf("%lld\n",sum) ;
}
}

  

BZOJ 4154 kd-tree dfs序 + 二维空间的区间(矩阵)更新单点查找的更多相关文章

  1. bzoj 2434 fail tree+dfs序

    首先比较明显的是我们可以将字符串组建立ac自动机,那么对于询问s1字符串在s2字符串中出现的次数,就是在以s1结尾为根的fail tree中,子树有多少个节点是s2的节点,这样我们处理fail tre ...

  2. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  3. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  4. [poj3321]Apple Tree(dfs序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26762   Accepted: 7947 Descr ...

  5. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  6. Codeforces Round #200 (Div. 1)D. Water Tree dfs序

    D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...

  7. POJ3321Apple Tree Dfs序 树状数组

    出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...

  8. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  9. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. php通过post将表单数据保存到数据库实例

    html的form表单 <form id="contact-form" method="POST" action="../php/msg.php ...

  2. knockoutJs在移动设备上有时无法更新控件值

    最近在用cordova(phonegap)写一个移动app,表单比较复杂,用了knockoutJs作为前端的MVVM框架进行数据绑定. 但是发现有时候(其实是每次)如果最后在input中编辑一个值,然 ...

  3. TFS二次开发-基线文件管理器(3)-源码文件的读取

    TFS登录成功后,就可以开始读取源码目录树了. 一般来说,我不建议将整个树全部读取出来,因为这里不光存有项目文件,还有项目源码.如果全部读取出会是非常大的一棵树.因此我建议只读出根目录,每一次点击打开 ...

  4. 在ie和chrome浏览器中滚动条样式的设置

    1.IE下设置滚动条样式的属性 scrollbar-arrow-color: color; /*三角箭头的颜色*/scrollbar-face-color: color; /*立体滚动条的颜色(包括箭 ...

  5. 2014-08-28——移动端web开发,基本Meta标签

    1.<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scal ...

  6. PHP查看目录下的所有文件

    [1].[代码] [PHP]代码 跳至 [1] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  7. 前端基础 & 初识CSS

    CSS介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素.l 当浏览器读到一个样式表,它就会按照这个样式表来对文档进行格式化(渲染). CSS语法 每个CS ...

  8. 异常处理、socket基于TCP协议编程

    一.异常处理 1.错误和异常 1.程序中难免出现错误,而错误分成两种 (1)语法错误(这种错误过不了Python解释器的语法检测,必须在程序执行前改正) #语法错误示范一 if #语法错误示范二 de ...

  9. python2.7升级到python3.6注意事项

    python3.6下载地址:https://www.python.org/downloads/source/ 1.安装依赖包:gcc   openssl-devel.zlib-devel.readli ...

  10. django 操作前端数据

    django 利用json处理前端页面数据,FLASK当中也同样   def create_company(request):if request.user.is_superuser:custom_l ...