题目链接:点击这里
题目大意:
给定一个长度为
n
n
n 的序列
{
c
i
}
\{c_i\}
{ci} ,有
q
q
q 个询问,每次给出一个区间
(
l
,
r
)
(l,r)
(l,r) 和
a
,
b
a,b
a,b,求有多少个在该区间有多少种数字满足满足
c
⨁
a
≤
b
c\bigoplus a\le b
c⨁a≤b
题目分析:
我们可以用莫队离线问题,移动端点时对一个分过块的值域进行更新,来维护区间存在多少种数字。
对于答案的查询,我们将
c
⨁
a
≤
b
c\bigoplus a\le b
c⨁a≤b 拆分为小于和等于两部分,对于等于部分我们可以通过直接查询
a
⨁
b
a \bigoplus b
a⨁b 是否存在来获取贡献;对于小于部分可以通过从高位向低位逐位比较
a
,
b
a,b
a,b 的二进制位来获取答案,用一个变量
c
u
r
cur
cur 来限制数位辅助计算贡献,对于第
i
i
i 位:
- 如果 b b b 为 1 1 1 , a a a 为 1 1 1 ,那么 c c c 一定为 1 1 1 ,那么此时 c u r cur cur 添 0 0 0 即可,查询第 i i i 位为 1 1 1 的 c c c 的个数即可
- 如果 b b b 为 1 1 1 , a a a 为 0 0 0 ,那么 c c c 一定为 0 0 0 ,查询第 i i i 位为 0 0 0 的 c c c 的个数即可,由于 a , c a,c a,c 为 0 0 0 要让 c u r cur cur 添上 1 1 1 ,那样异或值这一位是 1 1 1 才会和 b b b 相等
- 如果 b b b 为 0 0 0 , a a a 为 1 1 1 ,那么 c c c 为 1 1 1 才有可能小于 b b b
- 如果
b
b
b 为
0
0
0 ,
a
a
a 为
0
0
0 ,那么
c
c
c 为
0
0
0 才有可能小于
b
b
b
由于 b b b 等于 0 0 0 时不可能存在 c ⨁ a < b c\bigoplus a<b c⨁a<b 所以在 c c c 为 1 1 1 时给 c u r cur cur 添上 1 1 1 ,为 0 0 0 时不用管即可
时间复杂度为 O ( n n + m l o g n ) O(n\sqrt n+mlog\sqrt n) O(nn+mlogn)
具体细节见代码:
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define int ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 1e6+5;
const int mod = 9973;
const double pi = acos(-1);
const double eps = 1e-8;
int n,m,block,a[maxn],cnt[maxn],sum[maxn],ans[maxn];
bool c[maxn];
struct query{
int l,r,a,b,id;
friend bool operator < (const query &a,const query &b)
{
return (a.l/block)^(b.l/block)?a.l<b.l:(((a.l/block)&1)?a.r<b.r:a.r>b.r);
}
}q[maxn];
void add(int pos)
{
cnt[pos]++;
if(cnt[pos] == 1) sum[pos/block]++,c[pos] = true;
}
void del(int pos)
{
cnt[pos]--;
if(!cnt[pos]) sum[pos/block]--,c[pos] = false;
}
int calc(int pos)
{
int res = 0;
for(int i = 0;i < pos/block;i++) res += sum[i];
for(int i = pos/block*block;i <= pos;i++) res += c[i];
return res;
}
signed main()
{
n = read();
for(int i = 1;i <= n;i++) a[i] = read(); m = read(); block = sqrt(m)+1;
for(int i = 1;i <= m;i++) q[i].l = read(),q[i].r = read(),q[i].a = read(),q[i].b = read(),q[i].id = i;
sort(q+1,q+m+1);
int l = 1,r = 0;
for(int i = 1;i <= m;i++)
{
int ql = q[i].l,qr = q[i].r,a = q[i].a,b = q[i].b;
while(l > ql) add(::a[--l]);
while(l < ql) del(::a[l++]);
while(r > qr) del(::a[r--]);
while(r < qr) add(::a[++r]);
int cur = 0;
for(int j = 20;~j;j--)
{
if(b>>j&1)
{
if(a>>j&1) ans[q[i].id] += calc(cur+(1<<(j+1))-1)-calc(cur+(1<<j)-1);
else ans[q[i].id] += calc(cur+(1<<j)-1)-calc(cur-1),cur |= 1<<j;
}
else if(a>>j&1) cur |= 1<<j;
}
ans[q[i].id] += c[a^b]; //等号的贡献别忘了
}
for(int i = 1;i <= m;i++) printf("%d\n",ans[i]);
return 0;
}