B. Code For 1 一个类似于线段树的东西
生活随笔
收集整理的這篇文章主要介紹了
B. Code For 1 一个类似于线段树的东西
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
http://codeforces.com/contest/768/problem/B
我的做法是,觀察到,只有是x % 2的情況下,才有可能出現(xiàn)0
其他的,都是1來(lái)的,所以開始的ans應(yīng)該是R - L + 1
那么現(xiàn)在就是要看那些是x % 2的,然后放在的位置是屬于【L, R】的,有多少個(gè)0,減去就行。
一開始,總長(zhǎng)度是可以算出來(lái)的,然后就能算出一開始的n % 2的位置是那個(gè),就是mid了,然后根據(jù)L和R選擇遞歸
左邊還是右邊,然后我發(fā)現(xiàn)類似于線段樹。。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <assert.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL;#include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> #include <bitset> const int maxn = 1e6 + 20; LL n, L, R; LL calc(LL val) {if (val > 1) {return 2 * calc(val / 2);}return 1; } LL ans; void dfs(LL cur, LL L, LL R, LL be, LL en) {if (cur == 0) {return;}if (be == en) {return;}LL mid = (be + en) >> 1;if (L <= mid && mid <= R && cur % 2 != 1) {ans--;}if (L > mid) {dfs(cur / 2, L, R, mid + 1, en);} else if (R <= mid) {dfs(cur / 2, L, R, be, mid);} else {dfs(cur / 2, L, R, be, mid);dfs(cur / 2, L, R, mid + 1, en);} } void work() {cin >> n >> L >> R;if (n == 0) {cout << 0 << endl;return;}ans = R - L + 1;dfs(n, L, R, 1, 2 * calc(n) - 1);cout << ans << endl; }int main() { #ifdef localfreopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endifwork();return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/liuweimingcprogram/p/6422924.html
總結(jié)
以上是生活随笔為你收集整理的B. Code For 1 一个类似于线段树的东西的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android 四大组件面试
- 下一篇: win10系统如何安装SQL服务器,在W