HDU4893-Wow! Such Sequence!(线段树)

Wow! Such Sequence!

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


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
 
题意:n个数,起始都为0,有m组操作:1.将位置为x的数加上d    2.查询[L,R] 的区间和,  3.将[L,R]区间的所有数变成离自己最近的斐波那契数。
思路:容易得出斐波那契数很少,那么这题就非常像HDU4027了。。。更新的时候如果发现整个区间都是斐波那契数,那么就不更新,否则单点更新。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const ll inf = 1e16;
const int maxn = 100000+10;
vector<ll>fib;
struct node{
    int lson,rson;
    ll sum;
    bool isFib;
    int mid(){
        return (lson+rson)>>1;
    }
}tree[maxn*4];

void pushUP(int rt){
    tree[rt].sum = tree[rt<<1].sum+tree[rt<<1|1].sum;
    tree[rt].isFib = tree[rt<<1].isFib&&tree[rt<<1|1].isFib;
}
void build(int L,int R,int rt){
    tree[rt].lson = L;
    tree[rt].rson = R;
    if(L==R){
        tree[rt].sum = 0;
        tree[rt].isFib = false;
        return;
    }
    int mid = tree[rt].mid();
    build(L,mid,rt<<1);
    build(mid+1,R,rt<<1|1);
    pushUP(rt);
}

void getFib(){
    fib.push_back(1);
    fib.push_back(1);
    while(true){
        ll d = fib[fib.size()-1]+fib[fib.size()-2];
        if(d>inf) break;
        fib.push_back(d);
    }
}
ll serFib(ll x){
    int idx = lower_bound(fib.begin(),fib.end(),x)-fib.begin();
    if(fib[idx]==x) return x;
    if(idx > 0&&abs(fib[idx]-x) >= abs(fib[idx-1]-x))   idx--;
    return fib[idx];
}
void add(int pos,int l,int r,int rt,int num){
    if(l==r){
        tree[rt].sum += num;
        tree[rt].isFib = false;
        return;
    }
    int mid = tree[rt].mid();
    if(mid >= pos){
        add(pos,l,mid,rt<<1,num);
    }else{
        add(pos,mid+1,r,rt<<1|1,num);
    }
    pushUP(rt);
}
ll query(int L,int R,int l,int r,int rt){
    if(L <= l && R >= r) return tree[rt].sum;
    ll ret = 0;
    int mid = (l+r)>>1;
    if(mid >= L){
        ret += query(L,R,l,mid,rt<<1);
    }
    if(mid < R){
        ret += query(L,R,mid+1,r,rt<<1|1);
    }
    return ret;
}
void setFib(int L,int R,int l,int r,int rt){
    if(tree[rt].isFib)  return;
    if(l==r){
        tree[rt].sum = serFib(tree[rt].sum);
        tree[rt].isFib = true;
        return;
    }
    int mid = tree[rt].mid();
    if(L <= mid){
        setFib(L,R,l,mid,rt<<1);
    }
    if(R > mid){
        setFib(L,R,mid+1,r,rt<<1|1);
    }
    pushUP(rt);
}
int n,m;
int main(){
    getFib();
    while(~scanf("%d%d",&n,&m)){
        int a,b,c;
        build(1,n,1);
        while(m--){
            scanf("%d%d%d",&a,&b,&c);
            if(a==1){
                add(b,1,n,1,c);
            }
            else if(a==2){
                printf("%I64d\n",query(b,c,1,n,1));
            }
            else{
                setFib(b,c,1,n,1);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值