int rank(int v) { int ans; if (v<=tra.size()) //at top area { ans=rm[MAX-1-tra.find_kth(v)]; } else { if (tra.size()==0) ans=v; else { v-=tra.size(); int lo=1,hi=n,mid; while (lo<=hi) { mid=(lo+hi)/2; int tmp=mid-trb.rank(mid); if (tmp>=v) hi=mid-1; else lo=mid+1; } ans=lo; } } return ans; }
int main() { int n, low; srand(time(0)); while (EOF != scanf("%d%d", &n, &low)) { int k; char s[4]; tr.init(); while (n--) { scanf("%s%d", s, &k); if (s[0] == 'I') { if (k >= low) { tr.insert(k); } else { // tr.del_cnt++; // 傻逼了,公司都没进,哪来的出公司啊 } } else if (s[0] == 'A') { tr.delta += k; } else if (s[0] == 'S') { tr.delta -= k; tr.del_low(low); } else if (s[0] == 'F') { printf("%d\n", tr.find_kth(tr.size() - k + 1)); } else { for(;;); } } printf("%d\n", tr.del_cnt); } return 0; }
forn(q,Q) { char op[8]; scanf("%s", op); if (op[0] == 'I') { int x,y; scanf("%d%d", &x,&y); x--; // Esto es parecido (pero bien diferente :P) al codigo de insertarAUnladoPointer, pero empezando en p en lugar de en root // De entrada me comi que era hacer lo mismo exacto que insertarAUnLado, y pegue WA. // Y luego me comi otro WA por olvidarme la linea p = p->h(0); :P // Moraleja: esto seria bastante mas error prone que el split obvio. Nodo<Datos> *p = iesimo(x, t), *nodo = new Nodo<Datos>(y); int lado = 0; if (p->h(0)) { p = p->h(0); lado = 1; while (p->h(1)) p = p->h(1); } p->hang(lado, nodo); nodo->flotar(); t.reroot(); } else if (op[0] == 'D') { int x; scanf("%d", &x); x--; t.erasePointer(iesimo(x, t)); } else if (op[0] == 'R') { int x,y; scanf("%d%d", &x, &y); x--; Nodo<Datos> *p = iesimo(x, t); p->dat.x = y; p->fullUpdate(); } else if (op[0] == 'Q') { int x,y; scanf("%d%d", &x,&y); x--; assert(x < y); Treap<Datos> t2,t3; t.splitPointer(iesimo(x, t), t2); t2.splitPointer(iesimo(y-x, t2), t3); printf("%d\n", t2.root->dat.bestSum); t.merge(t2); t.merge(t3); } else assert(false); }
void top(int v) { if (M.count(v)>0) tra.remove(M[v]); M[v]=--offset; tra.insert(M[v]); rm[MAX-1-offset]=v; if (!trb.contain(v)) trb.insert(v); }
Treap *left_rotate() { Treap *y = left; left = y->right; y->right = this; update(); y->update(); return y; }
Treap *right_rotate() { Treap *y = right; right = y->left; y->left = this; update(); y->update(); return y; }
int query(int v) { int ans; if (M.count(v)>0) // at top area { ans=tra.rank(M[v]); } else //add those vi>v at top area { ans=v+(trb.size()-trb.rank(v)); } return ans; }
void treap() { Treap<int> treap; #define TREAP_LOOP for (int i = 0; i < 1000; i ++) TREAP_LOOP { treap.Insert(i); assert(treap.GetMin() == 0); assert(treap.GetMax() == i); } std::cout<<"Treap constructed"<<std::endl; // print keys in sorted order treap.InOrderTraversal(); TREAP_LOOP { assert(treap.Exists(i)); } TREAP_LOOP { treap.Delete(i); } TREAP_LOOP { assert(!treap.Exists(i)); } }
int main() { int a[] = {4,1,3,5,2}; Treap bst = Treap(); for (int i=0; i<5; i++) { pair< Treap, Treap > splitedTreap = bst.SplitByVal(a[i]); Treap singleNodeTreap = Treap(a[i], 1); bst = splitedTreap.X.Join(singleNodeTreap); bst = bst.Join(splitedTreap.Y); } bst.Print(); return 0; }
int main() { Tree<int> tree; Treap<int> treap; int n = 0; cin >> n; int value = 0; int priority = 0; for(int i = 0; i < n; i++) { cin >> value >> priority; tree.Add(value); treap.Add(value, priority); } cout << tree.Height() - treap.Height(); return 0; }
int main() { freopen("Text/ORDERSET.txt","r",stdin); int Q; scanf("%d",&Q); Treap oTreap; while(Q--) { char t[5]; int p; scanf("%s%d",t,&p); if(t[0]=='I') { oTreap.Insert(p); } else if(t[0]=='D') { oTreap.Delete(p); } else if(t[0]=='K') { int v = oTreap.FindKth(p); if(v > -INF) { printf("%d\n",v); } else puts("invalid"); } else { int v = oTreap.Count(p); printf("%d\n",v); } } return 0; }
TEST(Treap , Remove) { Treap* tr = new Treap(false); size_t n=5000; uint32_t* A = (uint32_t*)malloc(n*sizeof(uint32_t)); for (size_t i=0; i<n; i++) A[i] = rand() % 5001; for (size_t i=0; i<n; i++) tr->Insert(A[i]); for (size_t i=1; i<n; i+=2) tr->Remove(A[i]); for (size_t i=0; i<n; i++) { if (i%2==1) { CHECK(tr->Find(A[i]) == false); } } free(A); delete tr; }
main() { int n,m ; scanf("%d%d",&n,&m) ; for(int i=1;i<=n;i++) scanf("%d",&a[i].x) ; for(int i=1;i<=n;i++) scanf("%d",&a[i].y) ; sort(a+1,a+n+1,cmp) ; LL ans=0LL ; node *root=NULL ; for(int i=1;i<=n;i++) ans+=trp.insert(root,a[i].y) ; printf("%lld\n",ans) ; }
TEST(Treap , InsertFind) { Treap* tr = new Treap(false); BinarySearchTree* bst = new BinarySearchTree(false); size_t n=5000; uint32_t* A = (uint32_t*)malloc(n*sizeof(uint32_t)); for (size_t i=0; i<n; i++) A[i] = rand() % 5001; for (size_t i=0; i<n; i++) { tr->Insert(A[i]); bst->Insert(A[i]); } for (size_t i=0; i<n; i++) { CHECK(tr->Find(A[i]) == bst->Find(A[i])); CHECK(tr->Find(A[i]+1) == bst->Find(A[i]+1)); } delete tr; delete bst; free(A); }
int main() { try { Treap<int> tr; tr.push(3); tr.push(2); tr.push(5); tr.push(7); tr.print(); tr.remove(2); cout << "\n"; tr.print(); return 0; } catch(const reaper::error_base &e) { cout << e.what() << endl; return 1; } }
int main(){ //freopen("My/4554/9.dat", "r", stdin); Treap * treap = new Treap(); int n, a, b, type; char c; scanf("%d", &n); for( int i=1; i<=n; i++ ){ scanf("%d %d %d", &type, &a, &b); bool res; if( type == 1 ){ scanf(" %c", &c); res = treap->insert( a, b - 1, c ); } else { res = treap->erase( a, b - 1 ); } if( res ){ printf("YES\n"); } else { printf("NO\n"); } } return 0; }
main() { srand(time(NULL)) ; int n ; while(scanf("%d",&n)!=EOF) { node *root=NULL ; for(int i=1;i<=n;i++) { int x ; scanf("%d",&x) ; printf("%d\n",trp.insert(root,x)+1) ; } } }
void solve(){ int N,X; char ch[5]; scanf("%d",&N); Treap root; while(N--){ scanf("%s%d",ch,&X); if (ch[0] == 'I') root.Insert_unq(X);//, root.print(); else if (ch[0] == 'D') root.Erase(X); else if (ch[0] == 'C'){ int res = root.Count(X); printf("%d\n",res); } else { int res = root.Kth(X); if (res == INT_MIN) printf("invalid\n"); else printf("%d\n",res); } } }
int main() { size_t elementsNumber = 0; cin >> elementsNumber; if ( elementsNumber == 0 ) { return 1; } BinaryTree binaryTree; Treap treap; for ( size_t i = 0; i < elementsNumber; ++i ) { int k = 0; int p = 0; cin >> k >> p; binaryTree.Add( k ); treap.Add( k, p ); } const int result = (int)treap.GetWidestLayer() - (int)binaryTree.GetWidestLayer(); cout << result; return 0; }
void Menu() { int val,choice,newVal; Treap myTreap; while(true) { do { cout<<"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"<<endl; cout<<" 1.插入"<<endl; cout<<" 2.删除"<<endl; cout<<" 3.修改"<<endl; cout<<" 4.查找"<<endl; cout<<" 5.显示"<<endl; cout<<" 6.返回"<<endl; cout<<"请输入你的选项[ ]\b\b"; cin>>choice; }while(choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6); if(1==choice) { cin>>val; myTreap.InsertTreap(val); } else if(2==choice)
int main() { srand(time(0)); const int MAX = 1000; Treap<int, int> treap; for (int i = 0; i < MAX; i++) { assert(treap.add(i, i)); } for (int i = 0; i < MAX; i++) { assert(treap.contains(i)); } assert(treap.contains(MAX+1) == false); assert(treap.remove(7)); assert(treap.contains(7) == false); std::cout << "Finished succesfully." << std::endl; return 0; }
void work() { printf("Case #%d:\n" , ++ ca); int i , j , x , y , z , l , r; scanf("%d%d",&n,&m); T.clear(); for (i = 1 ; i <= n ; ++ i) { scanf("%d" , &a[i]); root[i] = 0 , id[i] = i , f[i] = i; T.insert(root[i] , a[i]); } while (m --) { scanf("%d" , &j); if (j == 1) { scanf("%d%d",&x,&y); x = getf(id[x]) , y = getf(id[y]); if (x == y) continue; if (T.size[root[x]] > T.size[root[y]]) swap(x , y); T.merge(root[x] , root[y]); f[x] = y; } if (j == 2) { scanf("%d%d",&x,&y); z = getf(id[x]) , y = getf(id[y]); if (x == y) continue; T.erase(root[z] , a[x]); id[x] = ++ n; f[n] = y; T.insert(root[y] , a[x]); } if (j == 3) { scanf("%d%d",&x,&y); z = getf(id[x]); T.erase(root[z] , a[x]); a[x] = y; T.insert(root[z] , a[x]); } if (j == 4) { scanf("%d",&x); x = getf(id[x]); if (T.size[root[x]] < 3) { printf("%d\n" , T.size[root[x]]); } else { i = 2; y = T.getKth(root[x] , 1); z = T.getKth(root[x] , 2); while (1) { l = T.lower_bound(root[x] , y + z); if (l == 1 << 30) break; ++ i , y = z , z = l; } printf("%d\n" , i); } } if (j == 5) { scanf("%d%d%d",&x,&l,&r); x = getf(id[x]); z = T.range(root[x] , l , r); printf("%d\n" , z ? z : -1); } } //printf("%d\n" , T.nodecnt); }
TEST(Treap , Successor) { Treap* tr = new Treap(false); CHECK(tr->Successor(5) == -1); tr->Insert(1); tr->Insert(5); tr->Insert(2); CHECK(tr->Successor(5) == -1); tr->Insert(61); tr->Insert(61); tr->Insert(24); tr->Insert(51); tr->Insert(61); tr->Insert(57); tr->Insert(32); tr->Insert(24); CHECK(tr->Successor(1) == 2); CHECK(tr->Successor(2) == 5); CHECK(tr->Successor(5) == 24); CHECK(tr->Successor(24) == 32); CHECK(tr->Successor(32) == 51); CHECK(tr->Successor(51) == 57); CHECK(tr->Successor(57) == 61); CHECK(tr->Successor(61) == -1); CHECK(tr->Successor(6) == -1); delete tr; }
//test function int main() { Treap treap; //printf("test here\n"); treap.insert(1, 17); treap.insert(2, 30); treap.insert(3, 47); treap.insert(4, 33); treap.insert(5, 11); treap.print(); getchar(); treap.del(5); treap.print(); getchar(); treap.del(1); treap.print(); getchar(); treap.del(2); treap.print(); //check }
TEST(Treap , Max) { Treap* tr = new Treap(false); CHECK(tr->Max() == -1); tr->Insert(1); tr->Insert(5); tr->Insert(2); tr->Insert(61); tr->Insert(61); tr->Insert(24); tr->Insert(51); tr->Insert(61); tr->Insert(57); tr->Insert(32); tr->Insert(24); CHECK(tr->Max() == 61); tr->Remove(61); tr->Remove(57); CHECK(tr->Max() == 51); tr->Remove(51); CHECK(tr->Max() == 32); tr->Remove(32); CHECK(tr->Max() == 24); tr->Remove(24); CHECK(tr->Max() == 5); tr->Remove(5); CHECK(tr->Max() == 2); tr->Remove(2); CHECK(tr->Max() == 1); tr->Remove(1); CHECK(tr->Max() == -1); delete tr; }
int main() { /*TreapInit(); tra.init(); int i; for (int i=0;i<5;i++) tra.insert(i); tra.inorder(tra.root); puts(""); tra.insert(4); tra.inorder(tra.root); puts(""); tra.remove(2); tra.inorder(tra.root); puts(""); for (i=1;i<=5;i++) printf("the %dth is %d\n",i,tra.find_kth(i)); for (i=0;i<=5;i++) printf("rank of %d is %d\n",i,tra.rank(i));*/ //freopen("in","r",stdin); //freopen("out","w",stdout); int t,cas=0; char buffer[20]; int v; scanf("%d",&t); while (t--) { cas++; printf("Case %d:\n",cas); scanf("%d%d",&n,&Q); assert(1<=n && n<=100000000); assert(1<=Q && Q<=100000); TreapInit(); tra.init(); trb.init(); M.clear(); offset=MAX; int ans; while (Q--) { scanf("%s%d",buffer,&v); assert(1<=v && v<=n); switch(buffer[0]) { case 'T': top(v); break; case 'Q': ans=query(v); printf("%d\n",ans); break; case 'R': ans=rank(v); printf("%d\n",ans); break; } } } return 0; }