线段树的指针表示法。

代码还有待消化。。

代码里面多次用到了函数递归,感觉这次对递归又有了深一层的理解。

 #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 ;
}

代码君

POJ 3468 A Simple Problem with Integers的更多相关文章

  1. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  3. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  5. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  6. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  7. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  8. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  9. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  10. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

随机推荐

  1. HDOJ 1220 Cube

    CubeTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  2. 链表(c语言实现)--------------小练习

    #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 #d ...

  3. Codeforces 294B Shaass and Bookshelf(记忆化搜索)

    题目 记忆化搜索(深搜+记录状态) 感谢JLGG //记忆话搜索 //一本书2中状态,竖着放或者横着放 //初始先都竖着放,然后从左边往右边扫 #include<stdio.h> #inc ...

  4. java基础知识回顾之抽象类

    /* 抽象类: 抽象:笼统,模糊,看不懂!不具体. 特点: 1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰. 抽象方法必须定义在抽象类中.该类必须也被abstract修饰. ...

  5. Server Library [Apache Tomcat v6.0](unbound)服务未绑定解决办法

    (1) 单击File按钮---钩选Show AllWizard——>选择Server——>单击Next (2)Add Library    选择 WTP Server Runtime(My ...

  6. Spark源码分析(二)-SparkContext创建

    原创文章,转载请注明: 转载自http://www.cnblogs.com/tovin/p/3872785.html  SparkContext是应用启动时创建的Spark上下文对象,是一个重要的入口 ...

  7. Centos环境下部署游戏服务器-自动化

    计算机是没有智力的,只会接受指令,运行指令,得出结果.因此就算你让它重复做一件事多少次,它也学不会自动去做这件事.对于重复繁琐的事情,做为一个懒惰的程序员,必须告诉机器去做这件事情,然后就行了,而不是 ...

  8. Hibernate笔记——缓存机制详细分析

    原文:http://www.cnblogs.com/xiaoluo501395377/p/3377604.html ========================================== ...

  9. C语言预处理程序[转]

    c语言预处理程序有三种,分别是: 1.包含头文件,如:#include <stdio.h> 2.宏定义(本质是字符串的替换) 如 :#define  宏名  串(宏体) #define   ...

  10. Android:开发环境

    一.JAVA SDK(JDK)的安装 http://www.cnblogs.com/tinyphp/p/3664598.html 二.ADT-Bundle 包含了Eclipse.ADT插件和SDK T ...