线段树的指针表示法。

代码还有待消化。。

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

 #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 2152 Fruit(母函数)

    Fruit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. sql注入分类

    Sql注入根据数据提取通道的类型,从服务器接收到的响应等可以分为不同的类型. 基于从服务器接收到的响应 ▲基于错误的SQL注入 ▲联合查询的类型 ▲堆查询注射 ▲SQL盲注 •基于布尔SQL盲注 •基 ...

  3. Javascript获取URL参数值

    getQueryString: function (name) { var reg = new RegExp("(^|&)" + name.toLowerCase() + ...

  4. ZOJ题目分类

    ZOJ题目分类初学者题: 1001 1037 1048 1049 1051 1067 1115 1151 1201 1205 1216 1240 1241 1242 1251 1292 1331 13 ...

  5. 2014多校第七场1005 || HDU 4939 Stupid Tower Defense (DP)

    题目链接 题意 :长度n单位,从头走到尾,经过每个单位长度需要花费t秒,有三种塔: 红塔 :经过该塔所在单位时,每秒会受到x点伤害. 绿塔 : 经过该塔所在单位之后的每个单位长度时每秒都会经受y点伤害 ...

  6. Windbg 常用命令整理

    kd> !idt -a      查看idt kd> dt _ktrap_frame   异常帧 kd> ba e1 Address 下硬件执行断点kd> ba w4 Addr ...

  7. Session、Cookie及cache的区别

    Session 是单用户的会话状态.当用户访问网站时,产生一个 sessionid.并存在于 cookies中.每次向服务器请求时,发送这个 cookies,再从服务器中检索是否有这个 session ...

  8. java实现音频转换

    这里需要用到第三方 ffmpeg.exe package com.convertaudio; import java.io.File;import java.util.ArrayList;import ...

  9. ios开发 ad hoc

    iOS证书分2种,1种是开发证书,用来给你(开发人员)做真机测试的:1种是发布证书,发布证书又分发布到app store的(这里不提及)和发布测试的ad hoc证书. 那ad hoc证书和开发证书区别 ...

  10. ntelliJ IDEA 14 注册码

    user or company nameo license key63625-MQ87K-3SRZ2-8MQYB-6NQZC-2Z8K6