Esempio n. 1
0
void
Node::insertN(int key)
{
    if (value == key)
        return;

    Node *child;

    if (key > value) {
        if (rightChild) {
            rightChild->insertN(key);
            maintain();
            return;
        }

        child = new Node;
        child->value = key;
        rightChild = child;
        child->parent = this;
    } else {
        if (leftChild) {
            leftChild->insertN(key);
            maintain();
            return;
        }
        child = new Node;
        child->value = key;
        leftChild = child;
        child->parent = this;
    }
}
Esempio n. 2
0
int maintain(int &t,int flag)
{
	if (flag==0)//
	{
		if (SBT[SBT[SBT[t].left].left].s>SBT[SBT[t].right].s)
			right_rotate(t);
		else if (SBT[SBT[SBT[t].left].right].s>SBT[SBT[t].right].s)
		{
			left_rotate(SBT[t].left);
			right_rotate(t);
		}
		else return t;
	}
	else
	{
		if (SBT[SBT[SBT[t].right].right].s>SBT[SBT[t].left].s)
			left_rotate(t);
		else if (SBT[SBT[SBT[t].right].left].s>SBT[SBT[t].left].s)
		{
			right_rotate(SBT[t].right);
			left_rotate(t);
		}
		else return t;
	}
	maintain(SBT[t].left,0);
	maintain(SBT[t].right,1);
	maintain(t,0);
	maintain(t,1);
	return t;
}
Esempio n. 3
0
File: SBT.hpp Progetto: xyiyy/icpc
void maintain(int &r, bool flag) {
    if (flag) {
        if (sbt[sbt[sbt[r].right].right].size > sbt[sbt[r].left].size)
            left_rot(r);
        else if (sbt[sbt[sbt[r].right].left].size > sbt[sbt[r].left].size) {
            right_rot(sbt[r].right);
            left_rot(r);
        }
        else
            return;
    }
    else {
        if (sbt[sbt[sbt[r].left].left].size > sbt[sbt[r].right].size)
            right_rot(r);
        else if (sbt[sbt[sbt[r].left].right].size > sbt[sbt[r].right].size) {
            left_rot(sbt[r].left);
            right_rot(r);
        }
        else
            return;
    }
    maintain(sbt[r].left, false);
    maintain(sbt[r].right, true);
    maintain(r, false);
    maintain(r, true);
}
Esempio n. 4
0
	void maintain(int &p, bool flag)
	{
		if(flag)
		{
			if(S[R[R[p]]] > S[L[p]]) rotate_left(p);
			else if(S[R[L[p]]] > S[L[p]])
			{
				rotate_right(R[p]);
				rotate_left(p);
			}
			else return;
		}
		else
		{
			if(S[L[L[p]]] > S[R[p]]) rotate_right(p);
			else if(S[L[R[p]]] > S[R[p]])
			{
				rotate_left(L[p]);
				rotate_right(p);
			}
			else return;
		}
		maintain(L[p], 0);
		maintain(R[p], 1);
		maintain(p, 0);
		maintain(p, 1);
	}
// inspect data structures with x as root
void dfs2(int x, int from) {
  for (int y: adj[x]) if (y != from) {
    maintain(x, y);
    maintain(y, -1);
    dfs2(y, x);
  }
  maintain(x, from);
}
Esempio n. 6
0
void set_update(int o,int L,int R){ 
  int lc=o*2,rc=o*2+1; 
  if(y1<=L && y2>=R){ 
    setv[o]=v; 
  }else{ 
    pushdown(o); 
    int M=L+(R-L)/2; 
    if(y1<=M) set_update(lc,L,M);else maintain(lc,L,M); 
    if(y2>M)  set_update(rc,M+1,R);else maintain(rc,M+1,R); 
  } 
  maintain(o,L,R); 
}
Esempio n. 7
0
 bool erase(node_ptr &R, T value) {
     bool ret = false;
     if (R == nilptr) {
         return false;
     } else if (R->value == value) {
         if (R->rchild == nilptr) {
             node_ptr tmp = R;
             R = tmp->lchild;
             delete tmp;
         } else {
             node_ptr tmp = R->rchild;
             while (tmp->lchild != nilptr)
                 tmp = tmp->lchild;
             R->value = tmp->value;
             erase(R->rchild, tmp->value);
             fix(R);
         }
         return true;
     } else if (value < R->value) {
         ret = ret || erase(R->lchild, value);
     } else {
         ret = ret || erase(R->rchild, value);
     }
     fix(R);
     maintain(R);
     return ret;
 }
Esempio n. 8
0
void update(int o,int l,int r,int x,int a)
{
	if(x<l || x>r) return;
	if(l==r) {maxv[o]=sumv[o]=a;return;}
	int m=l+r>>1;
	if(x<=m) update(o<<1,l,m,x,a);
	else update(o<<1|1,m+1,r,x,a);
	maintain(o);
}
Esempio n. 9
0
void insert(Node*&cur, Node*node){
	if(cur == nil){
		cur = node;
	}else{
		insert(cur->s[node->ky > cur->ky], node);
		cur->rz();
		maintain(cur, node->ky > cur->ky);
	}
}
Esempio n. 10
0
void update(int o,int L,int R){ 
  int lc=o*2,rc=o*2+1; 
  if(y1<=L && y2>=R){ 
    addv[o]+=v; 
  }else{ 
    int M=L+(R-L)/2; 
    if(y1<=M) update(lc,L,M); 
    if(y2>M)  update(rc,M+1,R); 
  } 
  maintain(o,L,R); 
}
Esempio n. 11
0
void maintain(int x)
{
   int t;
   if (x==1 || h[x]<h[x/2])
      return;
   t=h[x],h[x]=h[x/2],h[x/2]=t;
   t=g[x],g[x]=g[x/2],g[x/2]=t;
   c[g[x]]=x;
   c[g[x/2]]=x/2;
   maintain(x/2);
}
Esempio n. 12
0
void insert(int x,int tail,int value,int p)
{
   void maintain(int,int,int);
   int t;
   t=heap[x].x,heap[x].x=heap[tail+1].x,heap[tail+1].x=t;
   heap[x].v=heap[tail+1].v;
   heap[tail+1].v=value;
   h[1][p][heap[x].x]=x;
   h[1][p][heap[tail+1].x]=tail+1;
   maintain(tail+1,p,tail+1);
}
Esempio n. 13
0
 void insert(node_ptr &R, T value) {
     if (R == nilptr) {
         R = new node<T>(value, 0, 1, nilptr, nilptr);
         return;
     } else if (value <= R->value) {
         insert(R->lchild, value);
     } else if (value > R->value) {
         insert(R->rchild, value);
     }
     fix(R);
     maintain(R);
 }
Esempio n. 14
0
 void maintain(int &root , bool flag)
 {
     if (root == 0) return ;
     if ( !flag )
     {
         if ( SZ[LC[LC[root]]] > SZ[RC[root]] )
         {
             RightRotate( root );
         }
         else if ( SZ[RC[LC[root]]] > SZ[RC[root]] )
         {
             LeftRotate( LC[root] );
             RightRotate( root );
         }
         else
         {
             return ;
         }
     }
     else
     {
         if ( SZ[RC[RC[root]]] > SZ[LC[root]] )
         {
             LeftRotate( root );
         }
         else if ( SZ[LC[RC[root]]] > SZ[LC[root]] )
         {
             RightRotate( RC[root] );
             LeftRotate( root );
         }
         else
         {
             return ;
         }
     }
     maintain(LC[root] , false);
     maintain(RC[root] , true);
     maintain(root , false);
     maintain(root , true);
 }
Esempio n. 15
0
void maintain(int x,int time,int tail)
{
   int unfair(int,int);
   int t,p,q;
   if (heap[x].father!=0 && heap[x].v>heap[heap[x].father].v)
   {
      t=heap[x].v,heap[x].v=heap[heap[x].father].v,heap[heap[x].father].v=t;
      t=heap[x].x,heap[x].x=heap[heap[x].father].x,heap[heap[x].father].x=t;
      h[1][time][heap[x].x]=x;
      h[1][time][heap[heap[x].father].x]=heap[x].father;
      maintain(heap[x].father,time,tail);
      return;
   }
   if (unfair(heap[x].left,tail))
   {
      p=heap[heap[x].left].v;
      if (unfair(heap[x].right,tail))
         q=heap[heap[x].right].v;
      else
         q=-10000000;
      if (heap[x].v>=p && heap[x].v>=q)
         return;
      if (p>q)
      {
         t=heap[x].v,heap[x].v=heap[heap[x].left].v,heap[heap[x].left].v=t;
         t=heap[x].x,heap[x].x=heap[heap[x].left].x,heap[heap[x].left].x=t;
         h[1][time][heap[x].x]=x;
         h[1][time][heap[heap[x].left].x]=heap[x].left;
         maintain(heap[x].left,time,tail);
      }
      else
      {
         t=heap[x].v,heap[x].v=heap[heap[x].right].v,heap[heap[x].right].v=t;
         t=heap[x].x,heap[x].x=heap[heap[x].right].x,heap[heap[x].right].x=t;
         h[1][time][heap[x].x]=x;
         h[1][time][heap[heap[x].right].x]=heap[x].right;
         maintain(heap[x].right,time,tail);
      }
   }
}
Esempio n. 16
0
	int playSoundAt(const std::string &name, bool loop, float volume, v3f pos)
	{
		maintain();
		if(name == "")
			return 0;
		SoundBuffer *buf = getFetchBuffer(name);
		if(!buf){
			infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
					<<std::endl;
			return -1;
		}
		return playSoundRawAt(buf, loop, volume, pos);
	}
Esempio n. 17
0
	void update(int x,int y,Mark a)
	{
		pushdown();
		if(x>=r || y<l) return;
		if(l>=x && r-1<=y) apply(a);
		else
		{
			int m=(l+r)>>1;
			pl->update(x,y,a);
			pr->update(x,y,a);
			maintain();
		}
	}
Esempio n. 18
0
	void Insert(int &p, int x)
	{
		if(!p)
		{
			p = ++total;
			L[p] = R[p] = 0;
			A[p] = x; S[p] = 1;
			return;
		}
		S[p]++;
		if(x < A[p]) Insert(L[p], x);
		else Insert(R[p], x);
		maintain(p, x >= A[p]);
	}
Esempio n. 19
0
void splay(int x)
{
    maintain(x);
    while(!rt[x])
    {
        int f=fa[x], ff=fa[f];
        if(rt[f])
            rotate(x);
        else if( (ch[ff][1]==f)==(ch[f][1]==x) )
            rotate(f),rotate(x);
        else
            rotate(x),rotate(x);
    }
}
Esempio n. 20
0
void del(int x,int tail,int p)
{
   void maintain(int,int,int);
   int t;
   if (heap[x].father==0 && x==tail)
   {
      heap[x].v=-1000000000;
      return;
   }
   t=heap[x].x,heap[x].x=heap[tail].x,heap[tail].x=t;
   t=heap[x].v,heap[x].v=heap[tail].v,heap[tail].v=t;
   h[1][p][heap[x].x]=x;
   h[1][p][heap[tail].x]=tail;
   maintain(x,p,tail-1);
}
Esempio n. 21
0
File: SBT.hpp Progetto: xyiyy/icpc
void insert(int &r, int k) {
    if (r == 0) {
        r = ++tot;
        sbt[r].init();
        sbt[r].key = k;
    }
    else {
        sbt[r].size++;
        if (k < sbt[r].key)
            insert(sbt[r].left, k);
        else
            insert(sbt[r].right, k);
        maintain(r, k >= sbt[r].key);
    }
}
Esempio n. 22
0
void work()
{
   void maintain(int);
   int i;
   h[0]=0;
   for (i=1;i<=n;i++)
   {
      if (!f[i])
         continue;
      h[0]++;
      h[h[0]]=a[i]->x;
      g[h[0]]=i;
      c[i]=h[0];
      maintain(h[0]);
   }
}
Esempio n. 23
0
void did()
{
   void build(int,int),work(),maintain(int),insert(int);
   int k,q,i,t,x,ans;
   scanf("%d%d%d",&n,&k,&q);
   memset(a,0,sizeof(a));
   for (i=1;i<=q;i++)
   {
      scanf("%d",&b[i]);
      build(b[i],i);
   }
   for (i=1;i<=n;i++)
      build(i,20000000);
   ans=0;
   memset(f,0,sizeof(f));
   i=t=0;
   while (t<k)
   {
      i++;
      if (i>q)
         break;
      if (!f[b[i]])
      {
         ans++;
         t++;
         f[b[i]]=1;
      }
      a[b[i]]=a[b[i]]->next;
   }
   work();
   t=i+1;
   for (i=t;i<=q;i++)
   {
      a[b[i]]=a[b[i]]->next;
      if (f[b[i]])
      {
         h[c[b[i]]]=a[b[i]]->x;
         maintain(c[b[i]]);
         continue;
      }
      ans++;
      insert(b[i]);
   }
   printf("%d\n",ans);
}
Esempio n. 24
0
int insert(int &t,int v)
{
	if (t==0)
	{
		t=free_node();
		SBT[t].init(v);
	}
	else
	{
		SBT[t].s++;
		if (v<SBT[t].key)
			insert(SBT[t].left,v);
		else 
			insert(SBT[t].right,v);
		maintain(t,v>=SBT[t].key);	
	}
	
	return t;
}
Esempio n. 25
0
void
Node::deleteN(int key)
{
    if (key > value && !rightChild)
        return;

    if (key < value && !leftChild)
        return;

 

    if (key > value)
        rightChild->deleteN(key);
    else if (key < value)
        leftChild->deleteN(key);
    else if (key == value) {
        if (rightChild) {
            value = rightChild->value;
            rightChild->deleteN(rightChild->value);
        }
        else if (leftChild) {
            value = leftChild->value;
            leftChild->deleteN(leftChild->value);
        } else {

            if (!parent) {
                std::cout<<"Cannot delete root without any other node.\n";
                return;
            }
            
            if (parent->leftChild == this)
                parent->leftChild = NULL;
            else if(parent)
                parent->rightChild = NULL;

            parent = NULL;
            delete(this);
            return;
        }
    }
    maintain();
}
Esempio n. 26
0
	int playSound(const std::string &name, bool loop, float volume, float fade, float pitch)
	{
		maintain();
		if (name.empty())
			return 0;
		SoundBuffer *buf = getFetchBuffer(name);
		if(!buf){
			infostream<<"OpenALSoundManager: \""<<name<<"\" not found."
					<<std::endl;
			return -1;
		}
		int handle = -1;
		if (fade > 0) {
			handle = playSoundRaw(buf, loop, 0.0f, pitch);
			fadeSound(handle, fade, volume);
		} else {
			handle = playSoundRaw(buf, loop, volume, pitch);
		}
		return handle;
	}
Esempio n. 27
0
 void Insert(int &root, Type key)
 {
     if (root == 0)
     {
         root = ++ sz;
         LC[root] = RC[root] = 0;
         SZ[root] = 1;
         K[root] = key;
         return ;
     }
     SZ[root] ++;
     if (key < K[root])
     {
         Insert(LC[root] , key);
     }
     else
     {
         Insert(RC[root] , key);
     }
     maintain(root , !(key < K[root]));
 }
Esempio n. 28
0
	bool soundExists(int sound)
	{
		maintain();
		return (m_sounds_playing.count(sound) != 0);
	}
Esempio n. 29
0
	void stopSound(int sound)
	{
		maintain();
		deleteSound(sound);
	}
Esempio n. 30
0
void maintain(int x)
{
    if(!rt[x]) maintain(fa[x]);
    push_down(x);
}