2013长春现场赛 (hdu4819)Mosaic

    xiaoxiao2025-01-28  8

    Mosaic

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total Submission(s): 1494    Accepted Submission(s): 654 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   Recommend liuyiding   |   We have carefully selected several similar problems for you:   5831  5830  5829  5827  5826  题意:给你一个大矩阵,给你一个点的位置,再给小矩阵的大小,该点为小矩阵的中心,求出小矩阵的最大值和最小值(max和min),更改中心点的值为(max+min)/2。

    思路:很明显要用二维线段树,这应该是二维线段树里的入门题。。。。。今天我也是第一次打,如果对一维线段树具有比较深刻的了解,只要稍微想想,画画图,大概就知道怎么写了,只不过代码比较多,找错要找半天。。。我手残l写成了1,还写错两次找了我半天,醉。。。下面给代码。

    #include<iostream> #include<stack> #include<cstring> #include<map> #include<string> #include<queue> #include<algorithm> #include<cstdio> #include<set> #include<cmath> using namespace std; #define maxn 805<<2 #define ll (now<<1) #define rr (now<<1|1) #define inf 0x3f3f3f3f typedef long long LL; int maxnum[maxn][maxn], minnum[maxn][maxn]; int n, index, jud, x, y, value, la, ra, lb, rb; void buildson(int l, int r, int now){ if (l == r){ if (jud){ scanf("%d", &maxnum[index][now]); minnum[index][now] = maxnum[index][now]; } else{ maxnum[index][now] = max(maxnum[index << 1][now], maxnum[index << 1 | 1][now]); minnum[index][now] = min(minnum[index << 1][now], minnum[index << 1 | 1][now]); } return; } int mid = (l + r) >> 1; buildson(l, mid, ll); buildson(mid + 1, r, rr); maxnum[index][now] = max(maxnum[index][ll], maxnum[index][rr]); minnum[index][now] = min(minnum[index][ll], minnum[index][rr]); } void build(int l, int r, int now){ if (l == r){ jud = true; index = now; buildson(1, n, 1); jud = false; return; } int mid = (l + r) >> 1; build(l, mid, ll); build(mid + 1, r, rr); index = now; buildson(1, n, 1); } void updateson(int l, int r, int now){ if (l == r){ if (jud){ minnum[index][now] = value; maxnum[index][now] = value; } else{ maxnum[index][now] = max(maxnum[index << 1][now], maxnum[index << 1 | 1][now]); minnum[index][now] = min(minnum[index << 1][now], minnum[index << 1 | 1][now]); } return; } int mid = (l + r) >> 1; if (y <= mid) updateson(l, mid, ll); else updateson(mid + 1, r, rr); maxnum[index][now] = max(maxnum[index][ll], maxnum[index][rr]); minnum[index][now] = min(minnum[index][ll], minnum[index][rr]); } void update(int l, int r, int now){ if (l == r){ jud = true; index = now; updateson(1, n, 1); jud = false; return; } int mid = (l + r) >> 1; if (x <= mid) update(l, mid, ll); else update(mid + 1, r, rr); index = now; updateson(1, n, 1); } int querymaxson(int l, int r, int now){ if (lb <= l&&rb >= r){ return maxnum[index][now]; } int mid = (l + r) >> 1; int a = 0, b = 0; if (lb <= mid) a = querymaxson(l, mid, ll); if (rb > mid) b = querymaxson(mid + 1, r, rr); return max(a, b); } int querymax(int l, int r, int now){ if (la <= l&&ra >= r){ index = now; return querymaxson(1, n, 1); } int mid = (l + r) >> 1; int a = 0, b = 0; if (la <= mid) a = querymax(l, mid, ll); if (ra > mid) b = querymax(mid + 1, r, rr); return max(a, b); } int queryminson(int l, int r, int now){ if (lb <= l&&rb >= r){ return minnum[index][now]; } int mid = (l + r) >> 1; int a = inf, b = inf; if (lb <= mid) a = queryminson(l, mid, ll); if (rb > mid) b = queryminson(mid + 1, r, rr); return min(a, b); } int querymin(int l, int r, int now){ if (la <= l&&ra >= r){ index = now; return queryminson(1, n, 1); } int mid = (l + r) >> 1; int a = inf, b = inf; if (la <= mid) a = querymin(l, mid, ll); if (ra > mid) b = querymin(mid + 1, r, rr); return min(a, b); } int main(){ int t; scanf("%d", &t); for (int tcase = 1; tcase <= t; tcase++){ scanf("%d", &n); build(1, n, 1); int q; scanf("%d", &q); printf("Case #%d:\n", tcase); while (q--){ int len; scanf("%d%d%d", &x, &y, &len); la = max(1, x - (len >> 1)); ra = min(n, x + (len >> 1)); lb = max(1, y - (len >> 1)); rb = min(n, y + (len >> 1)); int a = querymax(1, n, 1); int b = querymin(1, n, 1); value = (a + b) >> 1; printf("%d\n", value); update(1, n, 1); } } }

    转载请注明原文地址: https://ju.6miu.com/read-1295853.html
    最新回复(0)