poj 3468【线段树】

题意:给定Q(1<=Q<=100000)个数A1,A2…AQ,以及可能多次进行的两个操作

1)对某个区间Ai……Aj的每个数都加n(n可变)

2)对某个区间Ai……Aj的数求和

分析:

树结点只存和,会导致每次加数时都要更新到叶子节点,速度太慢(O(nlog(n))),这是必须避免的

1.在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新nSum(加上本次增量)

再将增量往下传。这样更新的复杂度就是O(log(n))

2.在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc代表的所有增量累加到nSum上后将

Inc清0,接下来再往下查询。Inc往下带的过程也是区间分解过程,复杂度也是O(log(n))

#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
typedef long long LL;
const int INF = 0x7FFFFFFF;
const int maxn = 1e3 + 10;

struct CNode
{
	int L, R;
	CNode *pLeft, *pRight;
	long long nSum;//原来的和
	long long Inc;//增量c的累加
};

CNode Tree[200010];//2倍叶子节点数目就够
int nCount = 0;
int Mid(CNode*pRoot)
{
	return (pRoot->L + pRoot->R) / 2;
}

void BuildTree(CNode *pRoot, int L, int R)
{
	pRoot->L = L;
	pRoot->R = R;
	pRoot->nSum = 0;
	pRoot->Inc = 0;
	if (L == R)
		return;
	nCount++;
	pRoot->pLeft = Tree + nCount;
	nCount++;
	pRoot->pRight = Tree + nCount;
	BuildTree(pRoot->pLeft, L, (L + R) / 2);
	BuildTree(pRoot->pRight, (L + R) / 2 + 1, 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 += c*(b - a + 1);
	if (b <= (pRoot->L + pRoot->R) / 2)
		Add(pRoot->pLeft, a, b, c);
	else if (a >= (pRoot->L + pRoot->R) / 2 + 1)
		Add(pRoot->pRight, a, b, c);
	else
	{
		Add(pRoot->pLeft, a, (pRoot->L + pRoot->R) / 2, c);
		Add(pRoot->pRight, (pRoot->L + pRoot->R) / 2 + 1, 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 + 1)*pRoot->Inc;
	pRoot->nSum += (pRoot->R - pRoot->L + 1)*pRoot->Inc;
	Add(pRoot->pLeft, pRoot->L, Mid(pRoot), pRoot->Inc);
	Add(pRoot->pRight, Mid(pRoot) + 1, pRoot->R, pRoot->Inc);
	pRoot->Inc = 0;

	if (b <= Mid(pRoot))
		return QuerynSum(pRoot->pLeft, a, b);
	else if (a >= Mid(pRoot) + 1)
		return QuerynSum(pRoot->pRight, a, b);
	else
	{
		return QuerynSum(pRoot->pLeft, a, Mid(pRoot)) +
               QuerynSum(pRoot->pRight, Mid(pRoot) + 1, b);
	}
}

int main()
{
	int n, q, a, b, c;
	char cmd[10];
	scanf("%d%d", &n, &q);
	int i, j, k;
	nCount = 0;
	BuildTree(Tree, 1, n);
	for (i = 1; i <= n;i++)
	{
		scanf("%d", &a);
		Insert(Tree, i, a);
	}
	for (i = 0; i < q; i++)
	{
		scanf("%s", cmd);
		if (cmd[0]==‘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 0;
}
时间: 2024-12-26 08:01:01

poj 3468【线段树】的相关文章

POJ 3468 线段树+lazy标记

lazy标记 Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to

poj 3468 线段树成段更新

http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58132   Accepted: 17704 Case Time Limit: 2000MS Description You have N integers, A1, A2, ... , AN. You need to deal with two k

POJ 3468 线段树(成段更新,区间求和)

题目链接:http://poj.org/problem?id=3468 题意:给定一个数列,每次操作可以是将某区间数字都加上一个相同的整数,也可以是询问一个区间中所有数字的和,对每次询问输出结果. 这个线段树运用了应用了add域优化,每个节点除了用value记录当前节点对应区间元素的和之外,还要用add域记录当前节点对应区间每个元素的增量.这样,没必要每次更新都要更新value更新到最底层每一个点,只需要将增量记录在某父节点的add域中即可,如果下次查询或者更新操作的是该父节点对应区间的子区间,

hdu 1698+poj 3468 (线段树 区间更新)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 这个题意翻译起来有点猥琐啊,还是和谐一点吧 和涂颜色差不多,区间初始都为1,然后操作都是将x到y改为z,注意 是改为z,不是加或减,最后输出区间总值 也是线段树加lazy操作 1 #include<cstdio> 2 using namespace std; 3 struct point { 4 int l,r; 5 int val,sum; 6 }; 7 point tree[400007]; 8

poj 3468 线段树 成段增减 区间求和

题意:Q是询问区间和,C是在区间内每个节点加上一个值 Sample Input 10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4Sample Output 455915 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cmath> 6

POJ 3468 线段树区间求和

线段树区间求和树节点不能只存和,只存和,会导致每次加数的时候都要更新到叶子节点,速度太慢(O(nlogn)).所以我们要存两个量,一个是原来的和nSum,一个是累加的增量Inc. 在增加时,如果要加的区间正好覆盖一个节点,则增加其节点的Inc值,不再往下走,否则要更新nSum(加上本次增量),再将增量往下传,这样更新的复杂度就是O(log(n)). 在查询时,如果待查区间不是正好覆盖一个节点,就将节点的Inc往下带,然后将Inc清0,接下来再往下查询. Inc往下带的过程也是区间分解的过程,复杂

POJ 3468 线段树裸题

这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了ACdreamer的模板,在此基础上自己用宏定义来精简了一下代码: 1 #include<cstdio> 2 typedef long long LL; 3 #define root int rt, int l, int r 4 #define lson rt*2, l, mid 5 #define

POJ 3468 (线段树 区间增减) A Simple Problem with Integers

这题WA了好久,一直以为是lld和I64d的问题,后来发现是自己的pushdown函数写错了,说到底还是因为自己对线段树理解得不好. 因为是懒惰标记,所以只有在区间分开的时候才会将标记往下传递.更新和查询都要pushdown. 1 #include <cstdio> 2 3 typedef long long LL; 4 5 const int maxn = 100000 + 10; 6 7 int n, m, qL, qR, v; 8 LL sum[maxn << 2], add

poj 3468 线段树

线段树的 建立build(初始化+左右相等+两个递归+别忘了sum)+更新update(递归出口+更新delta+三向递归+修正当前节点的value)+查找query(如果左右相等+更新delta+三种递归) #include<iostream> using namespace std; #define L(root) ((root) << 1) #define R(root) (((root) << 1) + 1) const int MAXN = 100001; i

C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 1 #include<cstdio> 2 using namespace std; 3 const int maxn=1e5+6; 4 int n,a[maxn]; 5 struct Node{ 6 int l,r; 7 long long sum,lazy; 8 void update(long long x){//用于更新区间和 和懒标记 9 sum+=1ll*(r-l+1)*x; 10 lazy+=x; 11 } 12