您的位置:首页 > Web前端 > JavaScript

[BZOJ1012][JSOI2008]最大数maxnumber

2015-07-18 23:16 656 查看
原题地址

线段树水题.

AC code:

#include <cstdio>
#include <cstdlib>
int m,d,t;

int Max(int x,int y)
{
return x>y?x:y;
}

struct Snode
{
int l,r,max;
Snode *Lc,*Rc;
};

struct Segtree
{
int tot;
Snode *root,*pool;

Segtree(int size)
{
pool=(Snode*)malloc(size*sizeof(Snode));
tot=0;
root=NULL;
build(&root,1,m);
}

void build(Snode **p,int L,int R)
{
*p=pool++;
(*p)->l=L;
(*p)->r=R;
(*p)->max=0;
if(L==R){
(*p)->Lc=(*p)->Rc=NULL;
return ;
}
int M=(L+R)>>1;
build(&(*p)->Lc,L,M);
build(&(*p)->Rc,M+1,R);
}

void insert(int k)
{
tot++;
_insert(root,k);
}

int getmax(int L,int R)
{
return _getmax(root,L,R);
}

void _insert(Snode *p,int k)
{
if(p->l==p->r){
p->max=k;
return ;
}
int M=(p->l+p->r)>>1;
if(tot<=M) _insert(p->Lc,k);
else _insert(p->Rc,k);
p->max=Max(p->Lc->max,p->Rc->max);
}

int _getmax(Snode *p,int L,int R)
{
if(p->l==L&&p->r==R) return p->max;
int M=(p->l+p->r)>>1;
if(R<=M) return _getmax(p->Lc,L,R);
else if(L>M) return _getmax(p->Rc,L,R);
else return Max(_getmax(p->Lc,L,M),_getmax(p->Rc,M+1,R));
}
};

int main()
{
scanf("%d%d\n",&m,&d);
Segtree T(m<<3);
for(int i=1;i<=m;i++){
int n;
char c;
scanf("%c%d\n",&c,&n);
if(c=='A') T.insert((n+t)%d);
else{
t=T.getmax(T.tot-n+1,T.tot);
printf("%d\n",t);
}
}

return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: