tyvj 1039忠诚2——初识线段树

搜狗截图_2011-10-23_07-46-46

 

分析:显然是一个线段树,不解释

至于具体的实现过程。。。稍说明一下

线段树的原理,就我个人理解,

是属于分治的。

对本题来说,

你把整个线段分成若干小段,

再分下去,直到分成点树为止。

然后,利用递归求最小值,

改变值的时候也使用递归即可。

由于树的结构原因,

时间复杂度降为nlogn的,需要的空间稍大些。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
program ty_1039;
  var
    i,n,m,k,ans,tot,bb,ww,cc,x,y:longint;
    tree:array[0..200100]of
      record
        a,b,le,ri,da:longint;
      end;
    w,e:array[1..100000]of longint;
 
  function min(x,y:longint):Longint;
    begin
      if x<y then exit(x);
      exit(y);
    end;
 
  procedure maketree(l,r:longint);
    var
      now,mid:longint;
    begin
      tot:=tot+1;
      now:=tot;
      tree[now].a:=l;
      tree[now].b:=r;
      if l=r then begin tree[now].da:=w[l];exit;end;
      mid:=(l+r)>>1;
      tree[now].le:=tot+1;
      maketree(l,mid);
      tree[now].ri:=tot+1;
      maketree(mid+1,r);
      tree[now].da:=min(tree[tree[now].le].da,tree[tree[now].ri].da);
    end;
 
  function find(loc,x,y:longint):longint;
    var
      mid,i,j:longint;
    begin
      if (tree[loc].a=x)and(tree[loc].b=y) then exit(tree[loc].da);
      mid:=(tree[loc].a+tree[loc].b)>>1;
      if y<=mid then exit(find(tree[loc].le,x,y))
        else if x>mid then exit(find(tree[loc].ri,x,y))
          else begin
            i:=find(tree[loc].le,x,mid);
            j:=find(tree[loc].ri,mid+1,y);
            exit(min(i,j));
          end;
    end;
 
  procedure change(loc,x,y:longint);
    var
      mid:longint;
    begin
      if (tree[loc].a=x)and(x=tree[loc].b) then begin tree[loc].da:=y;exit;end;
      mid:=(tree[loc].a+tree[loc].b)>>1;
      if x<=mid then change(tree[loc].le,x,y)
        else if x>mid then change(tree[loc].ri,x,y);
      tree[loc].da:=min(tree[tree[loc].le].da,tree[tree[loc].ri].da);
    end;
 
  begin
    assign(input,'ty.in');
    reset(input);
    assign(output,'ty.out');
    rewrite(output);
    readln(m,n);
    for i:=1 to m do read(w[i]);
    maketree(1,m);
    for i:=1 to n do
      begin
        readln(bb,ww,cc);
        if bb=1 then write(find(1,ww,cc),' ')
                else change(1,ww,cc);
      end;
    close(input);
    close(output);
  end.

posted on   codeway3  阅读(297)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?

导航

< 2011年10月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示