P3369 [TEMPLATE] Normal Balance Tree (Treap/SBT), p31_treap

Source: Internet
Author: User

P3369 [TEMPLATE] Normal Balance Tree (Treap/SBT), p31_treap
Description

You need to write a data structure (refer to the title of the question) to maintain the number. The following operations are required:

Input/Output Format

Input Format:

 

The first behavior n indicates the number of operations. Each row in the n rows below has two numbers opt and x, and opt indicates the number of operations (1 <= opt <= 6)

 

Output Format:

 

For operations 3, 4, 5, 6, each row outputs a number, indicating the corresponding answer

 

Input and Output sample input sample #1:
101 1064654 11 3177211 4609291 6449851 841851 898516 819681 4927375 493598
Output sample #1:
10646584185492737
Description

Time-Space limit: 1000 ms, 128 M

1. Data range of n: n <= 100000

2. Data range of each number: [-1e7, 1e7]

Source: Tyvj1728 Formerly known as: normal Balance Tree

Thank you here

 

My God, splay and other such things need human life ,,

The idea is better than KMP, and the code is simpler than DP.

But debugging ......

Haha ,,,,,

 

As for how to do this:

Https://www.luogu.org/wiki/show? Name = % E9 % A2 % 98% E8 % A7 % A3 + P3369

 

1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cmath> 5 using namespace std; 6 const int MAXN = 100005; 7 const int maxn = 0x7fffff; 8 void read (int & n) 9 {10 char c = '+'; int x = 0; bool flag = 0; 11 while (c <'0' | c> '9') {c = getchar (); if (c = '-') flag = 1 ;} 12 while (c> = '0' & c <= '9') {x = x * 10 + (c-48); c = getchar ();} 13 flag = 1? N =-x: n = x; 14} 15 struct SPLAY 16 {17 # define root e [0]. ch [1] 18 struct node 19 {20 int v; // the weight of the node 21 int fa; 22 int ch [2]; 23 int sum; // The current node's subtree element 24 int recy; // number of times the current node repeats 25}; 26 node e [MAXN]; 27 int tot, points; 28 SPLAY () 29 {30 tot = 0; points = 0; 31} 32 void update (int pos) // upload tag 33 {34 e [pos]. sum = e [e [pos]. ch [0]. sum + e [e [pos]. ch [1]. sum + e [pos]. recy; 35} 36 int identify (int x) // find the node numbered x Father's left child node or right child node 37 {38 return e [e [x]. fa]. ch [0] = x?; 39} 40 int connect (int son, int father, int how) // connect the node numbered son to the node numbered father. how children 41 {42 e [son]. fa = father; 43 e [father]. ch [how] = son; 44} 45 void rotate (int x) // double spin 46 {47 int y = e [x]. fa; 48 int R = e [y]. fa; 49 int Rson = identify (y); 50 int yson = identify (x); 51 int B = e [x]. ch [yson ^ 1]; 52 connect (B, y, yson); 53 connect (y, x, (yson ^ 1); 54 connect (x, R, rson); 55 update (y); update (x); 56} 57 void Splay (I Nt pos, int to) // rotate the node numbered pos to node 58 {59 to = e [to]. fa; 60 while (e [pos]. fa! = To) 61 {62 int up = e [pos]. fa; 63 if (e [up]. fa = to) rotate (pos); 64 else if (identify (up) = identify (pos) 65 {66 rotate (up); 67 rotate (pos ); 68} 69 else 70 {71 rotate (pos); 72 rotate (pos); 73} 74} 75} 76 77 int crepoint (int v, int father) // create a node 78 {79 tot ++; 80 e [tot]. fa = father; 81 e [tot]. recy = e [tot]. sum = 1; 82 e [tot]. v = v; 83 return tot; 84} 85 void destroy (int x) // directly destroy a node 86 {87 e [x]. ch [0] = e [x]. ch [1] = e [x]. fa = e [x]. recy = e [x]. sum = e [x]. v = 0; 88 if (x = tot) tot --; 89} 90 int find (int v) // locate the location of the vertex whose weight is v. 91 {92 int now = root; 93 while (1) 94 {95 if (e [now]. v = v) 96 {97 Splay (now, root); 98 return now; 99} 100 int nxt = v <e [now]. v?; 101 if (! E [now]. ch [nxt]) return 0; 102 now = e [now]. ch [nxt]; 103} 104} 105 int build (int v) // open a new node 106 {107 points ++; 108 if (tot = 0) 109 {110 root = 1; 111 crepoint (v, 0); 112} 113 else114 {115 int now = root; 116 while (1) 117 {118 e [now]. sum ++; // the element of the Child tree of the vertex on the path that passes through will certainly be ++ 119 if (e [now]. v = v) 120 {121 e [now]. recy ++; 122 return now; 123} 124 int nxt = v <e [now]. v?; 125 if (! E [now]. ch [nxt]) 126 {127 crepoint (v, now); 128 e [now]. ch [nxt] = tot; 129 return tot; 130} 131 now = e [now]. ch [nxt]; 132} 133} 134 return 0; // what .... 135} 136 int push (int v) // create a new node and rotate it to the root 137 {138 int p = build (v); 139 Splay (p, root ); 140} 141 void pop (int v) // Delete the node and adjust the tree structure 142 {143 int deal = find (v); 144 if (! Deal) return; 145 points --; 146 if (e [deal]. recy> 1) 147 {148 e [deal]. recy --; 149 e [deal]. sum --; 150 returned; 151} 152 if (! E [deal]. ch [0]) 153 {154 root = e [deal]. ch [1]; 155 e [root]. fa = 0; 156} 157 else158 {159 int le = e [deal]. ch [0]; 160 while (e [le]. ch [1]) 161 le = e [le]. ch [1]; 162 163 Splay (le, e [deal]. ch [0]); 164 165 int ri = e [deal]. ch [1]; 166 connect (ri, le, 1); 167 connect (le, 168); 169 update (le); 170} destroy (deal ); 171} 172 int rank (int v) // rank 173 in the number of queried values of v {174 int ans = 0, now = root; 175 while (1) 176 {177 if (e [now]. v= = v) 178 return ans + e [e [now]. ch [0]. sum + 1; 179 if (now = 0) return 0; 180 if (v <e [now]. v) now = e [now]. ch [0]; 181 else182 {183 ans + = e [e [now]. ch [0]. sum + e [now]. recy; 184 now = e [now]. ch [1]; 185} 186 187} 188 if (now) Splay (now, root); 189 return 0; 190} 191 int arank (int x) // What is the number of x in the query ranking? 192 {193 if (x> points) return-maxn; 194 int now = root; 195 while (1) 196 {197 int used = e [now]. sum-e [e [now]. ch [1]. sum; 198 if (x> e [e [now]. ch [0]. sum & x <= used) break; 199 if (x <used) 200 now = e [now]. ch [0]; 201 else202 {203 x = x-used; 204 now = e [now]. ch [1]; 205} 206} 207 Splay (now, root); 208 return e [now]. v; 209} 210 int lower (int v) // The maximum value less than v is 211 {212 int now = root; 213 int ans =-maxn; 214 while (now) 215 {216 if (e [now]. v <v & e [now]. v> ans) ans = e [now]. v; 217 if (v> e [now]. v) now = e [now]. ch [1]; 218 else now = e [now]. ch [0]; 219} 220 return ans; 221} 222 int upper (int v) 223 {224 int now = root; 225 int ans = maxn; 226 while (now) 227 {228 if (e [now]. v> v & e [now]. v <ans) ans = e [now]. v; 229 if (v <e [now]. v) now = e [now]. ch [0]; 230 else now = e [now]. ch [1]; 231} 232 return ans; 233} 234}; 235 SPLAY s; 236 int main () 237 {238 239 int n; 240 read (n ); 241 for (int I = 1; I <= n; I ++) 242 {243 int opt, x; 244 read (opt); read (x ); 245 if (opt = 1) 246 s. push (x); 247 else if (opt = 2) 248 s. pop (x); 249 else if (opt = 3) 250 printf ("% d \ n", s. rank (x); 251 else if (opt = 4) 252 printf ("% d \ n", s. arank (x); 253 else if (opt = 5) 254 printf ("% d \ n", s. lower (x); 255 else if (opt = 6) 256 printf ("% d \ n", s. upper (x); 257} 258 return 0; 259}

 

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.