section>

Problem Statement

For sequences $B=(B_1,B_2,\dots,B_M)$ and $C=(C_1,C_2,\dots,C_M)$, each of length $M$, consisting of non-negative integers, let the XOR sum $S(B,C)$ of $B$ and $C$ be defined as the sequence $(B_1\oplus C_1, B_2\oplus C_2, ..., B_{M}\oplus C_{M})$ of length $M$ consisting of non-negative integers. Here, $\oplus$ represents bitwise XOR.

For instance, if $B = (1, 2, 3)$ and $C = (3, 5, 7)$, we have $S(B, C) = (1\oplus 3, 2\oplus 5, 3\oplus 7) = (2, 7, 4)$.

You are given a sequence $A = (A_1, A_2, \dots, A_N)$ of non-negative integers. Let $A(i, j)$ denote the contiguous subsequence composed of the $i$-th through $j$-th elements of $A$.

You will be given $Q$ queries explained below and asked to process all of them.

Each query gives you integers $a$, $b$, $c$, $d$, $e$, and $f$, each between $1$ and $N$, inclusive. These integers satisfy $a \leq b$, $c \leq d$, $e \leq f$, and $b-a=d-c$. If $S(A(a, b), A(c, d))$ is strictly lexicographically smaller than $A(e, f)$, print Yes; otherwise, print No.

What is bitwise XOR?

The exclusive logical sum $a \oplus b$ of two integers $a$ and $b$ is defined as follows.

  • The $2^k$'s place ($k \geq 0$) in the binary notation of $a \oplus b$ is $1$ if exactly one of the $2^k$'s places in the binary notation of $a$ and $b$ is $1$; otherwise, it is $0$.

For example, $3 \oplus 5 = 6$ (In binary notation: $011 \oplus 101 = 110$).

What is lexicographical order on sequences?

A sequence $A = (A_1, \ldots, A_{|A|})$ is said to be strictly lexicographically smaller than a sequence $B = (B_1, \ldots, B_{|B|})$ if and only if 1. or 2. below is satisfied.

  1. $|A|<|B|$ and $(A_{1},\ldots,A_{|A|}) = (B_1,\ldots,B_{|A|})$.
  2. There is an integer $1\leq i\leq \min\{|A|,|B|\}$ that satisfies both of the following.
    • $(A_{1},\ldots,A_{i-1}) = (B_1,\ldots,B_{i-1})$.
    • $A_i < B_i$.

Constraints

  • $1 \leq N \leq 5 \times 10^5$
  • $0 \leq A_i \leq 10^{18}$
  • $1 \leq Q \leq 5 \times 10^4$
  • $1 \leq a \leq b \leq N$
  • $1 \leq c \leq d \leq N$
  • $1 \leq e \leq f \leq N$
  • $b - a = d - c$
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format, where $\text{query}_i$ represents the $i$-th query:

$N$ $Q$
$A_1$ $A_2$ $\dots$ $A_N$
$\text{query}_1$
$\text{query}_2$
$\vdots$
$\text{query}_Q$

The queries are in the following format:

$a$ $b$ $c$ $d$ $e$ $f$

Output

Print $Q$ lines. The $i$-th line should contain the answer to the $i$-th query.


Sample Input 1

4 5
1 2 3 1
1 3 2 4 1 4
1 2 2 3 3 4
1 1 2 2 3 4
1 2 2 3 3 3
1 4 1 4 1 1

Sample Output 1

No
No
Yes
No
Yes

For the first query, we have $A(1, 3) = (1, 2, 3)$ and $A(2, 4) = (2, 3, 1)$, so $S(A(1,3),A(2,4)) = (1 \oplus 2, 2 \oplus 3, 3 \oplus 1) = (3, 1, 2)$. This is lexicographcially larger than $A(1, 4) = (1, 2, 3, 1)$, so the answer is No.

For the second query, we have $S(A(1,2),A(2,3)) = (3, 1)$ and $A(3,4) = (3, 1)$, which are equal, so the answer is No.


Sample Input 2

10 10
725560240 9175925348 9627229768 7408031479 623321125 4845892509 8712345300 1026746010 4844359340 2169008582
5 6 5 6 2 6
5 6 1 2 1 1
3 8 3 8 1 6
5 10 1 6 1 7
3 4 1 2 5 5
7 10 4 7 2 3
3 6 1 4 7 9
4 5 3 4 8 9
2 6 1 5 5 8
4 8 1 5 1 9

Sample Output 2

Yes
Yes
Yes
Yes
No
No
No
No
No
No

很明显,是哈希的题。要判断哪个字典序大,可以通过二分找到第一个 \(i\) 使 \(a_i\ne b_i\) 然后判断 \(a_i\) 和 \(b_i\) 的大小。然后二分过程中只需要判断两个序列是否相同,就可以用哈希判断。

官方题解的做法太神奇,这里参考了大多赛时AC程序的方法。

首先如果把异或改成加法,就是使用经典的 Robin-Karp 哈希。以一个数 \(x\) 作为位权,定义一个序列 \(a_1,a_2\cdots a_k\) 的哈希值为 \(x^{k-1}a_1+x^{k-2}a_2\cdots xa_{k-1}+a_{k}\) 这个 \(x\) 可以随机取。然后易得序列 \(a_1+b_1,a_2+b_2\cdots a_k+b_k\) 的哈希值就是 \(a\) 序列的哈希值加上 \(b\) 序列的哈希值。所以判断是否是否相同就可以直接将前两个的哈希值相加判断是否与第三个序列相同。

但是异或呢?如果是异或的话,我们的哈希就完全不行了,因为乘法对异或没有分配律。我们要找一种运算对异或满足分配律的,想到与和或,但是他们都不支持幂次。

这个运算是矩阵乘法。不妨把一个二进制当作一个行向量。众所周知,正常矩阵乘法的形式是 $$c_{i,j}=\sum{a_{i,j}\times b_{j,k}}$$

然后把他修改成 $$c_{i,j}=\bigoplus a_{i,j}\and b_{j,k}$$

\(\bigoplus\) 仍然表示异或, \(\and\) 表示按位与。

根据矩阵的结论,由于按位与对异或有分配律,所以其实这样出来的矩阵具有结合律,对异或具有分配律。所以方法就是以一个随机01矩阵为位权。这样哈希时我们就可以直接将两个序列的哈希值异或起来,是与按位异或后的序列的哈希值相等。

但是这样一次矩阵乘法的复杂度是 \(log^3\),算上二分过不了?类似的以位运算为运算的矩阵乘法大多是可以压位的。观察上面的式子,如果 \(a_{i,j}=1,c_i^=b_j\)(c,b已经压成了二进制)。矩阵乘法复杂度降到 \(log^2\)

另外,由于矩阵满足分配律,所以可以预处理矩阵的幂后,可以像正常的哈希一样直接提取出区间 \([l,r]\) 的哈希值。但是我保险起见,打了一个倍增。这样更有可能对。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int N=5e5+5,M=63;
mt19937_64 gen(time(0));
struct matrix{
int a[M];
void init()
{
for(int i=0;i<M;i++)
a[i]=1LL<<i;
}
matrix operator*(const matrix&b)const{
matrix c;
memset(c.a,0,sizeof(c.a));
for(int i=0;i<M;i++)
for(int j=0;j<M;j++)
if(a[i]>>j&1)
c.a[i]^=b.a[j];
return c;
}
void operator=(matrix b){
for(int i=0;i<M;i++)
a[i]=b.a[i];
}
}pw[25];
LL mul(LL x,matrix a)
{
LL ret=0;
for(int i=0;i<M;i++)
if(x>>i&1)
ret^=a.a[i];
return ret;
}
LL x[N],st[25][N];
int n,q,a,b,c,d,e,f;
int main()
{
scanf("%d%d",&n,&q);
for(int i=0;i<M;i++)
pw[0].a[i]=gen();
for(int i=1;i<=n;i++)
scanf("%lld",x+i),st[0][i]=x[i];
for(int i=1;i<25;i++)
pw[i]=pw[i-1]*pw[i-1];
for(int i=1;i<25;i++)
for(int j=1;j+(1<<i)-1<=n;j++)
st[i][j]=st[i-1][j]^mul(st[i-1][j+(1<<i-1)],pw[i-1]);
while(q--)
{
scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
int l=0;
for(int i=24;i>=0;i--)
if(a+l+(1<<i)<=b+1&&e+l+(1<<i)<=f+1&&(st[i][a+l]^st[i][c+l])==st[i][e+l])
l+=1<<i;
if(e+l>f)
printf("No\n");
else if(a+l>b)
printf("Yes\n");
else if((x[a+l]^x[c+l])<x[e+l])
printf("Yes\n");
else
printf("No\n");
}
}

[ABC274Ex] XOR Sum of Arrays的更多相关文章

  1. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  2. 字典树-百度之星-Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  3. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  4. 2014百度之星第三题Xor Sum(字典树+异或运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  5. Xor Sum 01字典树 hdu4825

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

  6. hdu 4825 Xor Sum (01 Trie)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4825 题面: Xor Sum Time Limit: 2000/1000 MS (Java/Others) ...

  7. HDU--4825 Xor Sum (字典树)

    题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...

  8. hdu 4825 Xor Sum trie树

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Proble ...

  9. hdu 4825 Xor Sum(trie+贪心)

    hdu 4825 Xor Sum(trie+贪心) 刚刚补了前天的CF的D题再做这题感觉轻松了许多.简直一个模子啊...跑树上异或x最大值.贪心地让某位的值与x对应位的值不同即可. #include ...

  10. UVALive4682 XOR Sum

    UVALive4682 XOR Sum 题意 给定一个数组, 求连续子序列中异或值最大的值. 题解 假设答案区间为 [L, R], 则答案为 XOR[L, R], 可以将区间分解为 XOR[L,R] ...

随机推荐

  1. c++算法之离散化

    什么是离散化? 离散化,故离散数学,其中的"离散"就是不连续的意思.离散化可以保持原数值之间相对大小关系不变的情况下将其映射成正整数. 也就是给可能用到的数值按大小关系分配一个编号 ...

  2. [ABC305D] Sleep Log题解

    题目大意 给 \(N\) 个时刻: 当 \(i\) 为奇数时,\(A_i\) 表示刚刚起床的时刻. 当 \(i\) 为偶数时,\(A_i\) 表示开始睡觉的时刻. 有 \(Q\) 次询问,每次求在 \ ...

  3. 使用.NET Jieba.NET 的 PosSegmenter 实现中文分词匹配

    ​ 目录 引言 1. 什么是中文分词 2. Jieba.NET简介 3. PosSegmenter介绍 4. 实现中文分词匹配 4.1 安装Jieba.NET库 4.2 创建PosSegmenter实 ...

  4. 如何调用API接口获取淘宝商品数据

    淘宝商品数据的获取是一项非常重要的技术,它可以为淘宝卖家和买家提供有利的数据分析和扩展市场的机会.调用API接口是一种快速.方便.高效的方式获取淘宝商品数据. 以下是一些步骤来调用API接口来获取淘宝 ...

  5. 「codeforces - 1720」

    壹 最近 cq 情况很急急,昨天出去排核酸整了两个半小时,十分无语.提前放假自然是一大好事,但是一个人在家也蛮无聊.不要再涨体重了为好,这一年间他妈 delta 了 10 kilos,算了下 BMI ...

  6. c语言代码练习8

    //输入两个数组,输出两个数字的最大公约数#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int n = ...

  7. Chrome Extensions v3 迁移清单

    一.前置问题 1.1为什么需要迁移 v3? Chrome 计划完全停止 v2 版本维护,后续 v2 版本将无法上架谷歌插件商店,除此之外,未来新版本 Chrome 对于 v2 版本插件的限制会越来越大 ...

  8. 如何调用Metabase开放API

    简介: Metabase是什么? 在传统企业的数据可视化业务中,通常需要从需求到审批,再到安排开发人员和排期,还要开发人员撰写代码最后再做导出.流程繁琐,参与的人员也多,往往需要几天甚至几周的时间! ...

  9. 关于STM32F407ZGT6的USB损坏后使用ST-Link和USART1实现串口功能

    开发板:STM32F407ZGT6: 目标:想使用软件"串口调试助手" 情况:开发板上的USB_UART口所在器件损坏或者直接没有: 解决办法:查看该开发板的原理图,可得:串口1的 ...

  10. React 基础介绍以及demo实践

    这篇文章是之前给新同事培训react基础所写的文章,现贴这里供大家参考: 1.什么是React? React 是一个用于构建用户界面的JavaScript库核心专注于视图,目的实现组件化开发 2.组件 ...