bzoj 1012 基础线段树

原题传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1012

今儿一天状态不好,都没怎么刷题。。快下课了,刷道水题。。。。

裸的线段树

/**************************************************************
    Problem: 1012
    User: BLADEVIL
    Language: Pascal
    Result: Accepted
    Time:1688 ms
    Memory:12920 kb
****************************************************************/
 
//By BLADEVIL
type
    rec                     =record
        left, right, max    :longint;
    end;
     
var
    query                   :array[0..200010] of longint;
    querys                  :array[0..200010] of char;
    m, d                    :longint;
    n                       :longint;
    t                       :array[0..1000010] of rec;
    tot                     :longint;   
    tt                      :longint;
     
function max(a,b:longint):longint;
begin
    if a>b then max:=a else max:=b;
end;
     
procedure build(x,l,r:longint);
var
    mid                     :longint;
begin
    t[x].left:=l; t[x].right:=r;
    if l=r then exit;
    mid:=(r+l) div 2;
    build(x*2,l,mid);
    build(x*2+1,mid+1,r);
end;
     
procedure init;
var
    i                       :longint;
begin
    readln(m,d);
    for i:=1 to m do
    begin
        read(querys[i]);
        readln(query[i]);
    end;
    for i:=1 to m do if querys[i]='A' then inc(n);
    build(1,1,n);
end;
 
procedure change(x,y,z:longint);
var
    mid                     :longint;
begin
    if (t[x].left=y) and (t[x].right=y) then
    begin
        t[x].max:=z;
        exit;
    end;
    with t[x] do mid:=(left+right) div 2;
    if y>mid then change(x*2+1,y,z) else change(x*2,y,z);
    t[x].max:=max(t[x*2].max,t[x*2+1].max);
end;
 
function ask(x,l,r:longint):longint;
var
    mid                     :longint;
begin
    if (t[x].left=l) and (t[x].right=r) then
    begin
        ask:=t[x].max;
        exit;
    end;
    with t[x] do mid:=(right+left) div 2;
    if l>mid then ask:=ask(x*2+1,l,r) else
    if r<=mid then ask:=ask(x*2,l,r) else
    ask:=max(ask(x*2,l,mid),ask(x*2+1,mid+1,r));
end;
 
procedure main;
var
    i                       :longint;
 
begin
    tot:=0;
    tt:=0;
    for i:=1 to m do
    begin
        if querys[i]='A' then
        begin
            inc(tot);
            change(1,tot,(query[i]+tt) mod d);
        end else
        begin
            tt:=ask(1,tot-query[i]+1,tot);
            writeln(tt);
        end;
    end;
end;
 
begin
    init;
    main;
end.

 

posted on 2013-11-21 21:10  BLADEVIL  阅读(170)  评论(0编辑  收藏  举报