Pagini recente » Cod sursa (job #2558091) | Cod sursa (job #2826391) | Cod sursa (job #614534) | Cod sursa (job #1548805) | Cod sursa (job #2414330)
#include <bits/stdc++.h>
using namespace std;
ifstream f("order.in");
ofstream g("order.out");
struct T{
T *left,*right;
int key,priority,sz;
T() {}
T(int key,int priority,T *left,T *right){
this->left=left;
this->right=right;
this->key=key;
this->priority=priority;
this->sz=1;
}
} *root,*nil;;
int cnt(T* n){
if(n==nil)return 0;
return (n->sz);
}
void upd(T* &n){
if(n==nil)return ;
n->sz=cnt(n->left)+cnt(n->right)+1;
return ;
}
void rotleft(T* &n){
T* t=n->left;
n->left=t->right,t->right=n;
upd(n);upd(t);
n=t;
}
void rotright(T* &n){
T*t=n->right;
n->right=t->left,t->left=n;
upd(n);upd(t);
n=t;
}
void balance(T* &n){
upd(n);
if(n->priority < n->left->priority)
rotleft(n);
else if(n->priority < n->right->priority)
rotright(n);
}
void insert(T* &n,int key,int priority){
if(n==nil){
n=new T(key,priority,nil,nil);
return ;
}
if(key<n->key)
insert(n->left,key,priority);
else if(key>n->key)
insert(n->right,key,priority);
balance(n);
}
pair<T*,T*> split(T* &n,int k){
if(n==nil)return {nil,nil};
upd(n);
if(cnt(n->left)>=k){
auto pa=split(n->left,k);
n->left=pa.second;
upd(n);
return {pa.first,n};
}else{
auto pa=split(n->right,k-cnt(n->left)-1);
n->right=pa.first;
upd(n);
return {n,pa.second};
}
return {nil,nil};
}
T* join(T* left,T* right){
if(left==nil)return right;
if(right==nil)return left;
upd(left);upd(right);
if(left->priority > right->priority){
left->right=join(left->right,right);
upd(left);
return left;
}else{
right->left=join(left,right->left);
upd(right);
return right;
}
}
void erase(T* &n,int k){
auto pa=split(n,k-1);
auto u =split(pa.second,1);
g<<u.first->key<<' ';
n=join(pa.first,u.second);
}
int main()
{
srand(time(0));
root=nil=new T(0,0,NULL,NULL);
int n,lastpoz=1;f>>n;
for(int i=1;i<=n;i++)
insert(root,i,rand());
for(int i=1;i<=n;i++){
lastpoz+=i;
lastpoz=(lastpoz-1)%(n-i+1)+1;
erase(root,lastpoz);
lastpoz--;
}
return 0;
}