JSOI2008最大數(線段樹)

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

注意到數列只增不減,而題目中又明確說道m<=200000;這樣的資料規模線段樹完全可以承受得了。所以我們可以事先建好一棵200000個子節點的線段樹,然後求極值就好了。

type node=record l,r,mx:longint;end;var i,j,m,p,x,tmp,tot:longint;    ch:char;    t:array[0..1500000] of node;    function max(x,y:longint):longint;     begin     if x>y then exit(x) else exit(y);     end;procedure build(x,y,k:longint); var mid:longint; begin with t[k] do  begin   l:=x;r:=y;   if l=r then exit;   mid:=(l+r)>>1;   build(x,mid,k<<1);   build(mid+1,r,k<<1+1);  end; end;procedure change(x,y,k:longint); var mid:longint; begin with t[k] do  begin   if l=r then    begin    mx:=y;    exit;    end;   mid:=(l+r)>>1;   if x>mid then change(x,y,k<<1+1)   else change(x,y,k<<1);  mx:=max(t[k<<1].mx,t[k<<1+1].mx);  end; end;function getmx(x,y,k:longint):longint; var mid:longint; begin with t[k] do  begin  if (l=x) and (r=y) then exit(mx);  mid:=(l+r)>>1;  if x>mid then exit(getmx(x,y,k<<1+1))  else if y<=mid then exit(getmx(x,y,k<<1))  else exit(max(getmx(x,mid,k<<1),getmx(mid+1,y,k<<1+1)));  end; end;procedure main; begin build(1,200000,1); readln(m,p);tmp:=0;tot:=0; for i:=1 to m do  begin   read(ch);   if ch=‘A‘ then    begin    readln(x);    inc(tot);    change(tot,(x+tmp) mod p,1);    end   else    begin    readln(x);    tmp:=getmx(tot-x+1,tot,1);    writeln(tmp);    end;  end; end;begin main;end.                 

不過話說起來,1A的感覺真棒!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.