JSOI最大值 (线段树)

change 单点修改

query 区间最值

 1 Program XJOI2321;
 2 const maxn=200008;
 3 var l,r,max:array[0..maxn*4] of longint;
 4     i,m,n,ans,p,x:longint;
 5     ch:char;
 6 function mx(a,b:longint):longint;
 7 begin
 8     if a>b then exit(a) else exit(b);
 9 end;
10 procedure up(x:longint);
11 begin
12     max[x]:=mx(max[x*2],max[x*2+1]);
13 end;
14 procedure build(x,lt,rt:longint);
15 var mid:longint;
16 begin
17     l[x]:=lt;
18     r[x]:=rt;
19     if lt=rt then
20     begin
21         max[i]:=0;
22         exit;
23     end;
24     mid:=(lt+rt) div 2;
25     build(x*2,lt,mid);
26     build(x*2+1,mid+1,rt);
27     up(x);
28 end;
29 procedure change(x,G,val:longint);
30 var mid:longint;
31 begin
32     if l[x]=r[x] then
33     begin
34         max[x]:=val;
35         exit;
36     end;
37     mid:=(l[x]+r[x]) div 2;
38     if G<=mid then change(x*2,G,val) else change(x*2+1,G,val);
39     up(x);
40 end;
41 function query(x,lt,rt:longint):longint;
42 var mid,as:longint;
43 begin
44     if (lt<=l[x]) and (r[x]<=rt) then exit(max[x]);
45     mid:=(l[x]+r[x]) div 2;
46     as:=0;
47     if lt<=mid then as:=mx(as,query(x*2,lt,rt));
48     if mid<rt  then as:=mx(as,query(x*2+1,lt,rt));
49     exit(as);
50 end;
51 begin
52     readln(m,p);
53     ans:=0; n:=0;
54     build(1,1,m);
55     for i:=1 to m do
56     begin
57         readln(ch,x);
58         if ch=A then
59         begin
60             x:=(x+ans) mod p;
61             inc(n);
62             change(1,n,x);
63         end else
64         begin
65             ans:=query(1,n-x+1,n);
66             writeln(ans);
67         end;
68     
69     end;
70 
71 end.

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。