XTU 1238 Segment Tree (线段树)

题目:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1238

题意:设计一棵线段树,支持四种操作。①将区间[l,r]的值加上v ②将区间[l,r]里面的每个值tree[i]修改为x=min(tree[i],v) ③将区间[l,r]里面的每个值tree[i]修改为x=max(tree[i],v) ④查询区间[l,r]的最小值和最大值

分析:吸取uva11992那题的经验,每次更新的时候修改当前节点的所有信息,pushdown的时候修改左右孩子的所有信息,所以下次访问任意一个的时候可以保证节点的信息是已经更新好了的。查询的时候直接用当前节点的信息就行。注:懒惰标记为下面的节点需要更新的信息,不包括本节点。

代码:

#include <iostream>
#include <cstdio>
using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = 2e5+5;
const int INF =2e9;
int _min,_max;
struct node
{
    int Max,Min,add;
};
struct segtree
{
    node tree[maxn<<2];
    void pushup(int rt)
    {
        tree[rt].Min=min(tree[rt<<1].Min,tree[rt<<1|1].Min);
        tree[rt].Max=max(tree[rt<<1].Max,tree[rt<<1|1].Max);
    }
    void pushdown(int rt)
    {
        if(tree[rt].add)
        {
            tree[rt<<1].add+=tree[rt].add;
            tree[rt<<1].Max+=tree[rt].add;
            tree[rt<<1].Min+=tree[rt].add;
            
            tree[rt<<1|1].add+=tree[rt].add;
            tree[rt<<1|1].Max+=tree[rt].add;
            tree[rt<<1|1].Min+=tree[rt].add;
            tree[rt].add=0;
        }
        tree[rt<<1].Max=min(tree[rt<<1].Max,tree[rt].Max);
        //假如tree[rt].Max是由左孩子(本身)推上来的,显然该语句并没改变什么。 
        //假如tree[rt].Max是由右孩子推上来的,那么tree[rt].Max大于tree[rt<<1].Max,这条语句不会改变什么。
		//假如tree[rt].Max是由题目操作更新而来(即一个min:v操作中的v小于tree[rt].Max),tree[rt<<1].Max将被正确更新
		// 假如tree[rt].Max是由题目操作更新而来(即一个max:v操作中的v大于tree[rt].Max),这条语句不会改变什么。
		 
        tree[rt<<1].Max=max(tree[rt<<1].Max,tree[rt].Min);
		//假如tree[rt].Min是由左右孩子推上来的,tree[rt<<1].Max会大于等于tree[rt].Min,该语句对tree[rt<<1].Max无影响
		//假如tree[rt].Min是由题目操作更新而来(即一个max:v操作中的v大于tree[rt].Min),tree[rt<<1].Max将被正确更新 
		//假如tree[rt].Min是由题目操作更新而来(即一个min:v操作中的v小于tree[rt].Min),这条语句不会改变什么。 
        
		tree[rt<<1].Min=max(tree[rt<<1].Min,tree[rt].Min);
		tree[rt<<1].Min=min(tree[rt<<1].Min,tree[rt].Max);
		
        tree[rt<<1|1].Max=min(tree[rt<<1|1].Max,tree[rt].Max);
        tree[rt<<1|1].Max=max(tree[rt<<1|1].Max,tree[rt].Min);
        tree[rt<<1|1].Min=max(tree[rt<<1|1].Min,tree[rt].Min);
    	tree[rt<<1|1].Min=min(tree[rt<<1|1].Min,tree[rt].Max);
	
	//假如tree[rt].Max或tree[rt].Min是由左右孩子推上来的,那么这8条语句并不会改变左边的值
	//显然tree[rt].Max总会大于等于左右孩子的Max和Min,除非tree[rt].Max是由题目操作更新而来。tree[rt].Min同理
	//自己写了几发wa了,参考别人的,感觉这个向上更新叼叼的,假如题目没有操作3 max:v,怎么破? 
	} 
    void build(int l,int r,int rt)
    {
        tree[rt].add=0;
        if(l==r)
        {
            scanf("%d",&tree[rt].Min);
            tree[rt].Max=tree[rt].Min;
            return ;
        }
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        pushup(rt);
    }
    void update_add(int L,int R,int v,int l,int r,int rt)
    {
        if(L<=l && r<=R)
        {
            tree[rt].add+=v;
            tree[rt].Max+=v;
            tree[rt].Min+=v;
            return ;
        }
        pushdown(rt);
        int m=(l+r)>>1;
        if(L<=m)
            update_add(L,R,v,lson);
        if(R>m)
            update_add(L,R,v,rson);
        pushup(rt);
    }
    void update_max(int L,int R,int v,int l,int r,int rt)
    {
        if(L<=l && r<=R)
        {
            tree[rt].Max=max(v,tree[rt].Max);
            tree[rt].Min=max(v,tree[rt].Min);
            return ;
        }
        pushdown(rt);
        int m=(l+r)>>1;
        if(L<=m)
            update_max(L,R,v,lson);
        if(R>m)
            update_max(L,R,v,rson);
        pushup(rt);
    }
    void update_min(int L,int R,int v,int l,int r,int rt)
    {
        if(L<=l && r<=R)
        {
            tree[rt].Max=min(v,tree[rt].Max);
            tree[rt].Min=min(v,tree[rt].Min);
            return ;
        }
        pushdown(rt);
        int m=(l+r)>>1;
        if(L<=m)
            update_min(L,R,v,lson);
        if(R>m)
            update_min(L,R,v,rson);
        pushup(rt);
    }
    void query(int L,int R,int l,int r,int rt)
    {
        if(L<=l && r<=R)
        {
            _max=max(tree[rt].Max,_max);
            _min=min(tree[rt].Min,_min);
            return ;
        }
        pushdown(rt);
        int m=(l+r)>>1;
        if(L<=m)
            query(L,R,lson);
        if(R>m)
            query(L,R,rson);
        pushup(rt);
    }
}T;

int main()
{
    int ncase,n,q,L,R,v,tp;
    scanf("%d",&ncase);
    while(ncase--)
    {
        scanf("%d%d",&n,&q);
        T.build(1,n,1);
        while(q--)
        {
            scanf("%d",&tp);
            if(tp==1)
            {
                scanf("%d%d%d",&L,&R,&v);
                T.update_add(L,R,v,1,n,1);
            }
            else if(tp==2)
            {
                scanf("%d%d%d",&L,&R,&v);
                T.update_min(L,R,v,1,n,1);
            }
            else if(tp==3)
            {
                scanf("%d%d%d",&L,&R,&v);
                T.update_max(L,R,v,1,n,1);
            }
            else
            {
                scanf("%d%d",&L,&R);
                _min=INF;
                _max=-INF;
                T.query(L,R,1,n,1);
                printf("%d %d\n",_min,_max);
            }
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值