计算几何
#include <bits/stdc++.h>
using namespace std;
iinline int read() {
int f = 1, ans = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
ans = ans * 10 + c - '0';
c = getchar();
}
return f * ans;
}
int T, l, r;
int lowbit(int x) {
return x & -x; // x & -x 得到的结果是x的最低位1以及后面的所有0
}
int cont(int x) {
int c = 0;
while (x) {
x -= lowbit(x);
c++;
}
return c; //查找x有多少个二进制1
}
int Get(int x) {
return x / 2 +
((x & 1) | (cont(x) & 1)); //如果x是奇数,那么x一定有奇数个二进制1
}
signed main() {
T = read();
while (T--) {
l = read(), r = read();
printf("%lld\n", Get(r) - Get(l - 1));
}
return 0;
}
题解: