procedure2012
It's not worth it to know you're not worth it!

[关键字]:数据结构

[题目大意]:有两个操作:输出后x个数中的最大值、将一个数插入到最后。

//===============================================================================

[分析]:就是一个线段树的维护,连延迟都没有。

[代码]:

View Code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int MAXN=4000000;

struct node
{
       int dat,x,y,lc,rc;
}tree[MAXN];
int m,MOD,x,n,pos,ans,tot;
char ch;

void Build(int l,int r)
{
     int now=++tot;
     tree[now].x=l,tree[now].y=r;
     if (l<r)
     {
             tree[now].lc=tot+1;
             Build(l,(l+r)/2);
             tree[now].rc=tot+1;
             Build((l+r)/2+1,r);
     }
}

void Insert(int v,int pos,int dat)
{
     if (tree[v].x==pos && pos==tree[v].y)
     {
                        tree[v].dat=dat;
                        return;
     }
     if (pos<=tree[tree[v].lc].y) Insert(tree[v].lc,pos,dat); else
     if (pos>=tree[tree[v].rc].x) Insert(tree[v].rc,pos,dat);
     tree[v].dat=max(tree[tree[v].lc].dat,tree[tree[v].rc].dat);
}
     
void Find(int v,int l,int r)
{
    if (l<=tree[v].x && tree[v].y<=r)
    {
                     ans=max(ans,tree[v].dat);
                     return;
    }
    if (r<=tree[tree[v].lc].y) Find(tree[v].lc,l,r); else
    if (l>=tree[tree[v].rc].x) Find(tree[v].rc,l,r); else
    {
                               Find(tree[v].lc,l,tree[tree[v].lc].y);
                               Find(tree[v].rc,tree[tree[v].rc].x,r);
    }
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    scanf("%d%d",&m,&MOD);
    scanf("%c",&ch);
    Build(1,2000000);
    int x=0,pos=0;
    while (m--)
    {
          scanf("%c %d",&ch,&n);
          //printf("%c %d\n",ch,n);
          if (ch=='A')
          {
                      int temp=(n+x)%MOD;
                      Insert(1,++pos,temp);
          }
          if (ch=='Q')
          {
                      int temp=pos-n+1;
                      ans=-1000000000;
                      Find(1,temp,pos);
                      x=ans;
                      printf("%d\n",x);
          }
          scanf("%c",&ch);
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
posted on 2012-04-12 23:43  procedure2012  阅读(621)  评论(0编辑  收藏  举报