标签:
题目链接:
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
#include <bits/stdc++.h> /* #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <algorithm> #include <cstdio> */ using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=1e5+25; int n; LL L[70],R[70],dp[70][2]; int fun() { L[1]=1; R[1]=1; dp[1][0]=1; dp[1][1]=0; for(int i=2;i<=65;i++) { L[i]=1; R[i]=2*R[i-1]+1; dp[i][0]=dp[i-1][0]+1+dp[i-1][1]; dp[i][1]=dp[i-1][0]+dp[i-1][1]; } } LL dfs(LL l, LL r,int node) { if(l>r||node<1)return 0; if(l==L[node]&&r==R[node])return dp[node][0]; LL mid=(L[node]+R[node])>>1; if(r<=mid) { if(r==mid)return 1+dfs(l,r-1,node-1); else return dfs(l,r,node-1); } else { if(l>=mid) { if(l==mid)return r-l+1-dfs(R[node]-r+1,R[node]-l,node-1); else return r-l+1-dfs(R[node]-r+1,R[node]-l+1,node-1); } else return dfs(l,mid-1,node-1)+1+r-mid-dfs(R[node]-r+1,R[node]-mid,node-1); } } int main() { int t; scanf("%d",&t); fun(); while(t--) { LL l,r; scanf("%I64d%I64d",&l,&r); LL ans=dfs(l,r,62); printf("%I64d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5515443.html