hdu-6964 I love counting(莫队+分块)

题目链接:点击这里

题目大意:
给定一个长度为 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 cab

题目分析:
我们可以用莫队离线问题,移动端点时对一个分过块的值域进行更新,来维护区间存在多少种数字。
对于答案的查询,我们将 c ⨁ a ≤ b c\bigoplus a\le b cab 拆分为小于和等于两部分,对于等于部分我们可以通过直接查询 a ⨁ b a \bigoplus b ab 是否存在来获取贡献;对于小于部分可以通过从高位向低位逐位比较 a , b a,b a,b 的二进制位来获取答案,用一个变量 c u r cur cur 来限制数位辅助计算贡献,对于第 i i i 位:

  1. 如果 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 的个数即可
  2. 如果 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 相等
  3. 如果 b b b 0 0 0 a a a 1 1 1 ,那么 c c c 1 1 1 才有可能小于 b b b
  4. 如果 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 ca<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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值