當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Luogu P1198 [JSOI2008]最大数 线段树
生活随笔
收集整理的這篇文章主要介紹了
Luogu P1198 [JSOI2008]最大数 线段树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
P1198 [JSOI2008]最大數
題目描述
現在請求你維護一個數列,要求提供以下兩種操作:
1、 查詢操作。
語法:Q L
功能:查詢當前數列中末尾L個數中的最大的數,并輸出這個數的值。
限制:L不超過當前數列的長度。
2、 插入操作。
語法:A n
功能:將n加上t,其中t是最近一次查詢操作的答案(如果還未執行過查詢操作,則t=0),并將所得結果對一個固定的常數D取模,將所得答案插入到數列的末尾。
限制:n是整數(可能為負數)并且在長整范圍內。
注意:初始時數列是空的,沒有一個數。
輸入輸出格式
輸入格式:第一行兩個整數,M和D,其中M表示操作的個數(M <= 200,000),D如上文中所述,滿足(0<D<2,000,000,000)
接下來的M行,每行一個字符串,描述一個具體的操作。語法如上文所述。
輸出格式:對于每一個查詢操作,你應該按照順序依次輸出結果,每個結果占一行。
輸入輸出樣例
輸入樣例#1:5 100 A 96 Q 1 A 97 Q 1 Q 2 輸出樣例#1:
96 93 96
說明
[JSOI2008]
思路
線段樹小試牛刀!qwq
先建一顆空的線段樹,然后每次把值插入,用num統計個數
然后每次查詢num-n+1到num這個區間的最大值輸出就是啦
代碼
1 program maxnumber; 2 const 3 inf='maxnumber.in'; 4 outf='maxnumber.out'; 5 type 6 tree=^node; 7 node=record 8 lc,rc:tree; 9 l,r,val:longint; 10 end; 11 12 var 13 i,m,n,d,ctot,num,l:longint; 14 ch:char; 15 t:tree; 16 s,e,a:array[1..200000] of longint; 17 18 function max(aa,bb:longint):longint; 19 begin 20 if aa>bb then exit(aa) 21 else exit(bb); 22 end; 23 24 function min(aa,bb:longint):longint; 25 begin 26 if aa>bb then exit(bb) 27 else exit(aa); 28 end; 29 30 procedure add(apple:longint); 31 begin 32 inc(num); 33 a[num]:=apple mod d; 34 end; 35 36 procedure add(var t:tree; seat:longint; data:longint); 37 begin 38 if t^.l=t^.r then begin 39 t^.val:=max(t^.val,data); 40 exit; 41 end; 42 if seat<=t^.lc^.r then add(t^.lc,seat,data) 43 else add(t^.rc,seat,data); 44 t^.val:=max(t^.val,data); 45 end; 46 47 procedure build(var t:tree; l,r:longint); 48 var 49 mid:longint; 50 begin 51 new(t); 52 t^.l:=l; t^.r:=r; 53 if l=r then begin 54 t^.val:=a[l]; 55 exit; 56 end; 57 mid:=(l+r) div 2; 58 build(t^.lc,l,mid); 59 build(t^.rc,mid+1,r); 60 t^.val:=max(t^.lc^.val,t^.rc^.val); 61 end; 62 63 function query(var t:tree; l,r:longint):longint; 64 var 65 mm:longint; 66 begin 67 if (t^.l=t^.r) or ((t^.l>=l) and (t^.r<=r)) then exit(t^.val); 68 mm:=-maxlongint; 69 if l<=t^.lc^.r then mm:=query(t^.lc,l,min(t^.lc^.r,r)); 70 if r>=t^.rc^.l then mm:=max(mm,query(t^.rc,max(t^.rc^.l,l),r)); 71 exit(mm); 72 end; 73 74 begin 75 // assign(input,inf); assign(output,outf); 76 reset(input); rewrite(output); 77 78 readln(m,d); 79 build(t,1,m); 80 l:=0; 81 for i:= 1 to m do 82 begin 83 readln(ch,n); 84 if ch='A' then begin num:=num+1; add(t,num,(n+l) mod d); end 85 else begin 86 l:=query(t,num-n+1,num); 87 writeln(l); 88 end; 89 end; 90 91 close(input); 92 close(output); 93 end.?
?
轉載于:https://www.cnblogs.com/bobble/p/6596186.html
總結
以上是生活随笔為你收集整理的Luogu P1198 [JSOI2008]最大数 线段树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android学习笔记36:使用SQLi
- 下一篇: UML实践---用例图、顺序图、状态图、