hdu4819 Mosaic 二维线段树(树套树)

/*
    题目描述:原问题中拆分出来这样的一个问题:
              给定一个n*n的矩阵,查询某一块最大最小值,修改某一个位置的值,操作数共有约1e5次

    思路:二维的线段树,写法是树套树,其中修改操作略微复杂,大致可以表述为先“沿x轴生长,每生长一点,就沿着y轴延展”。
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#include<bitset>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
const int maxn = 1e3 + 5;
int locx[maxn] , locy[maxn] , n;
struct nodey
{
    int l , r , Min , Max;
};
struct nodex
{
    int l , r ;
    nodey sty[4 * maxn];
    void build(int i , int _l , int _r)
    {
        sty[i].l = _l;
        sty[i].r = _r;
        sty[i].Min = INF;
        sty[i].Max = -INF;
        int mid = _l + (_r - _l) / 2;
        if(_l == _r){
            locy[_l] = i;
            return ;
        }
        build(i << 1 , _l , mid);
        build(i << 1 | 1 , mid + 1 , _r);
    }

    int queryMax(int i , int _l , int _r)
    {
        if(sty[i].l == _l && sty[i].r == _r)
            return sty[i].Max;
        int mid = sty[i].l + (sty[i].r - sty[i].l) / 2;
        if(_r <= mid)
            return queryMax(i << 1 , _l , _r);
        else if(_l > mid)
            return queryMax(i << 1 | 1 , _l , _r);
        else
            return max( queryMax(i << 1 , _l , mid) , queryMax(i << 1 | 1 , mid + 1 , _r) );
    }

    int queryMin(int i , int _l , int _r)
    {
        if(sty[i].l == _l && sty[i].r == _r)
            return sty[i].Min;
        int mid = sty[i].l + (sty[i].r - sty[i].l) / 2;
        if(_r <= mid)
            return queryMin(i << 1 , _l , _r);
        else if(_l > mid)
            return queryMin(i << 1 | 1 , _l , _r);
        else
            return min( queryMin(i << 1 , _l , mid) , queryMin(i << 1 | 1 , mid + 1 , _r) );
    }
}stx[4 * maxn];
void build(int i , int l , int r)
{
    stx[i].l = l;
    stx[i].r = r;
    stx[i].build(1 , 1 , n);
    int mid = l + (r - l) / 2;
    if(l == r){
        locx[l] = i;
        return ;
    }
    build(i << 1 , l , mid);
    build(i << 1 | 1 , mid + 1 , r);
}
void modify(int x , int y , int val)        //更新方式:沿x轴生长,每生长一点,就沿着y轴延展
{
    int tx = locx[x] , ty = locy[y];
    stx[tx].sty[ty].Max = stx[tx].sty[ty].Min = val;
    for(int i = tx ; i ; i >>= 1 ){
        for(int j  = ty ; j ; j>>= 1){
            if(i == tx && j == ty)      continue;
            if(j == ty){
                stx[i].sty[j].Max = max(stx[i << 1].sty[j].Max , stx[i<<1|1].sty[j].Max);
                stx[i].sty[j].Min = min(stx[i << 1].sty[j].Min , stx[i<<1|1].sty[j].Min);
            }
            else{
                stx[i].sty[j].Max = max(stx[i].sty[j << 1].Max , stx[i].sty[j<<1|1].Max);
                stx[i].sty[j].Min = min(stx[i].sty[j << 1].Min , stx[i].sty[j<<1|1].Min);
            }
        }
    }
}
int queryMin(int i , int x1 , int y1 , int x2 , int y2)
{
    if(stx[i].l == x1 && stx[i].r == x2)
        return stx[i].queryMin(1 , y1 , y2);
    int mid = stx[i].l + (stx[i].r - stx[i].l)/2;
    if(x2 <= mid)
        return queryMin(i << 1 , x1 , y1 , x2 , y2);
    else if(x1 > mid)
        return queryMin(i << 1 |1 , x1 , y1 , x2 , y2);
    else
        return min( queryMin(i << 1 , x1 , y1 , mid , y2) , queryMin(i << 1 | 1 , mid + 1 , y1 , x2 , y2) ) ;
}
int queryMax(int i , int x1 , int y1 , int x2 , int y2)
{
    if(stx[i].l == x1 && stx[i].r == x2)
        return stx[i].queryMax(1 , y1 , y2);
    int mid = stx[i].l + (stx[i].r - stx[i].l)/2;
    if(x2 <= mid)
        return queryMax(i << 1 , x1 , y1 , x2 , y2);
    else if(x1 > mid)
        return queryMax(i << 1 |1 , x1 , y1 , x2 , y2);
    else
        return max( queryMax(i << 1 , x1 , y1 , mid , y2) , queryMax(i << 1 | 1 , mid + 1 , y1 , x2 , y2) ) ;
}
int main()
{
    int T  , Q , kase = 0 , x , y , L;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        build(1 , 1 , n);
        int val;
        for(int i = 1 ; i<= n ; i++){
            for(int j = 1; j<= n ; j++){
                scanf("%d",&val);
                modify(i , j , val);
            }
        }
        printf("Case #%d:\n",++kase);
        scanf("%d",&Q);
        int x1 , y1 , x2 , y2 , A , B ;
        for(int i = 0 ; i<Q ; i++){
            scanf("%d%d%d",&x , &y , &L);
            x1 = max(1 , x - L / 2);
            x2 = min(n , x + L / 2);
            y1 = max(1 , y - L / 2);
            y2 = min(n , y + L / 2);
            A = queryMax(1 , x1 , y1 , x2 , y2);
            B = queryMin(1 , x1 , y1 , x2 , y2);
            printf("%d\n" , (A + B) / 2);
            modify(x , y , (A + B) / 2);
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值