hdu4893Wow! Such Sequence! (线段树)
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.
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.
1 1
2 1 1
5 4
1 1 7
1 3 17
3 2 4
2 1 5
0
22
4896
pid=4895" target="_blank">
4895
pid=4892" target="_blank">
4892
pid=4891" target="_blank">
4891
#include<iostream>
#include<stdio.h>
using namespace std;
struct tree
{
int l,r,t;
__int64 s,ts;
}ss[100010*3];
__int64 f[100]={1,1};
void setit(int l,int r,int d)
{
ss[d].l=l;
ss[d].r=r;
ss[d].t=0;//推断子结点是否须要进行3号操作
ss[d].s=0;//当前区间内的总和
ss[d].ts=1;//当前区间进行3号操作的备用和
if(l==r)
return;
setit(l,(l+r)/2,d*2);
setit((l+r)/2+1,r,d*2+1);
ss[d].ts=ss[d*2].ts+ss[d*2+1].ts;
}
__int64 doublekill(__int64 k)//查找与k最相近的Fibonacci
{
int l=0,r=77,mid;
__int64 x1,x2;
while(l<=r)
{
mid=(l+r)/2;
if(f[mid]==k)return f[mid];
if(f[mid]<k)l=mid+1;
else r=mid-1;
}
x1=f[l]-k;
if(x1<0)x1=-x1;
if(l>0)
{
x2=f[l-1]-k;
if(x2<0)x2=-x2;
if(x2<=x1)return f[l-1];
}
return f[l];
}
void insert1(__int64 e,int k,int d)//点更新
{
int l,r,mid;
l=ss[d].l;
r=ss[d].r;
mid=(l+r)/2;
if(l==r)
{
if(ss[d].t)
{
ss[d].s=ss[d].ts; ss[d].t=0;
}
ss[d].s+=e; ss[d].ts=doublekill(ss[d].s);//同一时候更新备用
return;
}
if(ss[d].t)//假设当前段须要进行3号操作
{
ss[d*2].t=1; ss[d*2+1].t=1; ss[d].t=0;
ss[d*2].s=ss[d*2].ts;
ss[d*2+1].s=ss[d*2+1].ts;
}
if(k>mid)insert1(e,k,d*2+1);
else insert1(e,k,d*2);
ss[d].s=ss[d*2].s+ss[d*2+1].s;
ss[d].ts=ss[d*2].ts+ss[d*2+1].ts;
}
void insert2(int l,int r,int d)//区间进行3号操作
{
int ll,rr,mid;
ll=ss[d].l;
rr=ss[d].r;
mid=(ll+rr)/2;
if(ll>=l&&rr<=r)
{
ss[d].s=ss[d].ts; ss[d].t=1;//子结点须要3号操作更新
return;
}
if(ss[d].t)
{
ss[d*2].t=1; ss[d*2+1].t=1; ss[d].t=0;
ss[d*2].s=ss[d*2].ts;
ss[d*2+1].s=ss[d*2+1].ts;
}
if(l<=mid)insert2(l,r,d*2);
if(r>mid)insert2(l,r,d*2+1);
ss[d].s=ss[d*2].s+ss[d*2+1].s;
}
__int64 find(int l,int r,int d)//求和
{
__int64 ll,rr,mid,s=0;
ll=ss[d].l;
rr=ss[d].r;
mid=(ll+rr)/2;
if(ll>=l&&rr<=r)
{
return ss[d].s;
}
if(ss[d].t)
{
ss[d*2].t=1; ss[d*2+1].t=1; ss[d].t=0;
ss[d*2].s=ss[d*2].ts;
ss[d*2+1].s=ss[d*2+1].ts;
}
if(l<=mid)s+=find(l,r,d*2);
if(r>mid)s+=find(l,r,d*2+1);
ss[d].s=ss[d*2].s+ss[d*2+1].s;
return s;
}
int main (void)
{
int n,m,i,j,k,l;
__int64 d;
for(i=2;i<78;i++)
f[i]=f[i-1]+f[i-2];
while(scanf("%d%d",&n,&m)>0)
{
setit(1,n,1);
while(m--)
{
scanf("%d%d",&j,&k);
if(j==1)
{
scanf("%I64d",&d);insert1(d,k,1);
}
else
{
scanf("%d",&l);
if(j==2)printf("%I64d\n",find(k,l,1));
else insert2(k,l,1);
}
}
}
return 0;
}
hdu4893Wow! Such Sequence! (线段树)的更多相关文章
- hdu-4893-Wow! Such Sequence!-线段树【2014多校第三场-J】
题意:一个初始为0的数组,支持三种操作:1.向第k个数添加d,(|d| < 2^31);2.把[l, r]区间内的数字都换成与它最相近的Fibonacci数;3.询问[l, r]区间的和. 思路 ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- Wow! Such Sequence!(线段树4893)
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 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 ...
- HDU 6047 Maximum Sequence(线段树)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=6047 题目: Maximum Sequence Time Limit: 4000/2000 MS (J ...
- Codeforces 438D The Child and Sequence - 线段树
At the children's day, the child came to Picks's house, and messed his house up. Picks was angry at ...
- hdu 5828 Rikka with Sequence 线段树
Rikka with Sequence 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5828 Description As we know, Rik ...
- 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区间上的所以数变成 ...
- hdu-5805 NanoApe Loves Sequence(线段树+概率期望)
题目链接: NanoApe Loves Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 ...
随机推荐
- C#中yield用法
yield 关键字向编译器指示它所在的方法是迭代器块.编译器生成一个类来实现迭代器块中表示的行为.在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值.这是一个返回值, ...
- java socket报文通信(三)java对象和xml格式文件的相互转换
前两节讲了socket服务端,客户端的建立以及报文的封装.今天就来讲一下java对象和xml格式文件的相互转换. 上一节中我们列举了一个报文格式,其实我们可以理解为其实就是一个字符串.但是我们不可能每 ...
- BitmapImage使用FileStream读取文件
var bitmapImage = new BitmapImage(); using (FileStream fs = new FileStream(file.FullName, FileMode.O ...
- oracle 数据库开发面试题,当时笔试的时候一个没做出来,现附原题及答案
1. ID123567810111215 表名tt,用sql找出ID列中不连续的ID,例如其中没有的4: --创建表及数据 CREATE TABLE tt(ID INTEGER); INSERT IN ...
- 武汉科技大学ACM:1004: 零起点学算法36——3n+1问题
Problem Description 任给一个正整数n,如果n为偶数,就将它变为n/2,如果为奇数,则将它乘3加1(即3n+1).不断重复这样的运算,经过有限步后,一定可以得到1 . Input 输 ...
- 请写一个C函数,判断处理器是大端存储还是小端存储,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1
[解答] int checkCPU() { { union w { int a; char b; }c; c.a=1; return (c.b==1); } } [剖析] 嵌入式系统开发者应该对Lit ...
- 极度郁闷的错误调试——ajax jquery
今天在写一个简单邮件验证的页面时,本来以为二十分钟的事情,却调试了一个半小时,简直郁闷,具体的错误如下: 在页面中,有一段如下的代码: <td colspan="3"> ...
- MySQL查询执行的基础
当希望MySQL能够以更高的性能运行查询时,最好的办法就是弄清楚MySQL是如何优化和执行查询的.一旦理解这一点,很多查询优化实际上就是遵循一些原则让优化器能够按照预想的合理的方式运行. 换句话说,是 ...
- jsp之间url传值出现中文乱码
示例: T1.jsp http://localhost:8080/test/Test.action?site=北京 T2.jsp ..... <%@ page language="ja ...
- ASP.net 中关于Session的存储信息及其它方式存储信息的讨论与总结
通过学习和实践笔者总结一下Session 的存储方式.虽然里面的理论众所周知,但是我还是想记录并整理一下.作为备忘录吧.除了ASP.net通过Web.config配置的方式,还有通过其它方式来存储的方 ...