Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2512    Accepted Submission(s): 751

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 numbers internally, initially all numbers are zero, and there are THREE "operations":



1.Add d to the k-th number of the sequence.

2.Query the sum of ai where l ≤ i ≤ r.

3.Change ai to the nearest Fibonacci number, where l ≤ i ≤ r.

4.Play sound "Chee-rio!", a bit useless.



Let F0 = 1,F1 = 1,Fibonacci number Fn is defined as Fn = Fn - 1 + Fn - 2 for n ≥ 2.



Nearest Fibonacci number of number x means the smallest Fn where |Fn - x| is also smallest.



Doge doesn't believe the machine could respond each request in less than 10ms. Help Doge figure out the reason.
 
Input
Input contains several test cases, please process till EOF.

For each test case, there will be one line containing two integers n, m.

Next m lines, each line indicates a query:



1 k d - "add"

2 l r - "query sum"

3 l r - "change to nearest Fibonacci"



1 ≤ n ≤ 100000, 1 ≤ m ≤ 100000, |d| < 231, all queries will be valid.
 
Output
For each Type 2 ("query sum") operation, output one line containing an integer represent the answer of this query.
 
Sample Input
  1. 1 1
  2. 2 1 1
  3. 5 4
  4. 1 1 7
  5. 1 3 17
  6. 3 2 4
  7. 2 1 5
 
Sample Output
  1. 0
  2. 22
 
Author
Fudan University
 
Source

题意不说了,非常easy的线段树题目了。就是打标记改点求段,改动段时因为极限次数不多。直接暴力更新到点,

好久没写线段树了,错了好多次,写的好傻逼。

代码:

  1. /* ***********************************************
  2. Author :rabbit
  3. Created Time :2014/8/4 14:58:15
  4. File Name :11.cpp
  5. ************************************************ */
  6. #pragma comment(linker, "/STACK:102400000,102400000")
  7. #include <stdio.h>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include <sstream>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <limits.h>
  14. #include <string>
  15. #include <time.h>
  16. #include <math.h>
  17. #include <queue>
  18. #include <stack>
  19. #include <set>
  20. #include <map>
  21. using namespace std;
  22. #define INF 0x3f3f3f3f
  23. #define eps 1e-8
  24. #define pi acos(-1.0)
  25. typedef long long ll;
  26. ll fib[100];
  27. struct node{
  28. ll l,r;
  29. ll sum,flag;
  30. }a[800300];
  31. ll m,n;
  32. void pushup(ll t){
  33. if(a[t].l==a[t].r)return;
  34. a[t].sum=a[2*t].sum+a[2*t+1].sum;
  35. a[t].flag=a[2*t].flag&a[2*t+1].flag;
  36. }
  37. void build(ll t,ll l,ll r){
  38. // cout<<"hhh "<<l<<" "<<r<<endl;
  39. a[t].l=l;
  40. a[t].r=r;
  41. a[t].sum=a[t].flag=0;
  42. if(l==r)return;
  43. ll mid=(l+r)/2;
  44. build(2*t,l,mid);
  45. build(2*t+1,mid+1,r);
  46. pushup(t);
  47. }
  48. void update1(ll t,ll p,ll val){
  49. if(a[t].l==a[t].r){
  50. a[t].sum+=val;
  51. a[t].flag=0;
  52. return;
  53. }
  54. ll mid=(a[t].l+a[t].r)/2;
  55. if(p<=mid)update1(2*t,p,val);
  56. else update1(2*t+1,p,val);
  57. pushup(t);
  58. }
  59. ll Find(ll x){
  60. if(x <= 1)return 1;
  61. int l = 1, r = 80, id = 80;
  62. while(l <= r){
  63. int mid = l+r>>1;
  64. if(fib[mid] > x) id = mid, r = mid-1;
  65. else l = mid+1;
  66. }
  67. if(x-fib[id-1] <= fib[id]-x) return fib[id-1];
  68. return fib[id];
  69. }
  70.  
  71. void update2(ll t,ll l,ll r){
  72. if(a[t].flag)return;
  73. if(a[t].l==a[t].r){
  74. a[t].sum=Find(a[t].sum);
  75. a[t].flag=1;
  76. //cout<<"ddd "<<l<<" "<<r<<" "<<a[t].sum<<endl;
  77. return;
  78. }
  79. ll mid=(a[t].l+a[t].r)/2;
  80. if(l<=mid)update2(2*t,l,r);
  81. if(r>mid)update2(2*t+1,l,r);
  82. pushup(t);
  83. }
  84.  
  85. ll getsum(ll t,ll l,ll r){
  86. if(a[t].l>=l&&a[t].r<=r)return a[t].sum;
  87. ll mid=(a[t].l+a[t].r)/2;
  88. ll ans=0;
  89. if(l<=mid)ans+=getsum(2*t,l,r);
  90. if(r> mid) ans+=getsum(2*t+1,l,r);
  91. return ans;
  92. }
  93. int main()
  94. {
  95. // freopen("data.in","r",stdin);
  96. // freopen("data.out","w",stdout);
  97. fib[0]=1;fib[1]=1;
  98. for(ll i=2;i<=90;i++)fib[i]=fib[i-1]+fib[i-2];
  99. while(~scanf("%I64d%I64d",&n,&m)){
  100. // cout<<"ddd "<<endl;
  101. build(1,1,n);
  102. //cout<<"ppp "<<endl;
  103. while(m--){
  104. ll l,r,op;
  105. scanf("%I64d%I64d%I64d",&op,&l,&r);
  106. if(op==1){
  107. update1(1,l,r);
  108. // cout<<"han 1"<<endl;
  109. }
  110. if(op==2){
  111. printf("%I64d\n",getsum(1,l,r));
  112. // cout<<"han 2"<<endl;
  113. }
  114. if(op==3){
  115. update2(1,l,r);
  116. // cout<<"han 3"<<endl;
  117. }
  118. }
  119. }
  120. return 0;
  121. }
  122. /*
  123. 5 10
  124. 2 1 5
  125. 3 1 5
  126. 2 1 5
  127. 1 1 10
  128. 2 1 5
  129. 3 1 5
  130. 2 1 5
  131.  
  132. 4 5
  133. 1 1 3
  134. 2 1 2
  135. 3 2 3
  136. 1 2 1
  137. 2 1 4
  138. */

HDU 4893 线段树裸题的更多相关文章

  1. POJ 3468 线段树裸题

    这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...

  2. BZOJ1067&P2471 [SCOI2007]降雨量[线段树裸题+细节注意]

    dlntqlwsl 很裸的一道线段树题,被硬生生刷成了紫题..可能因为细节问题吧,我也栽了一次WA50分.不过这个隐藏条件真的对本菜鸡来说不易发现啊. 未知的年份连续的就看成一个就好了,把年份都离散化 ...

  3. CPU监控 线段树裸题

    LINK:bzoj3064 此题甚好码了20min停下来思考的时候才发现不对的地方有点坑... 还真不好写来着 可这的确是线段树的裸题...我觉得我写应该没有什么大问题 不过思路非常的紊乱 如果是自己 ...

  4. HDU 4893 线段树的 点更新 区间求和

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. 【LOJ6029】「雅礼集训 2017 Day1」市场(线段树裸题)

    点此看题面 大致题意: 维护序列,支持区间加法,区间除法(向下取整),区间求\(min\)和区间求和. 线段树维护区间除法 区间加法.区间求\(min\)和区间求和都是线段树基本操作,因此略过不提. ...

  6. HDU 4893 线段树

    比赛时太大意,斐波拉契数列开小了. 题目大意:1个序列,3种操作,改变序列某个数大小,将序列中连续的一段每个数都变成其最近的斐波拉契数,以及查询序列中某一段的数之和. 解题思路:维护add[]数组表示 ...

  7. HDU1166 线段树裸题 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. hdu 1754 线段树模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 #include <cstdio> #include <cmath> # ...

  9. [HDU1754]I Hate It线段树裸题

    http://acm.hdu.edu.cn/showproblem.php?pid=1754 解题关键:刚开始死活超时,最后发现竟然是ch,和t1.t2每次循环都定义的锅,以后养成建全局变量的习惯. ...

随机推荐

  1. yolo源码解析(3):进行简单跳帧

    视频检测命令  ./darknet detector demo cfg/coco.data cfg/yolov3-tiny.cfg yolov3-tiny.weights ../../dataset/ ...

  2. 微阅读,不依赖playground,打包成H5版本--案例学习

    微阅读,不依赖playground,打包成H5版本 https://github.com/vczero/weex-yy-h5

  3. 关于api接口文档RAP和swagger

    前言: 在之前的项目中用了将近一年的RAP,RAP是由阿里开源出来的,非常好用.github地址:https://github.com/thx/RAP. 当初在用此工具时,项目成员需要在接口文档在所改 ...

  4. How to solve a login problem of SQL Server?

    Sometimes when we attempting to login the SQL Server 20xx Management Studio, when we type in the cor ...

  5. python3遍历选中文件夹下的文件【GUI编程】

    功能介绍 如标题 使用截图 1.第一步:运行python程序 2.第二步:点击按钮选中文件夹 3.运行结束: 1. 显示选中文件夹路径 2. 遍历打印文件夹内文件 代码 import os #程序功能 ...

  6. Scala 大数据 常用算法收集

    一:IP转数字,用于比大小,用在求IP段范围中 def ip2Long(ip: String): Long = { val fragments = ip.split("[.]") ...

  7. 用css修改HTML5 input placeholder颜色

    使用CSS修改HTML5 input placeholder颜色 本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和 ...

  8. STL:使用string、vector、complex和limits

    (有少量修改!)使用到了STL的算法库: #include<algorithm> #include<vector> //属于STL库 模板库 写库的人为了和标准C和C++库区分 ...

  9. getopt函数

    getopt -- 解析命令的可选项   [说明]getopt只是一个简单的解析命令可选项的函数,只能进行简单的格式命令解析,格式如下:   1.形如:cmd [-a][-b] //对短选项的解析: ...

  10. C# 遍历对象下的 属性

    foreach (System.Reflection.PropertyInfo p in users.GetType().GetProperties()) { var xx = p.Name; var ...