Sicily 1686 Happy Children's Day

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 100010;

//  declaration of the segment tree
struct STNode
{
    int l, r, maxv, maxi;  //  left bound, right bound, max value, max index
    int lazy;  //  delay value to update
    STNode *lchild, *rchild;  //  pointer to left child and right child
}node[MAXN << 1], *root;

int z, value, Index;

//  build the segment tree
STNode* build(int l, int r)
{
    STNode* tmp = &node[z++];
    tmp->l = l, tmp->r = r, tmp->maxv = 0, tmp->maxi = l;
    tmp->lazy = 0;
    if (l == r)  //  the leaf nodes
    {
        tmp->lchild= tmp->rchild = NULL;
    }
    else
    {
        int mid = (l + r) >> 1;
        tmp->lchild = build(l, mid);  //  build the left child tree
        tmp->rchild = build(mid + 1, r);  //  build the right child tree
    }
    return tmp;
}

//  pass the dalay value
inline void PassLazy(STNode *root)
{
    if (!root->lazy)
        return;
    root->lchild->lazy += root->lazy;
    root->lchild->maxv += root->lazy;

    root->rchild->lazy += root->lazy;
    root->rchild->maxv += root->lazy;

    root->lazy = 0;  //  clear the lazy value
}

//  update the segment tree
void update(STNode* root, int l, int r, int val)
{
	//  find the position
    if (root->l == l && root->r == r)
    {
        root->lazy += val;
        root->maxv += val;
        return ;
    }
    //  update the delay
    PassLazy(root);
    int mid = (root->l + root->r) >> 1;
    if (r <= mid)  //  all in the left child tree
        update(root->lchild, l, r, val);
    else if (l > mid)  //  all in the right child tree
        update(root->rchild, l, r, val);
    else  //  in two trees
    {
        update(root->lchild, l, mid, val);
        update(root->rchild, mid + 1, r, val);
    }

    //  update the max value and index
    if (root->lchild->maxv >= root->rchild->maxv)
    {
        root->maxv = root->lchild->maxv;
        root->maxi = root->lchild->maxi;
    }
    else
    {
        root->maxv = root->rchild->maxv;
        root->maxi = root->rchild->maxi;
    }
}

//  query the segment tree
void query(STNode* root, int l, int r)
{
	//  find the position
    if (root->l == l && root->r == r)
    {
        if (root->maxv > value)  //  compare the value with global variant
        {
            Index = root->maxi;
            value = root->maxv;
        }
        return;
    }
    PassLazy(root);
    int mid = (root->l + root->r) >> 1;
    if (r <= mid)  //  all in left child tree
        query(root->lchild, l, r);
    else if (l > mid)  //  all in right child tree
        query(root->rchild, l, r);
    else  //  in two child trees
    {
        query(root->lchild, l, mid);
        query(root->rchild, mid + 1, r);
    }
}

int main()
{
    int n, m;
    while (scanf("%d%d", &n, &m) && n + m)
    {
        z = 0;
        root = build(1, n);
        while (m--)
        {
            char opt;
            int x, y;
            scanf("\n%c%d%d", &opt, &x, &y);
            if (opt == 'I')
            {
                int v;
                scanf("%d", &v);
                update(root, x, y, v);
            }
            else
            {
                Index = value = -1;
                query(root, x, y);
                printf("%d\n", value);
                update(root, Index, Index, -value);  //  update the value of the Index
            }
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值