ll mergesort(int l, int r){ if (l >= r) return0; int mid = l + r >> 1; ll res = mergesort(l, mid) + mergesort(mid+1, r); int i = l, j = mid+1; int k = 0; while (i <= mid && j <= r) { if (q[i] <= q[j]) tmp[k++] = q[i++]; else { tmp[k++] = q[j++]; res += mid-i+1; } } while (i <= mid) tmp[k++] = q[i++]; while (j <= r) tmp[k++] = q[j++]; int t1 = l, t2 = 0; while (t1 <= r) q[t1++] = tmp[t2++]; return res; }
intmain(){ int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", q+i); printf("%lld\n", mergesort(0, n-1)); return0; }
constint N = 100010; int n, m, t; int row[N], col[N];
intsolve(int a[], int n){ staticint s[N], b[N]; for (int i = 1; i <= n; i++) s[i] = s[i-1] + a[i]; int avg = t / n ; for (int i = 1; i <= n; i++) b[i] = (i-1) * avg - (s[i] - s[1]); nth_element(b+1, b+n/2+1, b+1+n); int base = b[n/2+1], res = 0; for (int i = 1; i <= n; i++) res += abs(base - b[i]); return res; }
intmain(){ scanf("%d%d%d", &n, &m, &t); for (int i = 0; i < t; i++) { int x, y; scanf("%d%d", &x, &y); row[x]++, col[y]++; } if (t % n && t % m) puts("impossible"); else { longlong res = 0; if (t % n == 0 && t % m == 0) printf("both"); elseif (t % n == 0) printf("row"); elseprintf("column"); if (t % m == 0) res += solve(col, m); if (t % n == 0) res += solve(row, n); printf(" %lld\n", res); } return0; }
voidrotate(int x){ int y = tr[x].p, z = tr[y].p; int k = tr[y].s[1] == x; tr[z].s[tr[z].s[1] == y] = x, tr[x].p = z; tr[y].s[k] = tr[x].s[k^1], tr[tr[x].s[k^1]].p = y; tr[x].s[k^1] = y, tr[y].p = x; pushup(y), pushup(x); }
voidsplay(int x, int k){ while (tr[x].p != k) { int y = tr[x].p, z = tr[y].p; if (z != k) { if ((tr[y].s[0] == x) ^ (tr[z].s[0] == y)) rotate(x); elserotate(y); } rotate(x); }
if (k == 0) root = x; }
voidinsert(int v){ int u = root, p = 0; while (u && tr[u].v != v) { p = u, u = tr[u].s[v > tr[u].v]; } if (u) tr[u].cnt++; else { u = ++idx; tr[u].init(p, v); if (p) tr[p].s[v > tr[p].v] = u; } splay(u, 0); }
intget(){ int k = (tr[root].size+1) / 2, u = root;
while (u) { if (k <= tr[tr[u].s[0]].size) u = tr[u].s[0]; elseif (k <= tr[tr[u].s[0]].size + tr[u].cnt) { splay(u, 0); return tr[u].v; } else k -= tr[tr[u].s[0]].size + tr[u].cnt, u = tr[u].s[1]; }
return-1; }
intmain(){ scanf("%d", &n); for (int i = 1; i <= n; i++) { int v; scanf("%d", &v); insert(v); if (i & 1) { printf("%d\n", get()); } } return0; }
typedeflonglong LL; constint N = 1000010; int n, m, p, x, y, a[N];
LL count(int l, int r){ staticint tmp[N]; if (l >= r) return0;
int mid = (l+r) >> 1, k = 0; LL res = count(l, mid) + count(mid+1, r);
int i = l, j = mid+1; while (i <= mid && j <= r) { if (a[i] <= a[j]) tmp[k++] = a[i++]; else tmp[k++] = a[j++], res += mid-i+1; } while (i <= mid) tmp[k++] = a[i++]; while (j <= r) tmp[k++] = a[j++], res += mid-i+1;
for (k = 0, i = l; i <= r; i++, k++) a[i] = tmp[k]; return res; }
intmain(){ while (scanf("%d%d", &m, &n), m || n) { p = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { scanf("%d", &a[p]); if (a[p]) p++; else x = i, y = j; } printf("%s\n", check() ? "YES" : "NO"); } return0; }