BZOJ 1012 [JSOI2008]最大數maxnumber【線段樹】

來源:互聯網
上載者:User

標籤:io   os   ar   for   sp   c   amp   r   bs   

水題,每次記錄一下當前有多少個數,然後按照題目所指示的那樣類比就行,每次向線段樹末尾插入(其實是修改)題目中指定的數,然後詢問當前的個數到前面Q個數中最大值是多少結果就是,好久不碰線段樹了,用數組類比線段樹時數組要開大4倍,一開始沒發現這點貢獻了好多runtime_error TAT

 

 

#include<iostream>

#include<cstdio>

#include <math.h>

using namespace std;

int d;

int max(int a,int b)

{

    if(a>=b)return a;else return b;

}

void insert(int node,int l,int r,int po,intvalue,int m[])

{

    int mid=(l+r)>>1;

    if (l+1==r){m[node]=value;return;}

          else

     {

         if (po<=mid)insert(node*2,l,mid,po,value,m);

                     else insert(node*2+1,mid,r,po,value,m);

     }

          m[node]=max(m[node*2],m[node*2+1]);

 

}

int search(int node,int l,int r,int lq,intrq,int m[])

{

         int mid = (l+r)>>1,temp=0;

         if ((lq<=l)&&(r<=rq))return m[node];

         if(lq<mid)temp=max(temp,search(node*2,l,mid,lq,rq,m));

    if(rq>mid)temp=max(temp,search(node*2+1,mid,r,lq,rq,m));

         returntemp;

}

int main()

{

   int m,t,n[800001]={0},len=0,ans=0,ans_array[200001]={0},h=0;

   char ch[5];

   scanf("%d%d",&m,&d);

    for (inti=1;i<=m;i++)

    {

       scanf("%s %d",ch,&t);

       if (ch[0]==‘A‘)

       {

             len++;

             insert(1,0,m,len,(t+ans)%d,n);

       }

       

       if (ch[0]==‘Q‘)

       {

             ans=search(1,0,m,len-t,len,n);

             h++;

             ans_array[h]=ans;

       }

 

    }

   for (int i=1;i<=h;i++)printf("%d\n",ans_array[i]);

   return 0;

}

BZOJ 1012 [JSOI2008]最大數maxnumber【線段樹】

聯繫我們

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