zoukankan      html  css  js  c++  java
  • bzoj3166 ALO 可持久化01Trie

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3166

    题意:给出一个序列,找出一个区间使得这个区间内次大值与这个区间内任意一个数的异或和最大值最大。

    首先我们看这个次大值……可能想不出来?想不出来大概是正常的……但是如果我们换一个角度重新放回序列中去研究,就会发现,一个数能够做出贡献的区间就是$(前驱的前驱,后继的后继)$。落实到代码上,最简单的方法就是按数值大小排序,然后从大到小插入$set$,暴力找就行了,比某主席树快了一个$log$

    那么问题就变成了在这个区间之内找到一个数使得异或值最大。这个就交给可持久化$01Trie$去解决好了然而我不会还是现学的

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=50005,inf=(int)2e9;
     4 set<int>S;
     5 int ans,bin[35],n;
     6 struct ques
     7 {
     8     int val,pos;
     9     inline bool operator <(const ques &b)const
    10     {
    11         return val>b.val;
    12     }
    13 }Q[maxn];
    14 struct node
    15 {
    16     node *ch[2];int sum;
    17     node(){}
    18     node(int x){sum=x;ch[0]=ch[1]=0x0;}
    19 }mempool[maxn*50];int num;
    20 node* root[maxn];
    21 node* Newnode(int val)
    22 {
    23     mempool[++num]=node(val);return mempool+num;
    24 }
    25 void Insert(node* &now,node *las,int val,int deep)
    26 {
    27     now=Newnode(las->sum+1);
    28     if(!(~deep))return;
    29     int t=(val>>deep)&1;Insert(now->ch[t],las->ch[t],val,deep-1);now->ch[t^1]=las->ch[t^1];
    30 }
    31 int Query(node* L,node* R,int val,int deep)
    32 {
    33     if(!(~deep))return 0;
    34     int t=(val>>deep)&1;
    35     if(R->ch[t^1]->sum!=L->ch[t^1]->sum)
    36         return bin[deep]|Query(L->ch[t^1],R->ch[t^1],val,deep-1);
    37     else return Query(L->ch[t],R->ch[t],val,deep-1);
    38 }
    39 int haha()
    40 {
    41     bin[0]=1;
    42     for(int i=1;i<=30;i++)bin[i]=bin[i-1]<<1;
    43     scanf("%d",&n);
    44     for(int i=1;i<=n;i++)scanf("%d",&Q[i].val),Q[i].pos=i;
    45     root[0]=mempool;root[0]->ch[0]=root[0]->ch[1]=mempool;root[0]->sum=0;
    46     for(int i=1;i<=n;i++)Insert(root[i],root[i-1],Q[i].val,31);
    47     sort(Q+1,Q+n+1);
    48     S.insert(-1),S.insert(inf),S.insert(-2),S.insert(inf+1),S.insert(Q[1].pos);
    49     for(int i=2;i<=n;i++)
    50     {
    51         int l=Q[i].pos,r=Q[i].pos,x=Q[i].pos;
    52         set<int>::iterator it=S.lower_bound(x),it2=it;
    53         it++;r=(*it)-1;it2--;it2--;l=(*it2)+1;
    54         l=max(1,l);r=min(n,r);
    55         // cout<<l-1<<" "<<r<<" "<<x<<endl;
    56         if(l!=r)ans=max(ans,Query(root[l-1],root[r],Q[i].val,31));
    57         S.insert(Q[i].pos);
    58     }
    59     printf("%d
    ",ans);
    60 }
    61 int sb=haha();
    62 int main(){;}
    bzoj3166
  • 相关阅读:
    CentOS6.3升级GCC到GCC4.8.2
    监督式学习 -- 分类决策树(一)
    DICOM医学图像处理:fo-dicom网络传输之 C-Echo and C-Store
    百度地图-----&gt;地图类型、定位模式、实时交通、我的位置、加入覆盖物、覆盖物详情及提示
    &quot;浪潮杯&quot;第六届ACM山东省省赛山科场总结
    标题栏风格设置
    ActionBarActivity设置全屏无标题
    王立平--自己定义TitleBar
    C++ const限定符
    黑马day14 过滤器概述&amp;生命周期&amp;运行过程
  • 原文地址:https://www.cnblogs.com/Loser-of-Life/p/7703584.html
Copyright © 2011-2022 走看看