树状数组
procedure plus(x,delta:longint);
var k:longint;
begin
k:=x;
while k<=n do
begin
inc(c[k],delta);
inc(k,k and (-k));
end;
end;
function getsum(x:longint):longint;
var k,ans:longint;
begin
k:=x; ans:=0;
while k>0 do
begin
inc(ans,c[k]);
dec(k,k and (-k));
end;
exit(ans);
end;
堆的操作
procedure sink(i,n:longint);
var i,key:longint;
begin
key:=a[i];
while i*2<=n do
begin
j:=i*2;
if (j+1<=n)and(a[j+1]<a[j]) then inc(j);
if a[j]<key then
begin
a[i]:=a[j];
i:=j;
end
else
break;
end;
a[i]:=key;
end;
procedure float(i:longint);
var key:longint;
begin
key:=a[i];
while (i<>1) and (a[i]<a[i shr 1]) do
begin
a[i]:=a[i shr 1];
i:=i shr 1;
end;
a[i]:=key;
end;
procedure insert(key:longint; var n:longint);
begin
inc(n);
a[n]:=key;
float(n);
end;
procedure delete(i:longint; var n:longint);
begin
a[i]:=a[n];
dec(n);
sink(i,n);
end;
procedure heapsort(n:longint);
var i,tmp:longint;
begin
for i:=n shr 1 downto 1 do
sink(i,n);
for i:=n downto 2 do
begin
tmp:=a[1];
a[1]:=a[i];
a[i]:=tmp;
sink(1,i-1);
end;
end;
RMQ线段树
procedure build(i,left,right:longint);
var mid:longint;
begin
a[i].l:=left;
a[i].r:=right;
a[i].mark:=0;
if left=right then a[i].key:=data[left]
else
begin
mid:=(left+right) shr 1;
build(i shl 1,left,mid);
build(i shl 1+1,mid+1,right);
a[i].key:=max(a[i shl 1].key,a[i shl 1+1].key);
end;
end;
procedure pass(i:longint);
begin
if a[i].l<>a[i].r then
begin
inc(a[i shl 1].key,a[i].mark);
inc(a[i shl 1].mark,a[i].mark);
inc(a[i shl 1+1].key,a[i].mark);
inc(a[i shl 1+1].mark,a[i].mark);
end;
a[i].mark:=0;
end;
procedure add(i,st,en,va:longint);
var mid:longint;
begin
if (st=a[i].l) and (en=a[i].r) then
begin
inc(a[i].key,va);
inc(a[i].mark,va);
end
else
begin
pass(i);
mid:=(a[i].l+a[i].r) shr 1;
if mid>=en then add(i shl 1,st,en,va)
else if mid+1<=st then add(i shl 1+1,st,en,va)
else
begin
add(i shl 1,st,mid,va);
add(i shl 1+1,mid+1,en,va);
end;
a[i].key:=max(a[i shl 1].key,a[i shl 1+1].key);
end;
end;
function query(i,st,en:longint):longint;
var mid,ma,mb:longint;
begin
pass(i);
if (st=a[i].l) and (en=a[i].r) then exit(a[i].key);
mid:=(a[i].l+a[i].r) shr 1;
if mid>=en then exit(query(i shl 1,st,en))
else if mid+1<=st then exit(query(i shl 1+1,st,en))
else
begin
ma:=query(i shl 1,st,mid);
mb:=query(i shl 1+1,mid+1,en);
exit(max(ma,mb));
end;
end;
TREAP平衡树
procedure leftrotate(var x:longint);
var y:longint;
begin
y:=a[x].r;
a[x].r:=a[y].l;
a[y].l:=x;
a[y].size:=a[x].size;
a[x].size:=a[a[x].l].size+a[a[x].r].size+1;
x:=y;
end;
procedure rightrotate(var x:longint);
var y:longint;
begin
y:=a[x].l;
a[x].l:=a[y].r;
a[y].r:=x;
a[y].size:=a[x].size;
a[x].size:=a[a[x].l].size+a[a[x].r].size+1;
x:=y;
end;
function rank(head,k,sum:longint):longint;
begin
if sum+a[a[head].l].size+1>k then
exit(rank(a[head].l,k,sum))
else if sum+a[a[head].l].size+1=k then
exit(a[head].key)
else
exit(rank(a[head].r,k,sum+a[a[head].l].size+1));
end;
procedure insert(var head,aim:longint);
begin
if head=0 then
begin
inc(tot);
head:=tot;
a[tot].l:=0;
a[tot].r:=0;
a[tot].key:=aim;
a[tot].num:=random(maxlongint);
a[tot].size:=1;
end
else
begin
inc(a[head].size);
if aim<a[head].key then
begin
insert(a[head].l,aim);
if a[a[head].l].num>a[head].num then
rightrotate(head);
end
else
begin
insert(a[head].r,aim);
if a[a[head].r].num>a[head].num then
leftrotate(head);
end;
end;
end;
function delete(head,aim:longint):longint;
begin
dec(a[head].size);
if aim<a[head].key then a[head].l:=delete(a[head].l,aim)
else if aim>a[head].key then a[head].r:=delete(a[head].r,aim)
else
begin
if a[head].size=0 then exit(0);
if a[a[head].l].num>a[a[head].r].num then
begin
rightrotate(head);
a[head].r:=delete(a[head].r,aim);
end
else
begin
leftrotate(head);
a[head].l:=delete(a[head].l,aim);
end;
end;
exit(head);
end;
随机化快速排序
procedure qsort(s,t:longint);
var l,r,key:longint;
begin
l:=s; r:=t; key:=a[random(t-s+1)+s];
while l<=r do
begin
while a[l]<key do inc(l);
while a[r]>key do dec(r);
if l<=r then
begin
swap(a[l],a[r]);
inc(l); dec(r);
end;
end;
if s<r then qsort(s,r);
if l<t then qsort(l,t);
end;
堆排序
详见《基础代码之数据结构》
拓扑排序
procedure toposort;
var i,top,find,tmp:longint;
begin
top:=0;
for i:=1 to n do
if into[i]=0 then
begin
into[i]:=top;
top:=i;
end;
for find:=1 to n do
begin
sol[find]:=top;
tmp:=top;
for i:=1 to n do
if g[tmp,i] then
begin
dec(into[i]);
if into[i]=0 then
begin
into[i]:=top;
top:=i;
end;
end;
end;
end;
二路归并排序
procedure msort(s,t:longint);
var
i,p,q,mid:longint;
temp:array[1..100] of longint;
begin
if s=t then
exit;
mid:=(s+t) shr 1;
msort(s,mid);
msort(mid+1,t);
i:=1; p:=s; q:=mid+1;
while i<=t-s+1 do
begin
if (q=t+1) or ((a[p]<a[q]) and (p<=mid)) then
begin
temp[i]:=a[p];
inc(p);
end
else
begin
temp[i]:=a[q];
inc(q);
inc(ans,mid-p+1);
end;
inc(i);
end;
for i:=s to t do
a[i]:=temp[i-s+1];
end;
后 记
本人倒也不是什么强人。这些都是我仅会的一点点皮毛。我不指望这些能帮上大家什么忙,因为这些不过是基础中的基础。不过我相信对于那些拿一等比较危险的同学们或者以后的学弟学妹们来说这应该还是有一定帮助的。临阵磨枪,不快也光。在饱尝了各色山珍海味之后,欢迎大家也来尝尝我这道小菜。
在我这一年同各位YCOIer一同奋斗的时光里,我收获的不仅仅是一些这样那样的算法。我感受到的是一种无与伦比的团队精神,一种令人感慨的拼搏进取,一种爱与前进的绚丽。YCOIer代表的不单纯是键盘上飞舞的手指,屏幕上纵横的代码,而是所有NEYC的经历着所固有的不懈与追求。
距离NOIP day2仅剩下一周的时间了。如果说OI是一种缘分,那就在周末用高昂的凯歌让这份缘分延续…
While true do
Begin
Inc(the_rp_of _all_ycoier);
Inc(the_longing_fight);
Inc(the_glory_of _ycoier);
End;