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

未完待续

时间:2015-06-23 22:51:25      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

#include<stdio.h>
#include<string.h>
#include<iostream>
#define maxn 100000 + 10
using namespace std;

int val[maxn];

struct Tree
{
    int mark;
    int total;
    int left, right;
}tree[maxn*4];

int create(int root, int left, int right)
{
    tree[root].left = left;
    tre[root].right = right;
    if (left == right)
    return tree[root].total = val[left];
    int mid = (a + b) / 2;
    int a, b;
    a = create(2*root, left, mid);
    b = create(2*root+1, mid+1, right);
    return tree[root].total = a + b;
}

int update(int root, int left, int right, int val)  // 区间更新
{
    update_mark(root);
    if (tree[root].left > right || tree[root].right < left)  // 当前区间和更新区间无交集。不作处理。
    return tree[root].total;
    if (left <= tree[root].left && tree[root].right <= right)  // 没动延迟标记咋用的.这是当前区间完全包含在更新区间的时候。
    {
        tree[root].mark = val;
        return tree[root].total = val * (tree[root].right - tree[root].left + 1);
    }
    int a = update(2*root, left, right, val);
    int b = update(2*root+1, left, right, val);
    return tree[root].total = a + b;
}

int calculate(int root, int left, int right)  // 求区间和
{
    update_mark(root);
    if (tree[root].left > right || tree[root].right < left)
    return 0;
    if (left <= tree[root].left && tree[root].right <= right)
    return tree[root].total;
    int a = calculate(2*root, left, right);
    int b = calculate(2*root+1, left, right);
    return tree[root].total = a + b;
}

void update_mark(int root)
{
    if (tree[root].mark)
    {
        tree[root].total = tree[root].mark * (tree[root].right - tree[root].left + 1);
        if (tree[root].left != tree[root].root.right)
        tree[root*2].mark = tree[root*2+1].mark = tree[root].mark;
        tree[root].mark = 0;

    }
}



未完待续

标签:

原文地址:http://www.cnblogs.com/icode-girl/p/4596346.html

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