Multi school sprint NOIP simulation 27 - Super doubling - reconstruction tree

Keywords: Algorithm data structure Graph Theory

No link is provided for this question

Title Description

Given a tree. We think one x → y x\rightarrow y Simple path of x → y( x ≠ y x\neq y x  = y) is good if and only if the point number on the path is the smallest x x x. The biggest is y y y.

Find the number of good simple paths and the size of the tree n ≤ 2 × 1 0 6 n\le 2\times 10^6 n≤2×106.


I had a terrible headache this morning. It hurts when I eat, walk, read and think about questions. In the middle of the game, I got down and had a rest. As soon as I got down, I got down for nearly an hour. The problem was confused. It was found that it was false halfway through the divide and conquer. It was not easy to think of the correct solution later.

The last question could have cheated more points.

Now I just pray that the headache can be cured before NOIP.

Problem solution

First, there is a failed routine point divide and conquer method. The statistical path is a two-dimensional partial order, which can be achieved if it is well written O ( n log ⁡ 2 n ) O(n\log^2 n) O(nlog2n), but it can't be achieved by any optimization O ( n log ⁡ n ) O(n\log n) O(nlogn), not to mention that this problem requires a small constant.

But what can I do without some divide and conquer? DP or set of data structures directly on the tree cannot be achieved at all O ( n 2 ) O(n^2) Below O(n2).

We shuffle the conditions and find that we only need to meet the point number on the path is not less than x x x and not greater than y y y. And all numbers are greater than or equal to x x Point and of x x x The most common block composed of x is unique, x x x to y y The point number on the path of y is not less than x x x. If and only if y y y is in this connected block. If we call this connecting block x x If the suffix connected block of x is defined in a similar way, the original condition can be equivalent converted to x x x in y y The prefix of y is in the connected block, and y y y in x x The suffix of x is in the connected block.

The rest about how to get this connected block is another routine method, which we use similar methods K r u s k a l \rm Kruskal A point reconstruction tree is established by Kruskal reconstruction tree. K r u s k a l \rm Kruskal Kruskal reconstruction tree starts with a pile of scattered points, and then continuously connects the connected blocks at both ends of the edge to the same new father (the mouth here ignores many details). Then the reconstruction number of this point is that there is no point at first, and then add a new point and take all the connected blocks connected to it as their own son.

Two reconstruction trees can be built through two different enumeration sequences. The two connected blocks corresponding to a point are the subtrees of the point on the two reconstruction trees. The two reconstruction trees are divided into A A A, B B B. Then we just need to x x x. Statistics A A How many points are in the subtree of A B B Ancestors in B. Yes A A A first find the DFS order, convert the subtree into an interval, and then directly B B For DFS on B, use the data structure to maintain a single point and modify the interval to sum.

Single point modified interval summation can just be done with tree array, so the total complexity n log ⁡ n n\log n nlogn, the constant is very small.


#include<cstdio>//JZM yyds!!
#define ll long long
#define uns unsigned
#define MOD 
#define MAXN 2000005
#define INF 1e17
#define IF (it->first)
#define IS (it->second)
using namespace std;
inline ll read(){
	ll x=0;bool f=1;char s=getchar();
	return f?x:-x;
int pt[30],lp;
inline void print(ll x,char c='\n'){
inline ll lowbit(ll x){return x&-x;}
struct edge{
	int v,to;edge(){}
	edge(int V,int T){v=V,to=T;}
int EN,G[MAXN];
inline void addedge(int u,int v){
int n,fa[MAXN];
ll ans;
int G1[MAXN],G2[MAXN];
inline int finds(int x){
	return !fa[x]?x:(fa[x]=finds(fa[x]));
inline void addedge1(int u,int v){
inline void addedge2(int u,int v){
int hd[MAXN],tl[MAXN],IN,f[MAXN];
inline void add(int x,int d){
inline int sum(int x){
	int res=0;
	return res;
inline void pdfs(int x){
	for(int i=G2[x];i;i=e[i].to)pdfs(e[i].v);
inline void dfs(int x){
	for(int i=G1[x];i;i=e[i].to)dfs(e[i].v);
signed main()
	for(int i=2;i<=n;i++)addedge(read(),i);
	for(int x=1;x<=n;x++)
		for(int i=G[x];i;i=e[i].to){
			int v=e[i].v,fv;
	for(int i=1;i<=n;i++)fa[i]=0;
	for(int x=n;x>0;x--)
		for(int i=G[x];i;i=e[i].to){
			int v=e[i].v,fv;
	return 0;

Posted by phpnewbie911 on Wed, 10 Nov 2021 08:11:15 -0800