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

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 }

View Code

转载于:https://www.cnblogs.com/zyue/p/3876613.html

HDU4893 Wow! Such Sequence! 线段树相关推荐

  1. codeforces CF438D The Child and Sequence 线段树

    $ \Rightarrow $ 戳我进CF原题 D. The Child and Sequence time limit per test: 4 seconds memory limit per te ...

  2. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  3. 2019ICPC(南京) - Greedy Sequence(线段树)

    题目链接:点击查看 题目大意:题意不明(英语渣),网上的简洁版转化: 对于每个i,所在下标p[i],在[p[i]−k,p[i]+k]中找到小于i的最大数x,然后ans[i]=ans[x]+1即可. 题 ...

  4. 【bzoj4355】Play with sequence 线段树区间最值操作

    题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...

  5. 【 bzoj 4355 】 Play with sequence - 线段树乱搞

    先讲个故事... 据说某一天,claris扔了一道题到某群里面然后引起了不大的讨论~然后好学向上的whx同学发现了这题...聪明的whx想了很久...然后!whx发现看不懂claris给的暴力的证明. ...

  6. UESTC-1546___Bracket Sequence —— 线段树 + 括号序列

    题目链接:点我啊╭(╯^╰)╮ 题目大意: 给出一个括号序列      setsetset -- 将给定区间全部转化为指定括号      reversereversereverse -- 将给定区间的 ...

  7. 【BZOJ4355】Play with sequence 线段树

    [BZOJ4355]Play with sequence Description 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a ...

  8. HDU 5828 Rikka with Sequence (线段树+剪枝优化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5828 给你n个数,三种操作.操作1是将l到r之间的数都加上x:操作2是将l到r之间的数都开方:操作3是 ...

  9. Gorgeous Sequence线段树区间跟新

    http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1018&ojid=0&cid=12578&hide= ...

最新文章

  1. win2008在组件服务中未找到office组件服务
  2. Visual Studio 2010 Ultimate测试体系结构
  3. 安装itunes需要管理员身份_Windows 10 在microsoft store 微软商店里安装的itunes如何更改备份位置...
  4. 动态规划算法--矩形最小路径和
  5. 记得重用layout
  6. 2017年10月2日日志
  7. 国外开发者怒怼:“GitHub 变得不再有趣了!”
  8. [转]GeoHash核心原理解析
  9. delphi编程来记录QQ的聊天记录
  10. Ubuntu下安装Chrome浏览器的方法
  11. Atitit.异步编程的发展历史 1.1. TAP  async/await
  12. Spring Cloud Alibaba Sentinel之服务熔断篇
  13. StringUtil和StringUtils的区别
  14. 网站数据分析指标体系
  15. 如何利用Pix2Pix将黑白图片自动变成彩色图片
  16. 【vue】vue中element表格导出为excel表格
  17. ROS重大功能,无线WISP和桥接
  18. 大数据成神之路?那么你一定要看这里
  19. iOS AirDrop
  20. 【云原生 | Kubernetes 系列】---Skywalking部署和监控

热门文章

  1. IOS获取系统相簿里的照片
  2. 微信小程序实现消息推送(调用小程序推送模板接口) 完整示例
  3. 软件Beta测试悄悄变味
  4. springboot的yml不是小叶子处理
  5. 菲律宾Asia Leaders奖项揭晓,中兴5G拿下“年度电信供应商”大奖
  6. 网站个性化 - 添加人形时钟
  7. 美国五大高频交易商访谈录
  8. 王者荣耀安卓微信有什么服务器,王者荣耀安卓微信151区弦月斩
  9. AS升级到3.0后遇到的问题,Error:Resource shrinker cannot be used for libraries.
  10. 单片机多功能电子琴课设_基于51单片机的多功能电子琴设计.doc