线段树学习

线段树的根节点从1开始。它的左子树的编号是2 * n,右子树为2* n + 1;

经典例题:

hdu1166 敌兵布阵

#include<bits/stdc++.h>
using namespace std;
const int maxn = 50000 + 10;
typedef long long ll;
struct Tree
{
	int l;
	int r;
	ll sum;
}tree[maxn * 4];
int a[maxn];
void build(int root,int l,int r)
{

	tree[root].l = l;
	tree[root].r = r;
	if(tree[root].l == tree[root].r)
	{
		tree[root].sum = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(root* 2, l, mid);
	build(root * 2 + 1,mid + 1,r);
	tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
int getsum(int root,int L,int R)
{
	ll ret = 0;
	if(L <= tree[root].l  && tree[root].r <= R)
	{
		ret += tree[root].sum;
		return ret;
	}
	int mid = (tree[root].l + tree[root].r) / 2;
	if(L <= mid)
		ret += getsum(root*2,L,R);
	if(R >= mid + 1)
		ret += getsum(root * 2 + 1,L,R);
	return ret;
}
void update(int root,int val,int pos)
{
	if(tree[root].l == tree[root].r)
	{
		tree[root].sum = val;
		return ;
	}
	int mid =   ( tree[root].l + tree[root].r) / 2;
	if(pos <= mid)
		update(root * 2,val,pos);
	else update(root*2 + 1,val,pos);
    tree[root].sum = tree[root * 2].sum + tree[root * 2 + 1].sum;
}
void Print(int root)
{
    cout << root << " " << tree[root].l << " " << tree[root].r << " "<< tree[root].sum << endl;
    if(tree[root].l == tree[root].r)
        return;

    Print(root * 2);
    Print(root * 2 + 1);
}
int main()
{
	int Tcase;
	scanf("%d",&Tcase);
	for(int ii = 1; ii <= Tcase; ii ++)
	{
		int n;
		scanf("%d",&n);
		for(int i = 1; i <= n; i ++)
			scanf("%d",&a[i]);
        build(1,1,n);
//        cout << getsum(1,2,7) << endl;
//        cout << endl;
//        Print(1);
		cout << "Case " <<ii << ":" << endl;
		char s[10];
		int x,y;
		while(scanf("%s",s) && s[0] != 'E')
		{
			scanf("%d%d",&x,&y);
			if(s[0] == 'Q')
			{
				if(x > y)
				{
					int t = x;
					x = y;
					y = t;
				}
				cout << getsum(1,x,y) << endl;
			}
			else if(s[0] == 'A')
			{
			    a[x] += y;
				update(1,a[x],x);

//				Print(1);
			}
			else if(s[0] == 'S')
			{
			    a[x] -= y;
				update(1,a[x],x);
			}
		}
	}
	return 0;
}


hdu 1754 I hate it

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
typedef long long ll;
struct Tree
{
	int l;
	int r;
	int maxs;
}tree[maxn * 4];
int a[maxn];
void build(int root,int l,int r)
{

	tree[root].l = l;
	tree[root].r = r;
	if(tree[root].l == tree[root].r)
	{
		tree[root].maxs = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(root* 2, l, mid);
	build(root * 2 + 1,mid + 1,r);
	tree[root].maxs = max(tree[root * 2].maxs , tree[root * 2 + 1].maxs);
}
int getsum(int root,int L,int R)
{
	int ret = 0;
	if(L <= tree[root].l  && tree[root].r <= R)
	{
		return tree[root].maxs;
	}
	int mid = (tree[root].l + tree[root].r) / 2;
	if(L <= mid)
		ret = max(ret,getsum(root*2,L,R));
	if(R >= mid + 1)
		ret = max(ret,getsum(root * 2 + 1,L,R));
	return ret;
}
void update(int root,int val,int pos)
{
	if(tree[root].l == tree[root].r)
	{
		tree[root].maxs = val;
		return ;
	}
	int mid =   ( tree[root].l + tree[root].r) / 2;
	if(pos <= mid)
		update(root * 2,val,pos);
	else update(root*2 + 1,val,pos);
    tree[root].maxs = max(tree[root * 2].maxs , tree[root * 2 + 1].maxs);
}
//void Print(int root)
//{
//    cout << root << " " << tree[root].l << " " << tree[root].r << " "<< tree[root].maxs << endl;
//    if(tree[root].l == tree[root].r)
//        return;
//
//    Print(root * 2);
//    Print(root * 2 + 1);
//}
int main()
{
//	int Tcase;
//	scanf("%d",&Tcase);
//	for(int ii = 1; ii <= Tcase; ii ++)
    int n,m;
    while( ~ scanf("%d%d",&n,&m) )
	{
		for(int i = 1; i <= n; i ++)
			scanf("%d",&a[i]);
        build(1,1,n);
//        Print(1);
//		cout << "Case " <<ii << ":" << endl;
		char s[10];
		int x,y;
//		scanf("%d",&m);
//		while(scanf("%s",s) && s[0] != 'E')
        while(m --)
		{
			scanf("%s%d%d",s,&x,&y);
			if(s[0] == 'Q')
			{
				if(x > y)
				{
					int t = x;
					x = y;
					y = t;
				}
				cout << getsum(1,x,y) << endl;
			}
			else if(s[0] == 'U')
			{
				update(1,y,x);
			}
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值