HDU 4366 successor (line segment tree)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4366

Given the Successor Relationship, find the successor with the maximum value of the satisfied ability than that of the successor.

First, change the tree structure to a linear structure and use the adjacent table + DFS for processing. Sort the capacity values in ascending order to ensure that the inserted capacity value is greater than the queried value.

The line segment tree function is converted to the maximum value of the solution range.

In the original 1y code, the Loy and BLT inputs are reversed! I tried a lot of data and haven't tried it yet! Depressed, the code written over the past two days is so confusing...

Code:

# Include <cstdlib>
# Include <cctype>
# Include <cstring>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <vector>
# Include <string>
# Include <iostream>
# Include <sstream>
# Include <set>
# Include <queue>
# Include <stack>
# Include <fstream>
# Include <iomanip>
# Include <bitset>
# Include <list>
# Include <ctime>
Using namespace STD;

# Define set (ARR, what) memset (ARR, what, sizeof (ARR ))
# Define ff (I, A) for (I = 0; I <A; I ++)
# Define SD (a) scanf ("% d", &)
# Define SSD (a, B) scanf ("% d", & A, & B)
# Define SF (a) scanf ("% lf", &)
# Define SS (a) scanf ("% s",)
# Define SLD (a) scanf ("% LLD", &)
# Define PF (a) printf ("% d \ n",)
# Define PPF (a, B) printf ("% d \ n", a, B)
# Define SZ (ARR) (INT) A. Size ()
# Define swap (A, B) A = a xor B; B = a xor B; A = a xor B;
# Define read freopen ("in.txt", "r", stdin)
# Define write freopen ("out.txt", "W", stdout)
# Define max 1 <30
# Define ESP 1e-5
# Define lson L, M, RT <1
# Define rson m + 1, R, RT <1 | 1
Template <class T> inline t sqr (t a) {return a * ;}
Template <class T> inline void Amin (T & A, t B) {if (a =-1 | A> B) A = B ;}
Template <class T> inline void amax (T & A, t B) {if (a <B) A = B ;}
Template <class T> inline T min (t a, t B) {return A> B? B: ;}
Template <class T> inline T max (t a, t B) {return A> B? A: B ;}
Const int maxn = 55555;
Int Loy [maxn <2], hash [1000001], head [maxn], K, ANS [maxn];
Int L [maxn], R [maxn];
Struct node {
Int Loy, BLT, sup;
} Q [maxn];
Struct edge {
Int V, NEX;
} E [maxn];
Int CMP (const void * a, const void * B ){
Return (* (node *) a). BLT> (* (node *) B). BLT? -1: 1;
}
Void addedge (int A, int B ){
E [K]. V = B;
E [K]. NEX = head [a];
Head [a] = K;
K ++;
}
Void DFS (int x ){
L [x] = K ++;
For (INT I = head [X]; I; I = E [I]. NEX)
DFS (E [I]. V );
R [x] = K;
}
Int query (int l, int R, int L, int R, int RT ){
If (r <L) Return-1;
If (L <= L & R <= r ){
Return Loy [RT];
}
Int M = (L + r)> 1;
Int ret =-1;
If (L <= m) ret = max (Ret, query (L, R, lson ));
If (M <r) ret = max (Ret, query (L, R, rson ));
Return ret;
}
Void Update (INT idx, int Val, int L, int R, int RT ){
If (L = r ){
Loy [RT] = val;
Return;
}
Int M = (L + r)> 1;
If (idx <= m) Update (idx, Val, lson );
Else Update (idx, Val, rson );
Loy [RT] = max (Loy [RT <1], Loy [RT <1 | 1]);
}
Int main (){
Int t, n, m, I, J;
SD (t );
While (t --){
Set (Head, 0 );
SSD (n, m );
K = 1;
For (I = 1; I <n; I ++ ){
SD (Q [I]. Sup); SSD (Q [I]. Loy, Q [I]. BLT );
Hash [Q [I]. Loy] = I;
Addedge (Q [I]. Sup, I );
}
K = 1;
DFS (0 );
Set (Loy,-1 );
Qsort (q + 1, n-1, sizeof (node), CMP );
For (I = 1; I <n; I = J ){
J = I;
While (j <n & Q [I]. BLT = Q [J]. BLT ){
Int id = hash [Q [J]. Loy];
Int temp = query (L [ID] + 1, R [ID]-1, 1, K-1, 1 );
If (temp =-1) ans [ID] =-1;
Else ans [ID] = hash [temp];
J ++;
}
J = I;
While (j <n & Q [I]. BLT = Q [J]. BLT ){
Int id = hash [Q [J]. Loy];
Update (L [ID], Q [J]. Loy, 1, K-1, 1 );
J ++;
}
}
Ff (I, m ){
SD (j );
PF (ANS [J]);
}
}
Return 0;
}

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.