线段树初试 (zkw式)

以前对线段树的理解真是太肤浅了,看了zkw大牛的ppt发现线段树真是优美。。。

 

 

求最后几个数的最大值代码(看起来像树状数组,但没lowbit)。const inf='maxnumber.in'; ouf='maxnumber.out'; var a:array[1..5000000]of longint; n,m,p,m1,s:longint; ans:int64; procedure init; var i,l,r,j:longint; y:int64; x:char; begin assign(input,inf);reset(input); assign(output,ouf);rewrite(output); readln(m,p); fillchar(a,sizeof(a),0); m1:=1; while m1<m do m1:=m1*2; m1:=m1*2-1; ans:=0;s:=0; for j:=1 to m do begin readln(x,y); if x='A' then begin y:=(ans+y)mod p; inc(s);a[s+m1]:=y; i:=(s+m1)>>1; while i<>0 do begin if y>a[i] then a[i]:=y; i:=i>>1 end end else if x='Q' then begin ans:=-maxlongint; l:=s-y;r:=s+1; l:=l+m1;r:=r+m1; while not(l xor r=1) do begin if l and 1=0 then if a[l+1]>ans then ans:=a[l+1]; if r and 1=1 then if a[r-1]>ans then ans:=a[r-1]; l:=l>>1;r:=r>>1 end; writeln(ans) end end; close(input); close(output) end; begin init; end.  

你可能感兴趣的:(input,output)