SPOJ Query on a tree V】的更多相关文章

You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N. We define dist(a, b) as the number of edges on the path from node a to node b. Each node has a color, white or black. All the nodes ar…
传送门 题意简述: 给你一棵nnn个黑白点的树,初始全是黑点. 现在支持给一个点换颜色或者求整颗树中离某个点最近的白点跟这个点的距离. 思路: 考虑链分治维护答案,每个链顶用一个堆来维护答案,然后对于每条重链开一棵线段树维护子树里所有白点到线段树最左/右端点的最短距离. 然后瞎更新查询即可. 代码: #include<bits/stdc++.h> #define ri register int using namespace std; inline int read(){ int ans=0;…
QTREE5 - Query on a tree V 动态点分治和动态边分治用Qtree4的做法即可. LCT: 换根后,求子树最浅的白点深度. 但是也可以不换根.类似平常换根的往上g,往下f的拼凑 考虑深度的pushup必须考虑原树结构的联系,而ch[0],ch[1]又不是直接的前驱后继,每次pushup还要找前驱后继答案,还不如直接记下来. 故,节点里维护: 1.sz,大小 2.color节点颜色 3.set每个虚儿子贡献的最浅深度 4.lmn,rmn,当前x的splay子树最浅点和最深点的…
You are given a tree (an acyclic undirected connected graph) with N nodes. The tree nodes are numbered from 1 to N. We define dist(a, b) as the number of edges on the path from node a to node b. Each node has a color, white or black. All the nodes ar…
[题目分析] QTREE4的弱化版本 建立出分治树,每个节点的堆表示到改点的最近白点距离. 然后分治树上一直向上,取min即可. 正确性显然,不用担心出现在同一子树的情况(不会是最优解),请自行脑补. 然后弱渣我写了1.5h [代码] #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namesp…
题目传送门 题意:给你一棵树, 然后树上的点都有颜色,且原来为黑,现在有2个操作,1 改变某个点的颜色, 2 询问树上的白点到u点的最短距离是多少. 题解: 这里用的还是边分治的方法. 把所有东西都抠出来, 然后每次询问的时候都访问每幅分割图的另外一侧. 代码: #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen(…
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to tior QUERY a b : ask fo…
题目描述 给出N个点的一棵树(N-1条边),节点有白有黑,初始全为白 有两种操作: 0 i : 改变某点的颜色(原来是黑的变白,原来是白的变黑) 1 v : 询问1到v的路径上的第一个黑点,若无,输出-1 输入输出格式 输入格式: 第一行 N,Q,表示N个点和Q个操作 第二行到第N行N-1条无向边 再之后Q行,每行一个操作"0 i" 或者"1 v" (1 ≤ i, v ≤ N). 输出格式: 对每个1 v操作输出结果 输入输出样例 输入样例#1: 9 8 1 2 1…
You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose label is k-th largest in the subtree of the node x. Assume no two nodes have the same labels. Input The first line contains one integer n (1 <= n <=…
You are given a tree (an acyclic undirected connected graph) with N nodes, and nodes numbered 1,2,3...,N. Each edge has an integer value assigned to it(note that the value can be negative). Each node has a color, white or black. We define dist(a, b)…