POJ 1785 treap or RMQ

Source: Internet
Author: User

[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 5555
# Define INF 100000007
Using namespace std;
Struct node
{
Int w, fa, lch, rch;
Char s [111];
Bool operator <(const node & a) const
{
Return strcmp (s, a. s) <= 0;
}
} P [MAXN];
Int n;
Void insert (int I)
{
Int j = I-1;
While (p [j]. w <p [I]. w) j = p [j]. fa;
P [I]. lch = p [j]. rch;
P [j]. rch = I;
P [I]. fa = j;
}
Void solve (int rt)
{
If (rt = 0) return;
Printf ("(");
Solve (p [rt]. lch );
Printf ("% s/% d", p [rt]. s, p [rt]. w );
Solve (p [rt]. rch );
Printf (")");
}
Int main ()
{
Char s [111], tmp [111];
While (scanf ("% d", & n )! = EOF & n)
{
For (int I = 1; I <= n; I ++)
{
Scanf ("% s", s );
Int j;
For (j = 0; s [j]; j ++)
If (s [j] = '/') break;
Else tmp [j] = s [j];
Tmp [j ++] = '\ 0 ';
Int num = 0;
While (s [j]) {num = num * 10 + s [j]-'0'; j ++ ;}
Strcpy (p [I]. s, tmp );
P [I]. w = num;
P [I]. lch = p [I]. rch = 0;
P [I]. fa = 0;
}
Sort (p + 1, p + n + 1 );
P [0]. w = INF;
P [0]. lch = p [0]. rch = p [0]. fa = 0;
For (int I = 1; I <= n; I ++) insert (I );
Solve (p [0]. rch );
Printf ("\ n ");
}
Return 0;
}

# 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 5555
# Define INF 100000007
Using namespace std;
Struct node
{
Int w, fa, lch, rch;
Char s [111];
Bool operator <(const node & a) const
{
Return strcmp (s, a. s) <= 0;
}
} P [MAXN];
Int n;
Void insert (int I)
{
Int j = I-1;
While (p [j]. w <p [I]. w) j = p [j]. fa;
P [I]. lch = p [j]. rch;
P [j]. rch = I;
P [I]. fa = j;
}
Void solve (int rt)
{
If (rt = 0) return;
Printf ("(");
Solve (p [rt]. lch );
Printf ("% s/% d", p [rt]. s, p [rt]. w );
Solve (p [rt]. rch );
Printf (")");
}
Int main ()
{
Char s [111], tmp [111];
While (scanf ("% d", & n )! = EOF & n)
{
For (int I = 1; I <= n; I ++)
{
Scanf ("% s", s );
Int j;
For (j = 0; s [j]; j ++)
If (s [j] = '/') break;
Else tmp [j] = s [j];
Tmp [j ++] = '\ 0 ';
Int num = 0;
While (s [j]) {num = num * 10 + s [j]-'0'; j ++ ;}
Strcpy (p [I]. s, tmp );
P [I]. w = num;
P [I]. lch = p [I]. rch = 0;
P [I]. fa = 0;
}
Sort (p + 1, p + n + 1 );
P [0]. w = INF;
P [0]. lch = p [0]. rch = p [0]. fa = 0;
For (int I = 1; I <= n; I ++) insert (I );
Solve (p [0]. rch );
Printf ("\ n ");
}
Return 0;
}

You can also use RMQ

Or sort first

Each time we recursively process a range, we can find the root with the largest second keyword. On the left is his left subtree, and on the right is the right subtree.


[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 5555
# Define INF 100000007
Using namespace std;
Struct node
{
Int w;
Char s [111];
Bool operator <(const node & a) const
{
Return strcmp (s, a. s) <= 0;
}
} P [MAXN];
Int mi [MAXN] [17], mx [MAXN] [17];
Int n;
Void rmqinit ()
{
For (int I = 1; I <= n; I ++) mi [I] [0] = mx [I] [0] = I;
Int m = (int) (log (n * 1.0)/log (2.0 ));
For (int I = 1; I <= m; I ++)
For (int j = 1; j <= n; j ++)
{
Mx [j] [I] = mx [j] [I-1];
Mi [j] [I] = mi [j] [I-1];
Int k = j + (1 <(I-1 ));
If (k <= n)
{
If (p [mx [j] [I]. w <p [mx [k] [I-1]. w) mx [j] [I] = mx [k] [I-1];
If (p [mi [j] [I]. w> p [mi [k] [I-1]. w) mi [j] [I] = mi [k] [I-1];
}
}
}
Int rmqmax (int l, int r)
{
Int m = (int) (log (r-l + 1) * 1.0)/log (2.0 ));
If (p [mx [l] [m]. w <p [mx [r-(1 <m) + 1] [m]. w) return mx [r-(1 <m) + 1] [m];
Else return mx [l] [m];
}
Void solve (int s, int t)
{
If (s> t) return;
Int pos = rmqmax (s, t );
Printf ("(");
Solve (s, pos-1 );
Printf ("% s/% d", p [pos]. s, p [pos]. w );
Solve (pos + 1, t );
Printf (")");
}
Int main ()
{
Char s [111], tmp [111];
While (scanf ("% d", & n )! = EOF & n)
{
For (int I = 1; I <= n; I ++)
{
Scanf ("% s", s );
Int j;
For (j = 0; s [j]; j ++)
If (s [j] = '/') break;
Else tmp [j] = s [j];
Tmp [j ++] = '\ 0 ';
Int num = 0;
While (s [j]) {num = num * 10 + s [j]-'0'; j ++ ;}
Strcpy (p [I]. s, tmp );
P [I]. w = num;
}
Sort (p + 1, p + n + 1 );
Rmqinit ();
Solve (1, n );
Printf ("\ n ");
}
Return 0;
}

# 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 5555
# Define INF 100000007
Using namespace std;
Struct node
{
Int w;
Char s [111];
Bool operator <(const node & a) const
{
Return strcmp (s, a. s) <= 0;
}
} P [MAXN];
Int mi [MAXN] [17], mx [MAXN] [17];
Int n;
Void rmqinit ()
{
For (int I = 1; I <= n; I ++) mi [I] [0] = mx [I] [0] = I;
Int m = (int) (log (n * 1.0)/log (2.0 ));
For (int I = 1; I <= m; I ++)
For (int j = 1; j <= n; j ++)
{
Mx [j] [I] = mx [j] [I-1];
Mi [j] [I] = mi [j] [I-1];
Int k = j + (1 <(I-1 ));
If (k <= n)
{
If (p [mx [j] [I]. w <p [mx [k] [I-1]. w) mx [j] [I] = mx [k] [I-1];
If (p [mi [j] [I]. w> p [mi [k] [I-1]. w) mi [j] [I] = mi [k] [I-1];
}
}
}
Int rmqmax (int l, int r)
{
Int m = (int) (log (r-l + 1) * 1.0)/log (2.0 ));
If (p [mx [l] [m]. w <p [mx [r-(1 <m) + 1] [m]. w) return mx [r-(1 <m) + 1] [m];
Else return mx [l] [m];
}
Void solve (int s, int t)
{
If (s> t) return;
Int pos = rmqmax (s, t );
Printf ("(");
Solve (s, pos-1 );
Printf ("% s/% d", p [pos]. s, p [pos]. w );
Solve (pos + 1, t );
Printf (")");
}
Int main ()
{
Char s [111], tmp [111];
While (scanf ("% d", & n )! = EOF & n)
{
For (int I = 1; I <= n; I ++)
{
Scanf ("% s", s );
Int j;
For (j = 0; s [j]; j ++)
If (s [j] = '/') break;
Else tmp [j] = s [j];
Tmp [j ++] = '\ 0 ';
Int num = 0;
While (s [j]) {num = num * 10 + s [j]-'0'; j ++ ;}
Strcpy (p [I]. s, tmp );
P [I]. w = num;
}
Sort (p + 1, p + n + 1 );
Rmqinit ();
Solve (1, n );
Printf ("\ n ");
}
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.