[BZOJ1012][JSOI2008]最大数maxnumber
2015-07-18 23:16
656 查看
原题地址
线段树水题.
AC code:
线段树水题.
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; }
相关文章推荐
- json 总结
- js笔记
- 网页性能之HTML,CSS,JavaScript
- JavaScript学习要点(二)
- JavaScript匿名函数与闭包
- [LeetCode][JavaScript]Sliding Window Maximum
- Javascript下IE与Firefox下的差异兼容写法总结
- VS 2013 配置jsoncpp 库(亲测可行)
- JavaScript中逗号运算符和this
- JavaScript---DOM文档
- JS中怎么将json对象转化成字符串
- 【JavaScript】——JS入门
- JDK8在Java转让Javascript脚本引擎动态地定义和运行代码
- a href="" 和 href="#" 以及 href="javascript:void(0)"时 以及其中的 onclick 的区别
- javascript深入理解js闭包
- extjs url中文乱码处理 -- encodeURI(filePath)
- js中对radio和checkbox是否选中的判断
- javascript中获取string的长度
- javaScript 删除确认实现方法总结分享
- js-json-2