Bitwise And Queries

Time limit: 1500 ms
Memory limit: 128 MB

You are given QQ queries of the form a\ b\ xa b x. Count the number of values yy such that a \leq y \leq ba≤y≤b and x\ \& \ y = xx & y=x, where we denote by \&& the bitwise and operation.

Standard input

The first line contains a single integer QQ.

Each of the following QQ lines contains three integers a\ b\ xa b x, representing a query.

Standard output

Output QQ lines, each containing a single integer representing the answer to a query.

Constraints and notes

  • 1 \leq Q \leq 10^51≤Q≤10​5​​
  • 1 \leq a \leq b \leq 10^{18}1≤a≤b≤10​18​​
  • 0 \leq x \leq 10^{18}0≤x≤10​18​​
Input Output
4
1 10 3
5 10 0
1 63 7
32 100 32
2
6
8
37
 

x&y==x,说明x二进制表示上,有1的地方,y也要有1,是0的地方,y可以是0或者1,记忆化瞎几把搜一下就行了,每次可以选择的有0或1,再判断一下能否取0或1。

lr表示此时这个位置y能否取任意数,只要前面x为0,y为1时,递归选0的话,就说明y后面的数可以任意取了,因为前面有1选了0,那后面无论选什么都不可能超过上界了。
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = ;
const int inf = 0x3f3f3f3f;
const int mod = ;
LL dp[][];
int A[],X[];
LL solve(int cur,int lr){
if(cur == )return ;
if(dp[cur][lr] != -)return dp[cur][lr];
if(lr){
if(A[cur]){
return dp[cur][lr] = solve(cur + ,lr);
}
else {
return dp[cur][lr] = * solve(cur + ,lr);
}
}
else {
if(A[cur]){
if(X[cur])return dp[cur][lr] = solve(cur + ,lr);
else return dp[cur][lr] = ;
}
else {
if(X[cur])return dp[cur][lr] = solve(cur+,) + solve(cur+,lr);
else return dp[cur][lr] = solve(cur+,lr);
}
}
}
void print(int *x){
for(int i = ; i < ; i++)
printf("%d",x[i]);
puts("");
}
int main(){
#ifdef local
freopen("in", "r", stdin);
#endif
int T;
scanf("%d",&T);
while(T--){
LL x,y,a;
scanf("%lld%lld%lld",&x,&y,&a);
x--;
for(int i = ; i < ; i++){
A[i] = a & ;
a >>= ;
}
reverse(A,A+);
// print(A);
for(int i = ; i < ; i++){
X[i] = x & ;
x >>= ;
}
reverse(X,X+);
// print(X);
memset(dp,-,sizeof dp);
LL res = solve(,);
for(int i = ; i < ; i++){
X[i] = y & ;
y >>= ;
}
reverse(X,X+);
// print(X);
memset(dp,-,sizeof dp);
res = solve(,) - res;
printf("%lld\n",res);
}
}

Bitwise And Queries的更多相关文章

  1. CF-1451 E Bitwise Queries 异或 交互题

    E - Bitwise Queries 传送门 题意 有一组序列,长度为 \(n(4\le n \le 2^{16})\),且 \(n\) 为 2 的整数次幂,序列中数值范围为 [0,n-1], 每次 ...

  2. 【做题记录】CF1451E2 Bitwise Queries (Hard Version)

    CF1451E2 Bitwise Queries (Hard Version) 题意: 有 \(n\) 个数( \(n\le 2^{16}\) ,且为 \(2\) 的整数次幂,且每一个数都属于区间 \ ...

  3. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  4. 实践 HTML5 的 CSS3 Media Queries

    先来介绍下 media,确切的说应该是 CSS media queries(CSS 媒体查询),媒体查询包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3 ...

  5. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  6. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  7. CSS3 Media Queries 实现响应式设计

    在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...

  8. 使用CSS3 Media Queries实现网页自适应

    原文来源:http://webdesignerwall.com 翻译:http://xinyo.org 当今银屏分辨率从 320px (iPhone)到 2560px (大屏显示器)或者更大.人们也不 ...

  9. SQL Queries from Transactional Plugin Pipeline

    Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...

随机推荐

  1. 关于JavaScript的模块化

    为什么需要模块化 最近在学习网易微专业的<前端系统架构>课程,里面讲到了关于JavaScript的模块化问题.具体指的是当随着Web系统不断强大起来,需要在客户端进行的操作就多了起来(比如 ...

  2. JavaWeb总结(十)—文件上传和下载

    一.文件的上传 1.文件的基本上传 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用Servlet获取上传文件的输入流然后再解析里面的请求参数是比较麻烦,所以一般选择 ...

  3. Struts 2 实现登录以及显示信息

    ---恢复内容开始--- 这个星期主要做了一个项目的开始,基于开始学习Struts2的框架首先要先做好Action的按钮 这是我一个Action的展示,chain中是把一个数据带着去跳转另一个网页中 ...

  4. java基础--动态代理实现与原理详细分析

    关于Java中的动态代理,我们首先需要了解的是一种常用的设计模式--代理模式,而对于代理,根据创建代理类的时间点,又可以分为静态代理和动态代理. 一.代理模式                     ...

  5. phpcms ——模板标签详细使用说明

    使用phpcms总是要查询各种标签,实在很烦,只好找个比较全的来备查.因为自己写一个orm来配合调用也没那么容易无缝的嵌入到引擎当中. 获取父分类下面的子分类 {loop subcat(77) $k ...

  6. Node.js基本开发流程

    创建一个hello world: 1.打开一个文本编辑器,在其中输入console.log("hello world"),并保存为hello.js; 注意:输入中文如果编码不是ut ...

  7. 500. Keyboard Row

    Given a List of words, return the words that can be typed using letters of alphabet on only one row' ...

  8. css3 新属性

    一 选择器1 兄弟选择器 0 以第一个选择器开始,往后找满足条件的兄弟节点 class~class() <-- lorem+数字 -tab --> 可以输出默认文字2 属性选择器 标签[a ...

  9. ASP.NET MVC5请求管道和生命周期

    请求处理管道 请求管道是一些用于处理HTTP请求的模块组合,在ASP.NET中,请求管道有两个核心组件:IHttpModule和IHttpHandler.所有的HTTP请求都会进入IHttpHandl ...

  10. AOJ/数据结构习题集

    ALDS1_3_A-Stack. Description: Write a program which reads an expression in the Reverse Polish notati ...