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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值