AmazingCounters.com

BZOJ 1012 [JSOI2008]最大数maxnumber【线段树】

水题,每次记录一下当前有多少个数,然后按照题目所指示的那样模拟就行,每次向线段树末尾插入(其实是修改)题目中指定的数,然后询问当前的个数到前面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;

}

posted @ 2014-10-05 13:27  philippica  阅读(236)  评论(0编辑  收藏  举报