日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codechef REBXOR HYSBZ - 4260(01字典树+区间异或最大)

發布時間:2023/12/15 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codechef REBXOR HYSBZ - 4260(01字典树+区间异或最大) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


Input
輸入數據的第一行包含一個整數N,表示數組中的元素個數。
第二行包含N個整數A1,A2,…,AN。
Output
輸出一行包含給定表達式可能的最大值。
Sample Input
5
1 2 3 1 2
Sample Output
6
Hint
滿足條件的(l1,r1,l2,r2)有:(1,2,3,3),(1,2,4,5),(3,3,4,5)。

對于100%的數據,2 ≤ N ≤ 4*105,0 ≤ Ai ≤ 109。
做了幾個求區間異或最大的題目。求區間異或最大就是求個前綴,然后就是普通的01字典樹了。
就像這個題目,求兩個不相交的區間,然后兩個區間的異或和最大。
我們想求一個前綴區間異或最大值并且記錄下來存到數組中,然后再去枚舉后綴的區間,再去更新最大值。
代碼如下:

#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<string> #define ll long long using namespace std;const int maxx=4e5+100; struct node{int trie[2];int num; }p[maxx*33]; int sum[maxx],pre[maxx]; int a[maxx],dp[maxx]; int n,tot;inline void Insert(int x) {int root=0;int id;for(int i=32;i>=0;i--){id=(x>>i)&1;if(!p[root].trie[id]) p[root].trie[id]=++tot;root=p[root].trie[id];}p[root].num=x; } inline int Find(int x) {int root=0;int id;for(int i=32;i>=0;i--){id=(x>>i)&1;if(p[root].trie[id^1]) root=p[root].trie[id^1];else root=p[root].trie[id];}return p[root].num^x; } int main() {while(scanf("%d",&n)!=EOF){tot=0;for(int i=1;i<=n;i++) scanf("%d",&a[i]);for(int i=1;i<=n;i++) sum[i]=sum[i-1]^a[i];//前綴for(int i=n;i>=1;i--) pre[i]=pre[i+1]^a[i];//后綴Insert(0);for(int i=1;i<=n;i++){dp[i]=max(dp[i-1],Find(sum[i]));Insert(sum[i]);}int _max=0;for(int i=n;i>=1;i--){_max=max(_max,Find(pre[i])+dp[i-1]);//枚舉后綴區間異或最大值再加上前面求過的前綴,他們的和去更新最大值Insert(pre[i]);}printf("%d\n",_max);}return 0; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的Codechef REBXOR HYSBZ - 4260(01字典树+区间异或最大)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。