pku3264 Balanced Lineup

求区间最值之差,需要Qlogn的算法,可以用线段树或者ST。

线段树1750ms

View Code
 1 program pku3264_line_tree(input,output);
 2 type
 3    node    = record
 4          left,right,x,y,minx,maxx : longint;
 5       end;
 6 var
 7    tree    : array[0..500000] of node;
 8    n,q    : longint;
 9    a    : array[0..200000] of longint;
10    tot    : longint;
11 function min(aa,bb :longint ):longint;
12 begin
13    if aa<bb then
14       exit(aa);
15    exit(bb);
16 end; { min }
17 function max(aa,bb :longint ):longint;
18 begin
19    if aa>bb then
20       exit(aa);
21    exit(bb);
22 end; { max }
23 procedure build(xx,yy: longint );
24 var
25    mid,now : longint;
26 begin
27    inc(tot);
28    tree[tot].x:=xx;
29    tree[tot].y:=yy;
30    if xx=yy then
31    begin
32       tree[tot].minx:=a[xx];
33       tree[tot].maxx:=a[xx];
34       exit;
35    end;
36    now:=tot;
37    mid:=(xx+yy)>>1;
38    tree[now].left:=tot+1;
39    build(xx,mid);
40    tree[now].right:=tot+1;
41    build(mid+1,yy);
42    tree[now].minx:=min(tree[tree[now].left].minx,tree[tree[now].right].minx);
43    tree[now].maxx:=max(tree[tree[now].left].maxx,tree[tree[now].right].maxx);
44 end; { build }
45 procedure init;
46 var
47    i : longint;
48 begin
49    readln(n,q);
50    for i:=1 to n do
51       readln(a[i]);
52    build(1,n);
53 end; { init }
54 function getmax(now,xx,yy :longint ):longint;
55 var
56    mid : longint;
57 begin
58    if (tree[now].x=xx)and(tree[now].y=yy) then
59       exit(tree[now].maxx);
60    mid:=(tree[now].x+tree[now].y)>>1;
61    if yy<=mid then
62       exit(getmax(tree[now].left,xx,yy))
63    else
64       if xx>mid then
65      exit(getmax(tree[now].right,xx,yy))
66       else
67      exit(max(getmax(tree[now].left,xx,mid),getmax(tree[now].right,mid+1,yy)));
68 end; { getmax }
69 function getmin(now,xx,yy :longint ):longint;
70 var
71    mid : longint;
72 begin
73    if (tree[now].x=xx)and(tree[now].y=yy) then
74       exit(tree[now].minx);
75    mid:=(tree[now].x+tree[now].y)>>1;
76    if yy<=mid then
77       exit(getmin(tree[now].left,xx,yy))
78    else
79       if xx>mid then
80      exit(getmin(tree[now].right,xx,yy))
81       else
82      exit(min(getmin(tree[now].left,xx,mid),getmin(tree[now].right,mid+1,yy)));
83 end; { getmin }
84 procedure solve();
85 var
86    i,xx,yy : longint;
87 begin
88    for i:=1 to q do
89    begin
90       readln(xx,yy);
91       writeln(getmax(1,xx,yy)-getmin(1,xx,yy));
92    end;
93 end; { solve }
94 begin
95    init;
96    solve;
97 end.

ST算法1204ms

View Code
 1 program pku3264(input,output);
 2 var
 3    f,g : array[0..200001,0..18] of longint;
 4    n,q : longint;
 5 procedure init;
 6 var
 7    i : longint;
 8 begin
 9    readln(n,q);
10    for i:=1 to n do
11    begin
12       readln(f[i,0]);
13       g[i,0]:=f[i,0];
14    end;
15 end; { init }
16 function min(aa,bb :longint ):longint;
17 begin
18    if aa<bb then
19       exit(aa);
20    exit(bb);
21 end; { min }
22 function max(aa,bb :longint ):longint;
23 begin
24    if aa>bb then
25       exit(aa);
26    exit(bb);
27 end; { max }
28 procedure main;
29 var
30    i,j : longint;
31 begin
32    for j:=1 to trunc(ln(n)/ln(2)) do
33       for i:=1 to n do
34      if i+1<<(j-1)>n then
35         break
36      else
37      begin
38         f[i,j]:=min(f[i,j-1],f[i+1<<(j-1),j-1]);
39         g[i,j]:=max(g[i,j-1],g[i+1<<(j-1),j-1]);
40      end;
41 end; { main }
42 procedure solve();
43 var
44    i,x,y,t : longint;
45 begin
46    for i:=1 to q do
47    begin
48       readln(x,y);
49       t:=trunc(ln(y-x+1)/ln(2));
50       writeln(max(g[x,t],g[y-1<<t+1,t])-min(f[x,t],f[y-1<<t+1,t]));
51    end;
52 end; { solve }
53 begin
54    init;
55    main;
56    solve;
57 end.

还是ST简洁啊,DP就是NB!

posted @ 2012-04-14 09:56  Codinginging  阅读(181)  评论(0编辑  收藏  举报