當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
【JSOI2008】最大数 线段树
生活随笔
收集整理的這篇文章主要介紹了
【JSOI2008】最大数 线段树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
現在請求你維護一個數列,要求提供以下兩種操作:
1.查詢操作。
語法:Q L
功能:查詢當前數列中末尾L個數中的最大的數,并輸出這個數的值。
限制:L不超過當前數列的長度。
2.插入操作。
語法:A n
功能:將n加上t,其中t是最近一次查詢操作的答案(如果還未執行過查詢操作,則t=0),并將所得結果對一個固定的常數D取模,將所得答案插入到數列的末尾。
限制:n是非負整數并且在長整范圍內。
注意:初始時數列是空的,沒有一個數。
題目大意
單點插入,區間求最大值。
數據范圍
(M <= 200,000)(0 < D < 2,000,000,000)
樣例輸入
5 100
A 96
Q 1
A 97
Q 1
Q 2
樣例輸出
96
93
96
解題思路
無腦線段樹
代碼
#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> using namespace std; inline char Getch(){char ch=getchar();while(ch!='A'&&ch!='Q')ch=getchar();return ch; } inline int Getint(){int x=0,f=1;char ch=getchar();while('0'>ch||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while('0'<=ch&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;} struct node{int L,r,Max; }Tree[800005]; void Build(int v,int L,int r){Tree[v]=(node){L,r,0};if(L==r)return;Build(2*v,L,(L+r)/2);Build(2*v+1,(L+r)/2+1,r); } void Modify(int v,int pos,int vl){if(pos<Tree[v].L||Tree[v].r<pos)return;if(Tree[v].L==Tree[v].r){Tree[v].Max=vl;return;}Modify(2*v,pos,vl);Modify(2*v+1,pos,vl);Tree[v].Max=max(Tree[2*v].Max,Tree[2*v+1].Max); } int Ask(int v,int L,int r){if(r<Tree[v].L||Tree[v].r<L)return 0;if(L<=Tree[v].L&&Tree[v].r<=r)return Tree[v].Max;return max(Ask(2*v,L,r),Ask(2*v+1,L,r)); } int main(){int n=Getint(),MOD=Getint(),LastAns=0,ed=0;Build(1,1,n);while(n--){char ch=Getch();if(ch=='A')Modify(1,++ed,(LastAns+Getint())%MOD);else cout<<(LastAns=Ask(1,ed-Getint()+1,ed))<<"\n";}return 0; }轉載于:https://www.cnblogs.com/Cedric341561/p/6811021.html
總結
以上是生活随笔為你收集整理的【JSOI2008】最大数 线段树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fragment error
- 下一篇: Java其他API介绍