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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

AtCoder - arc098_b Xor Sum 2(尺取+位运算)

發(fā)布時間:2024/4/11 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AtCoder - arc098_b Xor Sum 2(尺取+位运算) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出一個長度為 nnn 的序列,現(xiàn)在要求 AlxorAl+1xor...xorAr=Al+Al+1+...+ArA_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_rAl??xor?Al+1??xor?...?xor?Ar?=Al??+?Al+1??+?...?+?Ar? 的子區(qū)間個數(shù)

題目分析:拆位分類討論:

  • 0 0:0+0=0,0⊕0=00+0=0,0\oplus 0=00+0=0,00=0
  • 0 1:0+1=1,0⊕1=10+1=1,0\oplus 1=10+1=1,01=1
  • 1 0:1+0=1,1⊕0=11+0=1,1\oplus 0=11+0=1,10=1
  • 1 1:1+1=2,1⊕1=01+1=2,1\oplus 1=01+1=2,11=0
  • 不難發(fā)現(xiàn)當且僅當兩個數(shù)字不同時為 000 時加法才和異或等價,換句話說,當且僅當 x&y=0x\&y=0x&y=0 時,x+y=x⊕yx+y=x\oplus yx+y=xy

    所以可以對于每個左端點尺取找到滿足條件的右端點的最大值,使得 sum[l:r]=xor[l:r]sum[l:r]=xor[l:r]sum[l:r]=xor[l:r],不難看出區(qū)間 [l,r][l,r][l,r] 內(nèi)的所有點都可以作為右端點

    代碼:

    // Problem: D - Xor Sum 2 // Contest: Virtual Judge - 7.31限時訓(xùn)練(生成樹,樹的直徑,單調(diào)棧)2 // URL: https://vjudge.net/contest/450440#problem/D // Memory Limit: 1048 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; LL a[N],sum[N],_xor[N]; int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n;read(n);for(int i=1;i<=n;i++) {read(a[i]);sum[i]=sum[i-1]+a[i];_xor[i]=_xor[i-1]^a[i];}LL ans=0;int r=1;for(int l=1;l<=n;l++) {while(r<=n&&(_xor[r]^_xor[l-1])==sum[r]-sum[l-1]) {r++;}ans+=r-l;}cout<<ans<<endl;return 0; }

    總結(jié)

    以上是生活随笔為你收集整理的AtCoder - arc098_b Xor Sum 2(尺取+位运算)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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