template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
char s[N]; int t, pos, n;
intmain(){ read(t); while (t --) { scanf("%s", s + 1); pos = 1; n = strlen(s + 1); for (int i = 2; i <= n; ++ i) if (s[pos] > s[i]) pos = i; putchar(s[pos]); putchar(' '); for (int i = 1; i <= n; ++ i) if (i != pos) putchar(s[i]); putchar('\n'); } return0; }
constint N = 4010; constint INF = 0x3fffffff; template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } int t, n, a[N], q, b[N][N], tot[N]; intmain(){ read(t); while (t --) { read(n); for (int i = 1; i <= n; ++ i) read(b[i][0]); for (int i = 1; i <= n; ++ i) tot[i] = 0; for (int i = 1; i <= n; ++ i) ++ tot[b[i][0]]; for (int i = 1; i <= 2000; ++ i) { for (int j = 1; j <= n; ++ j) b[j][i] = tot[b[j][i - 1]]; for (int j = 1; j <= n; ++ j) tot[j] = 0; for (int j = 1; j <= n; ++ j) ++ tot[b[j][i]]; } read(q); while (q --) { int x, k; read(x), read(k); if (k >= 2000) printf("%d\n", b[x][2000]); elseprintf("%d\n", b[x][k]); } } return0; }
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
int t, n, tot[N], a[N], ans[N], cnt;
intgcd(int x, int y){return y ? gcd(y, x % y) : x;}
intmain(){ read(t); while (t --) { read(n); memset(tot, 0, sizeof tot); cnt = 0; for (int i = 1; i <= n; ++ i) read(a[i]); for (int i = 1; i <= n; ++ i) for (int j = 0; j < 30; ++ j) tot[j] += (a[i] >> j) & 1; int g = 0; for (int i = 0; i < 30; ++ i) g = gcd(g, tot[i]); for (int i = 1; i * i <= g; ++ i) if (!(g % i)) { ans[++ cnt] = i; if (i * i != g) ans[++ cnt] = g / i; } sort(ans + 1, ans + cnt + 1); if (!g) for (int i = 1; i <= n; ++ i) printf("%d ", i); elsefor (int i = 1; i <= cnt; ++i) printf("%d ", ans[i]); putchar('\n'); } return0; }
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
int n, a[N], b[N], head[N], ecnt(1), cnt; int npos[N], id[N], rk[N], endpos, rt;
inlinevoidadd_edge(int u, int v, int w){ e[ecnt].u = u, e[ecnt].v = v, e[ecnt].w = w; e[ecnt].nxt = head[u], head[u] = ecnt ++; }
voidbuild(int &k, int l, int r){ if (!k) k = ++ cnt; if (l == r) { id[l] = cnt; rk[cnt] = l; if (!l) endpos = cnt; return; } int mid = l + r >> 1; build(p[k].ls, l, mid); build(p[k].rs, mid + 1, r); add_edge(k, p[k].ls, 0); add_edge(k, p[k].rs, 0); }
voidconnect(int k, int l, int r, int x, int y, int c){ if (x <= l && r <= y) {add_edge(c, k, 1); return;} int mid = l + r >> 1; if (x <= mid) connect(p[k].ls, l, mid, x, y, c); if (mid < y) connect(p[k].rs, mid + 1, r, x, y, c); }
voidmapping(){ for (int i = 1; i <= n; ++ i) npos[i] = ++ cnt; for (int i = 1; i <= n; ++ i) add_edge(id[i], npos[i + b[i]], 0); for (int i = 1; i <= n; ++ i) connect(rt, 0, n, i - a[i], i, npos[i]); }
deque <int> q; int d[N], vis[N], pre[N];
inlinevoidshortest_path(int u){ mset(d, 0x3f); mset(vis, 0); d[u] = 0; q.push_back(u); while (q.size()) { int x = q.front(); q.pop_front(); if (vis[x]) continue; vis[x] = 1; for (int i = head[x]; i; i = e[i].nxt) if (d[e[i].v] > d[x] + e[i].w) { d[e[i].v] = d[x] + e[i].w; pre[e[i].v] = i; if (e[i].w) q.push_back(e[i].v); else q.push_front(e[i].v); } } }
int ans[N], acnt;
inlinevoidprint(){ if (d[id[0]] >= INF) {puts("-1"); return;} printf("%d\n", d[id[0]]); int now = id[0]; while (now) { ans[++ acnt] = pre[now]; now = e[pre[now]].u; } reverse(ans + 1, ans + acnt + 1); for (int i = 1; i <= acnt; ++ i) if (rk[e[ans[i]].v] || e[ans[i]].v == endpos) printf("%d ", rk[e[ans[i]].v]); }
intmain(){ read(n); build(rt, 0, n); for (int i = 1; i <= n; ++ i) read(a[i]); for (int i = 1; i <= n; ++ i) read(b[i]); mapping(); shortest_path(id[n]); print(); return0; }
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
structBIT { int val[N], len; inlineBIT(){len = 0;}; inlinevoidinit(int x){len = x;} inlinevoidreset(){while (len) val[len --] = 0;} inlinevoidadd(int x, int c){while (x <= len) val[x] += c, x += (x & -x);} inline ll query(int x){ ll res = 0; while (x) res += val[x], x -= (x & -x); return res; } } bit;
#define lb(l, len, x) lower_bound(l + 1, l + len + 1, x) #define ub(l, len, x) upper_bound(l + 1, l + len + 1, x)
int t, n, m, a[N], b[N], c[N], p[N], upper[N], lower[N], dct[N]; ll ans;
inlinevoidreset(){bit.reset(); ans = 0;}
inlinevoiddiscretize(){ for (int i = 1; i <= n; ++ i) dct[i] = a[i]; for (int i = 1; i <= m; ++ i) dct[n + i] = b[i]; sort(dct + 1, dct + n + m + 1); int len = unique(dct + 1, dct + n + m + 1) - dct - 1; for (int i = 1; i <= n; ++ i) a[i] = lb(dct, len, a[i]) - dct; for (int i = 1; i <= m; ++ i) b[i] = lb(dct, len, b[i]) - dct; }
voidsolve(int l, int r, int pl, int pr){ if (l > r) return; int mid = l + r >> 1, res = INF; upper[pl - 1] = 0, upper[pl] = (a[pl] > b[mid]); lower[pr + 1] = 0, lower[pr] = (a[pr] < b[mid]); for (int i = pl; i < pr; ++ i) upper[i + 1] = upper[i] + (a[i + 1] > b[mid]); for (int i = pr; i > pl; -- i) lower[i - 1] = lower[i] + (a[i - 1] < b[mid]); for (int i = pl; i <= pr; ++ i) if (upper[i - 1] + lower[i] < res) res = upper[i - 1] + lower[i], p[mid] = i; if (pl == pr) p[mid] = pl; solve(l, mid - 1, pl, p[mid]); solve(mid + 1, r, p[mid], pr); }
inlinevoidget_list(){ int ap = 1, lp = 1; for (int i = 1; i <= m; ++ i) { while (ap <= n && ap < p[i]) c[lp ++] = a[ap], ++ ap; c[lp ++] = b[i]; } while (ap <= n) c[lp ++] = a[ap], ++ ap; }
inlinevoidcalculate(){ for (int i = n + m; i >= 1; -- i) { ans += bit.query(c[i] - 1); bit.add(c[i], 1); } }
voidMAIN(){ read(n), read(m); reset(); bit.init(n + m + 1); for (int i = 1; i <= n; ++ i) read(a[i]); for (int i = 1; i <= m; ++ i) read(b[i]); discretize(); sort(b + 1, b + m + 1); a[n + 1] = INF; solve(1, m, 1, n + 1); get_list(); calculate(); printf("%lld\n", ans); }
intmain(){ read(t); while (t --) MAIN(); return0; }
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
template <typename T> inline T Max(T a, T b){return a > b ? a : b;}
structAlpinists { int s, a; inlinebooloperator < (const Alpinists b) const { int mx1 = Max(s, a), mx2 = Max(b.s, b.a); if (mx1 != mx2) return mx1 < mx2; elsereturn s == b.s ? a < b.a : s < b.s; } } p[N];
int n, d, ans;
intmain(){ read(n); read(d); for (int i = 1; i <= n; ++ i) read(p[i].s), read(p[i].a); sort(p + 1, p + n + 1); for (int i = 1; i <= n; ++ i) if (p[i].s >= d) ++ ans, d = Max(d, p[i].a); printf("%d", ans); return0; }