【HDU4893Wow! Such Sequence!】线段树-单点+区间更新、区间查询(再次体会两种写法)

Wow! Such Sequence!

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


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


#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
// #pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 100005
#define lson (id<<1)
#define rson (id<<1 | 1)
struct Node
{
    int l, r;
    int mid;
    int id;
    long long sum;
    long long sumf;
    bool mark;
    void get()
    {
        sum = sumf;
        mark = 1;
    }
};
long long y[N];
Node tree[N << 2];
long long fabb[100];
int fabbmax;
int n, m, op;
long long l, r;
void init()
{
    memset(fabb, 0, sizeof(fabb));
    fabb[0] = 1;
    fabb[1] = 1;
    int i;
    for ( i = 2; ; i++)
    {
        fabb[i] = fabb[i - 1] + fabb[i - 2];
        if (fabb[i] < 0) break;
    }
    fabbmax = i - 1;
}
LL getl(LL x)
{
    if (x <= 0) return 1;
    for (int i = 1; i < fabbmax; i++)
    {
        if (fabb[i] == x) return fabb[i];
        if (fabb[i] <= x && fabb[i + 1] >= x)
        {
            if (x - fabb[i] <= fabb[i + 1] - x)
                return fabb[i];
            else return fabb[i + 1];
        }
    }
    return fabbmax;
}
void build(int id, int l, int r)
{
    Node &t = tree[id];
    t.l = l; t.r = r; t.mid = (l + r) >> 1;
    t.id = id; t.mark = 0;
    t.sum = t.sumf = 0;
    if (l == r)
    {
        t.sumf = 1;
        return;
    }
    build(lson, l, t.mid);
    build(rson, t.mid + 1, r);
    t.sumf = tree[lson].sumf + tree[rson].sumf;
}
void pushdown(int id)
{
    if (tree[id].mark)
    {
        tree[lson].get();
        tree[rson].get();
        tree[id].mark = 0;
    }
}
long long query(int id, int l, int r)
{
    Node &t = tree[id];
    if (l == t.l && r == t.r)
        return t.sum;
    pushdown(id);
    long long sum1 = 0, sum2 = 0;
    if (l > t.mid)
        sum1 = query(rson, l, r);
    else if (r <= t.mid)
        sum2 = query(lson, l, r);
    else
    {
        sum1 = query(lson, l, t.mid);
        sum2 = query(rson, t.mid + 1, r);
    }
    return sum1 + sum2;
}
void updatey(int id, int x, long long delta)
{
    Node &t = tree[id];
    if (t.l == t.r)
    {
        t.sum += delta;
        t.sumf = getl(t.sum);
        return ;
    }
    pushdown(id);
    if (x <= t.mid)//
        updatey(lson, x, delta);
    else
        updatey(rson, x, delta);
    t.sum = tree[lson].sum + tree[rson].sum;
    t.sumf = tree[lson].sumf + tree[rson].sumf;
}
void updatef(int id, int l, int r)
{
    Node &t = tree[id];
    if (l == t.l && t.r == r)
    {
        t.get();
        return;
    }
    pushdown(id);
    if (l > t.mid)
        updatef(rson, l, r);
    else if (r <= t.mid)
        updatef(lson, l, r);
    else
    {
        updatef(lson, l, t.mid);
        updatef(rson, t.mid + 1, r);
    }
    t.sum = tree[lson].sum + tree[rson].sum;
    t.sumf = tree[lson].sumf + tree[rson].sumf;
}
long long query2(int id, int l, int r)
{
    Node &t = tree[id];
    if (l <= t.l && r >= t.r)
        return t.sum;
    pushdown(id);
    long long sum1 = 0, sum2 = 0;
    if (l <= t.mid)
        sum1 = query2(lson, l, r);
    if (r > t.mid)
        sum2 = query2(rson, l, r);
    return sum1 + sum2;
}
void updatef2(int id, int l, int r)
{
    Node &t = tree[id];
    if (l<=t.l&&t.r<=r)
    {
        t.get();
        return;
    }
    pushdown(id);
    if (l <= t.mid)
        updatef2(lson, l, r);
    if (r > t.mid)
        updatef2(rson, l, r);
    t.sum = tree[lson].sum + tree[rson].sum;
    t.sumf = tree[lson].sumf + tree[rson].sumf;
}
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    init();
    long long l, r;
    while (scanf("%d%d", &n, &m) + 1)
    {
        build(1, 1, n);
        for (int i = 0; i < m; i++)
        {
            scanf("%d%I64d%I64d", &op, &l, &r);
            if (op == 1)updatey(1, l, r);
            if (op == 2)
               printf("%I64d\n", query2(1, l, r) );
            if (op == 3)updatef2(1, l, r);
        }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值