码迷,mamicode.com
首页 > 其他好文 > 详细

POJ3468A Simple Problem with Integers

时间:2014-07-20 22:38:44      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:c++   算法   基础   线段树   

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct CNode
{
    int L,R;
    CNode* pLeft,* pRight;
    long long Inc;
    long long nSum;
};
CNode Tree[200010];
int nCount=0;
int Mid(CNode* pRoot)
{
    return (pRoot->L+pRoot->R)/2;
}

void BuildTree(CNode* pRoot,int L,int R)
{
    pRoot->L=L;
    pRoot->R=R;
    pRoot->nSum=0;
    pRoot->Inc=0;
    if(L==R) return;
    nCount++;
    pRoot->pLeft=Tree+nCount;
    nCount++;
    pRoot->pRight=Tree+nCount;
    BuildTree(pRoot->pLeft,L,(L+R)/2);
    BuildTree(pRoot->pRight,(L+R)/2+1,R);
}

void Insert(CNode* pRoot,int i,int v)
{
    if(pRoot->L==i&&pRoot->R==i)
    {
        pRoot->nSum=v;
        return;
    }
    pRoot->nSum+=v;
    if(i<=Mid(pRoot))
    {
        Insert(pRoot->pLeft,i,v);
    }
    else
    {
        Insert(pRoot->pRight,i,v);
    }
}

void Add(CNode* pRoot,int a,int b,long long c)
{
    if(pRoot->L==a&&pRoot->R==b)
    {
        pRoot->Inc+=c;
        return;
    }
    pRoot->nSum+=c*(b-a+1);
    if(b<=Mid(pRoot))
    {
        Add(pRoot->pLeft,a,b,c);
    }
    else if(a>Mid(pRoot))
    {
        Add(pRoot->pRight,a,b,c);
    }
    else
    {
        Add(pRoot->pLeft,a,Mid(pRoot),c);
        Add(pRoot->pRight,Mid(pRoot)+1,b,c);
    }
}

long long QuerynSum(CNode* pRoot,int a,int b)
{
    if(pRoot->L==a&&pRoot->R==b)
    {
        return pRoot->nSum+(pRoot->R-pRoot->L+1)*pRoot->Inc;
    }
    pRoot->nSum+=(pRoot->R-pRoot->L+1)*pRoot->Inc;
    Add(pRoot->pLeft,pRoot->L,Mid(pRoot),pRoot->Inc);
    Add(pRoot->pRight,Mid(pRoot)+1,pRoot->R,pRoot->Inc);
    pRoot->Inc=0;
    if(b<=Mid(pRoot))
    {
        return QuerynSum(pRoot->pLeft,a,b);
    }
    else if(a>Mid(pRoot))
    {
        return QuerynSum(pRoot->pRight,a,b);
    }
    else
    {
        return QuerynSum(pRoot->pLeft,a,Mid(pRoot))+QuerynSum(pRoot->pRight,Mid(pRoot)+1,b);
    }
}

int main()
{
    int n,q,a,b,c;
    char cmd[10];
    //freopen("d:\\test.txt","r",stdin);
    scanf("%d%d",&n,&q);
    int i,j,k;
    nCount= 0;
    BuildTree(Tree,1,n);
    for( i= 1; i <= n; i++ )
    {
        scanf("%d",&a);
        Insert(Tree,i,a);
    }
    for( i= 0; i < q; i++ )
    {
        scanf("%s",cmd);
        if ( cmd[0] == 'C' )
        {
            scanf("%d%d%d",&a,&b,&c);
            Add( Tree,a,b,c);
        }
        else
        {
            scanf("%d%d",&a,&b);
            printf("%I64d\n",QuerynSum(Tree,a,b));
        }
    }
    return 0;
}

POJ3468A Simple Problem with Integers

标签:c++   算法   基础   线段树   

原文地址:http://blog.csdn.net/u012198382/article/details/37997197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!