[可持久化字典树 set] BZOJ 3166 [Heoi2013]Alo
2016-05-15 12:54
429 查看
枚举每个点 处理出它能作为次大值的区间
然后就是数据结构的问题了
“按照权值的倒序,set维护位置,依次插入,则x的可行区间为[x前驱的前驱+1,x后继的后继-1](两半合起来)” ——hzwer
然后就是数据结构的问题了
“按照权值的倒序,set维护位置,依次插入,则x的可行区间为[x前驱的前驱+1,x后继的后继-1](两半合起来)” ——hzwer
#include<cstdio> #include<cstdlib> #include<algorithm> #include<set> using namespace std; inline char nc() { static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++; } inline void read(int &x) { char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b; } inline void read(char &x) { for (x=nc();x!='Q' && x!='A';x=nc()); } const int N=50005; const int K=30; int ncnt; int root ; int ch[N*(K+5)][2],sum[N*(K+5)]; inline void insert(int &rt,int x,int t){ rt=++ncnt; int y; sum[y=rt]=sum[x]+1; for (int i=K;~i;i--) { if ((t>>i)&1) ch[y][0]=ch[x][0],y=(ch[y][1]=++ncnt),x=ch[x][1]; else ch[y][1]=ch[x][1],y=(ch[y][0]=++ncnt),x=ch[x][0]; sum[y]=sum[x]+1; } } inline int query(int l,int r,int t){ int ret=0,tmp; for (int i=K;~i;i--) { tmp=(t>>i)&1; if (sum[ch[r][tmp^1]]-sum[ch[l][tmp^1]]) ret+=(1<<i),r=ch[r][tmp^1],l=ch[l][tmp^1]; else r=ch[r][tmp],l=ch[l][tmp]; } return ret; } int n; struct abcd{ int pos,val; bool operator <(const abcd &B) const{ return val>B.val; } }ege ; int ans=0; set<int> Set; typedef set<int>::iterator ITER; int main() { int l,r; read(n); for (int i=1;i<=n;i++) { read(ege[i].val); ege[i].pos=i; insert(root[i],root[i-1],ege[i].val); } Set.insert(-2); Set.insert(-1); Set.insert(1<<30); Set.insert((1<<30)+1); sort(ege+1,ege+n+1); Set.insert(ege[1].pos); for (int i=2;i<=n;i++) { ITER p,q; p=q=Set.lower_bound(ege[i].pos); r=ege[i].pos; r=*q; q++; r=*q-1; l=ege[i].pos; l=*--p; p--; l=*p+1; l=max(1,l); r=min(r,n); if (l!=r) ans=max(ans,query(root[l-1],root[r],ege[i].val)); Set.insert(ege[i].pos); } printf("%d\n",ans); return 0; }
相关文章推荐
- iOS冰与火之歌番外篇 - 在非越狱手机上进行App Hook
- essential c++ 练习题(2)
- Andriod 学习笔记之八 自定义view实现圆圈标记
- HTML5表单及其验证
- 【模板】高斯消元
- Java常见内存溢出异常分析
- LayoutInflater
- 关于算法复杂度
- [可持久化字典树] BZOJ 3261 最大异或和
- JSP如何在servlet将一个数据模型对象传递给jsp页面
- 257. Binary Tree Paths
- Linux下php7.0.6+nginx环境编译安装
- 年薪10万和年薪100万的人,究竟差在哪里?
- Qt TCP通信,多线程服务器端
- KVO、KVC、单例的使用(16.5.15)
- android开发笔记之多媒体—振动器(Vibrator)
- Apache日志分割脚本
- 第10.11周项目?-车类
- DBUtils
- Sublime Text 3 python和Package Control配置方法