• 01字典树+dp


    D2. Xor-Subsequence (hard version)

    time limit per test

    2 seconds

    memory limit per test

    512 megabytes

    input

    standard input

    output

    standard output

    It is the hard version of the problem. The only difference is that in this version ai≤109ai≤109.

    You are given an array of nn integers a0,a1,a2,…an−1a0,a1,a2,…an−1. Bryap wants to find the longest beautiful subsequence in the array.

    An array b=[b0,b1,…,bm−1]b=[b0,b1,…,bm−1], where 0≤b0

    Subsequence b=[b0,b1,…,bm−1]b=[b0,b1,…,bm−1] of length mm is called beautiful, if the following condition holds:

    • For any pp (0≤p

    Here a⊕ba⊕b denotes the bitwise XOR of aa and bb. For example, 2⊕4=62⊕4=6 and 3⊕1=23⊕1=2.

    Bryap is a simple person so he only wants to know the length of the longest such subsequence. Help Bryap and find the answer to his question.

    Input

    The first line contains a single integer tt (1≤t≤1051≤t≤105)  — the number of test cases. The description of the test cases follows.

    The first line of each test case contains a single integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the length of the array.

    The second line of each test case contains nn integers a0,a1,...,an−1a0,a1,...,an−1 (0≤ai≤1090≤ai≤109) — the elements of the array.

    It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053⋅105.

    Output

    For each test case print a single integer — the length of the longest beautiful subsequence.

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. const int N=9e6+5;
    6. int tr[N][2],f[N][2],idx;
    7. int dp[N],a[N];
    8. void insert(int n,int k)
    9. {
    10. int root=0;
    11. for(int i=30;i>=0;i--)
    12. {
    13. int now=(n>>i)&1;
    14. if(tr[root][now]) root=tr[root][now];
    15. else root=tr[root][now]=++idx;
    16. f[root][(k>>i)&1]=max(f[root][(k>>i)&1],dp[k]);
    17. }
    18. }
    19. int query(int n,int k)
    20. {
    21. int root=0,res=1;
    22. for(int i=30;i>=0;i--)
    23. {
    24. int now=(n>>i)&1;
    25. int rev=tr[root][now^1];
    26. if(rev) res=max(res,f[rev][(k>>i)&1^1]+1);
    27. root=tr[root][now];
    28. if(!root) break;
    29. }
    30. return res;
    31. }
    32. void solve()
    33. {
    34. idx=0;
    35. int n;cin>>n;
    36. for(int i=0;i>a[i],dp[i]=1;
    37. int mx=0;
    38. for(int i=0;i
    39. {
    40. dp[i]=query(a[i]^i,a[i]);
    41. insert(a[i]^i,i);
    42. mx=max(mx,dp[i]);
    43. }
    44. cout<'\n';
    45. for(int i=0;i<=idx;i++) f[i][0]=f[i][1]=tr[i][0]=tr[i][1]=0;
    46. }
    47. int main()
    48. {
    49. ios_base::sync_with_stdio(false);
    50. cin.tie(0),cout.tie(0);
    51. int t;cin>>t;
    52. while(t--)
    53. {
    54. solve();
    55. }
    56. }

  • 相关阅读:
    Flask 学习99-Flask-SocketIO 快速入门与使用
    【Openxml】如何为OpenXml元素创建超链接
    海康威视热成像实时测温java - 23版
    基于SSM的考研图书电子商务平台的设计与实现
    【笔记】元素水平滑动(松手查看更多、滑动回弹)
    二、电脑装机实践
    React 脚手架
    【数据结构与算法】万字顺序表与OJ题
    词汇与词组
    图片gif怎么做?这一招分分钟制作
  • 原文地址:https://blog.csdn.net/qq_52004482/article/details/126686810