hdu 4893 Wow! Such Sequence! 水线段树。

本文探讨了一个包含三个操作的序列处理问题:修改序列元素、查询区间和值、以及将区间内元素调整为最近的斐波那契数。通过使用线段树维护序列状态并应用懒标记技术,实现快速响应每个请求,同时利用预计算减少斐波那契数查找的时间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2019    Accepted Submission(s): 607


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 F 0 = 1,F 1 = 1,Fibonacci number Fn is defined as F n = F n - 1 + F n - 2 for n ≥ 2.

Nearest Fibonacci number of number x means the smallest Fn where |F n - 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| < 2 31, 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
 


初始每个值都是0,然后有3种操作,第一种是给一个数加上一个数,第二中操作是求l到r的和,第三种操作是把l-r的数变成最近的斐波那契数。

用线段树维护三个值,lazy来区分当前区间段要求的是斐波那契数列的和还是原始数列的和,sf,表示如果当前区间是斐波那契的sum,sy表示当前区间是原始数的sum.

斐波那契数在 long long 范围下只有93个,可推断数的范围在long long 内,因为初始值都为0.

知道这些,剩下的就是比较裸的线段树了。

/***********************************************\
 |Author: YMC
 |Created Time: 2014-7-29 15:21:06
 |File Name: 1007.cpp
 |Description: 
\***********************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define mset(l,n) memset(l,n,sizeof(l))
#define rep(i,n) for(int i=0;i<n;++i)
#define maxx(a) memset(a, 0x3f, sizeof(a))
#define zero(a) memset(a, 0, sizeof(a))
#define srep(i,n) for(int i = 1;i <= n;i ++)
#define MP make_pair
const int inf=0x3f3f3f3f ;
const double eps=1e-8 ;
const double pi=acos (-1.0);
typedef long long ll;

using namespace std;
#define maxn 100005
struct Tree{
    int l,r;
    ll sf,sy;
    int lazy;
}tree[maxn<<2];
ll fb[100] ;
ll cal(ll x){
    if(x <= 1) return 1;
    for(int i=0;i<93;++i){
        if(fb[i] > x){
            if(i >= 1){
            	//return fb[i-1];
            	if(x-fb[i-1]<=fb[i]-x) return fb[i-1];
            	else return fb[i];
            }
            else return 1;
        }
    }
    return fb[92];
}
void pushup(int rt){
    tree[rt].sf = tree[L(rt)].sf + tree[R(rt)].sf;
    tree[rt].sy = tree[L(rt)].sy + tree[R(rt)].sy;
    if(tree[rt].l == tree[rt].r) return ;
    else {
        if(tree[L(rt)].lazy == tree[R(rt)].lazy){
            tree[rt].lazy = tree[L(rt)].lazy;
        } else {
            tree[rt].lazy == -1;
        }
    }
}
void build(int l,int r,int rt){
    tree[rt].l = l;tree[rt].r = r;
    tree[rt].lazy = 1;
    if(l == r){
        tree[rt].sf = 1;
        tree[rt].sy = 0;
        return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,L(rt));
    build(mid+1,r,R(rt));
    pushup(rt);
}
void add(int a,ll b,int rt){
    int l = tree[rt].l,r = tree[rt].r;
    if(l == r){
        if(tree[rt].lazy == 1){
            tree[rt].sy += b;
            tree[rt].sf = cal(tree[rt].sy);
        } else {
            tree[rt].sy = tree[rt].sf + b;
            tree[rt].sf = cal(tree[rt].sy);
            tree[rt].lazy = 1;
        }
        return ;
    }
    if(tree[rt].lazy != -1) {
    	tree[L(rt)].lazy = tree[R(rt)].lazy = tree[rt].lazy;
    	tree[rt].lazy = -1;
    }
    int mid = (tree[rt].l+tree[rt].r)>>1;
    if(a <= mid) add(a,b,L(rt));
    else add(a,b,R(rt));
    pushup(rt);
}
void update(int x,int y,int rt){
    int l = tree[rt].l,r = tree[rt].r;
    if(l >= x && y >= r){
        tree[rt].lazy = 2;
        return ;
    }
    if(tree[rt].lazy != -1){
        tree[L(rt)].lazy = tree[R(rt)].lazy = tree[rt].lazy;
        tree[rt].lazy = -1;
    }
    int mid = (l+r)>>1;
    if(y <= mid) update(x,y,L(rt));
    else if(x >= mid+1) update(x,y,R(rt));
    else {
        update(x,mid,L(rt));
        update(mid+1,y,R(rt));
    }
    pushup(rt);
}
ll query(int x,int y,int rt){
    ll ans = 0;
    int l = tree[rt].l,r = tree[rt].r;
    if(l >= x && y >= r){
        if(tree[rt].lazy == 1) return tree[rt].sy;
        if(tree[rt].lazy == 2) return tree[rt].sf;
    }
    if(tree[rt].lazy!=-1){
    	tree[L(rt)].lazy = tree[R(rt)].lazy = tree[rt].lazy;
    	tree[rt].lazy = -1;
	}
    int mid = (l + r )>>1;
    if(mid >= y) ans += query(x,y,L(rt));
    else if(mid + 1 <= x) ans += query(x,y,R(rt));
    else {
        ans += query(x,mid,L(rt));
        ans += query(mid+1,y,R(rt));
    }
    return ans;
}
void pre(){
    fb[1] = 1;fb[2] = 1;
    for(int i=3;i<93;++i){
        fb[i] = fb[i-1] + fb[i-2];
    }
}
int main() {
	//freopen("input.txt","r",stdin); 
    pre();
    int n,m;
    int a,b;
    ll c;
    while(~scanf("%d%d",&n,&m)){
        build(1,n,1);
        while(m--){
            scanf("%d%d%lld",&a,&b,&c);
            if(a == 1){
                add(b,c,1);
            } else if (a == 2){
                printf("%I64d\n",query(b,c,1));
            } else {
                update(b,c,1);
            }
        }
    }
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值