当前位置: 首页 > 编程日记 > 正文

bzoj 4771: 七彩树 树链的并+可持久化线段树

题目大意:

给定一颗树,询问树中某个点x的子树中与其距离不超过d的所有点中本质不同的颜色数
强制在线

题解:

一下午终于把这道题叉掉了。

写了三个算法,前两个都是错的,后一个是%的网上大爷们的题解.

首先我们发现这道题有一个特点:没有修改操作 !!
这就使我们能够对颜色进行预处理。
所以我们可以考虑分别枚举每个颜色,对其去重后更新树上的信息。
所以我们可以考虑树链的并,我们可以对每种颜色都做一遍树链的并
容易发现复杂度仍然是\(O(nlogn)\)
但是这样我们只求出来的每个节点子树中不同的颜色的个数
并没有满足对深度的限制
弱弱的我想到这里就继续不下去了,不知道下面改怎么写,YY了两个算法
第一个算法打着打着感觉不对,(Ctrl+A) + (Backspace)
第二个算法打出来了调样例,手模一下发现算法是错的.(Alt+F4)


去%了大爷们的题解:

我们把所有的点按照深度动态地进行树链的并.
这样,我们就发现我们实际上可以求出每一个深度到树根中不同颜色的种类
但是我们发现我们单单考虑了深度的一个边界还没有结束.
我们还需要限制深度另一个边界和在x子树中的限制
我们发现其实这两个限制等价于dfs序在x的子树dfs序范围之间.
所以。。。在深度上用线段树可持久化即可...

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
using namespace std;
typedef long long ll;
inline void read(int &x){x=0;char ch;bool flag = false;while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 100010;
struct Edge{int to,next;
}G[maxn<<2];
int head[maxn],cnt;
void add(int u,int v){G[++cnt].to = v;G[cnt].next = head[u];head[u] = cnt;
}
int dfn[maxn],dfs_clock,son[maxn],siz[maxn];
int dep[maxn],top[maxn],fa[maxn],oud[maxn];#define v G[i].to
void dfs(int u){siz[u] = 1;for(int i = head[u];i;i=G[i].next){if(v == fa[u]) continue;fa[v] = u;dep[v] = dep[u] + 1;dfs(v);siz[u] += siz[v];if(siz[son[u]] < siz[v]) son[u] = v;}
}
void dfs(int u,int tp){top[u] = tp;dfn[u] = ++dfs_clock;if(son[u]) dfs(son[u],tp);for(int i = head[u];i;i=G[i].next){if(v == fa[u] || v == son[u]) continue;dfs(v,v);}oud[u] = dfs_clock;
}
#undef v
inline int lca(int u,int v){while(top[u] != top[v]){if(dep[top[u]] < dep[top[v]]) swap(u,v);u = fa[top[u]];}return dep[u] < dep[v] ? u : v;
}
int col[maxn],n;
namespace Trp{struct Node{Node *ch[2];int dfn,siz,fix,id;void update(){siz = ch[0]->siz + ch[1]->siz + 1;}}mem[maxn<<2],*it,*null,*root[maxn];inline void init(){it = mem;null = it++;null->ch[0] = null->ch[1] = 0;null->id = -1;null->dfn = null->siz = 0;}inline Node* newNode(int x,int i){Node *p = it++;p->ch[0] = p->ch[1] = null;p->dfn = x;p->fix = rand();p->siz = 1;p->id = i;return p;}void rotate(Node* &p,int k){Node *y = p->ch[k^1];p->ch[k^1] = y->ch[k];y->ch[k] = p;p->update();y->update();p = y;}void insert(Node* &p,int x,int id){if(p == null) p = newNode(x,id);else{insert(p->ch[p->dfn < x],x,id);p->update();if(p->ch[p->dfn<x]->fix < p->fix)rotate(p,p->dfn > x);}}inline int find(int k,Node *root){Node *p = root;if(k < 1 || k > p->siz) return -1;while(p != null){if(p->ch[0]->siz + 1 == k) return p->id;if(p->ch[0]->siz + 1 > k) p = p->ch[0];else k -= p->ch[0]->siz + 1,p = p->ch[1];}assert(0);}inline int rank(int d,Node* root){int ret = 1;Node *p = root;while(p != null){if(p->dfn < d) ret += p->ch[0]->siz + 1,p = p->ch[1];else p = p->ch[0];}return ret;}
}
namespace seg{struct Node{Node* ch[2];int val;void update(){val = ch[0]->val + ch[1]->val;}}mem[maxn*100],*it,*null,*root[maxn];inline void init(){it = mem;null = it++;null->ch[0] = null->ch[1] = null;null->val = 0;root[0] = null;}Node* insert(Node *rt,int l,int r,int pos,int w){Node *p = it++;*p = *rt;if(l == r){p->val += w;return p;}int mid = l+r >> 1;if(pos <= mid) p->ch[0] = insert(p->ch[0],l,mid,pos,w);else p->ch[1] = insert(p->ch[1],mid+1,r,pos,w);p->update();return p;}int query(Node *p,int l,int r,int L,int R){if(L <= l && r <= R) return p->val;int mid = l+r >> 1;if(R <= mid) return query(p->ch[0],l,mid,L,R);if(L >  mid) return query(p->ch[1],mid+1,r,L,R);return query(p->ch[0],l,mid,L,R) + query(p->ch[1],mid+1,r,L,R);}
}
int q[maxn],l,r,mx;
inline void bfs(){l = 0;r = -1;q[++r] = 1;while(l <= r){int u = q[l++],x = Trp::rank(dfn[u],Trp::root[col[u]]),y,z;mx = max(mx,dep[u]);seg::root[dep[u]] = seg::insert(seg::root[dep[q[l-2]]],1,n,dfn[u],1);Trp::insert(Trp::root[col[u]],dfn[u],u);y = Trp::find(x-1,Trp::root[col[u]]);z = Trp::find(x+1,Trp::root[col[u]]);if(y != -1 && z != -1){int lc = lca(y,z);seg::root[dep[u]] = seg::insert(seg::root[dep[u]],1,n,dfn[lc],1);}if(y != -1){int lc = lca(y,u);seg::root[dep[u]] = seg::insert(seg::root[dep[u]],1,n,dfn[lc],-1);}if(z != -1){int lc = lca(z,u);seg::root[dep[u]] = seg::insert(seg::root[dep[u]],1,n,dfn[lc],-1);}for(int i = head[u];i;i=G[i].next){int v = G[i].to;if(v == fa[u]) continue;q[++r] = v;}}
}
inline void init(){memset(head,0,sizeof head);cnt = 0;memset(son,0,sizeof son);memset(siz,0,sizeof siz);dfs_clock = 0;mx = 0;
}
int main(){int T;read(T);srand(6613);while(T--){init();seg::init();Trp::init();int m;read(n);read(m);for(int i=0;i<=n;++i){Trp::root[i] = Trp::null;seg::root[i] = seg::null;}for(int i=1;i<=n;++i) read(col[i]);for(int i=2;i<=n;++i){read(fa[i]);add(fa[i],i);}dfs(1);dfs(1,1);bfs();int ans = 0;int x,d;while(m--){read(x);read(d);x ^= ans;d ^= ans;int de = dep[x] + d;if(de > mx) de = mx;ans = seg::query(seg::root[de],1,n,dfn[x],oud[x]);printf("%d\n",ans);}}getchar();getchar();return 0;
}

转载于:https://www.cnblogs.com/Skyminer/p/6556815.html

相关文章:

Chapter 0: 引论

引论我之前就看过了&#xff0c;在我刚买到这本书的时候。 而我买这本书的日子&#xff0c;已经是两年前了。我就是这样子的&#xff0c;我买了好多好多关于技术的书&#xff0c;这些书都是很贵很贵的&#xff0c;可是买完回来之后就看了第一章&#xff0c;然后就一直丢在一边&…

开发常用CSS

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; keyframes -> 使 div 元素匀速向下移动 div{animation:myanimation 5s infinite;} keyframes myanimation {from {top:0px;}to {top:200px;}} 注:animation ->Css3动画属性 …

javascript网络_没有JavaScript的网络外观

javascript网络A Berlin-based web developer — who codes JavaScript for a living — decided to go an entire day without JavaScript.一家位于柏林的网络开发人员(为JavaScript编写代码为生)决定不使用JavaScript进行一整天的工作。 Let’s face it — in an insane wor…

js中的各种宽高以及位置总结

在javascript中操作dom节点让其运动的时候&#xff0c;常常会涉及到各种宽高以及位置坐标等概念&#xff0c;如果不能很好地理解这些属性所代表的意义&#xff0c;就不能理解js的运动原理&#xff0c;同时&#xff0c;由于这些属性概念较多&#xff0c;加上浏览器之间 实现方式…

关于百度编辑器UEditor在asp.net中的使用方法!

为了完成自己想要的功能效果&#xff0c;在项目中使用到了百度编辑器&#xff0c;为了搞明白&#xff0c;苦心学习查资料搞了整整一天&#xff0c;总结一下。 在asp.net 的项目中目前我觉得有两种情况&#xff0c;一种是没有使用模板页的&#xff0c;一种是使用了模板页的&…

微信小程序点击图片实现长按预览、保存、识别带参数二维码、转发等功能

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 先上效果图&#xff0c;再附上完整源码&#xff1a; 1.多张图片循环渲染后预览、保存、识别带参数二维码 <view wx:for"{{imgalist}}" class"previewimg">…

vba编程教程视频教程_我已经完成了编程教程。 怎么办?

vba编程教程视频教程by Preethi Kasireddy通过Preethi Kasireddy 我已经完成了编程教程。 怎么办&#xff1f; (I’ve done programming tutorials. Now what?) This week’s question for my Ask Preethi series is about how to go from simply doing tutorials to the act…

【官方文档】Nginx负载均衡学习笔记(二)负载均衡基本概念介绍

简介 负载均衡&#xff08;Server Load Balancer&#xff09;是将访问流量根据转发策略分发到后端多台 ECS 的流量分发控制服务。负载均衡可以通过流量分发扩展应用系统对外的服务能力&#xff0c;通过消除单点故障提升应用系统的可用性。 负载均衡主要有如下几个功能点&#x…

微信小程序本地缓存

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 关于微信小程序本地缓存&#xff0c;做一下笔记&#xff0c;希望能够帮助到看到这篇分享的人 //index.js 这里是保存 var a 1 wx.setStorageSync(a, a) //logo.js 这里是取保存的…

css 形状_在CSS形状之外思考

css 形状CSS is based off a box model. If you have an image that is a circle that you want to wrap text around, it will wrap around the images’ bounding box.CSS基于盒模型。 如果您要环绕的图像是一个圆&#xff0c;则它将环绕图像的边界框。 外型 (Shape-outside…

js-ES6学习笔记-module(4)

1、<script>标签打开defer或async属性&#xff0c;脚本就会异步加载。渲染引擎遇到这一行命令&#xff0c;就会开始下载外部脚本&#xff0c;但不会等它下载和执行&#xff0c;而是直接执行后面的命令。 defer与async的区别是&#xff1a;前者要等到整个页面正常渲染结束…

图像边缘检测--OpenCV之cvCanny函数

图像边缘检测--OpenCV之cvCanny函数 分类&#xff1a; C/C void cvCanny( const CvArr* image, CvArr* edges, double threshold1, double threshold2, int aperture_size3 ); image单通道输入图像.edges单通道存储边缘的输出图像threshold1第一个阈值threshold2第二个阈值aper…

微信小程序 封装网络请求并调用

微信小程序开发交流qq群 526474645 正文&#xff1a; util.js // 网络请求 const request function(url, method, data, msg, succ, fail, com) {// 小程序顶部显示Loadingwx.showNavigationBarLoading();if (msg ! "") {wx.showLoading({title: msg})}wx.requ…

什么是导师负责制_为什么一个导师是不够的

什么是导师负责制by Rick West由里克韦斯特(Rick West) 为什么一个导师是不够的 (Why one mentor just isn’t enough) A mentor can give career guidance and help with learning. They can teach you how to solve problems, network, and the list goes on.导师可以提供职…

CodeForces 114B 【STL应用】

思路&#xff1a; 原来string类能sort 和 swap....太强了.... 注意&#xff1a;字典序最小输出&#xff0c;因为某个地方写挫了&#xff0c;sort了n发&#xff0c;代码挫。 #include <bits/stdc.h> using namespace std; typedef long long LL;int tol; map<string,in…

微信小程序订单页面下拉刷新上拉分页加载

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 效果图&#xff1a; 代码&#xff1a; json代码&#xff1a; {"enablePullDownRefresh": true,"backgroundColor": "#19ad19" } js代码&#xff1a…

从网络上获取一张图片简单的

告诉ScrollView缩放的视图&#xff0c;要设置scrollView的代理。 转载于:https://www.cnblogs.com/x1024598115/p/4182674.html

es6 generator_让我们探索一下ES6 Generators

es6 generatorby Tiago Lopes Ferreira由Tiago Lopes Ferreira 让我们探索一下ES6 Generators (Let’s explore ES6 Generators) Generators are an implementation of iterables.生成器是可迭代对象的实现 。 The big deal about generators is that they are functions tha…

没听说过这些,就不要说你懂并发了,three。

引言 很久没有跟大家再聊聊并发了&#xff0c;今天LZ闲来无事&#xff0c;跟大家再聊聊并发。由于时间过去的有点久&#xff0c;因此LZ就不按照常理出牌了&#xff0c;只是把自己的理解记录在此&#xff0c;如果各位猿友觉得有所收获&#xff0c;就点个推荐或者留言激励下LZ&am…

设计模式之代理模式(Proxy Pattern)

定义&#xff1a;为其他对象提供一种代理以控制这个对象的访问&#xff0c;也叫做委托模式。 咱们比作游戏&#xff0c;通俗讲代理模式就是&#xff0c;一个主题虚基类派生出两个子类&#xff0c;一个玩家类&#xff0c;实现相关操作&#xff0c;一个是代练类&#xff0c;代替执…

[微信小程序]给data的对象的属性赋值

有问题可以扫码加我微信&#xff0c;有偿解决问题。承接小程序开发。 微信小程序开发交流qq群 173683895 、 526474645 &#xff1b; 正文&#xff1a; <view wx:for"{{leixing}}"><button class"leixing_btn {{user_infor.lx_btnitem.divingtype…

无家可归的iPhone

by Fabrice Dubois通过Fabrice Dubois 无家可归的iPhone (Homeless iPhone) So, apparently the next iPhone won’t have a physical Home button. There’s been much speculation already about what that means for the user. The bottom area of the device, for some, w…

Spring 自动化装配Bean

声明一张cd的接口&#xff1a; public interface CompactDisc {public abstract void play(); } 实现cd接口&#xff1a; Component("SgtPeppers") public class SgtPeppers implements CompactDisc {private String title "Sgt.Peppers Lonely Hearts Club Ba…

js中函数,方法,事件对比区分,什么是方法,什么是函数

微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文&#xff1a; 简单的理解&#xff1a;函数是运行在本地的&#xff0c;方法是公用的。 事件是开关&#xff0c;通过某某事件触发某个函数 通常命名规范 函数的命名使用小写字母和下划线&#xff…

笔记 JVM调优流程

待续 转载于:https://www.cnblogs.com/leeeee/p/7276287.html

创建新的apple id_Google是新的Apple吗?

创建新的apple idby Sumit Gupta由Sumit Gupta Google是新的Apple吗&#xff1f; (Is Google the new Apple?) 随着众多设备的推出&#xff0c;谷歌试图击败苹果。 “由Google制造”会使Google更像Apple吗&#xff1f; (With the launch of numerous devices, Google is tryi…

yeomen/bower/grunt

yeomen: npm install yo angular-in-action project npm install -g generator-angular npm install -g genrator-webapp yo angular learnangular new angular project yo webapp grunt-by-yeomen package.json npm install (执行package.json所指定的依赖包) bower: npm ins…

Window Server 2008 R2 安装 Share Point 2013

原文地址&#xff1a;http://www.cnblogs.com/jianyus/p/3631905.html转载于:https://www.cnblogs.com/gaobing/p/4191060.html

esp freertos_如何开始使用FreeRTOS和ESP8266

esp freertosby Denis Nuțiu丹尼斯努尤(Denis Nuțiu) 如何开始使用FreeRTOS和ESP8266 (How to get started with FreeRTOS and ESP8266) Recently, I purchased a NodeMCU from AliExpress for about $4. The reason I did this was to find out what all the fuss is about…

[SCOI2007]修车

题目描述 同一时刻有N位车主带着他们的爱车来到了汽车维修中心。维修中心共有M位技术人员&#xff0c;不同的技术人员对不同的车进行维修所用的时间是不同的。现在需要安排这M位技术人员所维修的车及顺序&#xff0c;使得顾客平均等待的时间最小。 说明&#xff1a;顾客的等待时…