BZOJ 3166 HEOI2013 Alo 可持久化Trie樹

來源:互聯網
上載者:User

標籤:bzoj   bzoj3166   可持久化trie樹   

題目大意:給定一個不重複的序列a,在a中任選一個區間,求區間內的次大值與區間內的任意一個其它數的最大的異或值

首先我們枚舉次大值 對於一個次大值 它可能選擇的另一個數的取值範圍為(l,r) 其中l為這個數左側第二個比它大的數 r為這個數右側第二個比它大的數

在這個區間內的Trie樹中貪心尋找最大值即可

這個區間怎麼求呢?我們維護一棵平衡樹 將數從大到小將下標加進平衡樹 每加進一個下標 比它大的數的下標都在平衡樹中 求兩次後繼就是r 求兩次前驅就是l

我偷懶寫了set……

#include<set>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define M 50500#define ls son[0]#define rs son[1]using namespace std;struct Trie{Trie *son[2];int cnt;}*tree[M],mempool[M*40],*C=mempool;int n,maxnum,ans,a[M],l[M],r[M];pair<int,int>b[M];set<int>s;set<int>::iterator it;inline Trie* New_Node(Trie *_,Trie *__,int ___){C->ls=_;C->rs=__;C->cnt=___;return C++;}Trie* Build_Tree(Trie *p,int x,int pos){if(!pos)return New_Node(0x0,0x0,p->cnt+1);if(~x&pos)return New_Node(Build_Tree(p->ls,x,pos>>1),p->rs,p->cnt+1);elsereturn New_Node(p->ls,Build_Tree(p->rs,x,pos>>1),p->cnt+1);}int Get_Ans(Trie *l,Trie *r,int x,int pos){int num=x&pos?1:0;if(!pos)return 0;if(r->son[!num]->cnt-l->son[!num]->cnt)return pos + Get_Ans(l->son[!num],r->son[!num],x,pos>>1);elsereturn Get_Ans(l->son[num],r->son[num],x,pos>>1);}inline int Get_L(int x){it=s.find(x);if(it--==s.begin()) return 0;if(it--==s.begin()) return 0;return *it;}inline int Get_R(int x){it=s.find(x);if(++it==s.end()) return n+1;if(++it==s.end()) return n+1;return *it;}int main(){int i;cin>>n;for(i=1;i<=n;i++){scanf("%d",&a[i]);b[i].first=a[i];b[i].second=i;maxnum=max(maxnum,a[i]);}sort(b+1,b+n+1);for(i=n;i;i--){s.insert(b[i].second);l[b[i].second]=Get_L(b[i].second);r[b[i].second]=Get_R(b[i].second);}tree[0]=New_Node(C,C,0);for(i=1;i<=n;i++)tree[i]=Build_Tree(tree[i-1],a[i],1<<30);for(i=1;i<=n;i++)if(a[i]!=maxnum)ans=max(Get_Ans(tree[l[i]],tree[r[i]-1],a[i],1<<30),ans);cout<<ans<<endl;}


BZOJ 3166 HEOI2013 Alo 可持久化Trie樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.