HDU-4819-Mosaic (2d line segment tree)

Source: Internet
Author: User
Problem descriptionthe 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?
Inputthe 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 integerand 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.
Outputfor 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
131 2 34 5 67 8 952 2 13 2 31 1 31 2 32 2 3
 
Sample output
Case #1:56346
 
Source2013 Asia Regional Changchun


Idea: Two-dimensional line segment tree, single point update.


# Include <stdio. h> # define INF 1000000001 # define min (a, B) (a <B? A: B) # define max (A, B) (A> B? A: B) int MX [3200] [3200], Mn [3200] [3200], n, minnum, maxnum; void buildy (INT X, int idx, int s, int e, bool flag) // 1 indicates that the first dimension is a leaf node, 0 indicates that the first dimension is not a leaf node {If (S = e) {If (FLAG) {scanf ("% d", & Mn [x] [idx]); MX [x] [idx] = Mn [x] [idx];} else {int mid = (S + E)> 1; buildy (x, idx <1, S, mid, flag); buildy (X, idx <1 | 1, Mid + 1, E, flag); MX [x] [idx] = max (MX [x] [idx <1], MX [x] [idx <1 | 1]); Mn [x] [idx] = min (Mn [x] [idx <1], mn [x] [idx <1 | 1]);} If (! Flag) // if the first dimension is not a leaf node, update the first dimension {MX [x] [idx] = max (MX [x <1] [idx], MX [x <1 | 1] [idx]); Mn [x] [idx] = min (Mn [x <1] [idx], mn [x <1 | 1] [idx]) ;}} void build (INT idx, int S, int e) {If (s! = E) {int mid = (S + E)> 1; build (idx <1, S, mid); Build (idx <1 | 1, mid + 1, e); buildy (idx, 1, 1, n, 0);} else buildy (idx, 1, 1, n, 1);} void updatey (int x, int idx, int S, int e, int POs, int Val, bool flag) {If (S = E) mn [x] [idx] = Mx [x] [idx] = val; else {int mid = (S + E)> 1; if (Pos <= mid) updatey (x, idx <1, S, mid, POs, Val, flag); else updatey (x, idx <1 | 1, Mid + 1, E, POs, val, flag); MX [x] [idx] = max (MX [x] [idx <1], MX [x] [idx <1 | 1]); mn [x] [idx] = Min (Mn [x] [idx <1], Mn [x] [idx <1 | 1]);} If (! Flag) // if the first dimension is not a leaf node, update the first dimension {MX [x] [idx] = max (MX [x <1] [idx], MX [x <1 | 1] [idx]); Mn [x] [idx] = min (Mn [x <1] [idx], mn [x <1 | 1] [idx]) ;}} void Update (INT idx, int S, int e, int POs, int Posy, int Val) {If (S = e) updatey (idx, 1,1, N, Posy, Val, 1); else {int mid = (S + E)> 1; if (Pos <= mid) Update (idx <1, S, mid, POs, Posy, Val); else Update (idx <1 | 1, Mid + 1, e, POs, Posy, Val); updatey (idx, 1,1, N, Posy, Val, 0) ;}} void queryy (int x, int idx, int s, int e, int L, int R) {If (S = L & E = r) {minnum = min (minnum, Mn [x] [idx]); maxnum = max (maxnum, MX [x] [idx]);} else {int mid = (S + E)> 1; if (r <= mid) queryy (x, idx <1, S, mid, L, R); else if (L> mid) queryy (x, idx <1 | 1, Mid + 1, e, L, R); else {queryy (x, idx <1, S, mid, L, mid); queryy (x, idx <1 | 1, mid + 1, E, Mid + 1, R) ;}} void query (INT idx, int S, int e, int L, int R, int ly, int ry) {If (S = L & E = r) queryy (idx, 1,1, N, Ly, ry); else {int mid = (S + E)> 1; if (r <= mid) query (idx <1, S, mid, L, R, Ly, ry); else if (L> mid) query (idx <1 | 1, Mid + 1, E, L, R, Ly, ry); else {query (idx <1, S, mid, l, mid, Ly, ry); query (idx <1 | 1, Mid + 1, E, Mid + 1, R, Ly, ry) ;}} int main () {int T, I, Q, A, B, C, L, R, Ly, Ry, T; scanf ("% d", & T ); for (I = 1; I <= T; I ++) {scanf ("% d", & N); Build (1, 1, n ); scanf ("% d", & Q); printf ("case # % d: \ n", I); While (Q --) {scanf ("% d", & A, & B, & C); L = max (a-c/2,1 ); R = min (A + C/2, n); Ly = max (B-c/2, 1); ry = min (B + C/2, N ); minnum = inf; maxnum =-INF; query (1,1, n, l, R, Ly, ry); t = (minnum + maxnum)/2; printf ("% d \ n", T); Update (1, 1, n, a, B, T );}}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.