I hate it stretch tree

Source: Internet
Author: User
View code

# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <stdlib. h>
# Include <math. h>
# Include <algorithm>
# Include <time. h>

Using namespace STD;

Int left [200100], right [200100], maxn [200100], PF [200100], value [200100], root; // a point with a value of-1 indicates that the node is empty.

Void Update (int n)
{
If (n =-1)
Return;
Else if (left [N]! =-1 & right [N]! =-1)
Maxn [N] = max (maxn [left [N], maxn [right [N]), value [N]);
Else if (left [N]! =-1)
Maxn [N] = max (maxn [left [N], value [N]);
Else if (right [N]! =-1)
Maxn [N] = max (maxn [right [N], value [N]);
Else
Maxn [N] = value [N];
}
// Right-handed
Inline void Zig (int n)
{
Int M = pf [N];
Left [m] = right [N];
PF [right [N] = m;
Right [N] = m;
If (left [pf [m] = m)
Left [pf [m] = N;
Else
Right [pf [m] = N;
Update (m );
Update (N );
PF [N] = pf [m];
PF [m] = N;
}

// Left-handed
Inline void zag (int n)
{
Int M = pf [N];
Right [m] = left [N];
PF [left [N] = m;
Left [N] = m;
If (left [pf [m] = m)
Left [pf [m] = N;
Else
Right [pf [m] = N;
Update (m );
Update (N );
PF [N] = pf [m];
PF [m] = N;

}



Inline int splay (int n, int P)
{
While (PF [N]! = P)
{
If (PF [pf [N] = P)
{
If (n = left [pf [N])
Zig (N );
Else
Zag (N );

}
Else
{
If (left [pf [pf [N] = pf [N])
{
If (left [pf [N] = N)
{
Zig (PF [N]);
Zig (N );
}
Else
{
Zag (N );
Zig (N );
}
}
Else if (right [pf [pf [N] = pf [N])
{
If (right [pf [N] = N)
{
Zag (PF [N]);
Zag (N );
}
Else
{
Zig (N );
Zag (N );
}
}
}
}
Return N;

}


Inline void insert (INT head, int N, int F, int flag) // header node number, inserted node number, and its ancestor
{
If (Head =-1)
{
If (flag = 0)
Root = N;
Else if (flag = 1)
Left [f] = N;
Else if (flag = 2)
Right [f] = N;
PF [N] = F;
Right [N] =-1;
Left [N] =-1;
Maxn [N] = value [N];
}
Else
{
If (head> N)
{
If (left [head] =-1)
Flag = 1;
Insert (left [head], n, Head, flag );
}
Else
{
If (left [head] =-1)
Flag = 2;
Insert (right [head], n, Head, flag );
}
}
Update (head );
}


Void preordertravel (int n)
{
If (n! =-1 ){
Printf ("% d's Father's Day is % d \ n", N, PF [N]);
Preordertravel (left [N]);
Preordertravel (right [N]);
}
}

Int modify (int p, int A, int B)
{
P = splay (A,-1 );
Value [a] = B;
Update ();
Return P;
}
Int main ()
{
Int n, m, A, B;
Char STR [100];
While (scanf ("% d", & N, & M )! = EOF)
{
Root =-1;
Insert (root, 0,-1, 0 );
Insert (root, n + 1,-1, 0 );
Value [0] = value [n + 1] = 0;
For (INT I = 1; I <= N; I ++)
{
Scanf ("% d", & value [I]);
Insert (root, I,-1, 0 );
Root = splay (I,-1 );
}
// Preordertravel (Root );
For (INT I = 1; I <= m; I ++)
{
Scanf ("% S % d", STR, & A, & B );
If (STR [0] = 'U ')
{
Modify (root, a, B );
Continue;
}
Root = splay (A-1,-1 );
Right [root] = splay (B + 1, root );
Printf ("% d \ n", maxn [left [right [root]);
}
}
Return 0;
}
/*
10
10
43 12 54 76 432
298 12894 1 34 5
The Father's Day in December 10 is-1.
9's Father's Day is 10
8's Father's Day is 9
7's Father's Day is 8
Father's Day is 7
Father's Day is 6
4's Father's Day is 5
3's Father's Day is 4
2's Father's Day is 3
1's Father's Day is 2
0's Father's Day is 1
11's Father's Day is 10
*/

Use the splay tree to maintain the range information and solve the range correlation quantity.

To solve the range [a, B] information, you can extend the A-1 to the root node, B + 1 to the right child of the root node.

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.