切换到宽版
  • 5157阅读
  • 4回复

新手问题,关于线段树! [复制链接]

上一主题 下一主题
离线mjs_oi
 
只看楼主 倒序阅读 0 发表于: 2007-04-18
能不能用线段树写个 校门外的树 给我, 或者 VIJOS的1056 离散化+线段树怎么写(希望有具体程序)
离线fdhyctctc
只看该作者 1 发表于: 2007-04-20
校门外的树
type
treedata=record
  s,t,l,r:longint;
  bj:boolean;
end;
var
tree:array[1..200001]of treedata;
l,m,i,j,k,tot,x,y,ans:longint;
procedure DLR(i:longint);
begin
  if tree[i].bj then
  begin inc(ans,tree[i].t-tree[i].s+1); exit end;
  if tree[i].l<>0 then DLR(tree[i].l);
  if tree[i].r<>0 then DLR(tree[i].r);
end;
procedure Insert(i:longint);
begin
  if (tree[i].s>=x) and (tree[i].t<=y) then
  tree[i].bj:=true else
    begin
    if (x<=(tree[i].s+tree[i].t)div 2)and(tree[i].l<>0)
      then Insert(tree[i].l);
    if (y>=(tree[i].s+tree[i].t)div 2)and(tree[i].r<>0)
      then Insert(tree[i].r);
    end;
end;
procedure MakeTree(a,b:longint);
var k,m:longint;
begin
  m:=tot;
  k:=(a+b)div 2;
  tree[tot].s:=a;
  tree[tot].t:=b;
  tree[tot].bj:=false;
  if b-a>=1 then
  begin
    inc(tot); tree[m].l:=tot; maketree(a,k);
    inc(tot); tree[m].r:=tot; maketree(k+1,b);
  end;
end;
begin
assign(input,'tree.in');
reset(input);
assign(output,'tree.out');
rewrite(output);
readln(l,m);
tot:=1;
Maketree(0,l);
for i:=1 to m do
  begin
  readln(x,y);
  Insert(1);
  end;
ans:=0;
DLR(1);
writeln(l+1-ans);
close(input);
close(output);
end.
离线mjs_oi
只看该作者 2 发表于: 2007-04-21
能不能加点注释,记录数组我没学过
离线mjs_oi
只看该作者 3 发表于: 2007-04-21
这程序编译不通过
离线fdhyctctc
只看该作者 4 发表于: 2007-04-30
汗LS...您用的是TP????
我数组开这么大,TP是过不了的...
记录没学过...学什么线段树...
快速回复
限100 字节
 
上一个 下一个