B. Code For 1
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon’s place as maester of Castle Black. Jon agrees to Sam’s proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility.
Initially Sam has a list with a single element n. Then he has to perform certain operations on this list. In each operation Sam must remove any element x, such that x > 1, from the list and insert at the same position , , sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.
Now the masters want the total number of 1s in the range l to r (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test?
Input
The first line contains three integers n, l, r (0 ≤ n < 250, 0 ≤ r - l ≤ 105, r ≥ 1, l ≥ 1) – initial element and the range l to r.
It is guaranteed that r is not greater than the length of the final list.
Output
Output the total number of 1s in the range l to r in the final sequence.
Examples
Input
Copy
7 2 5
Output
Copy
4
Input
Copy
10 3 10
Output
Copy
5
Note
Consider first example:
Elements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.
For the second example:
Elements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5.
二分处理;
可以发现每次实际都是对称的;
显然可以求出总的区间长度,然后类似与线段树的操作,每次二分区间,顺便也统计答案即可,orz ;
(思维题)
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 500005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
typedef pair<int, int> pii;
inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }
ll n;
ll ans;
void dfs(ll l, ll r, ll L, ll R, ll sum) {
if (l > r || L > R)return;
ll mid = (l + r) >> 1;
if (R < mid)dfs(l, mid - 1, L, R, sum / 2);
else if (L > mid)dfs(mid + 1, r, L, R, sum / 2);
else {
ans += sum % 2;
dfs(l, mid - 1, L, mid - 1, sum / 2);
dfs(mid + 1, r, mid + 1, R, sum / 2);
}
}
int main()
{
//ios::sync_with_stdio(false);
rdllt(n); ll l, r;
rdllt(l); rdllt(r);
ll s = 1;
ll tmp = n;
while (tmp>1) {
tmp /= 2;
s = s * 2 + 1;
}
dfs(1, s, l, r, n);
cout << ans << endl;
}