线段树(1)

数据结构学习的又一阶段,很早就学过线段树,当时只知道敲模板,不是很了解,现在连模版都忘了,还是参考大牛的博客学习:http://www.notonlysuccess.com/index.php/segment-tree-complete/。我很弱,但我不惧批评。欢迎大家指出错误,让我进步,有更好的资源欢迎分享。谢谢。


1、hdu1166 敌兵布阵

http://acm.hdu.edu.cn/showproblem.php?pid=1166

可能是最简单的线段树了,单点更新,区间查询。

 

#include <stdio.h>
const int MAX = 50000+10;
struct node
{
	int l;
	int r;
	int sum;
}a[MAX<<2];//为什么是4倍,在稿纸上画一种最极端情况就可以知道了 

/*更新子节点后,更新父节点*/
void pushup(int pos)
{
	a[pos].sum = a[pos<<1].sum + a[pos<<1|1].sum;
}
/*建立初始线段树,线段树是递归定义,也应当递归建立(也可以非递归建立,烦)*/
void build(int l, int r, int pos)
{
	a[pos].l = l;
	a[pos].r = r;
	a[pos].sum = 0;
	if (l == r)
	{
		scanf("%d", &a[pos].sum);
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, pos<<1);
	build(mid+1, r, pos<<1|1);
	pushup(pos);
}
/*单点更新*/
void update(int goal, int v, int pos)
{
	if (a[pos].l == a[pos].r)
	{
		a[pos].sum += v;
		return;
	}
	int mid = (a[pos].l + a[pos].r) >> 1;
	if (goal <= mid)
	{
		update(goal, v, pos<<1);
	}
	else
	{
		update(goal, v, pos<<1|1);
	}
	pushup(pos);
}
/*区间查询*/ 
int query(int l, int r, int pos)
{
	if (a[pos].l == l && a[pos].r == r)
	{
		return a[pos].sum;
	}
	int mid = (a[pos].l + a[pos].r) >> 1;
	if (r<=mid)
	{
		return query(l, r, pos<<1);
	}
	else if (l>mid)
	{
		return query(l, r ,pos<<1|1);
	}
	else
	{
		return query(l, mid, pos<<1) + query(mid+1, r, pos<<1|1);
	}
}

int main()
{
	int t, n, x, y, Cas;
	char order[11];
	while (scanf("%d", &t) == 1)
	{
		Cas=1;
		while (t--)
		{
			printf("Case %d:\n", Cas++);
			scanf("%d", &n);
			build(1, n, 1);
			while (scanf("%s", order)==1)
			{
				if (order[0] == 'E')
				{
					break;
				}
				scanf("%d%d", &x, &y);
				if (order[0] == 'Q')
				{
					printf("%d\n", query(x, y, 1));
				}
				else if (order[0] == 'A')
				{
					update(x, y, 1);
				}
				else
				{
					update(x, -y, 1);
				}
			}
		}
	}
}


 2、hdu 1754 I Hate It

http://acm.hdu.edu.cn/showproblem.php?pid=1754

 很奇怪,自己写了个预定义的max居然TLE,使用algorithm,居然过了,估计是自己预定写错了,上完课再想想。这道题依旧是单点更新,成段查询。

#include <stdio.h>

const int MAX = 200000 + 1;


struct node
{
	int l;
	int r;
	int key;
}a[MAX<<2];
int max(int x, int y)
{
	return x > y ? x:y;
}
void pushup(int pos)
{
	a[pos].key = max(a[pos<<1].key, a[pos<<1|1].key);
}
/*想了一下,l,r指导走向以及线段首尾,pos记录数组位置*/
void build(int l, int r, int pos)
{
	a[pos].l = l;
	a[pos].r = r;
	if (l == r)
	{
		scanf("%d", &a[pos].key);
		return;
	}
	int mid = (l + r)>>1;
	build(l, mid, pos<<1);
	build(mid+1, r, pos<<1|1);
	pushup(pos);
}

void update(int goal, int v, int pos)
{
	if (a[pos].l == a[pos].r)
	{
		a[pos].key = v;
		return;
	}
	int mid = (a[pos].l + a[pos].r) >> 1;
	if (goal<=mid)
	{
		update(goal, v, pos<<1);
	}
	else
	{
		update(goal, v, pos<<1|1);
	}
	pushup(pos);
}

int query(int l, int r, int pos)
{
	if (a[pos].l  == l && a[pos].r == r)
	{
		return a[pos].key;
	}
	int mid = (a[pos].l + a[pos].r)>>1;
	if (r<=mid)
	{
		return query(l, r, pos<<1);
	}
	else if (l>mid)
	{
		return query(l, r, pos<<1|1);
	}
	else
	{
		return max(query(l, mid, pos<<1), query(mid+1, r, pos<<1|1));
	}
}
int main()
{
	int i, x, y;
	char order;
	int n, m;
	while (scanf("%d%d", &n, &m) == 2)
	{
		build(1, n, 1);
		getchar();
		while (m--)
		{
			scanf("%c%d%d", &order, &x, &y);
			getchar();
			if (order == 'Q')
			{
				printf("%d\n", query(x, y, 1));
			}
			if (order == 'U')
			{
				update(x, y, 1);
			}
		}
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值