Converting the HDU 4366 tree into a continuous sequence line segment tree

Source: Internet
Author: User

This question is about a tree. Then find the child node with the highest power value and highest loyalty.

Because the structure of the tree is difficult to build a line segment tree, it is easy to map the tree to a continuous sequence and map the Subtrees of this node to a continuous interval.

A common routine is the DFS timestamp, which records the timestamp of the entry node and the timestamp of the exit node. The two timestamps are the child nodes of the node.

However, if we are looking for a capacity value greater than the parent node, we must be the most loyal. Two limits.

Then sort by capacity value from high to low, and then query the largest loyal node in the subtree for each node in the sorted order, and then update it. This ensures that the capacity value is greater than the node of the parent node.

The question tells us that the value of loyalty is different from each other, which saves us some trouble. Because each loyal value must correspond to a unique sequence number, it is better to set a map.


[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <cstring>
# Include <string>
# Include <cstdio>
# Include <cmath>
# Include <queue>
# Include <map>
# Include <set>
# Define eps 1e-5
# Define maxn55555
# Define MAXM 111111
# Define INF 1000000007
# Define lch (x) x <1
# Define rch (x) x <1 | 1
# Define lson l, m, rt <1
# Define rson m + 1, r, rt <1 | 1
Using namespace std;
Int n, m;
Struct P
{
Int id, a, l;
Bool operator <(const P & t) const
{
Return a> t.;
}
} P [MAXN];
Struct EDGE
{
Int v, next;
} Edge [MAXM];
Int head [MAXN], e;
Int index, lf [MAXN], rf [MAXN];
Int mx [4 * MAXN], ans [MAXN];
Void init ()
{
Memset (head,-1, sizeof (head ));
Memset (ans,-1, sizeof (ans ));
E = 0;
Index = 0;
For (int I = 0; I <= 4 * n; I ++) mx [I] =-1;
}
Void add (int x, int y)
{
Edge [e]. v = y;
Edge [e]. next = head [x];
Head [x] = e ++;
}
Void dfs (int u)
{
Lf [u] = index ++;
For (int I = head [u]; I! =-1; I = edge [I]. next)
Dfs (edge [I]. v );
Rf [u] = index;
}
Void up (int rt)
{
Mx [rt] = mx [lch (rt)]> mx [rch (rt)]? Mx [lch (rt)]: mx [rch (rt)];
}
Void update (int pos, int v, int l, int r, int rt)
{
If (l = r) {mx [rt] = v; return ;}
Int m = (l + r)> 1;
If (pos <= m) update (pos, v, lson );
Else update (pos, v, rson );
Up (rt );
}
Int query (int L, int R, int l, int r, int rt)
{
If (L> R) return-1;
If (L <= l & R> = r) return mx [rt];
Int ret =-1;
Int m = (l + r)> 1;
If (L <= m) ret = max (ret, query (L, R, lson ));
If (m <R) ret = max (ret, query (L, R, rson ));
Return ret;
}
Int main ()
{
Int T;
Scanf ("% d", & T );
While (T --)
{
Int x;
Scanf ("% d", & n, & m );
Init ();
Map <int, int> mp;
For (int I = 1; I <n; I ++)
{
Scanf ("% d", & x, & p [I]. l, & p [I]. );
P [I]. id = I;
Mp [p [I]. l] = I;
Add (x, I );
}
Sort (p + 1, p + n );
Dfs (0 );
For (int I = 1; I <n ;)
{
Int pos = I;
While (pos <n & p [pos]. a = p [I].)
{
Int id = p [pos]. id;
Int tmp = query (lf [id] + 1, rf [id]-1, 0, index-1, 1 );
If (tmp =-1) ans [id] =-1;
Else ans [id] = mp [tmp];
Pos ++;
}
Pos = I;
While (pos <n & p [pos]. a = p [I].)
{
Int id = p [pos]. id;
Update (lf [id], p [pos]. l, 0, index-1, 1 );
Pos ++;
}
I = pos;
}
While (m --)
{
Scanf ("% d", & x );
Printf ("% d \ n", ans [x]);
}
}
Return 0;


Author: sdj222555

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.