During the last 24 hours Hamed and Malek spent all their time playing “Sharti”. Now they are too exhausted to finish the last round. So they asked you for help to determine the winner of this round.
“Sharti” is played on a n × n board with some of cells colored white and others colored black. The rows of the board are numbered from top to bottom using number 1 to n. Also the columns of the board are numbered from left to right using numbers 1 to n. The cell located at the intersection of i-th row and j-th column is denoted by (i, j).
The players alternatively take turns. In each turn the player must choose a square with side-length at most k with its lower-right cell painted white. Then the colors of all the cells in this square are inversed (white cells become black and vice-versa). The player who cannot perform a move in his turn loses.
You know Hamed and Malek are very clever and they would have played their best moves at each turn. Knowing this and the fact that Hamed takes the first turn, given the initial board as described in the input, you must determine which one of them will be the winner.
Input
In this problem the initial board is specified as a set of m rectangles. All cells that lie inside at least one of these rectangles are colored white and the rest are colored black.
In the first line of input three space-spereated integers n, m, k (1 ≤ k ≤ n ≤ 109, 1 ≤ m ≤ 5·104) follow, denoting size of the board, number of rectangles and maximum size of the turn square during the game, respectively.
In i-th line of the next m lines four space-seperated integers a i, b i, c i, d i (1 ≤ a i ≤ c i ≤ n, 1 ≤ b i ≤ d i ≤ n) are given meaning that i-th rectangle determining the initial board is a rectangle with upper-left cell at (a i, b i) and lower-right cell at (c i, d i).
Output
If Hamed wins, print “Hamed”, otherwise print “Malek” (without the quotes).
Examples
input
5 2 1
1 1 3 3
2 2 4 4
output
Malek
input
12 5 7
3 4 5 6
1 2 1 2
4 5 9 9
8 6 12 10
12 4 12 4
output
Hamed
Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | #include <cstring> #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <ctime> #include <memory.h> #include <cassert> using namespace std; const int N = 888888; int mn[N], add[N], cnt[N]; int ry[N]; void build( int x, int l, int r) { mn[x] = 0; add[x] = 0; cnt[x] = ry[r] - ry[l - 1]; if (l < r) { int y = (l + r) >> 1; build(x + x, l, y); build(x + x + 1, y + 1, r); } } void modify( int x, int l, int r, int ll, int rr, int v) { if (ll <= l && r <= rr) { add[x] += v; return ; } if (add[x] != 0) { add[x + x] += add[x]; add[x + x + 1] += add[x]; add[x] = 0; } int y = (l + r) >> 1; if (ll <= y) { modify(x + x, l, y, ll, rr, v); } if (rr > y) { modify(x + x + 1, y + 1, r, ll, rr, v); } int w1 = mn[x + x] + add[x + x]; int w2 = mn[x + x + 1] + add[x + x + 1]; mn[x] = (w1 < w2 ? w1 : w2); cnt[x] = (w1 == mn[x] ? cnt[x + x] : 0) + (w2 == mn[x] ? cnt[x + x + 1] : 0); } int xa[N], ya[N], xb[N], yb[N]; int rya[N], ryb[N]; int main() { int n, m, k; scanf ( "%d %d %d" , &n, &m, &k); for ( int i = 0; i < m; i++) { scanf ( "%d %d %d %d" , xa + i, ya + i, xb + i, yb + i); } int step = 1; while (step <= k) { vector < pair < int , int > > ys; for ( int i = 0; i < m; i++) { if (xa[i] > xb[i] || ya[i] > yb[i]) { continue ; } ys.push_back(make_pair(ya[i] - 1, i)); ys.push_back(make_pair(yb[i], ~i)); } if (ys.empty()) { break ; } sort(ys.begin(), ys.end()); int t = 0; ry[0] = ys[0].first; int yss = ys.size(); for ( int i = 0; i < yss; i++) { if (i > 0 && ys[i].first != ys[i - 1].first) { t++; ry[t] = ys[i].first; } if (ys[i].second >= 0) { rya[ys[i].second] = t + 1; } else { ryb[~ys[i].second] = t; } } if (t == 0) { break ; } build(1, 1, t); vector < pair < int , int > > e; for ( int i = 0; i < m; i++) { if (xa[i] > xb[i] || ya[i] > yb[i]) { continue ; } e.push_back(make_pair(xa[i] - 1, i)); e.push_back(make_pair(xb[i], ~i)); } sort(e.begin(), e.end()); int es = e.size(); int ans = 0; for ( int id = 0; id < es - 1; id++) { int i = e[id].second; if (i >= 0) { modify(1, 1, t, rya[i], ryb[i], 1); } else { modify(1, 1, t, rya[~i], ryb[~i], -1); } int dx = e[id + 1].first - e[id].first; int mul = ry[t] - ry[0]; if (mn[1] + add[1] == 0) { mul -= cnt[1]; } ans ^= ((dx & 1) * (mul & 1)); } if (ans == 1) { puts ( "Hamed" ); return 0; } for ( int i = 0; i < m; i++) { xa[i] = ((xa[i] + 1) >> 1); ya[i] = ((ya[i] + 1) >> 1); xb[i] = (xb[i] >> 1); yb[i] = (yb[i] >> 1); } n >>= 1; step <<= 1; } puts ( "Malek" ); return 0; } |