2018.6.10数据结构串讲_HugeGun

时间:2021-04-08 05:35:49

链接: https://pan.baidu.com/s/1uQwLZAT8gjENDWLDm7-Oig 密码: mk8p

@echo off
: )
shuju
test
test_
fc test.out test.ans
if errorlevel == 1 pause
goto )

对拍

#include<stdio.h>
int a[],Next[],pre[],head,last;
void add(int x,int y)//在标号为x的数后面插入标号为y的数
{
int nex=Next[x];
Next[x]=y,pre[y]=x;
pre[nex]=y,Next[y]=nex;
if(x==last)last=y;
}
void del(int x)//在链表中删掉标号为x的数
{
int prex=pre[x],nex=Next[x];
Next[prex]=nex,pre[nex]=prex;
pre[x]=Next[x]=;
}
int main()
{
return ;
}

链表

#include<stdio.h>
int head[],Next[],poi[],w[],ed;
void add(int a,int b,int c,int d)//在a,b之间加一条权值为c的边,d==1是为无向边,d==0是为有向边
{
Next[++ed]=head[a],head[a]=ed,poi[ed]=b,w[ed]=c;
if(d)Next[++ed]=head[b],head[b]=ed,poi[ed]=a,w[ed]=c;
}
void dfs(int x,int f)//遍历一棵树,f是x的父亲
{
int i;
for(i=head[x];i;i=Next[i])if(poi[i]!=f)
dfs(poi[i],x);
}
int main()
{
return ;
}

邻接表

 #include<stdio.h>
int ch[][],w[],ed,root;
void add(int *k,int x)
{
if(!*k)
{
*k=++ed,w[*k]=x;
return ;
}
int d=x>w[*k];
add(&ch[*k][d],x);
}
int main()
{
return ;
}

BST

 #include<stdio.h>
#include<stdlib.h>
int ch[][],w[],c[],s[],r[],ed,root;
void maintain(int x){if(x)s[x]=c[x]+s[ch[x][]]+s[ch[x][]];}
void rotate(int *k,int d)
{
int p=ch[*k][d^];
ch[*k][d^]=ch[p][d];
ch[p][d]=*k;
maintain(*k);
maintain(p);*k=p;
}
void add(int *k,int x)
{
if(!*k)
{
*k=++ed;w[*k]=x,s[*k]=c[*k]=,ch[*k][]=ch[*k][]=,r[*k]=rand();
return ;
}
s[*k]++;
if(w[*k]==x){c[*k]++;return ;}
int d=x>w[*k];
add(&ch[*k][d],x);
if(r[ch[*k][d]]<r[*k])rotate(k,d^);
}
int del(int *k,int x)
{
if(!*k)return ;
if(w[*k]==x)
{
if(c[*k]>){s[*k]--;c[*k]--;return ;}
if(!ch[*k][]){*k=ch[*k][];return ;}
if(!ch[*k][]){*k=ch[*k][];return ;}
if(r[ch[*k][]]<r[ch[*k][]])rotate(k,);
else rotate(k,);
return del(k,x);
}
int d=x>w[*k];
if(del(&ch[*k][d],x)){s[*k]--;return ;}
return ;
}
int findrank(int x)
{
int k=root,ret=;
while(k)
{
int pp=s[ch[k][]];
if(x==w[k])return ret+pp;
else if(x<w[k])k=ch[k][];
else ret+=(pp+c[k]),k=ch[k][];
}
return ret;
}
int findwei(int x)
{
int k=root;
while(k)
{
int pp=s[ch[k][]];
if(x<=pp)k=ch[k][];
else if(x>pp+c[k])x-=pp+c[k],k=ch[k][];
else return w[k];
}
return ;
}
int findqian(int x)
{
int k=root,ret=;
while(k)
{
if(w[k]<x)ret=w[k],k=ch[k][];
else k=ch[k][];
}
return ret;
}
int findhou(int x)
{
int k=root,ret=;
while(k)
{
if(w[k]>x)ret=w[k],k=ch[k][];
else k=ch[k][];
}
return ret;
}
int main()
{
return ;
}

treap