luogu P1198 [JSOI2008]最大數

來源:互聯網
上載者:User

標籤:查詢   space   應該   請求   結果   最大數   div   限制   std   

題目描述

現在請求你維護一個數列,要求提供以下兩種操作:

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 100A 96Q 1A 97Q 1Q 2
輸出範例#1:
969396
說明

[JSOI2008]

 線段樹維護

#include<cstdio>#include<iostream>using namespace std;#define N 200010 int n,d,t; int sum[N<<2];char a[10];int len=0,x;int to=0;void modify(int l,int r,int to,int rt,int c){    if(l==r)    {        sum[rt]=c;        return;    }    int mid=(l+r)>>1;    if(to<=mid)modify(l,mid,to,rt<<1,c);    else modify(mid+1,r,to,rt<<1|1,c);    sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);}int query(int l,int r,int x,int y,int rt){    if(l==x&&r==y)        return sum[rt];    int mid=(l+r)>>1;    if(y<=mid) return query(l,mid,x,y,rt<<1);    if(x>mid) return query(mid+1,r,x,y,rt<<1|1);    int left =query(l,mid,x,mid,rt<<1);      int right=query(mid+1,r,mid+1,y,rt<<1|1);      return max(left,right);}int main(){    scanf("%d%d",&n,&d);    while(n--)    {        scanf("%s",a);        if(a[0]==‘Q‘)        {            scanf("%d",&x);            t=query(1,N-1,len-x+1,len,1);            printf("%d\n",t);        }        else if(a[0]==‘A‘)        {            len++;            scanf("%d",&x);            modify(1,N-1,len,1,(x+t)%d);        }    }    return 0;}

 

luogu P1198 [JSOI2008]最大數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.