POJ 3468 A Simple Problem with Integers
线段树的指针表示法。
代码还有待消化。。
代码里面多次用到了函数递归,感觉这次对递归又有了深一层的理解。
#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的更多相关文章
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468 A Simple Problem with Integers(分块入门)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)
题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)
题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...
- 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 ...
- 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 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- 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 ...
随机推荐
- PE文件结构
PE头 typedef struct _IMAGE_NT_HEADERS { DWORD Signature; PE头标识 为固定的ascii码 PE\\ IMAGE_FILE_HEADER File ...
- MYSQL注入天书之后记
后记 对于工具的看法: 我之所以在每个例子中只写了几个示例,是因为我希望你能通过这一两个示例举一反三将其他的列出来.如果让我来完成每一次完整的注入,应该在知道原理的情况下,必然使用工具或者自己写代码实 ...
- Winform设置相关
>> Winform查找根目录 1) AppDomain.CurrentDomain.BaseDirectory 地址为: d:\MyProject\Bin\ Application. ...
- 【Spark】概述
大数据数据处理模型: 1.Google的MapReduce是一个简单通用和自动容错的批处理计算模型.但,不适合交互式和流式计算! 2.Storm 3.Impala 4.GraphLab 5.Spark ...
- 控制台应用程序的Main方法
总结一下Main方法规则: 1.Main 方法名大小写有规范. 2.Main 方法返回类型只有 void.int两种返回类型. 3.Main 方法的参数可以是string[] args,也可以为空,只 ...
- HDU 1392 Surround the Trees (Graham求凸包周长)
题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...
- 【转】Linux写时拷贝技术(copy-on-write)
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html 源于网上资料 COW技术初窥: 在Linux程序中,fork()会 ...
- Centos环境下部署游戏服务器-软件安装
这篇文章主要介绍一下游戏服务器需要安装的软件和需要修改的配置.现介绍下项目,本项目服务器端是c++ + mysql组合,客户端是as写的,需要安装的服务为Mysql,Php,Apache, 以及一个n ...
- HttpClient基本用法
<Apache HttpClient 4.3开发指南> Apache HttpClient 4系列已经发布很久了,但由于它与HttpClient 3.x版本完全不兼容,以至于业内采用此库的 ...
- mfc Clistctr 单元格嵌入图片(bmp)
示例:http://download.csdn.net/detail/zahxz/4652543 代码: CListCtrl mCtrlist;//列表控件 CImageList m_ImageLis ...