题目链接:点击查看

题目大意:给出一个长度为 nnn 的序列,现在要求 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​ 的子区间个数

题目分析:拆位分类讨论:

  1. 0 0:0+0=0,0⊕0=00+0=0,0\oplus 0=00+0=0,0⊕0=0
  2. 0 1:0+1=1,0⊕1=10+1=1,0\oplus 1=10+1=1,0⊕1=1
  3. 1 0:1+0=1,1⊕0=11+0=1,1\oplus 0=11+0=1,1⊕0=1
  4. 1 1:1+1=2,1⊕1=01+1=2,1\oplus 1=01+1=2,1⊕1=0

不难发现当且仅当两个数字不同时为 000 时加法才和异或等价,换句话说,当且仅当 x&y=0x\&y=0x&y=0 时,x+y=x⊕yx+y=x\oplus yx+y=x⊕y

所以可以对于每个左端点尺取找到满足条件的右端点的最大值,使得 sum[l:r]=xor[l:r]sum[l:r]=xor[l:r]sum[l:r]=xor[l:r],不难看出区间 [l,r][l,r][l,r] 内的所有点都可以作为右端点

代码:

// Problem: D - Xor Sum 2
// Contest: Virtual Judge - 7.31限时训练(生成树,树的直径,单调栈)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;
}

AtCoder - arc098_b Xor Sum 2(尺取+位运算)相关推荐

  1. 【寻找巧妙性质-双指针】 AtCoder - arc098_b Xor Sum 2

    题目链接 题意:问有多少个区间,异或等于加法的和 题解 [AtCoder - arc098_b]Xor Sum 2 以下copy自题解 方法:尺取法(双指针)+ 前缀和 一个前置芝士: a ^ a ^ ...

  2. Atcoder 4142 Xor Sum (双指针)

    传送门 题意: (1<=n<=1e6) a^b == a+b成立当且仅当对于每一位,a,b中最多只有1个数是1. 大胆推广到[l,r],a[l]+a[l+1]...+a[r]==a[l] ...

  3. c语言位运算+乘法,关于c语言中的位运算。。。

    标签:c++ 位运算是一种针对二进制数的一种运算 位运算 共有六种 都有其对应得操作符号 &      (and)      位于 |        (or)         位或 ~     ...

  4. AT4142 [ARC098B] Xor Sum 2(尺取)

    传送门 题目大意 给出一个序列 aaa,求出有多少个区间 [l,r][l,r][l,r],满足 al⊕al+1⊕...⊕ar=∑i=lraia_l \oplus a_{l + 1} \oplus .. ...

  5. 【Gym - 101608G】WiFi Password (区间或,线段树 或 按位处理+尺取 或 二分)

    题干: Just days before the JCPC, your internet service went down. You decided to continue your trainin ...

  6. Xor Sum 2(位运算)

    D - Xor Sum 2 Time limit : 2sec / Memory limit : 1024MB Score : 500 points Problem Statement There i ...

  7. Maximum XOR Sum 系列问题

    给定 $n$ 个两两不同的正整数 $a_1, a_2, \dots, a_n$,$a_i < 2^k$ . Problem 1(经典问题) 求 $a_i \xor a_j$ 的最大值,$ 1\l ...

  8. 蓝桥杯 日志统计 尺取

    参考代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; vector<int> v[1 ...

  9. Xor sum HDU - 6955

    Xor sum HDU - 6955 题意: 给定一个长度为n的整数序列,求其XOR和不小于k的最短连续子序列. 如果有多个相同长度的连续子序列,则打印具有最小左端点的连续子序列. 如果没有连续的子序 ...

最新文章

  1. Fine-tune之后的NLP新范式:Prompt越来越火,CMU华人博士后出了篇综述文章
  2. 【深度学习】上海交大发布 MedMNIST 医学图像分析数据集 新基准
  3. Leet Code OJ 100. Same Tree [Difficulty: Easy]
  4. Unity-Find-Script-References 查找脚本的引用
  5. c语言实参和形参占用存储单元_C语言判断题题库
  6. centos6.7环境下kvm虚拟机之virt-install和virsh及virt-manager工具的使用
  7. 交互题[CF1103B Game with modulo、CF1019B The hat、CF896B Ithea Plays With Chtholly]
  8. windows制作docker镜像_优秀软件推荐-微软官方工具升级windows或制作启动U盘及镜像...
  9. 笔记之_Java整理IO流
  10. 33.Jump Game(跳步游戏)
  11. 实时视频传输协议RTP
  12. 基于JAVA EE的临床科室管理系统
  13. java List复制:浅拷贝与深拷贝
  14. Android 用官方SDK实现第三方(qq、微信、微博等)分享和登录
  15. pixhawk飞控解锁方法
  16. IPTV与DTV:竞争还是共存?
  17. QT 实现WINDOWS下网络设置功能(IP、子网掩码、网关、DNS)
  18. doPost()详解
  19. 一句代码生成二维码,一句代码生成条形码,批量生成二维码和条形码,步骤教学
  20. java玫瑰花代码_给爱人的玫瑰花表白程序代码--Java版-Go语言中文社区

热门文章

  1. Nginx负载均衡状态介绍
  2. Spring配置文件中的细节
  3. 释放锁的逻辑-InterProcessMutex.release
  4. AbstractBeanFactory 的getBean()方法调用FactoryBean
  5. Redis Hash 哈希 结构
  6. Nacos配置中心-如何使用Nacos作为配置中心统一管理配置
  7. 使用固态存储SSD或PCIe卡
  8. 多库共存 包装集的问题 点击按钮创建一个p 几个属性介绍 jQuery的插件
  9. excel转html放在tomcat,Excel转web!把excel在线打开??
  10. python知识点汇总百度文库_PYTHON知识点汇总.doc