【线段树】 HDOJ 4893 Wow! Such Sequence!

来源:互联网 发布:淘宝买手机怎么退货 编辑:程序博客网 时间:2024/05/29 19:05

用一个标记记录该区间是否都是Fibonacci 数。。如果是就标记为1,否则就标记为0。。然后就用普通线段树向下传递就行了。。。

#include <iostream>  #include <queue>  #include <stack>  #include <map>  #include <set>  #include <bitset>  #include <cstdio>  #include <algorithm>  #include <cstring>  #include <climits>  #include <cstdlib>#include <cmath>#include <time.h>#define maxn 100005#define eps 1e-10#define mod 1000000009#define INF 99999999  #define lowbit(x) (x&(-x))  #define ls o<<1#define rs o<<1 | 1#define lson o<<1, L, mid  #define rson o<<1 | 1, mid+1, R  typedef long long LL;//typedef int LL;using namespace std;LL sum[maxn<<2];int mark[maxn<<2];LL f[maxn];int n, m, ql, qr, p, v, cnt;void init(void){memset(sum, 0, sizeof sum);memset(mark, 0, sizeof mark);}void get_f(void){f[0] = 0, f[1] = f[2] = 1;cnt = 80;for(int i = 3; i <= 80; i++) f[i] = f[i-1] + f[i-2];}LL find(LL tmp){int top = cnt, bot = 1, mid, res;while(top >= bot) {mid = (top+bot)>>1;if(f[mid] >= tmp) top = mid-1, res = mid;else bot = mid+1;}if(res == 1) return f[res];if(abs(f[res-1] - tmp) <= abs(f[res] - tmp)) return f[res-1];else return f[res];}void pushup(int o){if(mark[ls] && mark[rs]) mark[o] = 1;else mark[o] = 0;sum[o] = sum[ls] + sum[rs];}void updata(int o, int L, int R){if(ql <= L && qr >= R && mark[o]) return;if(L == R) {sum[o] = find(sum[o]);mark[o] = 1;return;}int mid = (L+R)>>1;if(ql <= mid) updata(lson);if(qr > mid) updata(rson);pushup(o);}void update(int o, int L, int R){if(L == R) {sum[o] += v;mark[o] = 0;return;}int mid = (L+R)>>1;if(p <= mid) update(lson);else update(rson);pushup(o);}LL query(int o, int L, int R){if(ql <= L && qr >= R) return sum[o];int mid = (L+R)>>1;LL ans = 0;if(ql <= mid) ans += query(lson);if(qr > mid) ans += query(rson);return ans;}void work(void){int k;while(m--) {scanf("%d", &k);if(k == 1) {scanf("%d%d", &p, &v);update(1, 1, n);}else if(k == 2) {scanf("%d%d", &ql, &qr);printf("%I64d\n", query(1, 1, n));}else {scanf("%d%d", &ql, &qr);updata(1, 1, n);}}}int main(void){get_f();while(scanf("%d%d", &n, &m)!=EOF) {init();work();}return 0;}


0 0
原创粉丝点击