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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HDU - 6955 Xor sum tire树 + 贪心

發(fā)布時(shí)間:2023/12/4 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU - 6955 Xor sum tire树 + 贪心 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

傳送門(mén)

文章目錄

  • 題意:
  • 思路:

題意:

給你一個(gè)數(shù)列aaa,你需要找出來(lái)一個(gè)長(zhǎng)度最小且左端點(diǎn)最靠前的區(qū)間,使其異或和≥k\ge kk
n≤1e5,0≤ai,k<230n\le1e5,0\le a_i,k<2^{30}n1e5,0ai?,k<230

思路:

考慮將其轉(zhuǎn)換成前綴和的形式,那么對(duì)于一個(gè)區(qū)間異或和≥k\ge kk那就是prerxorprel?1≥kpre_r\ \ xor \ \ pre_{l-1}\ge kprer???xor??prel?1?k
這啟發(fā)我們枚舉右端點(diǎn),在tiretiretire樹(shù)中維護(hù)前綴異或和,讓后在tiretiretire樹(shù)上貪心的找即可,也就是每次都向與kkk這一位相等的位置走,一直走到底,最后別忘記加上走到底的答案。

// Problem: Xor sum // Contest: HDOJ // URL: http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1006&cid=984 // Memory Limit: 524 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") //#pragma GCC optimize(2) #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<map> #include<cmath> #include<cctype> #include<vector> #include<set> #include<queue> #include<algorithm> #include<sstream> #include<ctime> #include<cstdlib> #include<random> #include<cassert> #define X first #define Y second #define L (u<<1) #define R (u<<1|1) #define pb push_back #define mk make_pair #define Mid ((tr[u].l+tr[u].r)>>1) #define Len(u) (tr[u].r-tr[u].l+1) #define random(a,b) ((a)+rand()%((b)-(a)+1)) #define db puts("---") using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); } //void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); } //void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII;const int N=100010,mod=1e9+7,INF=0x3f3f3f3f; const double eps=1e-6;int n,k; int a[N]; int tr[N*40][2],idx,mx[N*40];int newnode() {int u=++idx;mx[u]=-1;for(int i=0;i<2;i++) tr[u][i]=0;return u; }void insert(int x,int id) {int p=0;for(int i=30;i>=0;i--) {int u=x>>i&1;if(!tr[p][u]) tr[p][u]=newnode();p=tr[p][u]; mx[p]=max(mx[p],id);} }int query(int x) {int ans=-1;int p=0;for(int i=30;i>=0;i--) {int u=x>>i&1;if(k>>i&1) p=tr[p][!u];else ans=max(ans,mx[tr[p][!u]]),p=tr[p][u];if(!p) break;}if(p) ans=max(ans,mx[p]);return ans; }int main() { // ios::sync_with_stdio(false); // cin.tie(0);int _; scanf("%d",&_);memset(mx,-1,sizeof(mx));while(_--) {idx=0;tr[0][0]=tr[0][1]=0;int ans=INF,al=-1,ar=0;scanf("%d%d",&n,&k);int pre=0;for(int i=1;i<=n;i++) {scanf("%d",&a[i]);insert(pre,i);pre^=a[i];int l=query(pre);if(l!=-1) {if(i-l+1<ans) {ans=i-l+1;al=l; ar=i;}}}if(al==-1) puts("-1");else printf("%d %d\n",al,ar);}return 0; } /**/

總結(jié)

以上是生活随笔為你收集整理的HDU - 6955 Xor sum tire树 + 贪心的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。