HDU 4819 Mosaic (二维线段树)

题意:

           二维线段树,查询区间最大最小值,修改单点。

思路:

           树套树,树和树间各写各的操作,注意conbine(自下而上的结合)和trans(自上而下的连接)即可。

           这样写,代码量会长,但思路分明不易出错。

代码:

#include <bits/stdc++.h>
using namespace std;
#define ls l,mid,rt*2,id
#define rs mid+1,r,rt*2+1,id
#define sf l,r,rt,id
#define mi (l+r)/2
const int INF=0x3f3f3f3f;
const int MAXN=900;
int T,n,m,f[4],x,y,z,v;
int minn1[2][MAXN*4],maxn1[2][MAXN*4],st1,en1,ans1;
int minn2[MAXN][MAXN*4],maxn2[MAXN][MAXN*4],st2,en2,ans2;

int transpos(int l,int floor,int id){
    return (id-1)*f[floor]+l;
}
void conbine12(int l,int r,int rt,int id){
    minn1[id][rt]=minn2[transpos(l,1,id)][1];
    maxn1[id][rt]=maxn2[transpos(l,1,id)][1];
    return ;
}
void push_up2(int l,int r,int rt,int id){
    minn2[id][rt]=min(minn2[id][rt*2],minn2[id][rt*2+1]);
    maxn2[id][rt]=max(maxn2[id][rt*2],maxn2[id][rt*2+1]);
    return ;
}
void push_up1(int l,int r,int rt,int id){
    minn1[id][rt]=min(minn1[id][rt*2],minn1[id][rt*2+1]);
    maxn1[id][rt]=max(maxn1[id][rt*2],maxn1[id][rt*2+1]);
    return ;
}
void build2(int l,int r,int rt,int id){
    if(l==r){
        scanf("%d",&minn2[id][rt]);
        maxn2[id][rt]=minn2[id][rt];
        return ;
    }
    int mid=mi;
    build2(ls);
    build2(rs);
    push_up2(sf);
    return ;
}
void build1(int l,int r,int rt,int id){
    if(l==r){
        build2(1,n,1,transpos(l,1,id));
        conbine12(sf);
        return ;
    }
    int mid=mi;
    build1(ls);
    build1(rs);
    push_up1(sf);
    return ;
}
bool check2(int l,int r,int rt,int id){
    if(v==1){
        if(maxn2[id][rt]<ans1) return 0;
    }else{
        if(minn2[id][rt]>ans2) return 0;
    }
    return 1;
}
bool check1(int l,int r,int rt,int id){
    if(v==1){
        if(maxn1[id][rt]<ans1) return 0;
    }else{
        if(minn1[id][rt]>ans2) return 0;
    }
    return 1;
}
void query2(int l,int r,int rt,int id){
    if(st2>r||en2<l||!check2(sf)) return;
    if(l==r){
        if(v==1) ans1=maxn2[id][rt];
        else ans2=minn2[id][rt];
        return ;
    }
    int mid=mi;
    query2(ls);
    query2(rs);
    return ;
}
void query1(int l,int r,int rt,int id){
    if(st1>r||en1<l||!check1(sf)) return;
    if(l==r){
        query2(1,n,1,transpos(l,1,id));
        return ;
    }
    int mid=mi;
    query1(ls);
    query1(rs);
    return ;
}
void update2(int l,int r,int rt,int id){
    if(st2>r||en2<l) return;
    if(l==r){
        maxn2[id][rt]=minn2[id][rt]=v;
        return ;
    }
    int mid=mi;
    update2(ls);
    update2(rs);
    push_up2(sf);
    return ;
}
void update1(int l,int r,int rt,int id){
    if(st1>r||en1<l) return;
    if(l==r){
        update2(1,n,1,transpos(l,1,id));
        conbine12(sf);
        return ;
    }
    int mid=mi;
    update1(ls);
    update1(rs);
    push_up1(sf);
    return ;
}

void solve(){
    scanf("%d",&m);
    while(m--){
        scanf("%d%d%d",&x,&y,&z);
        st1=max(1,x-z/2);en1=min(n,x+z/2);
        st2=max(1,y-z/2);en2=min(n,y+z/2);
        v=1;ans1=-INF;
        query1(1,n,1,1);
        v=0;ans2=INF;
        query1(1,n,1,1);
        v=(ans1+ans2)/2;
        st1=en1=x;
        st2=en2=y;
        update1(1,n,1,1);
        printf("%d\n",v);
    }
}
int main(){
    scanf("%d",&T);
    for(int Case=1;Case<=T;Case++){
        scanf("%d",&n);
        f[1]=1;f[2]=n;
        build1(1,n,1,1);
        printf("Case #%d:\n",Case);
        solve();
    }
}

The God of sheep decides to pixelate some pictures (i.e., change them into pictures with mosaic). Here's how he is gonna make it: for each picture, he divides the picture into n x n cells, where each cell is assigned a color value. Then he chooses a cell, and checks the color values in the L x L region whose center is at this specific cell. Assuming the maximum and minimum color values in the region is A and B respectively, he will replace the color value in the chosen cell with floor((A + B) / 2). 

Can you help the God of sheep?
Input
The first line contains an integer T (T ≤ 5) indicating the number of test cases. Then T test cases follow. 

Each test case begins with an integer n (5 < n < 800). Then the following n rows describe the picture to pixelate, where each row has n integers representing the original color values. The j-th integer in the i-th row is the color value of cell (i, j) of the picture. Color values are nonnegative integers and will not exceed 1,000,000,000 (10^9). 

After the description of the picture, there is an integer Q (Q ≤ 100000 (10^5)), indicating the number of mosaics. 

Then Q actions follow: the i-th row gives the i-th replacement made by the God of sheep: xi, yi, Li (1 ≤ xi, yi ≤ n, 1 ≤ Li < 10000, Li is odd). This means the God of sheep will change the color value in (xi, yi) (located at row xi and column yi) according to the Li x Li region as described above. For example, an query (2, 3, 3) means changing the color value of the cell at the second row and the third column according to region (1, 2) (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4). Notice that if the region is not entirely inside the picture, only cells that are both in the region and the picture are considered. 

Note that the God of sheep will do the replacement one by one in the order given in the input.��
Output
For each test case, print a line "Case #t:"(without quotes, t means the index of the test case) at the beginning. 

For each action, print the new color value of the updated cell.
Sample Input
1
3
1 2 3
4 5 6
7 8 9
5
2 2 1
3 2 3
1 1 3
1 2 3
2 2 3
Sample Output
Case #1:
5
6
3
4
6


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值