首页 > 其他分享 >HDU 4893(线段树区间更新)

HDU 4893(线段树区间更新)

时间:2023-08-15 17:36:22浏览次数:47  
标签:rt HDU 4893 线段 fsum tree fib int sum


Wow! Such Sequence!



Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Total Submission(s): 3856    Accepted Submission(s): 1085




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


 


#include <iostream>
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <string.h>
using namespace std;
#define  ll __int64
const int maxn=100000+10;
ll fib[100],ans=0;
void fun()
{
    fib[0]=1;
    fib[1]=1,fib[2]=1;
    for(ll i=3;i<=90;i++)
        fib[i]=fib[i-1]+fib[i-2];
}
struct Node
{
    int l,r,flag;// flag=1  说明是fib标记
    ll sum,fsum;
    int mid()
    {
        return (l+r)/2;
    }
}tree[maxn*6];
ll Find(int l,int r,__int64 seach)
{
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(fib[mid]<=seach)
            l=mid+1;
        else
            r=mid-1;
    }
    if(fib[l]-seach>=seach-fib[r])
        return fib[r];
    else
        return fib[l];
}
void Buildtree(int rt,int l,int r)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].flag=tree[rt].sum=0;
    tree[rt].fsum=0;
    if(l!=r)
    {
        Buildtree(2*rt,l,(l+r)/2);
        Buildtree(2*rt+1,(l+r)/2+1,r);
        tree[rt].fsum=tree[2*rt].fsum+tree[2*rt+1].fsum;
        tree[rt].sum=tree[2*rt].sum+tree[rt*2+1].sum;
    }
    else
        tree[rt].fsum=1;
}
void Pushdown(int rt)
{
    tree[2*rt].sum=tree[2*rt].fsum;
    tree[2*rt+1].sum=tree[2*rt+1].fsum;
    tree[2*rt].flag=tree[2*rt+1].flag=1;
    tree[rt].flag=0;
}
void Pushup(int rt)
{
    tree[rt].sum=tree[2*rt].sum+tree[2*rt+1].sum;
    tree[rt].fsum=tree[2*rt].fsum+tree[2*rt+1].fsum;
}
void Update(int rt,int k,int d)
{
    if(tree[rt].flag) Pushdown(rt);
    if(tree[rt].l==tree[rt].r&&tree[rt].l==k)
    {
        tree[rt].sum+=d;
        tree[rt].fsum=Find(1,90,tree[rt].sum);
        tree[rt].flag=0;
        return;
    }
    if(k<=tree[rt].mid())
        Update(2*rt,k,d);
    else
        Update(2*rt+1,k,d);
    Pushup(rt);
}
void Update_1(int rt,int l,int r)
{
    if(tree[rt].flag)Pushdown(rt);
    if(tree[rt].l==l&&tree[rt].r==r)
    {
        tree[rt].sum=tree[rt].fsum;
        tree[rt].flag=1;
        return;
    }
    if(r<=tree[rt].mid())
        Update_1(2*rt,l,r);
    else if(l>tree[rt].mid())
        Update_1(2*rt+1,l,r);
    else
    {
        Update_1(2*rt,l,tree[rt].mid());
        Update_1(2*rt+1,tree[rt].mid()+1,r);
    }
    Pushup(rt);
}
void Query(int rt,int l,int r)
{
    if(tree[rt].flag)Pushdown(rt);
    if(tree[rt].l==l&&tree[rt].r==r)
    {
        ans+=tree[rt].sum;
        return;
    }
    if(r<=tree[rt].mid())
        Query(2*rt,l,r);
    else if(l>tree[rt].mid())
        Query(2*rt+1,l,r);
    else
    {
        Query(2*rt,l,tree[rt].mid());
        Query(2*rt+1,tree[rt].mid()+1,r);
    }
}
int main()
{
    int n,m;
    fun();
    while(~scanf("%d%d",&n,&m))
    {
        Buildtree(1,1,n);
        while(m--)
        {
            int type,l,r;
            scanf("%d%d%d",&type,&l,&r);
            if(type==1)
                Update(1,l,r);
            else if(type==3)
                Update_1(1,l,r);
            else if(type==2)
            {
                ans=0;
                Query(1,l,r);
                printf("%I64d\n",ans);
            }
        }
    }
    return 0;
}




标签:rt,HDU,4893,线段,fsum,tree,fib,int,sum
From: https://blog.51cto.com/u_3936220/7091386

相关文章

  • HDU 5495(dfs)
    LCSTimeLimit:6000/3000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):417    AcceptedSubmission(s):216ProblemDescription{a1,a2,...,an} and {b1,b2,...,bn}.Bothsequencesarepermutationof......
  • hdu 4291(矩阵快速幂+循环节)
    AShortproblemTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):2487    AcceptedSubmission(s):876ProblemDescriptionAccordingtoaresearch,VIMuserstendtohaveshorterfing......
  • HDU 5014
    NumberSequenceTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):2075    AcceptedSubmission(s):814SpecialJudgeProblemDescriptionThereisaspecialnumbersequencewhichhasn+1inte......
  • hdU 5024
    WangXifeng'sLittlePlotTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/65536K(Java/Others)TotalSubmission(s):786    AcceptedSubmission(s):474ProblemDescription《DreamoftheRedChamber》(also《TheStoryoftheStone......
  • HDU 5443
    TheWaterProblemTimeLimit:1500/1000MS(Java/Others)    MemoryLimit:131072/131072K(Java/Others)TotalSubmission(s):606    AcceptedSubmission(s):489ProblemDescriptiona1,a2,a3,...,anrepresentingthesizeofthewatersource.Given......
  • HDU 5313
    TheshortestproblemTimeLimit:3000/1500MS(Java/Others)  MemoryLimit:65536/65536K (Java/Others)TotalSubmission(s):1484  AcceptedSubmission(s):686ProblemDescriptionInthisproblem,weshouldsolveaninterestinggame.Atfirst,we hav......
  • HDU 1423
    GreatestCommonIncreasingSubsequenceTimeLimit:2000/1000MS(Java/Others)  MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):5384  AcceptedSubmission(s):1742ProblemDescriptionThisisaproblemfromZOJ2432.Tomakeiteasyer,you......
  • HDU 1025
    ConstructingRoadsInJGShining'sKingdomTimeLimit:2000/1000MS(Java/Others)  MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):19340  AcceptedSubmission(s):5473ProblemDescriptionJGShining'skingdomconsistsof2n(nis......
  • HDU 1950
    BridgingsignalsTimeLimit:5000/1000MS(Java/Others)  MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):1169  AcceptedSubmission(s):767ProblemDescription'Ohno,they'vedoneitagain',criesthechiefdesigneratth......
  • HDU 1087 (LIS)
    SuperJumping!Jumping!Jumping!TimeLimit:2000/1000MS(Java/Others)  MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):28091  AcceptedSubmission(s):12529ProblemDescriptionNowadays,akindofchessgamecalled“SuperJumping!Jump......