Bzoj 3166 heoi2013 Alo persistent trie tree

Source: Internet
Author: User

Given a non-repeating sequence A, you can select an interval in A to obtain the greatest difference or value between the subintervals in the interval and any other number in the interval.

First, we enumerate the secondary values. For a secondary value, the value range of another number may be (L, R) here, l is the second number on the left, which is greater than it. r is the second number on the right, which is greater than it.

Search for the maximum value in the trie tree in this interval.

How can we find this interval? We maintain a balance tree that adds subscripts to the balance tree from the largest to the smallest. Each subscript that adds a subscript larger than the subscripts is in the balance tree. The result is that R is used twice. l

I am too lazy to write 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 persistent trie tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.