码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4893 Wow! Such Sequence!(线段树)

时间:2014-07-30 10:07:43      阅读:438      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   java   os   strong   io   

很典型的线段树,注意就是一个数字如果变成了斐波那契数字之后如果在change的话,它是不会反生改变的,因为最近的斐波那契数字就是它本身了啊。

用一个flag表示这一段上的数字是否change过,如果flag == 1已经change过,就不会在向下更新。否则的话就进行更新,最后会到达一个节点,更新这个节点。这里用暴力更新就行,找到最近的斐波那契数字。

Add就是一个点更新,sum求和就是一个区间总和的查询。

Wow! Such Sequence!

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


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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#define clear(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
#define clearall(A, X) memset(A, X, sizeof(A))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
#define LL __int64

using namespace std;

const int maxn = 101000;

int vis[maxn];
LL Fi[maxn];

struct node
{
    int l, r;
    int flag;
    LL sum;
} f[4*maxn];

void Del()
{
    Fi[1] = 1;
    Fi[2] = 1;
    for(int i = 3; i <= 79; i++)
        Fi[i] = Fi[i-1]+Fi[i-2];
}

void Bulid(int l, int r, int site)
{
    f[site].l = l;
    f[site].r = r;
    f[site].flag = 0;
    f[site].sum = 0;
    if(l == r) return ;
    int mid = (l+r)/2;
    Bulid(l, mid, site<<1);
    Bulid(mid+1, r, site<<1|1);
    f[site].sum = f[site<<1].sum+f[site<<1|1].sum;
}

void Add(int k, int d, int site)
{
    if(f[site].l == f[site].r)
    {
        f[site].sum += d;
        f[site].flag = 0;
        return;
    }
    int mid = (f[site].l+f[site].r)/2;
    if(mid >= k) Add(k, d, site<<1);
    if(mid < k) Add(k, d, site<<1|1);

    f[site].sum = f[site<<1].sum+f[site<<1|1].sum;
    f[site].flag = 0;
}

LL Query_Sum(int l, int r, int site)
{
    if(f[site].l == l && f[site].r == r)
        return f[site].sum;

    int mid = (f[site].l+f[site].r)/2;
    if(r <= mid)
        return Query_Sum(l, r, site<<1);
    if(l > mid)
        return Query_Sum(l, r, site<<1|1);
    return Query_Sum(l, mid, site<<1)+Query_Sum(mid+1, r, site<<1|1);
}

void Change(int l, int r, int site)
{
    if(f[site].l == l && f[site].r == r )
    {
        if(f[site].flag) return ;
        if(f[site].l == f[site].r )
        {
            int i;
            for(i = 1; i <= 79; i++)
                if(f[site].sum <= Fi[i]) break;
            LL x1 = abs(f[site].sum-Fi[i-1]);
            LL x2 = abs(Fi[i]-f[site].sum);
            if(x1 <= x2 && i-1 > 0) f[site].sum = Fi[i-1];
            else f[site].sum = Fi[i];

            f[site].flag = 1;
            return;
        }

        int mid = (l+r)/2;
        Change(l, mid, site<<1);
        Change(mid+1, r, site<<1|1);

        f[site].sum = f[site<<1].sum+f[site<<1|1].sum;
        if(f[site<<1].flag&&f[site<<1|1].flag)
            f[site].flag = 1;
        return;
    }
    int mid = (f[site].l+f[site].r)/2;
    if(r <= mid)
        Change(l, r, site<<1);
    else if(l > mid)
        Change(l, r, site<<1|1);
    else
    {
        Change(l, mid, site<<1);
        Change(mid+1, r, site<<1|1);
    }

    f[site].sum = f[site<<1].sum+f[site<<1|1].sum;
    if(f[site<<1].flag && f[site<<1|1].flag)
        f[site].flag = 1;
}

int main()
{
    int n, m;
    Del();
    while(cin >>n>>m)
    {
        int x;
        int l, r;
        Bulid(1, n, 1);
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %d",&x, &l, &r);
            if(x == 1)
                Add(l, r, 1);
            else if(x == 2)
                cout<<Query_Sum(l, r, 1)<<endl;
            else if(x == 3)
                Change(l, r, 1);
        }
    }
}


HDU 4893 Wow! Such Sequence!(线段树),布布扣,bubuko.com

HDU 4893 Wow! Such Sequence!(线段树)

标签:des   style   http   color   java   os   strong   io   

原文地址:http://blog.csdn.net/xu12110501127/article/details/38292381

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!