Codeforces 768B - Code For 1(分治思想)
768B - Code For 1
思路:类似于线段树的区间查询。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a,b) memset((a),(b),sizeof(a))
int query(ll L,ll R,ll n,ll l,ll r)
{
if(L>r||R<l||!n)return 0;
if(n==1)return 1;
ll m=(l+r)>>1;
return query(L,R,n/2,l,m-1)+query(L,R,n%2,m,m)+query(L,R,n/2,m+1,r);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n,l,r;
cin>>n>>l>>r;
ll L=0,x=n;
while(x)L=L*2+1,x>>=1;
cout<<query(l,r,n,1,L)<<endl;
return 0;
}
转载于//www.cnblogs.com/widsom/p/7345521.html
还没有评论,来说两句吧...