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


    非常典型的线段树,注意就是一个数字假设变成了斐波那契数字之后假设在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); } } }



  • 相关阅读:
    Vue-基础(四)
    Vue-基础(三)
    Vue-基础(一)
    Vue-基础(二)
    CSS-初始化模板2(common.css)
    CSS-初始化模板1(normalize.css)
    CSS预处理器-Less
    MySQL视窗函数row_number(), rank(), denser_rank()
    LeetCode第4题:寻找两个有序数组的中位数
    无重复字符的最长子串
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/6905993.html
Copyright © 2020-2023  润新知