[BZOJ 1012] JSOI 2008 最大数maxnumber · 线段树
2015-03-05 10:01
543 查看
做法很多,单调队列、单调栈、平衡树什么的都能做,不过还是线段树看了比较直观一点。
一开始为一颗空树,tot为当前已经插入的节点数,对于每个Q操作就是log的插入,每个A操作就是从[tot-k+1,tot]区间里找最大值,总复杂度O(NlogN).
一开始为一颗空树,tot为当前已经插入的节点数,对于每个Q操作就是log的插入,每个A操作就是从[tot-k+1,tot]区间里找最大值,总复杂度O(NlogN).
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define ll long long const int maxn=200005; ll n,P,t[maxn*2],x,k,cnt,a[maxn],ans; void ins(int p,int l,int r){ if (l==r) { t[p]=a[cnt]; return ; } int mid=(l+r)>>1; if (mid>=cnt) ins(p+p,l,mid); else ins(p+p+1,mid+1,r); t[p]=max(t[p+p],t[p+p+1]); } void fin(int p,int l,int r){ if (l>=cnt-x+1 && r<=cnt) { ans=max(ans,t[p]); return ; } int mid=(l+r)>>1; if (mid>=cnt-x+1) fin(p+p,l,mid); if (mid+1<=cnt) fin(p+p+1,mid+1,r); } int main(){ scanf("%lld %lld\n",&n,&P);cnt=ans=0; for (int i=1;i<=n;i++){ char c; scanf("%c %lld\n",&c,&x); if (c=='A'){ a[++cnt]=(ans+x)%P; ins(1,1,n); } else { ans=0; fin(1,1,n); printf("%lld\n",ans); } } return 0; }
相关文章推荐
- BZOJ 1012: [JSOI2008]最大数maxnumber 线段树
- BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】
- BZOJ 1012[jsoi2008] 最大数maxnumber 线段树(或者是单调队列+二分)
- |BZOJ 1012|线段树|[JSOI2008]最大数maxnumber
- BZOJ 1012: [JSOI2008]最大数maxnumber 单调队列/线段树
- BZOJ 1012 JSOI2008 最大数maxnumber 单调栈+二分 / 线段树
- BZOJ 1012: [JSOI2008]最大数maxnumber【线段树||单调栈】
- BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]
- BZOJ 1012 [JSOI2008]最大数maxnumber【线段树】
- [BZOJ] 1012 - [JSOI2008] - 最大数maxnumber - 线段树 - 单点更新 - 区间查询最大
- 【听说是线段树】bzoj1012 [JSOI2008]最大数maxnumber
- [BZOJ 1012][JSOI2008]最大数maxnumber:线段树|单调栈
- BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值
- bzoj 1012: [JSOI2008]最大数maxnumber 线段树
- [BZOJ1012][JSOI2008][线段树]最大数maxnumber
- BZOJ_1012_[JSOI2008]最大数maxnumber_暴力?(线段树)
- bzoj1012 [JSOI2008] 最大数maxnumber 线段树
- 【线段树】【JSOI 2008】【bzoj 1012】最大数maxnumber
- BZOJ 1012: [JSOI2008]最大数maxnumber 线段树入门题
- BZOJ-1012[JSOI2008]最大数maxnumber 线段树区间最值