HDU4893 Wow! Such Sequence! 线段树

题意:给你一个序列,其中有三种操作

1)位置为K 的数+ D

2)求 l-r 区间和

3)把 l-r 区间里面的所有数都变为理它最近的斐波纳契数

解题思路:这个题的区间更新其实可以在单点更新的时候就得出,为节点维护两个 和,一个是 斐波纳契和 一个是正常和 ,再看这个区间有没有被3覆盖,特判一下就行了。

解题代码:

  1 // File Name: 1007.cpp
  2 // Author: darkdream
  3 // Created Time: 2014年07月29日 星期二 12时49分33秒
  4
  5 #include<vector>
  6 #include<list>
  7 #include<map>
  8 #include<set>
  9 #include<deque>
 10 #include<stack>
 11 #include<bitset>
 12 #include<algorithm>
 13 #include<functional>
 14 #include<numeric>
 15 #include<utility>
 16 #include<sstream>
 17 #include<iostream>
 18 #include<iomanip>
 19 #include<cstdio>
 20 #include<cmath>
 21 #include<cstdlib>
 22 #include<cstring>
 23 #include<ctime>
 24 #define LL long long
 25 using namespace std;
 26 #define maxn 100005
 27 struct node{
 28   int is3 ;
 29   int m , r , l ;
 30   LL sum ,fsum ;
 31 }tree[maxn << 2];
 32 LL f[100];
 33 int L(int x)
 34 {
 35    return 2* x;
 36 }
 37 int R(int x)
 38 {
 39    return 2*x + 1;
 40 }
 41 void pushup(int c)
 42 {
 43    tree[c].sum = tree[L(c)].sum + tree[R(c)].sum ;
 44    tree[c].fsum = tree[L(c)].fsum + tree[R(c)].fsum ;
 45 }
 46 void  build(int c, int l , int r )
 47 {
 48      tree[c].l = l ;
 49      tree[c].r = r;
 50      tree[c].m = (l+r)/2;
 51      tree[c].is3 = 0 ;
 52      if(l ==  r)
 53      {
 54         tree[c].sum = 0 ;
 55         tree[c].fsum = 1 ;
 56         return ;
 57      }
 58      build(L(c),l,tree[c].m);
 59      build(R(c),tree[c].m+1,r);
 60      pushup(c);
 61 }
 62 LL ABS(LL x)
 63 {
 64    if(x <= 0 )
 65     return -x;
 66    else return x;
 67 }
 68 LL find( LL x )
 69 {
 70     int l = 1 , r = 85 ;
 71     while(l <= r )
 72     {
 73        int m = (l + r)/2;
 74        if(f[m] > x)
 75        {
 76            r = m - 1;
 77        }else {
 78            l = m + 1;
 79        }
 80     }
 81 //    printf("%I64d %I64d\n",f[l],f[r]);
 82     if(ABS(f[l]-x) < ABS(f[r]-x))
 83     {
 84        return f[l];
 85     }else return f[r];
 86 }
 87 void pushdown(int c)
 88 {
 89     if(tree[c].is3)
 90     {
 91        tree[L(c)].is3 = 1;
 92        tree[R(c)].is3 = 1;
 93        tree[L(c)].sum = tree[L(c)].fsum;
 94        tree[R(c)].sum = tree[R(c)].fsum;
 95        tree[c].is3 = 0 ;
 96     }
 97 }
 98 void update(int c, int k , int d)
 99 {
100      if(tree[c].l == tree[c].r&& tree[c].l == k )
101      {
102          tree[c].is3  = 0 ;
103          tree[c].sum += d;
104          tree[c].fsum = find(tree[c].sum);
105          return ;
106      }
107      pushdown(c);
108      if(k <= tree[c].m)
109          update(L(c),k,d);
110      else update(R(c),k,d);
111      pushup(c);
112 }
113 LL ans = 0 ;
114 void get(int c , int l , int r)
115 {
116        if(l <= tree[c].l && r >= tree[c].r )
117        {
118          ans += tree[c].sum ;
119          // printf("%I64d\n",ans);
120          return ;
121        }
122      pushdown(c);
123      if(l <= tree[c].m)
124          get(L(c),l,r);
125      if(r > tree[c].m)
126          get(R(c),l,r);
127 }
128 void change(int c, int l , int r)
129 {
130       if(tree[c].is3 == 1)
131           return ;
132       if(l <= tree[c].l && r >= tree[c].r)
133         {
134               tree[c].is3 = 1;
135               tree[c].sum = tree[c].fsum;
136               return ;
137         }
138      if(l <= tree[c].m)
139          change(L(c),l,r);
140      if(r > tree[c].m)
141          change(R(c),l,r);
142      pushup(c);
143 }
144 int main(){
145    int n , m;
146    f[0] = 1 ;
147    f[1] = 1;
148    for(int i = 2;i <= 86;i ++)
149        f[i] = f[i-1] + f[i-2];
150    while(scanf("%d %d",&n,&m) != EOF)
151    {
152        build(1 ,1,n);
153        int a, b, c  ;
154        for(int i = 1;i <= m;i ++)
155        {
156          int a, b, c  ;
157          scanf("%d %d %d",&a,&b,&c);
158          if(a == 1 )
159          {
160             update(1,b,c);
161          }else if(a == 2 ){
162             ans = 0 ;
163             get(1,b,c);
164             printf("%I64d\n",ans);
165          }else{
166             change(1,b,c);
167          }
168        }
169    }
170 return 0;
171 }

HDU4893 Wow! Such Sequence! 线段树,布布扣,bubuko.com

时间: 2024-10-14 08:34:01

HDU4893 Wow! Such Sequence! 线段树的相关文章

HDU4893:Wow! Such Sequence!(线段树lazy)

Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's a mysterious blackbox. After some research, Doge found that the box is maintaining a sequence an of n nu

hdu 4893 Wow! Such Sequence!(线段树)

题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 修改k的为值增加d 2 l r, 查询l到r的区间和 3 l r, 间l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

hdu 4893 (多校1007)Wow! Such Sequence!(线段树&amp;二分&amp;思维)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 352    Accepted Submission(s): 104 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

HDOJ 4893 Wow! Such Sequence! 线段树

http://acm.hdu.edu.cn/showproblem.php?pid=4893 题意:10万的区间,初始都为0,10万次操作,三种操作为单点修改,区间将每个数改成最近的斐波那契数,以及区间求和. 分析:用一个flag记录该段是否被改成斐波那契数,同时多维护一个sum1表示如果该段改成斐波那契数,区间和为多少.开始sum1为区间长度,之后在单点做了修改以后对其更新,需要的时候用其覆盖sum. 1 #include<cstdio> 2 #include<cstring>

2014多校3 Wow! Such Sequence!线段树

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 这题实在是让人纠结啊--好久不写线段树的题了,因为这几天学伸展树,然后觉得线段树小case了.没想到栽在这题上了.尼玛-- 自己把自己给搞晕了--想复杂了,都不懂得预处理一下,唉--还得怒刷几十道啊!! #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #in

HDU 4893 Wow! Such Sequence 线段树暴力

题意:n个数 初始为0,三个操作:给某个数加上d,查询区间和,把区间[l,r]中每个a[i]变为离a[i]最近的斐波那契数,n,m<=1e5. 无操作1情况下,每个点最多变化一次,每次修改logn,算上操作1 最坏情况下修改n+m次 O((n+m)logn). 对区间设个标记 线段树暴力即可. #include <bits/stdc++.h> using namespace std; typedef long long ll; const ll mod=1e9+7; const int

HDU4893:Wow! Such Sequence!(段树lazy)

Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's a mysterious blackbox. After some research, Doge found that the box is maintaining a sequence an of n nu

hdu4893 Wow! Such Sequence!

线段树结点上保存一个一般的sum值,再同时保存一个fbsum,表示这个结点表示的一段数字若为斐波那契数时的和 当进行3操作时,只用将sum = fbsum即可 其他操作照常进行,只是单点更新的时候也要先向下更新 #include <cstdio> #include <ctime> #include <cstdlib> #include <cstring> #include <queue> #include <string> #incl

hdu-4893-Wow! Such Sequence!-线段树【2014多校第三场-J】

题意:一个初始为0的数组,支持三种操作:1.向第k个数添加d,(|d| < 2^31);2.把[l, r]区间内的数字都换成与它最相近的Fibonacci数;3.询问[l, r]区间的和. 思路:初始化Fibonacci数组,longlong 类型内90个就够用了. 线段树区间查询,用lazy标记, sgt[]记录线段树各个节点的区间和, fib_num_sum[]记录与各个叶子节点当前值最接近的Fibonacci数,传递到区间fib_num_sum[]就是区间Fibonacci数的和. 操作1