线段树的指针表示法。
代码还有待消化。。
代码里面多次用到了函数递归,感觉这次对递归又有了深一层的理解。
#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; struct CNode
{
int L, R;
CNode *pLeft, *pRight;
long long nSum;
long long Inc;
};
CNode Tree[ + ]; //2倍叶子节点就够
int nCount = ;
int Mid(CNode *pRoot)
{
return (pRoot->L + pRoot->R) / ;
} void BuildTree(CNode *pRoot, int L, int R)
{
pRoot->L = L;
pRoot->R = R;
pRoot->nSum = ;
pRoot->Inc = ;
if(L == R)
return;
++nCount;
pRoot->pLeft = Tree + nCount;
++nCount;
pRoot->pRight = Tree + nCount;
BuildTree(pRoot->pLeft, L, (L+R)/);
BuildTree(pRoot->pRight, (L+R)/ + , 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 += (b - a + ) * c;
if(b <= (pRoot->L + pRoot->R)/)
Add(pRoot->pLeft, a, b, c);
else if(a >= (pRoot->L + pRoot->R)/ + )
Add(pRoot->pRight, a, b, c);
else
{
Add(pRoot->pLeft, a, (pRoot->L + pRoot->R)/, c);
Add(pRoot->pRight, (pRoot->L + pRoot->R)/ + , 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 + ) * pRoot->Inc; pRoot->nSum += (pRoot->R - pRoot->L + ) * pRoot->Inc;
Add(pRoot->pLeft, pRoot->L, Mid(pRoot), pRoot->Inc);
Add(pRoot->pRight, Mid(pRoot) + , pRoot->R, pRoot->Inc);
pRoot->Inc = ;
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) + , b);
}
} int main(void)
{
#ifdef LOCAL
freopen("3468in.txt", "r", stdin);
#endif int n, q, a, b, c;
char cmd[];
scanf("%d%d", &n, &q);
int i, j, k;
nCount = ;
BuildTree(Tree, , n);
for (i = ; i <= n; ++i)
{
scanf("%d", &a);
Insert(Tree, i, a);
}
for(i = ; i < q; ++i)
{
scanf("%s", cmd);
if(cmd[] == '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 ;
}
代码君