• poj3468线段树_区间数字统计


       自从上次偶尔用了一次线段树后就很喜欢这东西,做各种区间统计挺有用的。抽空又玩了下3468。也是一道很常见的数段树应用,统计区间的和,有时候又要更新区间的值。

    #include<iostream>

    using namespace std;

    class TreeNode
    {
    public:
    int lIndex,rIndex;
    TreeNode *lNode;
    TreeNode *rNode;
    __int64 inc;
    __int64 sum;
    };

    //根据给定的数组建树
    void BuildTree(TreeNode *tNode,int lIndex,int rIndex)
    {
    tNode->lIndex=lIndex;
    tNode->rIndex=rIndex;
    tNode->inc=0;

    int midIndex=(lIndex+rIndex)/2;

    if(midIndex<rIndex)
    {
    tNode->lNode=new TreeNode();
    BuildTree(tNode->lNode,lIndex,midIndex);
    }
    if(midIndex+1>lIndex && midIndex+1<=rIndex)
    {
    tNode->rNode=new TreeNode();
    BuildTree(tNode->rNode,midIndex+1,rIndex);
    }
    return ;
    }

    //初始化区间的和
    __int64 InitTreeNodeSum(TreeNode *tnode,int arr[])
    {
    if(tnode==NULL)return 0;
    if(tnode->lIndex==tnode->rIndex)
    {
    tnode->sum=arr[tnode->lIndex];

    }

    else
    {
    __int64 leftSum=InitTreeNodeSum(tnode->lNode,arr);
    __int64 rightSum=InitTreeNodeSum(tnode->rNode,arr);
    tnode->sum=leftSum+rightSum;
    }
    return tnode->sum;
    }

    //增加操作
    void AddSubSequence(TreeNode *tNode,int lIndex,int rIndex,int tInc)
    {
    if(lIndex==tNode->lIndex && rIndex==tNode->rIndex)
    {
    tNode->inc+=tInc;
    return;
    }

    else
    {
    tNode->sum += (rIndex-lIndex+1)*tInc;

    if(rIndex <= tNode->lNode->rIndex)//totally in the left child
    {
    AddSubSequence(tNode->lNode,lIndex,rIndex,tInc);
    }

    else if(lIndex >= tNode->rNode->lIndex)//totally in the right child
    {
    AddSubSequence(tNode->rNode,lIndex,rIndex,tInc);
    }

    else
    {
    AddSubSequence(tNode->lNode,lIndex,tNode->lNode->rIndex,tInc);
    AddSubSequence(tNode->rNode,tNode->rNode->lIndex,rIndex,tInc);
    }
    }
    }

    //询问操作
    __int64 QuerySequence(TreeNode *tNode,int lIndex,int rIndex,int parentInc)
    {
    if(tNode->lIndex==lIndex && tNode->rIndex==rIndex)
    {
    __int64 sum = tNode->sum+(tNode->rIndex-tNode->lIndex+1)*(tNode->inc+parentInc);
    return sum;
    }
    else
    {
    if(rIndex <= tNode->lNode->rIndex)//totally in the left child
    {
    return QuerySequence(tNode->lNode,lIndex,rIndex,tNode->inc+parentInc);
    }

    else if(lIndex >= tNode->rNode->lIndex)//totally in the right child
    {
    return QuerySequence(tNode->rNode,lIndex,rIndex,tNode->inc+parentInc);
    }

    else
    {
    return QuerySequence(tNode->lNode,lIndex,tNode->lNode->rIndex,tNode->inc+parentInc) + 
    QuerySequence(tNode->rNode,tNode->rNode->lIndex,rIndex,tNode->inc+parentInc);
    }
    }
    }

    int main()
    {
    int N,Q;
    int inArray[100001];
    scanf("%d%d",&N,&Q);
    for(int i=1;i<=N;i++)
    {
    scanf("%d",&inArray[i]);
    }

    TreeNode treeRoot;
    BuildTree(&treeRoot,1,N);

    InitTreeNodeSum(&treeRoot,inArray);

    for (int i=0;i<Q;i++)
    {
    char ch=' ';
    int a,b,c;
            while(!(ch == 'Q' || ch=='C'))
    scanf("%c",&ch);
    if(ch=='Q')
    {
    scanf("%d%d",&a,&b);
    __int64 sum=QuerySequence(&treeRoot,a,b,0);
    printf("%lld\n",sum);
    }
    else
    {
    scanf("%d%d%d",&a,&b,&c);
    AddSubSequence(&treeRoot,a,b,c);
    }
    }
    return 0;
    }
  • 相关阅读:
    零基础学习Java,视频、网上直播和线下培训,哪个更好?
    艾编程:海量消息数据的处理kafka以及海量数控读取储存处理Mycat
    艾编程coding老师:深入JVM底层原理与性能调优
    Are Noisy Sentences Useless for Distant Supervised Relation Extraction?
    A Probabilistic Formulation of Unsupervised Text Style Transfer
    Orderless Recurrent Models for Multi-label Classification
    Learning from Easy to Complex: Adaptive Multi-curricula Learning for Neural Dialogue Generation
    Hyper-Parameter Optimization: A Review of Algorithms and Applications
    Capturing document context inside sentence-level neural machine translation models with self-training
    ReZero is All You Need: Fast Convergence at Large Depth
  • 原文地址:https://www.cnblogs.com/bester/p/3255770.html
Copyright © 2020-2023  润新知