Learning Cartesian tree

Cartesian tree (summary of knowledge + board finishing)

Cartesian tree

 

Example 1: hdu-6305

RMQ Similar Sequence

 

[Title] is intended to:

The meaning of problems: the definition RMQ (A, l, r) as follows: In sequence A, satisfies A [i] = max (A [l], A [l + 1], ..., A [r]) minimum i. If for any (l, r) is satisfied RMQ (A, l, r) = RMQ (B, l, r) A and B are compared RMQ Similar. Now the sequence A, B sequence of each number is a real number between 0 and 1, Q and A are all satisfied RMQ Similar B sequence and all of the desired numbers.

Solution: easy to see if A and B are RMQ Similar, then A and B are the same Cartesian tree structure. Each number B is considered a real number between 0 and 1, and therefore the probability of occurrence of the same number is 0, it may be assumed that each number B are not the same arrangement. A set of Cartesian size probability tree for each subtree sz [u], is any one of A and B are arranged it is isomorphic , as each a number satisfying B uniformly distributed, thus the expected value is 1/2, and the desired It is n / 2, thus satisfying the a and B is isomorphic to all of the number of desired

 


Therefore for: sz [i] Cartesian tree refers to the size of each subtree.

 

Paste the code:

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<stack>
 4 using namespace std;
 5 typedef long long ll;
 6 const int N = 1e6 + 5 ;
 7 const int INF = 0x3f3f3f3f;
 8 const int mod = 1e9+7;
 9 inline int read(){
10     int x = 0 , f = 1 ;
11     char c = getchar();
12     while ( c < '0' || c > '9' ) {
13         if( c=='-' ) f = -1 ;
14         c = getchar();
15     }
16     while ( '0' <= c && c <= '9' ){
17         x = x*10 + c-'0';
18         c = getchar();
19     }
20     return x * f ;
21 }
22 typedef struct Node{
23     int val,fa,sz;
24     int L,R;
25     Node(){}
26 }Node;
27 Node t[N];
28 stack<int>st;
29 void Init(int n){
30     for(int i=0;i<=n;i++){
31         t[i].sz = t[i].L = t[i].R = t[i].fa = 0;
32     }
33     t[0].val = INF;
34     while(!st.empty()) st.pop();
35     st.push(0);
36 }
37 
38 void build(int n){
39     int k ;
40     for(int i=1;i<=n;i++){
41         while( !st.empty() && t[st.top()].val < t[i].val )
42             st.pop();
43         k = st.top();
44         t[i].L = t[k].R;
45         t[k].R = i ;
46         t[i].fa = k;
47         t[t[i].L].fa = i ;
48         st.push(i);
49     }
50 }
51 void dfs(int u) {
52     if ( !u ) return ;
53     t[u].sz = 1 ;
54     dfs( t[u].L );
55     dfs( t[u].R );
56     t[u].sz += t[t[u].L].sz
57             + t[t[u].R].sz ;
58 }
59 ll inv[N];
60 void Init() {
61     inv[1] = 1;
62     for (int i = 2; i < N; i++)
63         inv[i] = inv[mod % i] * (mod - mod / i) % mod;
64 }
65 int main()
66 {
67     int T,n;
68     Init();
69     for ( scanf("%d",&T) ; T ; T-- ){
70 
71         n = read();
72         Init(n);
73         for(int i=1;i<=n;i++){
74             t[i].val = read() ;
75         }
76         //puts("####");
77 
78         build(n);
79         dfs(t[0].R);
80 
81         ll ans=n *inv[2]%mod;
82         for(int i=1;i<=n;i++)
83             ans=ans*inv[t[i].sz]%mod;
84 
85         printf("%lld\n",years);
86      }
 87      return  0 ;
88  }
 89  / * 
90  3
 91  3
 92  1 2 3
 93  3
 94  1 2 1
 95  5
 96  1 2 3 2 1
 97  * /
hdu-6305

 

 

[2] example poj-2201

Cartesian Tree

Subject to the effect: Cartesian bare tree, parent and son contribution output node numbers for each point, not zero.

Descartes constructed a tree, then the tree can be output.

First of all sorts, and maintenance information rightmost node of the tree with a stack, insert the time to look for in accordance with the second key, insert after finding, following the tree into its left subtree can be.

Then insert the three cases discussed (bottom, middle, to become the new roots)

Topic Analysis: Note that this problem will certainly be output YES, because not the same for each keyword, they are able to build only Cartesian tree, there is no case of NO. After sorting as well as the serial number on the chaos, attention on the line, the other did not have anything.
 1 #include<cstdio>
 2 #include<algorithm>
 3 const int N = 5e4+5;
 4 using namespace std;
 5 typedef struct Node{
 6     int id , L , R , val , key ,fa ;
 7     bool operator < (const Node &rhs )const {
 8         return key < rhs.key ;
 9     }
10 }Node ;
11 Node t[N];
12 int n,L[N],R[N],st[N],Fa[N];
13 void build(){
14     int top = 0 ;
15     st[top] = 1 ;
16     for(int i=2;i<=n;i++){
17         while( top >= 0 && t[st[top]].val > t[i].val )
18             top -- ;
19         if ( top > -1  ){
20             t[i].L = t[st[top]].R;
21             t[st[top]].R = i ;
22             t[i].fa = st[top];
23             t[t[i].L].fa = i ;
24         }else{
25             t[st[0]].fa = i ;
26             t[i].L = st[0];
27         }
28         st[++top] =  i ;
29     }
30 }
31 
32 int main()
33 {
34     while(~scanf("%d",&n)){
35         for(int i=1;i<=n;i++){
36             scanf("%d%d",&t[i].key,&t[i].val);
37             t[i].id = i ;
38             t[i].L = t[i].R = t[i].fa = 0 ;
39         }
40         sort( t+1 , t+1+n ) ;
41         build();
42         for(int i=1;i<=n;i++){
43             Fa[t[i].id] = t[t[i].fa].id;
44             L[t[i].id] = t[t[i].L].id ;
45             R[t[i].id] = t[t[i].R].id ;
46         }
47         puts("YES");
48         for(int i=1;i<=n;i++){
49             printf("%d %d %d\n",Fa[i],L[i],R[i]);
50         }
51     }
52     return 0;
53 }
54 /*
55 
56 7
57 5 4
58 2 2
59 3 9
60 0 5
61 1 3
62 6 6
63 4 11
64 
65 YES
66 2 3 6
67 0 5 1
68 1 0 7
69 5 0 0
70 2 4 0
71 1 0 0
72 3 0 0
73  */
capacity - 2201

 

Guess you like

Origin www.cnblogs.com/Osea/p/11216662.html