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<b1<…<bm−1<n0≤b0<b1<…<bm−1<n, is a subsequence of length mm of the array aa.

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<m−10≤p<m−1) holds: abp⊕bp+1<abp+1⊕bpabp⊕bp+1<abp+1⊕bp.

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.

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=9e6+5;
int tr[N][2],f[N][2],idx;
int dp[N],a[N];
void insert(int n,int k)
{
    int root=0;
    for(int i=30;i>=0;i--)
    {
        int now=(n>>i)&1;
        if(tr[root][now]) root=tr[root][now];
        else root=tr[root][now]=++idx;
        f[root][(k>>i)&1]=max(f[root][(k>>i)&1],dp[k]);
    }
}
int query(int n,int k)
{
    int root=0,res=1;
    for(int i=30;i>=0;i--)
    {
        int now=(n>>i)&1;
        int rev=tr[root][now^1];
        if(rev) res=max(res,f[rev][(k>>i)&1^1]+1);
        root=tr[root][now];
        if(!root) break;
    }
    return res;
}
void solve()
{
    idx=0;
    int n;cin>>n;
    for(int i=0;i<n;i++) cin>>a[i],dp[i]=1;
    int mx=0;
    for(int i=0;i<n;i++)
    {
        dp[i]=query(a[i]^i,a[i]);
        insert(a[i]^i,i);
        mx=max(mx,dp[i]);
    }
    cout<<mx<<'\n';
    for(int i=0;i<=idx;i++) f[i][0]=f[i][1]=tr[i][0]=tr[i][1]=0;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;cin>>t;
    while(t--)
    {
        solve();
    }
}

### 关于洛谷 P1481 的字典树 (Trie) 算法 #### 字典树 (Trie) 数据结构简介 字典树是一种用于高效存储和检索字符串集合的数据结构。它通过将公共前缀共享的方式来节省空间并提高查询效率。对于本题而言,字典树的核心思想在于构建一棵多叉树来表示一组单词的字符序列[^5]。 #### 构建字典树的过程 在字典树中,每个节点代表一个字符,而从根到某个节点的路径则构成了一部分字符串。以下是构建字典树的主要过程: 1. **初始化**: 创建一个根节点 `root`。 2. **插入操作**: 将每一个单词逐字符插入字典树中。如果当前字符不存在,则创建新的子节点;否则沿已有路径继续向下遍历直到完成整个单词的插入。 ```python class TrieNode: def __init__(self): self.children = {} self.is_end_of_word = False def insert(root, word): node = root for char in word: if char not in node.children: node.children[char] = TrieNode() node = node.children[char] node.is_end_of_word = True ``` #### 查询操作 为了判断某字符串是否存在或者统计某些特定条件下的匹配数量,可以通过递归或迭代的方式访问字典树中的相应节点。例如,在此题目背景下可能需要计算满足一定约束条件下能够组成的合法串数。 #### 动态规划与状态转移方程 由于题目涉及到构造固定长度且包含指定数目关键词汇表内的任意组合形式的新字符串问题,因此除了单纯依靠字典树外还需要引入动态规划的思想来进行求解。设 dp[i][j] 表示已经处理到了第 i 位,并且此时正好包含了 j 个目标词汇的情况总数,则有如下关系式成立: \[dp[i][j]=\sum_{w \in S} dp[i-len(w)][max(0,j-cnt[w])]\] 其中 \(S\) 是所有候选单词集,\(len(w)\) 和 \(cnt[w]\) 分别对应着单个词语本身的尺寸以及其贡献给最终计数值的部分大小[^4]。 #### 完整解决方案框架 综合以上分析我们可以给出这样一个完整的解决流程概述: - 初始化必要的辅助数组如 trie 结构体实例化对象; - 根据输入数据依次调用上述定义好的函数完成各项预处理工作比如建立索引映射关系等准备工作; - 使用双重循环枚举位置变量i及其关联参数k从而填充DP表格直至得出最后答案为止。 ```python MOD = int(1e9 + 7) trie_root = TrieNode() for word in words_set: insert(trie_root, word) # Initialize DP table with dimensions [N+1][K+1], where N is max length of string to be formed, # K represents number of distinct required substrings. dp = [[0]*(k_max+1) for _ in range(n_max+1)] dp[0][0] = 1 for l in range(1, n_max+1): for c in alphabet: current_node = trie_root temp_dp = list(dp[l]) # Traverse through possible prefixes ending at position 'l' prefix_length = 0 while current_node and prefix_length <= l: new_k = min(k_max, k_found[current_node]) if new_k >=0 : temp_dp[new_k]=(temp_dp[new_k]+dp[l-prefix_length][new_k-count(current_node)])% MOD if c in current_node.children: current_node=current_node.children[c] prefix_length+=1 else: break dp[l]=temp_dp[:] result=sum([d[k_target]%MOD for d in dp[n]]) print(result) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值