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 ...
随机推荐
- IT架构之IT架构模型——思维导图
参考: [日] 野村综合研究所系统咨询事业本部. 图解CIO工作指南. 周自恒译 人民邮电出版社,2014
- BZOJ1191: [HNOI2006]超级英雄Hero
这题标解是改一下匈牙利算法,显然,像我这种从不用匈牙利的人,会找个办法用网络流… 具体做法是这样,二分最后的答案ans,然后对前ans个问题建图跑网络流,看最大流能不能到ans. /********* ...
- HDU 4639 Hehe(字符串处理,斐波纳契数列,找规律)
题目 //每次for循环的时候总是会忘记最后一段,真是白痴.... //连续的he的个数 种数 //0 1 //1 1 //2 2 //3 3 //4 5 //5 8 //…… …… //斐波纳契数列 ...
- DF学Mysql(二)——数据表的基本操作
1.创建数据表 先使用“USE <数据库名>”指定在哪个数据库中操作 CREATE TABLE <表名> ( 字段1 数据类型 [列级别约束条件] [默认值], 字段2 数据类 ...
- SQL技术内幕-13 SQL优化方法论之分析实例级别的等待
优化方法论的第一步是在实例级别上找出什么类型的等待占用了大部分的等待时间,这可以通过查询动态管理图(DMV,dynamic management view)sys.dm_os_wait_stats 运 ...
- spring mvc 基于注解的使用总结
本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...
- 算法导论:Trie字典树
1. 概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. Trie一词来自retrieve,发音为/tr ...
- netbeans使用
下载地址 https://netbeans.org/downloads/ https://netbeans.org/downloads/start.html?platform=linux&la ...
- 《从零开始学习jQuery》及《jQuery风暴》学习笔记
第一章 jQuery入门 1.用$()函数其实是一个事件,使用这个函数调用的方法,会在DOM加载完毕.资源文件加载完之前触发. 第二章 必须知道的JavaScript知识 1.JavaScript实际 ...
- updmap-sys failed. Output has been stored in
Ubuntu 12.04升级到Ubuntu 12.04lts的时候,出现错误: Do you want to continue? [Y/n] ySetting up tex-common (4.04) ...