HDU 4819 Mosaic D区段树

pid=4819">点击打开链接

Mosaic

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)

Total Submission(s): 657    Accepted Submission(s): 248

Problem Description

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

Source

2013 Asia Regional Changchun

给你一个n*n的矩阵,然后给你m个请求。每一个请求有x,y,r这个三个数。代表在以第x行第y列为中心,长度为l的矩阵中,找一个最大值和一个最小值,并将(x,y)处改为(最大值+最小值)/2.

二维线段树中的一维代表行,还有一维代表列。

//1859MS	50772K
#include<stdio.h>
#include<algorithm>
#define M 1007
#define eps 1e-4
#define inf 0x3f3f3f3f
using namespace std;
int lx[M],ly[M];
int n;
struct Sub_Tree
{
    int left,right,minn,maxx;
    int mid(){return (left+right)>>1;}
};
struct Tree
{
    int left,right;
    int mid(){return (left+right)>>1;}
    Sub_Tree subtree[4*M];
}tree[M*4];

void build_subtree(int l,int r,int i,int fa)
{
    tree[fa].subtree[i].left=l;
    tree[fa].subtree[i].right=r;
    tree[fa].subtree[i].minn=inf;
    tree[fa].subtree[i].maxx=-inf;
    if(l==r){ly[l]=i;return;}
    int mid=(l+r)>>1;
    build_subtree(l,mid,i<<1,fa);
    build_subtree(mid+1,r,(i<<1)|1,fa);
}

void build(int l,int r,int i)
{
    tree[i].left=l;tree[i].right=r;
    build_subtree(1,n,1,i);
    if(l==r){lx[l]=i;return;}
    int mid=(l+r)>>1;
    build(l,mid,i<<1);
    build(mid+1,r,(i<<1)|1);
}

void update(int x,int y,int val)
{
    int xx=lx[x];
    int yy=ly[y];
    tree[xx].subtree[yy].minn=tree[xx].subtree[yy].maxx=val;
    for(int i=xx;i;i>>=1)
        for(int j=yy;j;j>>=1)
        {
            if(i==xx&&j==yy)continue;
            if(j==yy)
            {
                tree[i].subtree[j].minn=min(tree[i<<1].subtree[j].minn,tree[(i<<1)|1].subtree[j].minn);
                tree[i].subtree[j].maxx=max(tree[i<<1].subtree[j].maxx,tree[(i<<1)|1].subtree[j].maxx);
            }
            else
            {
                tree[i].subtree[j].minn=min(tree[i].subtree[j<<1].minn,tree[i].subtree[(j<<1)|1].minn);
                tree[i].subtree[j].maxx=max(tree[i].subtree[j<<1].maxx,tree[i].subtree[(j<<1)|1].maxx);
            }
        }
}

int query_subtree_min(int a1,int a2,int i,int fa)
{
    if(tree[fa].subtree[i].left==a1&&tree[fa].subtree[i].right==a2)return tree[fa].subtree[i].minn;
    int mid=tree[fa].subtree[i].mid();
    if(a2<=mid)return query_subtree_min(a1,a2,i<<1,fa);
    else if(mid<a1)return query_subtree_min(a1,a2,(i<<1)|1,fa);
    else return min(query_subtree_min(a1,mid,i<<1,fa),query_subtree_min(mid+1,a2,(i<<1)|1,fa));
}

int query_min(int x1,int x2,int y1,int y2,int i)
{
    if(tree[i].left==x1&&tree[i].right==x2)return query_subtree_min(y1,y2,1,i);
    int mid=tree[i].mid();
    if(x2<=mid)return query_min(x1,x2,y1,y2,i<<1);
    else if(mid<x1)return query_min(x1,x2,y1,y2,(i<<1)|1);
    else return min(query_min(x1,mid,y1,y2,i<<1),query_min(mid+1,x2,y1,y2,(i<<1)|1));
}

int query_subtree_max(int a1,int a2,int i,int fa)
{
    if(tree[fa].subtree[i].left==a1&&tree[fa].subtree[i].right==a2)return tree[fa].subtree[i].maxx;
    int mid=tree[fa].subtree[i].mid();
    if(a2<=mid)return query_subtree_max(a1,a2,i<<1,fa);
    else if(mid<a1)return query_subtree_max(a1,a2,(i<<1)|1,fa);
    else return max(query_subtree_max(a1,mid,i<<1,fa),query_subtree_max(mid+1,a2,(i<<1)|1,fa));
}

int query_max(int x1,int x2,int y1,int y2,int i)
{
    if(tree[i].left==x1&&tree[i].right==x2)return query_subtree_max(y1,y2,1,i);
    int mid=tree[i].mid();
    if(x2<=mid)return query_max(x1,x2,y1,y2,i<<1);
    else if(mid<x1)return query_max(x1,x2,y1,y2,(i<<1)|1);
    else return max(query_max(x1,mid,y1,y2,i<<1),query_max(mid+1,x2,y1,y2,(i<<1)|1));
}

int main()
{
    int t,cas=1;
    scanf("%d",&t);
    while(t--)
    {
       int m,a,x,y,r;
       scanf("%d",&n);
       build(1,n,1);
       for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a);
                update(i,j,a);
            }
        printf("Case #%d:\n",cas++);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&x,&y,&r);
            int x1=max(1,x-r/2);
            int x2=min(n,x+r/2);
            int y1=max(1,y-r/2);
            int y2=min(n,y+r/2);
            int maxx=query_max(x1,x2,y1,y2,1);
            int minn=query_min(x1,x2,y1,y2,1);
            int ans=(minn+maxx)>>1;
            printf("%d\n",ans);
            update(x,y,ans);
        }
    }
    return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

时间: 2024-11-10 15:17:19

HDU 4819 Mosaic D区段树的相关文章

hdu 4819 二维线段树模板

/* HDU 4819 Mosaic 题意:查询某个矩形内的最大最小值, 修改矩形内某点的值为该矩形(Mi+MA)/2; 二维线段树模板: 区间最值,单点更新. */ #include<bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 1010; int N, Q; struct Nodey { int l, r; int Max, Min; }; int locx[MAXN], l

HDU 4819 Mosaic

题意: 一个矩形内每个格子都有一个值  现在有q个操作  每个操作给出坐标(x,y)和长度L  每次操作输出以(x,y)为中心的边长为L的矩形内的最大值和最小值之和的一半  并将这个值更新到(x,y)坐标上 思路: 区间查询最大最小值  单点更新  明显是线段树的特征  不过这里是二维的线段树  我用的是树套树的写法 我对二维线段树的理解:(个人理解不一定正确) 初始化麻烦  相当于做n*n次单点更新 树套树操作的思维就先找到满足第一位的区间  这个区间对应几棵树  然后在这几棵树(即第二维)里

HDU 4819 Mosaic (二维线段树)

Problem Description 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

HDU 4819 Mosaic --二维线段树(树套树)

题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的线段树,所以没跳出来.后来熟悉了一下,原来很多细节地方都没有考虑到. 这里build,update,query都分为两个函数,第一个为Y轴的(sub_update),第二个为X轴的(update),不仅每个sub_update或sub_build后面要加Y轴的pushup函数,而且每个update或

HDU 4819 Mosaic 【二维线段树】

题目大意:给你一个n*n的矩阵,每次找到一个点(x,y)周围l*l的子矩阵中的最大值a和最小值b,将(x,y)更新为(a+b)/2 思路:裸的二维线段树 #include<iostream>#include<cstdio>#include <math.h>#include<algorithm>#include<string.h>#include<queue>#define MOD 10000007#define maxn 3500#d

#树套树,二维线段树#HDU 4819 Mosaic

题目 多组数据,给定一个\(n*n\)的矩阵(\(n\leq 80,a_{i,j}\leq 10^9\)) 多组询问一个以\((x,y)\)为中心,边长为\(L\)的子矩阵最大值\(mx\)和最小值\(mn\), 并将\((x,y)\)这一个位置修改为\(\lfloor\frac{mn+mx}{2}\rfloor\),每次询问输出修改后的\((x,y)\) 分析 二维线段树裸题,反正之前也没敲过, 其实和一维线段树相近,找到\(lx\sim rx\)的下标 再按照一维线段树的方式修改最大最小值就

HDU 4902 Nice boat(线段树)

HDU Nice boat 题目链接 题意:给定一个序列,两种操作,把一段变成x,把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 思路:线段树,每个结点多一个cover表示该位置以下区间是否数字全相同,然后每次延迟操作,最后输出的时候单点查询即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 1

HDU 3966 Aragorn&#39;s Story (树链点权剖分,成段修改单点查询)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. 1 //树链剖分 边权修改 单点查询 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstdio> 6 using namespace std; 7 const int M

Hdu 3966 Aragorn&#39;s Story (树链剖分 + 线段树区间更新)

题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2:( D, i, j, x ) 从i到j的路径上经过的节点全部都减去x: 3:(Q, x) 查询节点x的权值为多少? 解题思路: 可以用树链剖分对节点进行hash,然后用线段树维护(修改,查询),数据范围比较大,要对线段树进行区间更新 1 #include <cstdio> 2 #include