• 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. }

  • 相关阅读:
    硬件学习 PAD9.5 day01 原理图布局开始设置, 元器件的调用和绘制, 新建库, 库添加元器件,
    window11安装Python环境
    2.2.3 vim操作合集
    第55章 业务逻辑之订单、支付实体定义
    10 个 PHP 代码安全漏洞扫描程序
    0基础学习PyFlink——不可以用UDTAF装饰器装饰function的原因分析
    webpack5 基本配置
    多链世界的“高速公路”:一文读懂跨链协议演进与未来
    计算机网络入门
    lightdb-no_push_subq
  • 原文地址:https://blog.csdn.net/qq_52004482/article/details/126686810