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
2 1 1
5 4
1 1 7
1 3 17
3 2 4
2 1 5
 
Sample Output
0
22
 
Author
Fudan University
 
Source

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

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

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/8/4 14:58:15
File Name :11.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
ll fib[100];
struct node{
ll l,r;
ll sum,flag;
}a[800300];
ll m,n;
void pushup(ll t){
if(a[t].l==a[t].r)return;
a[t].sum=a[2*t].sum+a[2*t+1].sum;
a[t].flag=a[2*t].flag&a[2*t+1].flag;
}
void build(ll t,ll l,ll r){
// cout<<"hhh "<<l<<" "<<r<<endl;
a[t].l=l;
a[t].r=r;
a[t].sum=a[t].flag=0;
if(l==r)return;
ll mid=(l+r)/2;
build(2*t,l,mid);
build(2*t+1,mid+1,r);
pushup(t);
}
void update1(ll t,ll p,ll val){
if(a[t].l==a[t].r){
a[t].sum+=val;
a[t].flag=0;
return;
}
ll mid=(a[t].l+a[t].r)/2;
if(p<=mid)update1(2*t,p,val);
else update1(2*t+1,p,val);
pushup(t);
}
ll Find(ll x){
if(x <= 1)return 1;
int l = 1, r = 80, id = 80;
while(l <= r){
int mid = l+r>>1;
if(fib[mid] > x) id = mid, r = mid-1;
else l = mid+1;
}
if(x-fib[id-1] <= fib[id]-x) return fib[id-1];
return fib[id];
} void update2(ll t,ll l,ll r){
if(a[t].flag)return;
if(a[t].l==a[t].r){
a[t].sum=Find(a[t].sum);
a[t].flag=1;
//cout<<"ddd "<<l<<" "<<r<<" "<<a[t].sum<<endl;
return;
}
ll mid=(a[t].l+a[t].r)/2;
if(l<=mid)update2(2*t,l,r);
if(r>mid)update2(2*t+1,l,r);
pushup(t);
} ll getsum(ll t,ll l,ll r){
if(a[t].l>=l&&a[t].r<=r)return a[t].sum;
ll mid=(a[t].l+a[t].r)/2;
ll ans=0;
if(l<=mid)ans+=getsum(2*t,l,r);
if(r> mid) ans+=getsum(2*t+1,l,r);
return ans;
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
fib[0]=1;fib[1]=1;
for(ll i=2;i<=90;i++)fib[i]=fib[i-1]+fib[i-2];
while(~scanf("%I64d%I64d",&n,&m)){
// cout<<"ddd "<<endl;
build(1,1,n);
//cout<<"ppp "<<endl;
while(m--){
ll l,r,op;
scanf("%I64d%I64d%I64d",&op,&l,&r);
if(op==1){
update1(1,l,r);
// cout<<"han 1"<<endl;
}
if(op==2){
printf("%I64d\n",getsum(1,l,r));
// cout<<"han 2"<<endl;
}
if(op==3){
update2(1,l,r);
// cout<<"han 3"<<endl;
}
}
}
return 0;
}
/*
5 10
2 1 5
3 1 5
2 1 5
1 1 10
2 1 5
3 1 5
2 1 5 4 5
1 1 3
2 1 2
3 2 3
1 2 1
2 1 4
*/

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. 摘要提取算法——本质上就是pagerank,选择rank最高的句子作为摘要,如果结合word2vec应该有非常好的效果

    最近需要做一些文本摘要的东西,选取了TextRank(论文参见<TextRank: Bringing Order into Texts>)作为对比方案,该方案可以很方便的使用Python相 ...

  2. 获取id 获取当前点击元素节点的任意 属性

    <a id="haveproces" onclick="fnProces(event)" dataid="{{x.id}}" clas ...

  3. 南海区行政审批管理系统接口规范v0.3(规划) 2.业务申报API 2.1.businessApply【业务申报】

    {"v_interface":"2015987654327","c_project":"NH09A102"," ...

  4. Linux Shell Scripting Cookbook 读书笔记 6

    wget,curl, tar, rsync wget ftp://example.com/somefile.img -t 5 -O download.img -o log -t表示重试的次数 -O指定 ...

  5. SwiftUI 官方教程(四)

    SwiftUI 官方教程(四) 4. 自定义 Image View 搞定名称和位置 view 后,我们来给地标添加图片. 这不需要添加很多代码,只需要创建一个自定义 view,然后给图片加上遮罩.边框 ...

  6. 【转】什么是P问题、NP问题和NPC问题

    原文链接:http://www.matrix67.com/blog/archives/105,感谢Matrix67,看完这篇文章终于把这个几个概念弄明白了!! 这或许是众多OIer最大的误区之一.   ...

  7. HBase编程 API入门系列之delete.deleteColumn和delete.deleteColumns区别(客户端而言)(4)

    心得,写在前面的话,也许,中间会要多次执行,连接超时,多试试就好了. delete.deleteColumn和delete.deleteColumns区别是: deleteColumn是删除某一个列簇 ...

  8. 基于 Web 的 Go 语言 IDE - Wide 1.4.0 发布!

    Wide 是什么 Wide 是一个基于 Web 的 Go 语言团队 IDE . 在线开发:打开浏览器就可以进行开发.全快捷键 智能提示:代码自动完成.查看表达式.编译反馈. Lint 实时运行:极速编 ...

  9. c++ windows与linux通信中文乱码问题解决方法

    在linux中默认编码方式是UTF-8,在Windows下默认编码方式时GB2312.因此,在Windows和Linux进行通信的时候,如果没有进行转码则会出现乱码问题.因此,需要进行UTF-8和GB ...

  10. Junit使用第二弹

    实例总结 1. 参数化测试 有时一个测试方法,不同的参数值会产生不同的结果,那么我们为了测试全面,会把多个参数值都写出来并一一断言测试,这样有时难免费时费力,这是我们便可以采用参数化测试来解决这个问题 ...