Magical Girls ' matchmaking Conference
Description
Small wind energy randomly appears "XQ" When: Thick Line a girl, Girls will say a favorite object of handsome value, then the boy out of the team, and girls go ~
Small wind energy randomly appeared "WT" when: Thick line of a girl, girls will say a favorite object of the location, but, this boy shrinks he doesn't like girls. So she turned down the girl. Now output the handsome value of the boy.
small wind energy randomly appearing "you" When: Magic failed, output "single dog! "
Input
The first line enters an integer n (0<n<=111111), and the next n lines, each line enters the magic result c.
Outputwhen the magic result is "WT", the output girl said the position of handsome value. Output 0 when not found
when the magic result is "you", Output "single dog!". Sample Input
10
JX 11
JX 111
JX 1111
WT 1
XQ 11
WT 1
XQ 111
WT 1
WT 1111
You
Sample Output
11
111
1111
0
Single dog!
Ideas
Balanced binary Tree template problem
#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 111119; typedef struct sbtnod{int key,left,right,size;} sbtnode;int sbttail = 0,root = 0;sbtnode tree[maxn];void rrotate (INT&A mp t) {int k = Tree[t].left; if (!k) return; Tree[t].left = Tree[k].right; Tree[k].right = t; Tree[k].size = tree[t].size; Tree[t].size = tree[tree[t].left].size + tree[tree[t].right].size + 1; t = k;} void Lrotate (int& t) {int k = tree[t].right; if (!k) return; Tree[t].right =tree[k].left; Tree[k].left = t; Tree[k].size = tree[t].size; Tree[t].size = tree[tree[t].left].size + tree[tree[t].right].size + 1; t = k;} void maintain (int& T,bool flag) {if (!t) return; if (!flag) if (Tree[tree[tree[t].left].left].size > Tree[tree[t].right].size) rrotate (t); else if (Tree[tree[tree[t].left].right].size > Tree[tree[t].right].size) {lrotate (tree[t].left); RRotate (t); } else return; else if (Tree[tree[tree[t].right].right].size > Tree[tree[t].left].size) lrotate (t); else if (Tree[tree[tree[t].right].left].size > Tree[tree[t].left].size) {rrotate (tree[t].right); Lrotate (t); } else return; Maintain (TREE[T].LEFT,FALSE); Maintain (tree[t].right,true); Maintain (T,FALSE); Maintain (t,true);} void Insert (int& T,int v) {if (!t) {sbttail++; Tree[sbttail].key = v; Tree[sbttail].size = 1; t = sbttail; } else {tree[t].size++; if (v < tree[t].key) Insert (TREE[T].LEFT,V); else insert (TREE[T].RIGHT,V); Maintain (t,v >= tree[t].key); }}int del (int& T,int v) {int ret; tree[t].size--; if (v = = tree[t].key| | (V < tree[t].key && Tree[t].left = = 0) | | (V > Tree[t].key && tree[t].right = = 0)) {Ret=tree[t].key; if (Tree[t].left = = 0| | Tree[t].right = = 0) T = tree[t].left + tree[t].right; else Tree[t].key = del (Tree[t].left,tree[t].key + 1); } else {if (v < tree[t].key) ret = del (TREE[T].LEFT,V); else ret = del (TREE[T].RIGHT,V); } return ret;} int select (int &x,int k) {int r = tree[tree[x].left].size + 1; if (K < R) return select (Tree[x].left,k); else if (K > R) return select (Tree[x].right,k-r); else return tree[x].key; } int main () {int n,val; Char opt[10]; scanf ("%d", &n); while (n--) {scanf ("%s", opt); if (strcmp (OPT, "JX") = = 0) {scanf ("%d", &val); Insert (Root,val); } else if (strcmp (OPT, "XQ") = = 0) {scanf ("%d", &val); Del (root,val); } else if (strcmp (OPT, "WT") = = 0) { scanf ("%d", &val); printf ("%d\n", select (Root,val)); } else if (strcmp (OPT, "you") = = 0) {printf ("single dog!\n"); }} return 0; }
Data structure Job-magic girl (Balanced binary tree)