码迷,mamicode.com
首页 > 其他好文 > 详细

【线段树】模板

时间:2015-02-17 18:47:58      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

struct NODE
{
    int value;
    int left,right;
} node[maxn];
int father[MAX];

void BuildTree(int i,int left,int right)
{
    node[i].left = left;
    node[i].right = right;
    node[i].value = 0;
    if (left == right)
    {
        father[left] = i;
        return;
    }
    int mid = (left + right)/2;
    BuildTree(i<<1, left, mid);
    BuildTree(i<<1|1, mid + 1, right);
}


void UpdataTree(int ri)
{
    if (ri == 1)return;
    int fi = ri / 2;
    int a = node[fi<<1].value;
    int b = node[fi<<1|1].value;
    node[fi].value = max(a,b);
    UpdataTree(ri/2);
}

int Max = -9999;
void Query(int i,int l,int r)
{
    if (node[i].left == l && node[i].right == r)
    {
        Max = max(Max,node[i].value);
        return;
    }
    int ans = 0;
    i = i << 1;
    if (l <= node[i].right)
    {
        if (r <= node[i].right)
            Query(i, l, r);
        else
            Query(i, l, node[i].right);
    }
    i += 1;
    if (r >= node[i].left)
    {
        if (l >= node[i].left)
            Query(i, l, r);
        else
            Query(i, node[i].left, r);
    }
}

模板题目HDU1754:

#include <cstdio>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 4000000;
const int MAX = 1000003;

struct NODE
{
    int value;
    int left,right;
} node[maxn];
int father[MAX];

void BuildTree(int i,int left,int right)
{
    node[i].left = left;
    node[i].right = right;
    node[i].value = 0;
    if (left == right)
    {
        father[left] = i;
        return;
    }
    int mid = (left + right)/2;
    BuildTree(i<<1, left, mid);
    BuildTree(i<<1|1, mid + 1, right);
}


void UpdataTree(int ri)
{
    if (ri == 1)return;
    int fi = ri / 2;
    int a = node[fi<<1].value;
    int b = node[fi<<1|1].value;
    node[fi].value = max(a,b);
    UpdataTree(ri/2);
}

int Max = -9999;
void Query(int i,int l,int r)
{
    if (node[i].left == l && node[i].right == r)
    {
        Max = max(Max,node[i].value);
        return;
    }
    int ans = 0;
    i = i << 1;
    if (l <= node[i].right)
    {
        if (r <= node[i].right)
            Query(i, l, r);
        else
            Query(i, l, node[i].right);
    }
    i += 1;
    if (r >= node[i].left)
    {
        if (l >= node[i].left)
            Query(i, l, r);
        else
            Query(i, node[i].left, r);
    }
}

int main()
{
#ifdef xxz
    freopen("in.txt","r",stdin);
#endif // xxz
    int n,m,grade;
    while(~scanf("%d%d",&n,&m))
    {
        BuildTree(1,1,n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d",&grade);
            node[father[i]].value = grade;
            UpdataTree(father[i]);
        }

        while(m--)
        {
            char ch[3];
            int a,b;
            scanf("%s %d %d",ch,&a,&b);
            if (ch[0] == 'Q')
            {
                Max = 0;
                Query(1, a, b);
                printf("%d\n",Max);
            }
            else
            {
                node[father[a]].value = b;
                UpdataTree(father[a]);
            }
        }


    }
    return 0;
}


【线段树】模板

标签:

原文地址:http://blog.csdn.net/u013445530/article/details/43867005

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!