text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; const int N = 200005; const long long inf = 1e18; int n, m, q, fa[50][N]; long long T[N], F[50][N]; struct node { int a, b, c, d; int id; } A[N]; vector<node> v; int cov[N << 2], tj[N << 2]; void clear_tree() { memset(cov, 0, sizeof(cov)); memset(tj, 0, sizeof(tj)); } void pushdown(int o) { if (cov[o]) { tj[(o << 1)] = tj[(o << 1 | 1)] = cov[(o << 1)] = cov[(o << 1 | 1)] = cov[o]; cov[o] = 0; } } void modify(int o, int l, int r, int pl, int pr, int x) { if (l >= pl && r <= pr) { tj[o] = cov[o] = x; return; } if (l > pr || r < pl) return; pushdown(o); int mid = (l + r) >> 1; modify((o << 1), l, mid, pl, pr, x), modify((o << 1 | 1), mid + 1, r, pl, pr, x); } int query(int o, int l, int r, int p) { if (l == r) return tj[o]; pushdown(o); int mid = (l + r) >> 1; return mid >= p ? query((o << 1), l, mid, p) : query((o << 1 | 1), mid + 1, r, p); } void work() { sort(v.begin(), v.end(), [&](const node &p, const node &q) { int x = min(p.a, p.c); int y = min(q.a, q.c); return x != y ? x < y : p.id < q.id; }); clear_tree(); for (auto &p : v) { if (p.b == p.d && p.a > p.c) { int t = query(1, 0, m, p.b); fa[0][p.id] = t; if (!t) F[0][p.id] = inf; else F[0][p.id] = abs(p.c - A[t].c) + abs(p.d - A[t].d); } if (p.id <= n) modify(1, 0, m, min(p.b, p.d), max(p.b, p.d), p.id); } } void rotate() { for (auto &p : v) { int a = p.a, b = p.b, c = p.c, d = p.d; p.a = b; p.b = m - a; p.c = d; p.d = m - c; } for (int i = 1; i <= n; i++) { int a = A[i].a, b = A[i].b, c = A[i].c, d = A[i].d; A[i] = (node){b, m - a, d, m - c, A[i].id}; } } void build() { for (int i = 1; i < 50; ++i) for (int j = 1; j <= n + q; j++) { fa[i][j] = fa[i - 1][fa[i - 1][j]]; F[i][j] = F[i - 1][j] + F[i - 1][fa[i - 1][j]]; if (F[i][j] > inf) F[i][j] = inf; } } int main() { scanf("%d%d", &n, &m); for (int i = 1, a, b, c, d; i <= n; i++) { scanf("%d%d%d%d", &a, &b, &c, &d); v.push_back(A[i] = (node){a, b, c, d, i}); } scanf("%d", &q); for (int i = 1; i <= q; i++) { int x, y; char opt[3]; scanf("%d%d%s%lld", &x, &y, opt, T + i); int a, b, c, d; a = c = x; b = d = y; switch (*opt) { case 'R': --a; break; case 'L': ++a; break; case 'U': --b; break; case 'D': ++b; break; }; v.push_back(A[i + n] = (node){a, b, c, d, i + n}); } for (int t = 4; t; t--) { work(); rotate(); } build(); for (int i = 1; i <= q; i++) { long long t = T[i]; int u = i + n, v; for (int j = 49; ~j; --j) if (F[j][u] <= t) t -= F[j][u], u = fa[j][u]; v = fa[0][u]; int dx, dy; long long r, x = A[u].c, y = A[u].d; dx = A[u].c - A[u].a, dy = A[u].d - A[u].b; if (dx) dx /= abs(dx); if (dy) dy /= abs(dy); if (!v) r = inf; else if (dx) { if (A[v].a <= A[u].c && A[v].c > A[u].c) r = 0; else if (A[v].a >= A[u].c && A[v].c < A[u].c) r = 0; else r = abs(A[u].c - A[v].c); } else if (dy) { if (A[v].b <= A[u].d && A[v].d > A[u].d) r = 0; else if (A[v].b >= A[u].d && A[v].d < A[u].d) r = 0; else r = abs(A[u].d - A[v].d); } x += dx * min(t, r); y += dy * min(t, r); t -= min(t, r); if (v) { dx = A[v].c - A[v].a, dy = A[v].d - A[v].b; if (dx) dx /= abs(dx); if (dy) dy /= abs(dy); x += dx * t; y += dy * t; } x = max(0ll, min<long long>(x, m)); y = max(0ll, min<long long>(y, m)); printf("%lld %lld\n", x, y); } return 0; }
#include <bits/stdc++.h> using namespace std; int N, B, Q, SArr[100001]; int Next[51][100001]; long long dist[51][100001], Time[100001]; int dis(int x1, int y1, int x2, int y2) { return abs(x1 - x2) + abs(y1 - y2); } struct Segment { int x1, y1, x2, y2, dir; } seg[100001]; int IT[300001], SZ, LastC; struct point { int x, y; } Res[100001], Last[100001]; struct Xdir { int x1, y1, x2, y2, num, dir, tx; bool operator<(const Xdir &p) const { return tx < p.tx; } } X[100001]; struct Ydir { int x1, y1, x2, y2, num, dir, ty; bool operator<(const Ydir &p) const { return ty < p.ty; } } Y[100001]; struct vec { int t, dir, num; } tp; vector<vec> vecX[100001], vecY[100001]; void input() { scanf("%d%d", &N, &B); int i, x1, y1, x2, y2, dir; for (i = 1; i <= N; i++) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) dir = y1 < y2 ? 0 : 1; else dir = x1 < x2 ? 3 : 2; seg[i].x1 = x1, seg[i].y1 = y1, seg[i].x2 = x2, seg[i].y2 = y2, seg[i].dir = dir; X[i].x1 = x1, X[i].y1 = y1, X[i].x2 = x2, X[i].y2 = y2, X[i].dir = dir, X[i].num = i; Y[i].x1 = x1, Y[i].y1 = y1, Y[i].x2 = x2, Y[i].y2 = y2, Y[i].dir = dir, Y[i].num = i; } scanf("%d", &Q); char dir2[2]; for (i = 0; i < Q; i++) { scanf("%d%d", &x1, &y1); scanf("%s", dir2); if (dir2[0] == 'U') dir = 0; else if (dir2[0] == 'D') dir = 1; else if (dir2[0] == 'L') dir = 2; else dir = 3; scanf("%lld", &Time[i]); if (dir < 2) { tp.t = x1, tp.dir = dir, tp.num = i; vecY[y1].push_back(tp); } else { tp.t = y1, tp.dir = dir, tp.num = i; vecX[x1].push_back(tp); } } SZ = 1; while (SZ <= B) SZ *= 2; } void spread(int x) { IT[x * 2] = IT[x * 2 + 1] = IT[x]; IT[x] = 0; } void Ins(int node, int b, int e, int s, int l, int x) { if (b == s && e == l) { IT[node] = x; return; } if (IT[node]) spread(node); int m = (b + e) >> 1, c1 = node << 1; if (s <= m) Ins(c1, b, m, s, min(l, m), x); if (l > m) Ins(c1 + 1, m + 1, e, max(s, m + 1), l, x); } int find(int node, int b, int e, int x) { if (IT[node]) return IT[node]; if (b == e) return 0; int m = (b + e) >> 1; if (x > m) return find(node * 2 + 1, m + 1, e, x); else return find(node * 2, b, m, x); } void Calc(int x1, int y1, int x2, int y2, int dir, int a) { long long T = Time[a]; int d; if (T > dis(x1, y1, x2, y2)) { Res[a].x = x2, Res[a].y = y2; return; } Res[a].x = x1, Res[a].y = y1; if (dir < 2) { d = abs(y2 - y1); if (d >= T) { if (y1 < y2) Res[a].y += T; else Res[a].y -= T; return; } Res[a].y = y2, T -= d; if (x1 < x2) Res[a].x += T; else Res[a].x -= T; return; } d = abs(x2 - x1); if (d >= T) { if (x1 < x2) Res[a].x += T; else Res[a].x -= T; return; } Res[a].x = x2, T -= d; if (y1 < y2) Res[a].y += T; else Res[a].y -= T; return; } void Make_GraphX(int dir) { int i, st, ed, t, d, t2, pm, px, j, x, y; for (i = 1; i <= SZ + B + 1; i++) IT[i] = 0; if (dir == 3) st = N, ed = 0, pm = -1, px = B; else st = 1, ed = N + 1, pm = 1, px = 0; if (dir == 3) for (i = 1; i <= N; i++) X[i].tx = max(X[i].x1, X[i].x2); else for (i = 1; i <= N; i++) X[i].tx = min(X[i].x1, X[i].x2); sort(X + 1, X + N + 1); for (i = st;; i += pm) { while (i == ed || px != X[i].tx) { if (px == -1 || px == B + 1) break; for (j = 0; j < vecX[px].size(); j++) { tp = vecX[px][j]; if (tp.dir != dir) continue; t = find(1, 0, SZ - 1, tp.t); t2 = tp.num; if (t) { d = dis(px, tp.t, seg[t].x2, seg[t].y2); if (d <= Time[t2]) { Time[t2] -= d; SArr[t2] = t; continue; } Calc(px, tp.t, seg[t].x2, seg[t].y2, dir, t2); } else { x = px, y = tp.t; if (dir == 3) x = B; else x = 0; Calc(px, tp.t, x, y, dir, t2); } } px += pm; } if (i == ed) break; if (dir != X[i].dir) { Ins(1, 0, SZ - 1, min(X[i].y1, X[i].y2), max(X[i].y1, X[i].y2), X[i].num); continue; } t = find(1, 0, SZ - 1, X[i].y1); t2 = X[i].num; if (t) { d = dis(X[i].x2, X[i].y2, seg[t].x2, seg[t].y2); Next[0][t2] = t; dist[0][t2] = d; continue; } x = X[i].x2, y = X[i].y2; if (dir == 2) d = x, x = 0; else d = B - x, x = B; ++LastC; Next[0][t2] = N + LastC; dist[0][t2] = d; Last[LastC].x = x, Last[LastC].y = y; } } void Make_GraphY(int dir) { int i, st, ed, t, d, t2, pm, py, j, x, y; for (i = 1; i <= SZ + B + 1; i++) IT[i] = 0; if (dir == 0) st = N, ed = 0, pm = -1, py = B; else st = 1, ed = N + 1, pm = 1, py = 0; if (dir == 0) for (i = 1; i <= N; i++) Y[i].ty = max(Y[i].y1, Y[i].y2); else for (i = 1; i <= N; i++) Y[i].ty = min(Y[i].y1, Y[i].y2); sort(Y + 1, Y + N + 1); for (i = st;; i += pm) { while (i == ed || py != Y[i].ty) { if (py == -1 || py == B + 1) break; for (j = 0; j < vecY[py].size(); j++) { tp = vecY[py][j]; if (tp.dir != dir) continue; t = find(1, 0, SZ - 1, tp.t); t2 = tp.num; if (t) { d = dis(tp.t, py, seg[t].x2, seg[t].y2); if (d <= Time[t2]) { Time[t2] -= d; SArr[t2] = t; continue; } Calc(tp.t, py, seg[t].x2, seg[t].y2, dir, t2); } else { x = tp.t, y = py; if (dir == 0) y = B; else y = 0; Calc(tp.t, py, x, y, dir, t2); } } py += pm; } if (i == ed) break; if (dir != Y[i].dir) { Ins(1, 0, SZ - 1, min(Y[i].x1, Y[i].x2), max(Y[i].x1, Y[i].x2), Y[i].num); continue; } t = find(1, 0, SZ - 1, Y[i].x1); t2 = Y[i].num; if (t) { d = dis(Y[i].x2, Y[i].y2, seg[t].x2, seg[t].y2); Next[0][t2] = t; dist[0][t2] = d; continue; } x = Y[i].x2, y = Y[i].y2; if (dir == 1) d = y, y = 0; else d = B - y, y = B; ++LastC; Next[0][t2] = N + LastC; dist[0][t2] = d; Last[LastC].x = x, Last[LastC].y = y; } } void save_2k() { int i, j; for (i = 1; i <= 50; i++) { for (j = 1; j <= N; j++) { if (Next[i - 1][j] > N) Next[i][j] = Next[i - 1][j], dist[i][j] = dist[i - 1][j]; else Next[i][j] = Next[i - 1][Next[i - 1][j]], dist[i][j] = dist[i - 1][j] + dist[i - 1][Next[i - 1][j]]; } } } void GetAnswer(int a, int x, long long T) { int i, d, X, Y; if (dist[50][x] <= T) { Res[a] = Last[Next[50][x] - N]; return; } for (i = 49; i >= 0; i--) { if (dist[i][x] <= T) { T -= dist[i][x]; x = Next[i][x]; } } Time[a] = T; d = Next[0][x]; if (d <= N) X = seg[d].x2, Y = seg[d].y2; else X = Last[d - N].x, Y = Last[d - N].y; Calc(seg[x].x2, seg[x].y2, X, Y, seg[x].dir, a); } void Process() { int i; for (i = 0; i < Q; i++) { if (SArr[i]) { GetAnswer(i, SArr[i], Time[i]); } } } void Print() { int i; for (i = 0; i < Q; i++) printf("%d %d\n", Res[i].x, Res[i].y); } int main() { input(); Make_GraphX(2); Make_GraphX(3); Make_GraphY(0); Make_GraphY(1); save_2k(); Process(); Print(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 200005; struct arrow { int x0, y0, x1, y1; int dx, dy, id; bool operator<(const arrow &R) const { return id < R.id; } }; bool cmpX0(const arrow &A, const arrow &B) { return make_pair(max(A.x0, A.x1), -A.id) < make_pair(max(B.x0, B.x1), -B.id); } bool cmpX1(const arrow &A, const arrow &B) { return make_pair(-min(A.x0, A.x1), -A.id) < make_pair(-min(B.x0, B.x1), -B.id); } bool cmpY0(const arrow &A, const arrow &B) { return make_pair(max(A.y0, A.y1), -A.id) < make_pair(max(B.y0, B.y1), -B.id); } bool cmpY1(const arrow &A, const arrow &B) { return make_pair(-min(A.y0, A.y1), -A.id) < make_pair(-min(B.y0, B.y1), -B.id); } long long T[N]; arrow a[N]; int n, m, q; int nxt[51][N]; long long t[51][N]; const long long inf = 1e16; void work() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { scanf("%d%d%d%d", &a[i].x0, &a[i].y0, &a[i].x1, &a[i].y1); a[i].dx = a[i].x1 - a[i].x0; a[i].dy = a[i].y1 - a[i].y0; int len = abs(a[i].dx) + abs(a[i].dy); a[i].dx /= len, a[i].dy /= len; a[i].id = i; } scanf("%d", &q); char dir[10]; for (int i = n + 1; i <= n + q; ++i) { scanf("%d%d", &a[i].x0, &a[i].y0); a[i].x1 = a[i].x0, a[i].y1 = a[i].y0; a[i].id = i; scanf("%s%lld", dir, &T[i]); if (*dir == 'U') a[i].dy = 1; if (*dir == 'D') a[i].dy = -1; if (*dir == 'R') a[i].dx = 1; if (*dir == 'L') a[i].dx = -1; } multimap<int, arrow *> Hash; sort(a + 1, a + n + q + 1, cmpX0); Hash.clear(); for (int i = 1; i <= n + q; ++i) { if (a[i].id <= n) { int L = min(a[i].y0, a[i].y1), R = max(a[i].y0, a[i].y1); auto it = Hash.lower_bound(L); auto itr = Hash.upper_bound(R); while (it != itr) { arrow *A = it->second; nxt[0][A->id] = a[i].id; t[0][A->id] = abs(a[i].x1 - A->x1) + abs(a[i].y1 - A->y1); Hash.erase(it++); } } if (a[i].dx == 1 && a[i].dy == 0) Hash.insert(make_pair(a[i].y0, a + i)); } for (auto &it : Hash) { arrow *A = it.second; nxt[0][A->id] = 0; t[0][A->id] = m - A->x1; } sort(a + 1, a + n + q + 1, cmpX1); Hash.clear(); for (int i = 1; i <= n + q; ++i) { if (a[i].id <= n) { int L = min(a[i].y0, a[i].y1), R = max(a[i].y0, a[i].y1); auto it = Hash.lower_bound(L); auto itr = Hash.upper_bound(R); while (it != itr) { arrow *A = it->second; nxt[0][A->id] = a[i].id; t[0][A->id] = abs(a[i].x1 - A->x1) + abs(a[i].y1 - A->y1); Hash.erase(it++); } } if (a[i].dx == -1 && a[i].dy == 0) Hash.insert(make_pair(a[i].y0, a + i)); } for (auto &it : Hash) { arrow *A = it.second; nxt[0][A->id] = 0; t[0][A->id] = A->x1; } sort(a + 1, a + n + q + 1, cmpY0); Hash.clear(); for (int i = 1; i <= n + q; ++i) { if (a[i].id <= n) { int L = min(a[i].x0, a[i].x1), R = max(a[i].x0, a[i].x1); auto it = Hash.lower_bound(L); auto itr = Hash.upper_bound(R); while (it != itr) { arrow *A = it->second; nxt[0][A->id] = a[i].id; t[0][A->id] = abs(a[i].x1 - A->x1) + abs(a[i].y1 - A->y1); Hash.erase(it++); } } if (a[i].dx == 0 && a[i].dy == 1) Hash.insert(make_pair(a[i].x0, a + i)); } for (auto &it : Hash) { arrow *A = it.second; nxt[0][A->id] = 0; t[0][A->id] = m - A->y1; } sort(a + 1, a + n + q + 1, cmpY1); Hash.clear(); for (int i = 1; i <= n + q; ++i) { if (a[i].id <= n) { int L = min(a[i].x0, a[i].x1), R = max(a[i].x0, a[i].x1); auto it = Hash.lower_bound(L); auto itr = Hash.upper_bound(R); while (it != itr) { arrow *A = it->second; nxt[0][A->id] = a[i].id; t[0][A->id] = abs(a[i].x1 - A->x1) + abs(a[i].y1 - A->y1); Hash.erase(it++); } } if (a[i].dx == 0 && a[i].dy == -1) Hash.insert(make_pair(a[i].x0, a + i)); } for (auto &it : Hash) { arrow *A = it.second; nxt[0][A->id] = 0; t[0][A->id] = A->y1; } for (int j = 1; j <= 50; ++j) for (int i = 0; i <= n + q; ++i) { nxt[j][i] = nxt[j - 1][nxt[j - 1][i]]; t[j][i] = min(inf, t[j - 1][i] + t[j - 1][nxt[j - 1][i]]); } sort(a + 1, a + n + q + 1); for (int i = n + 1; i <= n + q; ++i) { int x = i; long long rt = T[i]; for (int j = 50; j >= 0; --j) { if (nxt[j][x] && t[j][x] <= rt) { rt -= t[j][x]; x = nxt[j][x]; } } int y = nxt[0][x]; if (y && rt > (a[y].x1 - a[x].x1) * a[x].dx + (a[y].y1 - a[x].y1) * a[x].dy) rt -= t[0][x], x = y; long long X = min((long long)m, max(0LL, a[x].x1 + rt * a[x].dx)); long long Y = min((long long)m, max(0LL, a[x].y1 + rt * a[x].dy)); printf("%d %d\n", (int)X, (int)Y); } } int main() { work(); return 0; }
#include <bits/stdc++.h> using namespace std; inline int gi() { char c = getchar(); int x = 0; for (; c < '0' || c > '9'; c = getchar()) ; for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0'; return x; } const int N = 2e5 + 5, L = 50; const long long inf = 1e16; struct node { int x1, x2, y1, y2, id; } a[N]; bool cmp1(node a, node b) { if (max(a.x1, a.x2) == max(b.x1, b.x2)) return a.id < b.id; return max(a.x1, a.x2) > max(b.x1, b.x2); } bool cmp2(node a, node b) { return a.id < b.id; } int n, m, q, to[L + 2][N], cov[N << 2]; long long t[N], dis[L + 2][N]; const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1}; void build(int x = 1, int l = -1, int r = m + 1) { cov[x] = 0; if (l == r) return; int mid = l + r >> 1; build((x << 1), l, mid), build((x << 1 | 1), mid + 1, r); } void update(int sl, int sr, int w, int x = 1, int l = -1, int r = m + 1) { if (sl <= l && r <= sr) { cov[x] = w; return; } if (cov[x]) cov[(x << 1)] = cov[(x << 1 | 1)] = cov[x]; int mid = l + r >> 1; if (sl <= mid) update(sl, sr, w, (x << 1), l, mid); if (sr > mid) update(sl, sr, w, (x << 1 | 1), mid + 1, r); cov[x] = (cov[(x << 1)] == cov[(x << 1 | 1)] ? cov[(x << 1)] : 0); } int query(int s, int x = 1, int l = -1, int r = m + 1) { if (cov[x] || l == r) return cov[x]; int mid = l + r >> 1; return s <= mid ? query(s, (x << 1), l, mid) : query(s, (x << 1 | 1), mid + 1, r); } void transform() { for (int i = 1; i <= n + q; ++i) { swap(a[i].x1, a[i].y1), a[i].x1 = m - a[i].x1; swap(a[i].x2, a[i].y2), a[i].x2 = m - a[i].x2; } } inline char getdir(node a) { if (a.x1 > a.x2) return 0; if (a.x1 < a.x2) return 1; if (a.y1 > a.y2) return 2; if (a.y1 < a.y2) return 3; } inline bool have(int l1, int r1, int l2, int r2) { if (l1 > r1) swap(l1, r1); if (l2 > r2) swap(l2, r2); if (l2 <= l1 && l1 <= r2) return true; if (l2 <= r1 && r1 <= r2) return true; return false; } inline int getdis(node a, node b) { if (getdir(a) < 2 && getdir(b) < 2 && have(a.x1, a.x2, b.x1, b.x2)) return 0; if (getdir(a) > 1 && getdir(b) > 1 && have(a.y1, a.y2, b.y1, b.y2)) return 0; switch (getdir(a)) { case 0: return a.x2 - max(b.x1, b.x2); case 1: return min(b.x1, b.x2) - a.x2; case 2: return a.y2 - max(b.y1, b.y2); case 3: return min(b.y1, b.y2) - a.y2; } } inline int odis(node a, node b) { return abs(a.x2 - b.x2) + abs(a.y2 - b.y2); } void move(int& x, int& y, int d, int t) { x += t * dx[d], y += t * dy[d]; } int main() { n = gi(), m = gi(); for (int i = 1; i <= n; ++i) a[i].x1 = gi(), a[i].y1 = gi(), a[i].x2 = gi(), a[i].y2 = gi(), a[i].id = i; q = gi(); for (int i = 1; i <= q; ++i) { int x = gi(), y = gi(), nx, ny; char op[2]; scanf("%s%lld", op, &t[i]); if (op[0] == 'L') nx = x + 1, ny = y; if (op[0] == 'R') nx = x - 1, ny = y; if (op[0] == 'U') nx = x, ny = y - 1; if (op[0] == 'D') nx = x, ny = y + 1; a[n + i] = (node){nx, x, ny, y, i + n}; } for (int T = 1; T <= 4; transform(), ++T) { sort(a + 1, a + 1 + n + q, cmp1), build(); for (int i = 1; i <= n + q; ++i) { if (a[i].x1 < a[i].x2) { int now = query(a[i].y1); to[0][a[i].id] = a[now].id; dis[0][a[i].id] = odis(a[i], a[now]); } if (a[i].id <= n) update(min(a[i].y1, a[i].y2), max(a[i].y1, a[i].y2), i); } } for (int i = 1; i <= L; ++i) for (int j = 1; j <= n + q; ++j) { to[i][j] = to[i - 1][to[i - 1][j]]; dis[i][j] = min(inf, dis[i - 1][j] + dis[i - 1][to[i - 1][j]]); } sort(a + 1, a + 1 + n + q, cmp2); for (int i = 1; i <= q; ++i) { int u = n + i; for (int j = L; ~j; --j) if (to[j][u] && t[i] >= dis[j][u]) t[i] -= dis[j][u], u = to[j][u]; int x = a[u].x2, y = a[u].y2; if (!to[0][u]) { move(x, y, getdir(a[u]), min(t[i], 1ll * m)); x = max(x, 0), x = min(x, m); y = max(y, 0), y = min(y, m); printf("%d %d\n", x, y); continue; } node nxt = a[to[0][u]]; int d = getdis(a[u], nxt); if (d > t[i]) move(x, y, getdir(a[u]), t[i]); else { t[i] -= d; move(x, y, getdir(a[u]), d); move(x, y, getdir(nxt), t[i]); } printf("%d %d\n", x, y); } }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int Maxn = 200005, Maxm = 300005, Mo = 10007, sp[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}}; const long long oo = LLONG_MAX >> 2; typedef int IArr[Maxn]; typedef long long LArr[Maxn]; typedef double DArr[Maxn]; struct Tree { pair<long long, int> v, mk; }; struct point { int x, y, dir, id, mk = 0; point(int n_x, int n_y, int n_dir, int n_id) { x = n_x, y = n_y, dir = n_dir, id = n_id; } point(){}; }; struct node { int v, h, id; node(int n_v, int n_h, int n_id) { v = n_v, h = n_h, id = n_id; } }; struct arrow { int l, r, h, id, dir; arrow(int n_l, int n_r, int n_h, int n_id, int n_dir) { l = n_l; r = n_r, h = n_h, id = n_id; dir = n_dir; } arrow() {} }; struct query { int x, y, dir, id; long long t; query(int n_x, int n_y, long long n_t, int n_dir, int n_id) { x = n_x, y = n_y; t = n_t; dir = n_dir, id = n_id; } query() {} }; int n, m, q, i, j; pair<long long, int> to[Maxn]; Tree t[Maxn * 4]; query Q[Maxn]; point p_all[Maxn]; arrow s_all[Maxn]; vector<arrow> s_x, s_y; vector<point> p_x, p_y; vector<node> pt[4]; LArr dis[60]; IArr nt[60]; bool cmp_x(point a, point b) { if (a.y != b.y) return a.y < b.y; return a.x < b.x; } bool cmp_y(point a, point b) { if (a.x != b.x) return a.x <= b.x; return a.y <= b.y; } bool cmp_s(arrow a, arrow b) { return a.h < b.h; } bool cmp_n(node a, node b) { return a.h < b.h; } void input() { cin >> n >> m; for (i = 1; i <= n; i++) { int x0, y0, x1, y1; cin >> x0 >> y0 >> x1 >> y1; if (y0 == y1) { int dir = x0 < x1 ? 1 : -1; int l = min(x0, x1); int r = max(x0, x1); s_x.push_back((arrow(l, r, y0, i, dir - 1 ? 0 : 1))); p_x.push_back((point(x1, y1, dir, i))); if (dir + 1) pt[1].push_back((node(y1, x1, i))); else pt[0].push_back((node(y1, x1, i))); p_all[i] = point(x1, y1, dir - 1 ? 0 : 1, i); s_all[i] = arrow(l, r, y0, i, dir - 1 ? 0 : 1); } else { int l = min(y0, y1); int r = max(y0, y1); int dir = y0 < y1 ? 1 : -1; s_y.push_back((arrow(l, r, x0, i, dir - 1 ? 3 : 2))); p_y.push_back((point(x1, y1, dir, i))); if (dir + 1) pt[2].push_back((node(x1, y1, i))); else pt[3].push_back((node(x1, y1, i))); p_all[i] = point(x1, y1, dir - 1 ? 3 : 2, i); s_all[i] = arrow(l, r, x0, i, dir - 1 ? 3 : 2); } } cin >> q; for (i = 1; i <= q; i++) { int x, y, dir; long long t; char ch; cin >> x >> y >> ch >> t; if (ch == 'L') dir = 0; if (ch == 'R') dir = 1; if (ch == 'U') dir = 2; if (ch == 'D') dir = 3; if (dir <= 1) pt[dir].push_back((node(y, x, n + i))); else pt[dir].push_back((node(x, y, n + i))); Q[i] = query(x, y, t, dir, i); p_all[n + i] = point(x, y, dir, i + n); } sort(s_x.begin(), s_x.end(), cmp_s); sort(s_y.begin(), s_y.end(), cmp_s); sort(p_x.begin(), p_x.end(), cmp_x); sort(p_y.begin(), p_y.end(), cmp_y); for (i = 0; i < 4; i++) sort(pt[i].begin(), pt[i].end(), cmp_n); } bool ck(long long a, long long b, int d) { if (d + 1) return a >= b; else return a <= b; } void check(int k, int l, int r) { if (!t[k].mk.second) return; t[k].v = t[k].mk; if (l != r) t[k * 2].mk = t[k].mk, t[k * 2 + 1].mk = t[k].mk; t[k].mk = make_pair((0), (0)); } void ins(int k, int l, int r, int x, int y, pair<long long, int> key, int d) { check(k, l, r); if (y < l || r < x) return; if (x <= l && r <= y) { t[k].mk = key; check(k, l, r); return; } int mid = (l + r) / 2; ins(k * 2, l, mid, x, y, key, d); ins(k * 2 + 1, mid + 1, r, x, y, key, d); } long long dist(point a, point b) { return abs(a.x - b.x) + abs(a.y - b.y); } pair<long long, int> ask(int k, int l, int r, int pos, int id1) { check(k, l, r); if (l == r) { int id2 = t[k].v.second; return make_pair((dist(p_all[id1], p_all[id2])), (t[k].v.second)); } int mid = (l + r) / 2; if (pos <= mid) return ask(k * 2, l, mid, pos, id1); else return ask(k * 2 + 1, mid + 1, r, pos, id1); } bool cover(int k, vector<point>& a, int v, int h) { if (k < 0 || k >= a.size()) return 0; int id = a[k].id; if (s_all[id].h == h) if (s_all[id].l <= v && v <= s_all[id].r) return 1; return 0; } pair<long long, int> find_same_axe(point& A) { point Q; Q.x = A.x; Q.y = A.y; if (A.dir <= 1) { int pos = lower_bound(p_x.begin(), p_x.end(), Q, cmp_x) - p_x.begin(); if (cover(pos, p_x, Q.x, Q.y)) return A.dir = s_all[p_x[pos].id].dir, A.mk = 1, make_pair((abs(Q.x - p_x[pos].x)), (p_x[pos].id)); if (cover(pos - 1, p_x, Q.x, Q.y)) return A.dir = s_all[p_x[pos - 1].id].dir, A.mk = 1, make_pair((abs(Q.x - p_x[pos - 1].x)), (p_x[pos - 1].id)); if (A.dir == 0) pos--; if (pos < 0 || pos >= p_x.size() || p_x[pos].y != A.y) return make_pair((oo), (0)); else return make_pair((abs(Q.x - p_x[pos].x)), (p_x[pos].id)); } else { int pos = lower_bound(p_y.begin(), p_y.end(), Q, cmp_y) - p_y.begin(); if (cover(pos, p_y, Q.y, Q.x)) return A.dir = s_all[p_y[pos].id].dir, A.mk = 1, make_pair((abs(Q.y - p_y[pos].y)), (p_y[pos].id)); if (cover(pos - 1, p_y, Q.y, Q.x)) return A.dir = s_all[p_y[pos - 1].id].dir, A.mk = 1, make_pair((abs(Q.y - p_y[pos - 1].y)), (p_y[pos - 1].id)); if (A.dir == 3) pos--; if (pos < 0 || pos >= p_y.size() || p_y[pos].x != A.x) return make_pair((oo), (0)); else return make_pair((abs(Q.y - p_y[pos].y)), (p_y[pos].id)); } } void work(vector<node>& p, vector<arrow>& s, int i, int j, int d, pair<long long, int>* ct) { memset((t), 0, sizeof((t))); int m1 = p.size(), m2 = s.size(); for (; 0 <= i && i < m1; i += d) if (!p_all[p[i].id].mk) { for (; 0 <= j && j < m2 && ck(p[i].h, s[j].h, d); j += d) { ins(1, 0, m, s[j].l, s[j].r, make_pair((s[j].h), (s[j].id)), d); } pair<long long, int> ans = ask(1, 0, m, p[i].v, p[i].id); if (!ans.second) continue; int nd = abs(s_all[ans.second].h - p[i].h); if (!ct[p[i].id].second || nd < ct[p[i].id].first) ct[p[i].id] = ans; if (!nd) { ct[p[i].id] = ans; p_all[p[i].id].dir = s_all[ans.second].dir; } } } void construct_graph() { for (i = 0; i < p_x.size(); i++) { int p = i + p_x[i].dir; int id = p_x[i].id; if (p < 0 || p >= p_x.size()) to[id] = make_pair((0), (0)); else if (p_x[p].y == p_x[i].y) to[id] = make_pair((abs(p_x[i].x - p_x[p].x)), (p_x[p].id)); else to[id] = make_pair((0), (0)); } for (i = 0; i < p_y.size(); i++) { int p = i + p_y[i].dir; int id = p_y[i].id; if (p < 0 || p >= p_y.size()) to[id] = make_pair((0), (0)); else if (p_y[p].x == p_y[i].x) to[id] = make_pair((abs(p_y[i].y - p_y[p].y)), (p_y[p].id)); else to[id] = make_pair((0), (0)); } for (i = n + 1; i <= n + q; i++) to[i] = find_same_axe(p_all[i]); work(pt[0], s_y, 0, 0, 1, to); work(pt[1], s_y, pt[1].size() - 1, s_y.size() - 1, -1, to); work(pt[2], s_x, pt[2].size() - 1, s_x.size() - 1, -1, to); work(pt[3], s_x, 0, 0, 1, to); } void pre_query() { for (i = 1; i <= n + q; i++) dis[0][i] = to[i].first, nt[0][i] = to[i].second; for (i = 1; i <= 50; i++) { for (j = 1; j <= n + q; j++) { if (!nt[i - 1][j]) nt[i][j] = nt[i - 1][j], dis[i][j] = dis[i - 1][j]; else nt[i][j] = nt[i - 1][nt[i - 1][j]], dis[i][j] = dis[i - 1][j] + dis[i - 1][nt[i - 1][j]]; dis[i][j] = min(dis[i][j], oo); } } } pair<long long, long long> calc(query cur) { int pos = cur.id + n; long long T = cur.t; for (int i = 50; i + 1; i--) if (T >= dis[i][pos]) { if (!nt[i][pos]) continue; T -= dis[i][pos]; pos = nt[i][pos]; } int dir = p_all[pos].dir; pair<long long, long long> ans = make_pair((p_all[pos].x), (p_all[pos].y)); if (!to[pos].second) { ans.first += sp[dir][0] * T; ans.second += sp[dir][1] * T; return ans; } int go = to[pos].second; if (dir <= 1) { int len = abs(p_all[pos].x - p_all[go].x); if (len >= T) { ans.first += sp[dir][0] * T; return ans; } ans.first += sp[dir][0] * len; T -= len; ans.second += sp[p_all[go].dir][1] * T; } else { int len = abs(p_all[pos].y - p_all[go].y); if (len >= T) { ans.second += sp[dir][1] * T; return ans; } ans.second += sp[dir][1] * len; T -= len; ans.first += sp[p_all[go].dir][0] * T; } return ans; } void do_query() { pre_query(); for (i = 1; i <= q; i++) { pair<long long, long long> ans = calc(Q[i]); if (ans.first > m) ans.first = m; if (ans.first < 0) ans.first = 0; if (ans.second > m) ans.second = m; if (ans.second < 0) ans.second = 0; cout << ans.first << ' ' << ans.second << endl; } } int main() { ios::sync_with_stdio(0); input(); construct_graph(); do_query(); }
#include <bits/stdc++.h> using namespace std; template <class T> void Read(T &digit) { digit = 0; char c; for (c = getchar(); c < '0' || c > '9'; c = getchar()) ; for (; c >= '0' && c <= '9'; digit = digit * 10 + c - '0', c = getchar()) ; } struct Point { int x, y; Point() {} Point(int x, int y) : x(x), y(y) {} friend Point operator-(Point a, Point b) { return Point(a.x - b.x, a.y - b.y); } friend Point operator+(Point a, Point b) { return Point(a.x + b.x, a.y + b.y); } friend Point operator*(Point a, int p) { return Point(a.x * p, a.y * p); } friend Point operator/(Point a, int p) { return Point(a.x / p, a.y / p); } friend bool operator<=(Point a, Point b) { return a.x < b.x || (a.x == b.x && a.y <= b.y); } friend bool operator<(Point a, Point b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } }; int Cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } int dist(Point a, Point b) { return abs(a.x - b.x) + abs(a.y - b.y); } Point getInter(Point a, Point v1, Point b, Point v2) { Point v3 = a - b; int t = Cross(v2, v3) / Cross(v1, v2); return a + v1 * t; } struct Seg { int x0, y0, x1, y1; } L[100010]; vector<pair<pair<int, int>, int> > row[100010], col[100010]; vector<int> toL[100010], toR[100010], toQ[100010]; set<pair<int, int> > T; int n, m; struct msgnode { int to; Point p; long long dis; } F[80][100010]; Point p[100010][2]; Point vec[100010]; void read() { Read(n), Read(m); for (int i = 0; i <= m; ++i) { row[i].push_back(make_pair(make_pair(-1 << 30, -1), 0)); row[i].push_back(make_pair(make_pair(1 << 30, 1 << 30 | 1), 0)); col[i].push_back(make_pair(make_pair(-1 << 30, -1), 0)); col[i].push_back(make_pair(make_pair(1 << 30, 1 << 30 | 1), 0)); } for (int i = 1; i <= n; ++i) { Read(L[i].x0), Read(L[i].y0), Read(L[i].x1), Read(L[i].y1); p[i][0] = Point(L[i].x0, L[i].y0), p[i][1] = Point(L[i].x1, L[i].y1); vec[i] = (p[i][1] - p[i][0]) / dist(p[i][0], p[i][1]); if (L[i].y0 == L[i].y1) { pair<pair<int, int>, int> v = make_pair(make_pair(L[i].x0, L[i].x1), i); if (v.first.first > v.first.second) swap(v.first.first, v.first.second); row[L[i].y0].push_back(v); } else { pair<pair<int, int>, int> v = make_pair(make_pair(L[i].y0, L[i].y1), i); if (v.first.first > v.first.second) swap(v.first.first, v.first.second); col[L[i].x0].push_back(v); } } for (int i = 0; i <= m; ++i) sort(row[i].begin(), row[i].end()), sort(col[i].begin(), col[i].end()); } void Prepare() { static int to[100010]; T.insert(make_pair(-1 << 30, 0)); T.insert(make_pair(1 << 30, 0)); for (int i = 1; i <= n; ++i) { if (L[i].y0 != L[i].y1) toQ[L[i].x0].push_back(i); else { toL[min(L[i].x0, L[i].x1)].push_back(i); toR[max(L[i].x0, L[i].x1)].push_back(i); } } for (int i = 0; i <= m; ++i) { for (__typeof(toL[i].begin()) x = toL[i].begin(); x != toL[i].end(); ++x) T.insert(make_pair(L[*x].y0, *x)); for (__typeof(toQ[i].begin()) x = toQ[i].begin(); x != toQ[i].end(); ++x) { set<pair<int, int> >::iterator it1 = T.upper_bound(make_pair(L[*x].y1, 0)); if (L[*x].y0 > L[*x].y1) --it1; vector<pair<pair<int, int>, int> >::iterator it2; it2 = upper_bound(col[L[*x].x0].begin(), col[L[*x].x1].end(), make_pair(make_pair(L[*x].y1, 1 << 30), 0)); if (L[*x].y0 > L[*x].y1) it2 -= 2; if (L[*x].y0 < L[*x].y1) to[*x] = it1->first < it2->first.first ? it1->second : it2->second; else to[*x] = it1->first > it2->first.first ? it1->second : it2->second; } for (__typeof(toR[i].begin()) x = toR[i].begin(); x != toR[i].end(); ++x) T.erase(make_pair(L[*x].y0, *x)); toL[i].clear(), toR[i].clear(), toQ[i].clear(); } for (int i = 1; i <= n; ++i) { if (L[i].x0 != L[i].x1) toQ[L[i].y0].push_back(i); else { toL[min(L[i].y0, L[i].y1)].push_back(i); toR[max(L[i].y0, L[i].y1)].push_back(i); } } for (int i = 0; i <= m; ++i) { for (__typeof(toL[i].begin()) x = toL[i].begin(); x != toL[i].end(); ++x) T.insert(make_pair(L[*x].x0, *x)); for (__typeof(toQ[i].begin()) x = toQ[i].begin(); x != toQ[i].end(); ++x) { set<pair<int, int> >::iterator it1 = T.upper_bound(make_pair(L[*x].x1, 0)); if (L[*x].x0 > L[*x].x1) --it1; vector<pair<pair<int, int>, int> >::iterator it2; it2 = upper_bound(row[L[*x].y0].begin(), row[L[*x].y1].end(), make_pair(make_pair(L[*x].x1, 1 << 30), 0)); if (L[*x].x0 > L[*x].x1) it2 -= 2; if (L[*x].x0 < L[*x].x1) to[*x] = it1->first < it2->first.first ? it1->second : it2->second; else to[*x] = it1->first > it2->first.first ? it1->second : it2->second; } for (__typeof(toR[i].begin()) x = toR[i].begin(); x != toR[i].end(); ++x) T.erase(make_pair(L[*x].x0, *x)); toL[i].clear(), toR[i].clear(), toQ[i].clear(); } for (int i = 1; i <= n; ++i) { F[0][i].to = to[i]; if (to[i]) { if (Cross(vec[i], vec[to[i]])) { Point inter = getInter(p[i][0], vec[i], p[to[i]][0], vec[to[i]]); F[0][i].dis = min(dist(p[i][0], inter), dist(p[i][1], inter)); F[0][i].p = inter; } else { F[0][i].dis = 1 << 30; for (int b = 0; b <= 1; ++b) if (F[0][i].dis > dist(p[i][1], p[to[i]][b])) F[0][i].dis = dist(p[i][1], p[to[i]][b]), F[0][i].p = p[to[i]][b]; } } else { Point &go = F[0][i].p; if (vec[i].x == 1) go = Point(m, p[i][0].y); if (vec[i].x == -1) go = Point(0, p[i][0].y); if (vec[i].y == 1) go = Point(p[i][0].x, m); if (vec[i].y == -1) go = Point(p[i][0].x, 0); F[0][i].dis = dist(go, p[i][1]); } } for (int i = 1; i <= 70; ++i) for (int j = 1; j <= n; ++j) { F[i][j].to = F[i - 1][F[i - 1][j].to].to; Point tmp = F[i - 1][j].p; if (F[i][j].to) { F[i][j].p = F[i - 1][F[i - 1][j].to].p; int sum = dist(tmp, p[F[i - 1][j].to][1]); F[i][j].dis = F[i - 1][j].dis + sum + F[i - 1][F[i - 1][j].to].dis; } else { F[i][j].p = F[i - 1][j].to ? F[i - 1][F[i - 1][j].to].p : F[i - 1][j].p; if (F[i - 1][j].to) { int sum = dist(tmp, p[F[i - 1][j].to][1]); F[i][j].dis = F[i - 1][j].dis + sum + F[i - 1][F[i - 1][j].to].dis; } else F[i][j].dis = F[i - 1][j].dis; } F[i][j].dis = min(F[i][j].dis, 1000000000000001LL); } } bool On(vector<pair<pair<int, int>, int> >::iterator &it, vector<pair<pair<int, int>, int> > &c, int y) { it = upper_bound(c.begin(), c.end(), make_pair(make_pair(y, 1 << 30), 1 << 30)) - 1; return it->first.first <= y && it->first.second >= y; } Point calc(Point a, int x, long long T, Point v) { if (!x) { long long px = a.x + v.x * T, py = a.y + v.y * T; px = max(px, 0LL), px = min(px, (long long)m); py = max(py, 0LL), py = min(py, (long long)m); return Point((int)px, (int)py); } Point tmp; if (Cross(v, vec[x])) tmp = getInter(a, v, p[x][0], vec[x]); else if (min(p[x][0], p[x][1]) <= a && a <= max(p[x][0], p[x][1])) tmp = a; else tmp = dist(p[x][0], a) < dist(p[x][1], a) ? p[x][0] : p[x][1]; if (dist(a, tmp) >= T) return a + v * T; T -= dist(a, tmp), a = tmp; if (dist(a, p[x][1]) >= T) return a + vec[x] * T; T -= dist(a, p[x][1]), a = p[x][1]; for (int i = 70; i >= 0; --i) if (F[i][x].dis <= T) { a = F[i][x].p, T -= F[i][x].dis; int b = F[i][x].to; if (b == 0) return a; if (dist(a, p[b][1]) >= T) return a + vec[b] * T; T -= dist(a, p[b][1]), a = p[b][1], x = b; } return a + vec[x] * T; } void Query() { static Point ans[100010], que[100010]; static Point vec[100010]; static long long que_T[100010]; int nQ; Read(nQ); for (int i = 1; i <= nQ; ++i) { char c; Read(que[i].x), Read(que[i].y); for (c = getchar(); c < 'A' || c > 'Z'; c = getchar()) ; if (c == 'U') vec[i] = Point(0, 1); if (c == 'D') vec[i] = Point(0, -1); if (c == 'L') vec[i] = Point(-1, 0); if (c == 'R') vec[i] = Point(1, 0); Read(que_T[i]); } for (int i = 1; i <= nQ; ++i) if (vec[i].x == 0) toQ[que[i].x].push_back(i); for (int i = 1; i <= n; ++i) if (p[i][0].y == p[i][1].y) { toL[min(p[i][0].x, p[i][1].x)].push_back(i); toR[max(p[i][0].x, p[i][1].x)].push_back(i); } for (int i = 0; i <= m; ++i) { for (__typeof(toL[i].begin()) x = toL[i].begin(); x != toL[i].end(); ++x) T.insert(make_pair(L[*x].y0, *x)); for (__typeof(toQ[i].begin()) x = toQ[i].begin(); x != toQ[i].end(); ++x) { set<pair<int, int> >::iterator it1 = T.lower_bound(make_pair(que[*x].y, 0)); if (vec[*x].y == -1 && it1->first != que[*x].y) --it1; vector<pair<pair<int, int>, int> >::iterator it2; it2 = upper_bound(col[que[*x].x].begin(), col[que[*x].x].end(), make_pair(make_pair(que[*x].y, 1 << 30), 0)); if (vec[*x].y == -1) --it2; int go; if (vec[*x].y == 1) go = it1->first < it2->first.first ? it1->second : it2->second; else go = it1->first > it2->first.first ? it1->second : it2->second; if (On(it2, col[que[*x].x], que[*x].y)) go = it2->second; if (On(it2, row[que[*x].y], que[*x].x)) go = it2->second; ans[*x] = calc(que[*x], go, que_T[*x], vec[*x]); } for (__typeof(toR[i].begin()) x = toR[i].begin(); x != toR[i].end(); ++x) T.erase(make_pair(L[*x].y0, *x)); toL[i].clear(), toR[i].clear(), toQ[i].clear(); } for (int i = 1; i <= nQ; ++i) if (vec[i].y == 0) toQ[que[i].y].push_back(i); for (int i = 1; i <= n; ++i) if (p[i][0].x == p[i][1].x) { toL[min(p[i][0].y, p[i][1].y)].push_back(i); toR[max(p[i][0].y, p[i][1].y)].push_back(i); } for (int i = 0; i <= m; ++i) { for (__typeof(toL[i].begin()) x = toL[i].begin(); x != toL[i].end(); ++x) T.insert(make_pair(L[*x].x0, *x)); for (__typeof(toQ[i].begin()) x = toQ[i].begin(); x != toQ[i].end(); ++x) { set<pair<int, int> >::iterator it1 = T.lower_bound(make_pair(que[*x].x, 0)); if (vec[*x].x == -1 && it1->first != que[*x].x) --it1; vector<pair<pair<int, int>, int> >::iterator it2; it2 = upper_bound(row[que[*x].y].begin(), row[que[*x].y].end(), make_pair(make_pair(que[*x].x, 1 << 30), 0)); if (vec[*x].x == -1) --it2; int go; if (vec[*x].x == 1) go = it1->first < it2->first.first ? it1->second : it2->second; else go = it1->first > it2->first.first ? it1->second : it2->second; if (On(it2, col[que[*x].x], que[*x].y)) go = it2->second; if (On(it2, row[que[*x].y], que[*x].x)) go = it2->second; ans[*x] = calc(que[*x], go, que_T[*x], vec[*x]); } for (__typeof(toR[i].begin()) x = toR[i].begin(); x != toR[i].end(); ++x) T.erase(make_pair(L[*x].x0, *x)); } for (int i = 1; i <= nQ; ++i) printf("%d %d\n", (int)ans[i].x, (int)ans[i].y); } int main() { read(); Prepare(); Query(); return 0; }
#include <bits/stdc++.h> using namespace std; const int dir[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; int n, b, Q, cpos[2][4016100], v[100010]; long long t[100010]; class node { public: int x, y, d; node() {} node(int tx, int ty, int td) { x = tx, y = ty, d = td; } inline int id() { return (x * (b + 1) + y) * 4 + d; } inline node mov(int k) { int nx = (x + dir[k][0]), ny = (y + dir[k][1]); nx = min(max(nx, 0), b); ny = min(max(ny, 0), b); return node(nx, ny, k); } } p[4016100]; int ti, tj; char tk; int main() { scanf("%d%d", &n, &b); for (int i = 0; i <= b; i++) { for (int j = 0; j <= b; j++) { for (int k = 0; k < 4; k++) { p[node(i, j, k).id()] = node(i, j, k).mov(k); } } } for (int i = 1, stx, sty, edx, edy; i <= n; i++) { scanf("%d%d%d%d", &stx, &sty, &edx, &edy); int len = abs(edx - stx) + abs(edy - sty); for (int j = 0; j < 4; j++) { if (edx == stx + dir[j][0] * len && edy == sty + dir[j][1] * len) { int curx = stx, cury = sty; for (int k = 0; k <= len; k++) { for (int l = 0; l < 4; l++) p[node(curx, cury, l).id()] = p[node(curx, cury, j).id()]; curx += dir[j][0]; cury += dir[j][1]; } break; } } } scanf("%d", &Q); for (int i = 1; i <= Q; i++) { scanf("%d %d %c %lld", &ti, &tj, &tk, &t[i]); if (tk == 'U') tk = 0; else if (tk == 'D') tk = 1; else if (tk == 'L') tk = 2; else tk = 3; v[i] = node(ti, tj, tk).id(); } int tot = 4 * (b + 1) * (b + 1); for (int i = 0; i < tot; i++) cpos[0][i] = p[i].id(); for (int i = 0; i < 50; i++) { for (int j = 1; j <= Q; j++) if ((t[j] >> i) & 1) v[j] = cpos[0][v[j]]; for (int j = 0; j < tot; j++) cpos[1][j] = cpos[0][cpos[0][j]]; memcpy(cpos[0], cpos[1], sizeof(cpos[0])); } for (int i = 1; i <= Q; i++) printf("%d %d\n", v[i] / 4 / (b + 1), (v[i] / 4) % (b + 1)); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int walk[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; struct ARROW { int x0, y0, x1, y1; ARROW *to[55]; long long dis[55]; int dir() { return x0 < x1 ? 0 : y0 < y1 ? 1 : x0 > x1 ? 2 : 3; } }; inline bool xl(ARROW *a, ARROW *b) { return max(a->x0, a->x1) < max(b->x0, b->x1); } inline bool xg(ARROW *a, ARROW *b) { return min(a->x0, a->x1) > min(b->x0, b->x1); } inline bool yl(ARROW *a, ARROW *b) { return max(a->y0, a->y1) < max(b->y0, b->y1); } inline bool yg(ARROW *a, ARROW *b) { return min(a->y0, a->y1) > min(b->y0, b->y1); } int N, B, Q; long long T[maxn]; ARROW A[maxn], *S[maxn]; void Init(bool (*cmp)(ARROW *, ARROW *), int dir, int ARROW::*y0, int ARROW::*y1) { for (int i = 1; i <= N + Q; i++) S[i] = A + N + Q - i + 1; stable_sort(S + 1, S + N + Q + 1, cmp); multimap<int, ARROW *> F; typedef multimap<int, ARROW *>::iterator it; for (int i = 1; i <= N + Q; i++) { if (S[i] - A <= N) { it lo = F.lower_bound(min(S[i]->*y0, S[i]->*y1)); it hi = F.upper_bound(max(S[i]->*y0, S[i]->*y1)); for (it j = lo; j != hi; j++) { j->second->to[0] = S[i]; j->second->dis[0] = abs(j->second->x1 - S[i]->x1) + abs(j->second->y1 - S[i]->y1); } F.erase(lo, hi); } if (S[i]->dir() == dir) F.insert(make_pair(S[i]->*y0, S[i])); } } int main() { scanf("%d%d", &N, &B); for (int i = 1; i <= N; i++) scanf("%d%d%d%d", &A[i].x0, &A[i].y0, &A[i].x1, &A[i].y1); scanf("%d", &Q); for (int i = 1; i <= Q; i++) { char dir; scanf("%d%d %c%I64d", &A[i + N].x0, &A[i + N].y0, &dir, &T[i]); A[i + N].x1 = A[i + N].x0, A[i + N].y1 = A[i + N].y0; switch (dir) { case 'R': A[i + N].x0--; break; case 'L': A[i + N].x0++; break; case 'U': A[i + N].y0--; break; case 'D': A[i + N].y0++; } } Init(xl, 0, &ARROW::y0, &ARROW::y1); Init(xg, 2, &ARROW::y0, &ARROW::y1); Init(yl, 1, &ARROW::x0, &ARROW::x1); Init(yg, 3, &ARROW::x0, &ARROW::x1); for (int k = 1; k < 50; k++) for (ARROW *i = A + 1; i <= A + N + Q; i++) if (i->to[k - 1] && i->to[k - 1]->to[k - 1]) { i->to[k] = i->to[k - 1]->to[k - 1]; i->dis[k] = i->dis[k - 1] + i->to[k - 1]->dis[k - 1]; } for (int i = 1; i <= Q; i++) { ARROW *now = A + i + N; long long t = T[i]; for (int k = (t ? 63 - __builtin_clzll(t) : -1); k >= 0; k--) if (now->to[k] && now->dis[k] <= t) t -= now->dis[k], now = now->to[k]; int x = now->x1, y = now->y1, dir = now->dir(); if (now->to[0]) { int half; if (now->x0 == now->x1) { half = max(0, min((now->to[0]->y0 - now->y1) / walk[dir][1], (now->to[0]->y1 - now->y1) / walk[dir][1])); if (half <= t) t -= half, y += walk[dir][1] * half, dir = now->to[0]->dir(); } else { half = max(0, min((now->to[0]->x0 - now->x1) / walk[dir][0], (now->to[0]->x1 - now->x1) / walk[dir][0])); if (half <= t) t -= half, x += walk[dir][0] * half, dir = now->to[0]->dir(); } } x = min<long long>(B, max(0ll, x + walk[dir][0] * t)); y = min<long long>(B, max(0ll, y + walk[dir][1] * t)); printf("%d %d\n", x, y); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; int dx[5] = {0, 1, 0, -1, 0}; int dy[5] = {0, 0, 1, 0, -1}; int n, b, q; int a[N + 5][N + 5]; int nx[N * N * 5 + 55][2]; int ans[111111]; long long t[111111]; int hash1(int x, int y, int d) { if (a[x][y]) d = a[x][y]; return d * N * N + x * N + y; } int main() { int i, j, k, x1, y1, x2, y2, d, h; char ch[5]; scanf("%d%d", &n, &b); for (i = 1; i <= n; i = i + 1) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { if (y1 > y2) d = 4; else d = 2; } else { if (x1 > x2) d = 3; else d = 1; } while (x1 != x2 || y1 != y2) { a[x1][y1] = d; x1 += dx[d]; y1 += dy[d]; } a[x2][y2] = d; } for (i = 0; i <= b; i = i + 1) for (j = 0; j <= b; j = j + 1) for (k = 1; k <= 4; k = k + 1) { if (a[i][j] && a[i][j] != k) continue; h = hash1(i, j, k); x1 = i + dx[k]; y1 = j + dy[k]; if (x1 < 0 || x1 > b || y1 < 0 || y1 > b) nx[h][0] = h; else nx[h][0] = hash1(x1, y1, k); } scanf("%d", &q); for (i = 1; i <= q; i = i + 1) { scanf("%d%d%s%lld", &x1, &x2, &ch, t + i); if (ch[0] == 'R') d = 1; if (ch[0] == 'U') d = 2; if (ch[0] == 'L') d = 3; if (ch[0] == 'D') d = 4; ans[i] = hash1(x1, x2, d); } for (i = 0; i <= 60; i = i + 1) { h = i & 1; for (j = 1; j <= q; j = j + 1) if (t[j] & ((long long)1 << i)) ans[j] = nx[ans[j]][h]; for (j = 1; j < N * N * 5; j = j + 1) nx[j][h ^ 1] = nx[nx[j][h]][h]; } for (i = 1; i <= q; i = i + 1) { y1 = ans[i] % N; ans[i] /= N; x1 = ans[i] % N; cout << x1 << ' ' << y1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; int dx[5] = {0, 1, 0, -1, 0}; int dy[5] = {0, 0, 1, 0, -1}; int n, b, q; int a[N + 5][N + 5]; int nx[N * N * 5 + 55][2]; int ans[111111]; long long t[111111]; int hash1(int x, int y, int d) { if (a[x][y]) d = a[x][y]; return d * N * N + x * N + y; } int main() { int i, j, k, x1, y1, x2, y2, d, h; char ch[5]; scanf("%d%d", &n, &b); for (i = 1; i <= n; i = i + 1) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { if (y1 > y2) d = 4; else d = 2; } else { if (x1 > x2) d = 3; else d = 1; } while (x1 != x2 || y1 != y2) { a[x1][y1] = d; x1 += dx[d]; y1 += dy[d]; } a[x2][y2] = d; } for (i = 0; i <= b; i = i + 1) for (j = 0; j <= b; j = j + 1) for (k = 1; k <= 4; k = k + 1) { if (a[i][j] && a[i][j] != k) continue; h = hash1(i, j, k); x1 = i + dx[k]; y1 = j + dy[k]; if (x1 < 0 || x1 > b || y1 < 0 || y1 > b) nx[h][0] = h; else nx[h][0] = hash1(x1, y1, k); } scanf("%d", &q); for (i = 1; i <= q; i = i + 1) { scanf("%d%d%s%lld", &x1, &x2, &ch, t + i); if (ch[0] == 'R') d = 1; if (ch[0] == 'U') d = 2; if (ch[0] == 'L') d = 3; if (ch[0] == 'D') d = 4; ans[i] = hash1(x1, x2, d); } for (i = 0; i <= 60; i = i + 1) { h = i & 1; for (j = 1; j <= q; j = j + 1) if (t[j] & ((long long)1 << i)) ans[j] = nx[ans[j]][h]; for (j = 1; j < N * N * 5; j = j + 1) nx[j][h ^ 1] = nx[nx[j][h]][h]; } for (i = 1; i <= q; i = i + 1) { y1 = ans[i] % N; ans[i] /= N; x1 = ans[i] % N; cout << x1 << ' ' << y1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class F, class S> ostream& operator<<(ostream& o, const pair<F, S>& p) { return o << "(" << p.first << ", " << p.second << ")"; } template <class C> void O__(ostream& o, const C& c) { bool f = 1; for (const auto& x : c) { if (!f) o << ", "; f = 0; o << x; } } template <class T> ostream& operator<<(ostream& o, const vector<T>& v) { o << "["; O__(o, v); o << "]"; return o; } template <class T, class V> ostream& operator<<(ostream& o, const map<T, V>& v) { o << "{"; O__(o, v); o << "}"; return o; } template <class T> ostream& operator<<(ostream& o, const set<T>& v) { o << "{"; O__(o, v); o << "}"; return o; } const double PI = 3.1415926535897932384626433832795; const double EPS = 1e-9; const int INF = (1 << 31) - 1; const long long LLINF = (1LL << 63) - 1; int b; int code(int x, int y, int dir) { return dir + 4 * ((b + 1) * x + y); } int code(int dx, int dy) { if (dx == 0) { return dy == -1 ? 0 : 1; } return dx == -1 ? 2 : 3; } void uncode(int code, int& dx, int& dy) { if (code < 2) { dx = 0; dy = code == 0 ? -1 : +1; return; } dy = 0; dx = code == 2 ? -1 : +1; } int sign(int x) { return x == 0 ? x : x / abs(x); } void pr(int c) { int x, y; c /= 4; y = c % (b + 1); x = c / (b + 1); printf("%d %d\n", x, y); } int main() { int n; cin >> n >> b; vector<vector<int>> plan(b + 1, vector<int>(b + 1, -1)); for (int i = 0; i < n; ++i) { int x, y, X, Y; scanf("%d%d%d%d", &x, &y, &X, &Y); int dx = sign(X - x); int dy = sign(Y - y); while (make_pair(x, y) != make_pair(X, Y)) { plan[x][y] = code(dx, dy); x += dx; y += dy; } plan[x][y] = code(dx, dy); } int size = (b + 1) * (b + 1) * 4; vector<int> to(size, -1); for (int i = 0; i < size; ++i) { to[i] = i; } for (int x = 0; x <= b; ++x) { for (int y = 0; y <= b; ++y) { for (int d = 0; d < 4; ++d) { int dir = -1; if (plan[x][y] != -1) { dir = plan[x][y]; } else { dir = d; } int dx, dy; uncode(dir, dx, dy); int nx = x + dx; int ny = y + dy; assert(dx == 0 ^ dy == 0); if (nx < 0 || ny < 0 || nx > b || ny > b) continue; to[code(x, y, d)] = code(nx, ny, dir); } } } vector<vector<int>> dp(25, vector<int>(size, -1)); dp[0] = to; for (int i = 1; i < dp.size(); ++i) { for (int s = 0; s < size; ++s) { int ne = s; for (int k = 0; k < 4; ++k) { ne = dp[i - 1][ne]; if (ne == -1) { dp[i][s] = -1; break; } dp[i][s] = ne; } } } int q; cin >> q; while (q-- > 0) { int x, y; char buf[123]; scanf("%d%d%s", &x, &y, buf); int dx, dy; char c = buf[0]; dx = dy = 0; if (c == 'U') { ++dy; } if (c == 'D') --dy; if (c == 'L') --dx; if (c == 'R') ++dx; long long t; scanf("%I64d", &t); int st = code(x, y, code(dx, dy)); for (int p2 = dp.size() - 1; p2 >= 0; --p2) { while (t >= (1LL << (p2 + p2))) { t -= (1LL << (p2 + p2)); st = dp[p2][st]; } } pr(st); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; int dx[5] = {0, 1, 0, -1, 0}; int dy[5] = {0, 0, 1, 0, -1}; int n, b, q; int a[N + 5][N + 5]; int nx[N * N * 5 + 55][2]; int ans[111111]; long long t[111111]; int hash1(int x, int y, int d) { if (a[x][y]) d = a[x][y]; return d * N * N + x * N + y; } int main() { int i, j, k, x1, y1, x2, y2, d, h; char ch[5]; scanf("%d%d", &n, &b); for (i = 1; i <= n; i = i + 1) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { if (y1 > y2) d = 4; else d = 2; } else { if (x1 > x2) d = 3; else d = 1; } while (x1 != x2 || y1 != y2) { a[x1][y1] = d; x1 += dx[d]; y1 += dy[d]; } a[x2][y2] = d; } for (i = 0; i <= b; i = i + 1) for (j = 0; j <= b; j = j + 1) for (k = 1; k <= 4; k = k + 1) { if (a[i][j] && a[i][j] != k) continue; h = hash1(i, j, k); x1 = i + dx[k]; y1 = j + dy[k]; if (x1 < 0 || x1 > b || y1 < 0 || y1 > b) nx[h][0] = h; else nx[h][0] = hash1(x1, y1, k); } scanf("%d", &q); for (i = 1; i <= q; i = i + 1) { scanf("%d%d%s%lld", &x1, &x2, &ch, t + i); if (ch[0] == 'R') d = 1; if (ch[0] == 'U') d = 2; if (ch[0] == 'L') d = 3; if (ch[0] == 'D') d = 4; ans[i] = hash1(x1, x2, d); } for (i = 0; i <= 60; i = i + 1) { h = i & 1; for (j = 1; j <= q; j = j + 1) if (t[j] & ((long long)1 << i)) ans[j] = nx[ans[j]][h]; for (j = 1; j < N * N * 5; j = j + 1) nx[j][h ^ 1] = nx[nx[j][h]][h]; } for (i = 1; i <= q; i = i + 1) { y1 = ans[i] % N; ans[i] /= N; x1 = ans[i] % N; cout << x1 << ' ' << y1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; bool debug = false; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; long long ln, lk, lm; pair<int, int> from[100005], to[100005]; int dir[100005], q, ans[100005 * 2]; char o[2]; string DIR = "URDL"; struct query { int x, y, id, d; long long t; } qy[100005]; int getdir(int x0, int y0, int x1, int y1) { if (x0 == x1) { if (y0 < y1) return 0; else return 2; } else { if (x0 < x1) return 1; else return 3; } } struct EVENT { int tim, type, id; int x; int l, r; } eve[100005 * 2]; struct segTree { int col[4 * 100005]; void pushDown(int rt) { if (col[rt] != -1) { col[(rt << 1)] = col[rt]; col[(rt << 1 | 1)] = col[rt]; col[rt] = -1; } } void update(int rt, int l, int r, int L, int R, int k) { if (L <= l && r <= R) { col[rt] = k; return; } pushDown(rt); int mid = (l + r) / 2; if (L <= mid) update((rt << 1), l, mid, L, R, k); if (R > mid) update((rt << 1 | 1), mid + 1, r, L, R, k); } int query(int rt, int l, int r, int x) { if (l == r) return col[rt]; pushDown(rt); int mid = (l + r) / 2; if (x <= mid) return query((rt << 1), l, mid, x); else return query((rt << 1 | 1), mid + 1, r, x); } } T; bool cmp(const EVENT &a, const EVENT &b) { return a.tim > b.tim || (a.tim == b.tim && a.type > b.type); } int ff[61][100005], d2[100005]; long long d1[61][100005]; void pre() { int tot = 0; for (int(k) = 0; (k) < (int)(4); (k)++) { tot = 0; for (int(i) = 1; (i) <= (int)(n + q); (i)++) { int x, y, dd; if (i <= n) { x = to[i].first + dx[k]; y = to[i].second + dy[k]; dd = dir[i]; } else { x = qy[i - n].x; y = qy[i - n].y; dd = qy[i - n].d; } if (dd != k) continue; tot++; eve[tot].tim = x * dx[k] + y * dy[k]; eve[tot].type = 0; if (k & 1) eve[tot].x = y; else eve[tot].x = x; eve[tot].id = i; } for (int(i) = 1; (i) <= (int)(n); (i)++) { tot++; eve[tot].tim = max(from[i].first * dx[k] + from[i].second * dy[k], to[i].first * dx[k] + to[i].second * dy[k]); eve[tot].type = 1; if (k & 1) { eve[tot].l = min(from[i].second, to[i].second); eve[tot].r = max(from[i].second, to[i].second); } else { eve[tot].l = min(from[i].first, to[i].first); eve[tot].r = max(from[i].first, to[i].first); } eve[tot].id = i; } sort(eve + 1, eve + tot + 1, cmp); T.update(1, 0, m, 0, m, 0); for (int(i) = 1; (i) <= (int)(tot); (i)++) { if (eve[i].type == 0) ans[eve[i].id] = T.query(1, 0, m, eve[i].x); else T.update(1, 0, m, eve[i].l, eve[i].r, eve[i].id); } } for (int(i) = 1; (i) <= (int)(n); (i)++) { int x, y, len, fa = ans[i]; ff[0][i] = fa; if (!fa) continue; if (dir[i] & 1) len = min(abs(to[i].first - to[fa].first), abs(to[i].first - from[fa].first)); else len = min(abs(to[i].second - to[fa].second), abs(to[i].second - from[fa].second)); d2[i] = len; x = to[i].first + len * dx[dir[i]]; y = to[i].second + len * dy[dir[i]]; d1[0][i] = d2[i] + abs(x - to[fa].first) + abs(y - to[fa].second); } for (int(i) = 1; (i) <= (int)(50); (i)++) { for (int(j) = 1; (j) <= (int)(n); (j)++) { ff[i][j] = ff[i - 1][ff[i - 1][j]]; d1[i][j] = d1[i - 1][j] + d1[i - 1][ff[i - 1][j]]; if (d1[i][j] > (1LL << 60)) d1[i][j] = (1LL << 60); } } } void walk(long long &x, long long &y, int dir, long long t) { x = min((long long)m, max(0ll, x + t * dx[dir])); y = min((long long)m, max(0ll, y + t * dy[dir])); } int main() { scanf("%d%d", &n, &m); for (int(i) = 1; (i) <= (int)(n); (i)++) { scanf("%d%d%d%d", &from[i].first, &from[i].second, &to[i].first, &to[i].second); dir[i] = getdir(from[i].first, from[i].second, to[i].first, to[i].second); } scanf("%d", &q); for (int(i) = 1; (i) <= (int)(q); (i)++) { scanf("%d%d%s%lld", &qy[i].x, &qy[i].y, o, &qy[i].t); for (int(k) = 0; (k) < (int)(4); (k)++) if (DIR[k] == o[0]) qy[i].d = k; } pre(); for (int(i) = 1; (i) <= (int)(q); (i)++) { long long x = qy[i].x, y = qy[i].y, w = qy[i].d, u = ans[i + n], dis1, dis2; long long t = qy[i].t; if (w & 1) { if (((x > from[u].first) ^ (x > to[u].first)) == 1) dis1 = 0; else dis1 = min(abs(x - from[u].first), abs(x - to[u].first)); } else { if (((y > from[u].second) ^ (y > to[u].second)) == 1) dis1 = 0; else dis1 = min(abs(y - from[u].second), abs(y - to[u].second)); } if (!u || t < dis1) { walk(x, y, w, t); printf("%lld %lld\n", x, y); continue; } t -= dis1; walk(x, y, w, dis1); dis2 = abs(x - to[u].first) + abs(y - to[u].second); if (t < dis2) { walk(x, y, dir[u], t); printf("%lld %lld\n", x, y); continue; } t -= dis2; walk(x, y, dir[u], dis2); for (int j = 50; j >= 0; --j) if (ff[j][u] && t >= d1[j][u]) { t -= d1[j][u]; u = ff[j][u]; x = to[u].first; y = to[u].second; } if (!ff[0][u] || t < d2[u]) { walk(x, y, dir[u], t); printf("%lld %lld\n", x, y); continue; } t -= d2[u]; walk(x, y, dir[u], d2[u]); walk(x, y, dir[ff[0][u]], t); printf("%lld %lld\n", x, y); } return 0; }
#include <bits/stdc++.h> using namespace std; short to[2][1001][1001][4][3]; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; int Sign(int x) { return x > 0 ? 1 : x < 0 ? -1 : 0; } int cur = 0, nxt = 1; int x[100005], y[100005], dir[100005]; long long t[100005]; int n, b; void MakeStep() { for (int x = 0; x < b; ++x) { for (int y = 0; y < b; ++y) { for (int j = 0; j < 4; ++j) { int nx = to[cur][x][y][j][0], ny = to[cur][x][y][j][1], dd = to[cur][x][y][j][2]; if (nx < 0 || nx >= b || ny < 0 || ny >= b) { to[nxt][x][y][j][0] = nx; to[nxt][x][y][j][1] = ny; to[nxt][x][y][j][2] = -1; } else { to[nxt][x][y][j][0] = to[cur][nx][ny][dd][0]; to[nxt][x][y][j][1] = to[cur][nx][ny][dd][1]; to[nxt][x][y][j][2] = to[cur][nx][ny][dd][2]; } } } } swap(cur, nxt); } int main() { cin >> n >> b; ++b; for (int i = 0; i < b; ++i) { for (int j = 0; j < b; ++j) { for (int d = 0; d < 4; ++d) { int nx = i + dx[d], ny = j + dy[d]; to[0][i][j][d][0] = nx; to[0][i][j][d][1] = ny; to[0][i][j][d][2] = d; } } } for (int i = 0; i < n; ++i) { int x0, y0, x1, y1; cin >> x0 >> y0 >> x1 >> y1; int dx = Sign(x1 - x0), dy = Sign(y1 - y0); int dd = 0; for (int j = 0; j < 4; ++j) { if (dx == ::dx[j] && dy == ::dy[j]) { dd = j; break; } } while (x0 != x1 || y0 != y1) { for (int j = 0; j < 4; ++j) { to[0][x0][y0][j][0] = x0 + dx; to[0][x0][y0][j][1] = y0 + dy; to[0][x0][y0][j][2] = dd; } x0 += dx; y0 += dy; } for (int j = 0; j < 4; ++j) { to[0][x0][y0][j][0] = x0 + dx; to[0][x0][y0][j][1] = y0 + dy; to[0][x0][y0][j][2] = dd; } } for (int i = 1; i < 55; ++i) { } int q; cin >> q; for (int i = 0; i < q; ++i) { char d; cin >> x[i] >> y[i] >> d >> t[i]; dir[i] = 0; if (d == 'L') { dir[i] = 3; } else if (d == 'R') { dir[i] = 1; } else if (d == 'D') { dir[i] = 2; } else { dir[i] = 0; } } for (int j = 0; j < 55; ++j) { for (int i = 0; i < q; ++i) { if (x[i] < 0 || x[i] >= b || y[i] < 0 || y[i] >= b) { continue; } if (t[i] & 1) { int nx = to[cur][x[i]][y[i]][dir[i]][0]; int ny = to[cur][x[i]][y[i]][dir[i]][1]; dir[i] = to[cur][x[i]][y[i]][dir[i]][2]; x[i] = nx; y[i] = ny; } t[i] >>= 1; } MakeStep(); } for (int i = 0; i < q; ++i) { x[i] = max(0, min(x[i], b - 1)); y[i] = max(0, min(y[i], b - 1)); cout << x[i] << " " << y[i] << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; int dx[5] = {0, 1, 0, -1, 0}; int dy[5] = {0, 0, 1, 0, -1}; int n, b, q; int a[N + 5][N + 5]; int nx[N * N * 5 + 55][2]; int ans[111111]; long long t[111111]; int hash1(int x, int y, int d) { if (a[x][y]) d = a[x][y]; return d * N * N + x * N + y; } int main() { int i, j, k, x1, y1, x2, y2, d, h; char ch[5]; scanf("%d%d", &n, &b); for (i = 1; i <= n; i = i + 1) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { if (y1 > y2) d = 4; else d = 2; } else { if (x1 > x2) d = 3; else d = 1; } while (x1 != x2 || y1 != y2) { a[x1][y1] = d; x1 += dx[d]; y1 += dy[d]; } a[x2][y2] = d; } for (i = 0; i <= b; i = i + 1) for (j = 0; j <= b; j = j + 1) for (k = 1; k <= 4; k = k + 1) { if (a[i][j] && a[i][j] != k) continue; h = hash1(i, j, k); x1 = i + dx[k]; y1 = j + dy[k]; if (x1 < 0 || x1 > b || y1 < 0 || y1 > b) nx[h][0] = h; else nx[h][0] = hash1(x1, y1, k); } scanf("%d", &q); for (i = 1; i <= q; i = i + 1) { scanf("%d%d%s%lld", &x1, &x2, &ch, t + i); if (ch[0] == 'R') d = 1; if (ch[0] == 'U') d = 2; if (ch[0] == 'L') d = 3; if (ch[0] == 'D') d = 4; ans[i] = hash1(x1, x2, d); } for (i = 0; i <= 60; i = i + 1) { h = i & 1; for (j = 1; j <= q; j = j + 1) if (t[j] & ((long long)1 << i)) ans[j] = nx[ans[j]][h]; for (j = 1; j < N * N * 5; j = j + 1) nx[j][h ^ 1] = nx[nx[j][h]][h]; } for (i = 1; i <= q; i = i + 1) { y1 = ans[i] % N; ans[i] /= N; x1 = ans[i] % N; cout << x1 << ' ' << y1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int bel[1007][1007], vis[1007][1007]; int mp[1007][1007]; int tox[1007][1007], toy[1007][1007]; long long step[1007][1007]; struct Node { int x1, y1, x2, y2; int d; } arr[1007]; int dir[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}}; int n, b; bool inside(int x, int y) { return x >= 0 && y >= 0 && x <= b && y <= b; } int main() { cin >> n >> b; int xx, yy, dx, dy; long long t; char s[5]; for (int i = 1; i <= n; ++i) { scanf("%d%d%d%d", &arr[i].x1, &arr[i].y1, &arr[i].x2, &arr[i].y2); if (arr[i].y1 > arr[i].y2) arr[i].d = 0; if (arr[i].y1 < arr[i].y2) arr[i].d = 1; if (arr[i].x1 > arr[i].x2) arr[i].d = 2; if (arr[i].x1 < arr[i].x2) arr[i].d = 3; for (int x = min(arr[i].x1, arr[i].x2); x <= max(arr[i].x1, arr[i].x2); ++x) for (int y = min(arr[i].y1, arr[i].y2); y <= max(arr[i].y1, arr[i].y2); ++y) bel[x][y] = i; } for (int i = 1; i <= n; ++i) { dx = dir[arr[i].d][0]; dy = dir[arr[i].d][1]; xx = arr[i].x2 + dx; yy = arr[i].y2 + dy; while (inside(xx, yy) && !bel[xx][yy]) xx += dx, yy += dy; for (int x = min(arr[i].x1, arr[i].x2); x <= max(arr[i].x1, arr[i].x2); ++x) for (int y = min(arr[i].y1, arr[i].y2); y <= max(arr[i].y1, arr[i].y2); ++y) tox[x][y] = xx, toy[x][y] = yy; } int m; cin >> m; int haha = 1; while (m--) { scanf("%d%d%s", &xx, &yy, s); cin >> t; ++haha; switch (s[0]) { case 'D': dx = dir[0][0], dy = dir[0][1]; break; case 'U': dx = dir[1][0], dy = dir[1][1]; break; case 'L': dx = dir[2][0], dy = dir[2][1]; break; case 'R': dx = dir[3][0], dy = dir[3][1]; break; } while (inside(xx, yy) && !bel[xx][yy] && t) xx += dx, yy += dy, --t; if (!inside(xx, yy)) { xx -= dx, yy -= dy; printf("%d %d\n", xx, yy); continue; } if (!t) { printf("%d %d\n", xx, yy); continue; } int tx, ty; while (t) { if (!inside(xx, yy)) break; tx = tox[xx][yy]; ty = toy[xx][yy]; if (vis[xx][yy] == haha) t %= step[xx][yy] - t; else step[xx][yy] = t, vis[xx][yy] = haha; dx = dy = 0; if (tx > xx) dx = 1; if (tx < xx) dx = -1; if (ty > yy) dy = 1; if (ty < yy) dy = -1; if (abs(tx - xx) + abs(ty - yy) < t) { t -= abs(tx - xx) + abs(ty - yy); xx = tx, yy = ty; } else break; } if (inside(xx, yy)) { xx += t * dx; yy += t * dy; } if (xx < 0) xx = 0; if (yy < 0) yy = 0; if (xx > b) xx = b; if (yy > b) yy = b; printf("%d %d\n", xx, yy); } }
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e17; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; const char dir[4] = {'U', 'R', 'D', 'L'}; inline int mabs(int x) { return (x > 0 ? x : -x); } const int N = 400010; int arr[N]; long long when[N], dist[N], qt[N]; int pv[55][N]; long long pt[55][N]; int xa[N], ya[N], xb[N], yb[N], arrd[N]; int wx[N], wy[N], wd[N]; int n, b, q; int what[1010][1010]; void get_next_all() { for (int i = 1; i <= n + q; i++) { int cx = wx[i], cy = wy[i]; arr[i] = 0; when[i] = 0; while (cx >= 0 && cy >= 0 && cx <= b && cy <= b) { if (what[cx][cy] != 0) { arr[i] = what[cx][cy]; break; } when[i]++; cx += dx[wd[i]]; cy += dy[wd[i]]; } if (arr[i] == 0) when[i] = inf; } } void mark(int xa, int ya, int xb, int yb, int arrow) { if (xa > xb) { int tmp = xa; xa = xb; xb = tmp; } if (ya > yb) { int tmp = ya; ya = yb; yb = tmp; } for (int i = xa; i <= xb; i++) for (int j = ya; j <= yb; j++) what[i][j] = arrow; } int main() { scanf("%d %d", &n, &b); for (int i = 0; i <= b; i++) for (int j = 0; j <= b; j++) what[i][j] = 0; for (int i = 1; i <= n; i++) { scanf("%d %d %d %d", xa + i, ya + i, xb + i, yb + i); if (yb[i] > ya[i]) arrd[i] = 0; else if (xb[i] > xa[i]) arrd[i] = 1; else if (yb[i] < ya[i]) arrd[i] = 2; else arrd[i] = 3; wx[i] = xb[i] + dx[arrd[i]]; wy[i] = yb[i] + dy[arrd[i]]; wd[i] = arrd[i]; mark(xa[i], ya[i], xb[i], yb[i], i); } scanf("%d", &q); for (int i = n + 1; i <= n + q; i++) { char foo; scanf("%d %d %c %I64d", wx + i, wy + i, &foo, qt + i); wd[i] = 0; for (int j = 0; j < 4; j++) if (foo == dir[j]) wd[i] = j; } get_next_all(); for (int i = 1; i <= n + q; i++) if (arr[i] == 0) dist[i] = inf; else dist[i] = mabs(wx[i] - xb[arr[i]]) + mabs(wy[i] - yb[arr[i]]); arr[0] = 0; when[0] = inf; dist[0] = inf; for (int i = 0; i <= n; i++) pv[0][i] = arr[i], pt[0][i] = dist[i] + 1; for (int j = 1; j <= 50; j++) for (int i = 0; i <= n; i++) { pv[j][i] = pv[j - 1][pv[j - 1][i]]; pt[j][i] = pt[j - 1][i] + pt[j - 1][pv[j - 1][i]]; if (pt[j][i] > inf) pt[j][i] = inf; } for (int i = n + 1; i <= n + q; i++) { if (qt[i] <= dist[i]) { if (qt[i] <= when[i]) { long long ax = wx[i] + dx[wd[i]] * qt[i]; long long ay = wy[i] + dy[wd[i]] * qt[i]; if (ax < 0) ax = 0; if (ay < 0) ay = 0; if (ax > b) ax = b; if (ay > b) ay = b; printf("%d %d\n", (int)ax, (int)ay); continue; } long long ax = wx[i] + dx[wd[i]] * when[i]; long long ay = wy[i] + dy[wd[i]] * when[i]; ax += dx[arrd[arr[i]]] * (qt[i] - when[i]); ay += dy[arrd[arr[i]]] * (qt[i] - when[i]); if (ax < 0) ax = 0; if (ay < 0) ay = 0; if (ax > b) ax = b; if (ay > b) ay = b; printf("%d %d\n", (int)ax, (int)ay); continue; } qt[i] -= dist[i]; int where = arr[i]; for (int j = 50; j >= 0; j--) if (pt[j][where] <= qt[i]) { qt[i] -= pt[j][where]; where = pv[j][where]; } if (arr[where] == 0 || qt[i] <= when[where] + 1) { long long ax = xb[where] + dx[arrd[where]] * qt[i]; long long ay = yb[where] + dy[arrd[where]] * qt[i]; if (ax < 0) ax = 0; if (ay < 0) ay = 0; if (ax > b) ax = b; if (ay > b) ay = b; printf("%d %d\n", (int)ax, (int)ay); continue; } int ax = xb[where] + dx[arrd[where]] * (when[where] + 1); int ay = yb[where] + dy[arrd[where]] * (when[where] + 1); ax += dx[arrd[arr[where]]] * (qt[i] - (when[where] + 1)); ay += dy[arrd[arr[where]]] * (qt[i] - (when[where] + 1)); printf("%d %d\n", ax, ay); } return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(0.0) * 2.0; const double eps = 1e-12; const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; template <class T> inline T abs1(T a) { return a < 0 ? -a : a; } template <class T> inline T max1(T a, T b, T c = -1234567, T d = -1234567) { T ans = a > b ? a : b; if (c != -1234567) ans = max1(ans, c); if (d != -1234567) ans = max1(ans, d); return ans; } template <class T> inline T min1(T a, T b, T c = -1234567, T d = -1234567) { T ans = a < b ? a : b; if (c != -1234567) ans = min(ans, c); if (d != -1234567) ans = min(ans, d); return ans; } template <class T> inline T gcd1(T a, T b) { if (a < b) swap(a, b); if (a % b == 0) return b; return gcd1(b, a % b); } template <class T> inline T lb(T num) { return num & (-num); } inline int jud(double a, double b) { if (abs(a) < eps && abs(b) < eps) return 0; else if (abs1(a - b) / abs1(a) < eps) return 0; if (a < b) return -1; return 1; } template <typename t> inline int jud(t a, t b) { if (a < b) return -1; if (a == b) return 0; return 1; } template <typename it, typename t1> inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) { int be = 0, en = na - 1; if (*a <= *(a + na - 1)) { if (f_lb == 0) while (be < en) { int mid = (be + en + 1) / 2; if (jud(*(a + mid), val) != 1) be = mid; else en = mid - 1; } else while (be < en) { int mid = (be + en) / 2; if (jud(*(a + mid), val) != -1) en = mid; else be = mid + 1; } if (f_small && jud(*(a + be), val) == 1) be--; if (!f_small && jud(*(a + be), val) == -1) be++; } else { if (f_lb) while (be < en) { int mid = (be + en + 1) / 2; if (jud(*(a + mid), val) != -1) be = mid; else en = mid - 1; } else while (be < en) { int mid = (be + en) / 2; if (jud(*(a + mid), val) != 1) en = mid; else be = mid + 1; } if (!f_small && jud(*(a + be), val) == -1) be--; if (f_small && jud(*(a + be), val) == 1) be++; } return be; } inline int bitnum(unsigned long long nValue) { nValue = ((0xaaaaaaaaaaaaaaaaull & nValue) >> 1) + (0x5555555555555555ull & nValue); nValue = ((0xccccccccccccccccull & nValue) >> 2) + (0x3333333333333333ull & nValue); nValue = ((0xf0f0f0f0f0f0f0f0ull & nValue) >> 4) + (0x0f0f0f0f0f0f0f0full & nValue); nValue = ((0xff00ff00ff00ff00ull & nValue) >> 8) + (0x00ff00ff00ff00ffull & nValue); nValue = ((0xffff0000ffff0000ull & nValue) >> 16) + (0x0000ffff0000ffffull & nValue); nValue = ((0xffffffff00000000ull & nValue) >> 32) + (0x00000000ffffffffull & nValue); return nValue; } long long pow(long long n, long long m, long long mod = 0) { long long ans = 1; long long k = n; while (m) { if (m & 1) { ans *= k; if (mod) ans %= mod; } k *= k; if (mod) k %= mod; m >>= 1; } return ans; } const int maxn = 100110; template <class t> struct segment_node { int be, en; t num, add; }; template <class t> struct segment_tree { int l; segment_node<t> tree[maxn * 4]; inline int gleft(int no) { return no << 1; } inline int gright(int no) { return (no << 1) + 1; } inline int gfa(int no) { return no >> 1; } inline segment_tree() { l = 0; } void build(int no, int l, int r) { if (l == r) { tree[no].be = tree[no].en = l; tree[no].num = -1; tree[no].add = -1; return; } tree[no].be = l; tree[no].en = r; int mid = (l + r) / 2; build(gleft(no), l, mid); build(gright(no), mid + 1, r); tree[no].add = -1; } void relax(int no) { tree[gleft(no)].add = tree[no].add; tree[gright(no)].add = tree[no].add; tree[no].add = -1; } void down(int l, int r, int no, t ranadd) { if (tree[no].be != tree[no].en && tree[no].add != -1) relax(no); if (l <= tree[no].be && r >= tree[no].en) { tree[no].add = ranadd; return; } int mid = (tree[no].be + tree[no].en) / 2; if (r >= tree[no].be && l <= mid) down(l, r, gleft(no), ranadd); if (r >= mid + 1 && l <= tree[no].en) down(l, r, gright(no), ranadd); } t getnum(int loc, int no) { if (tree[no].add != -1) { int ans = tree[no].add; if (tree[no].be != tree[no].en) relax(no); return ans; } if (tree[no].be == tree[no].en) return tree[no].num; int mid = (tree[no].be + tree[no].en) / 2; if (loc <= mid) return getnum(loc, gleft(no)); else return getnum(loc, gright(no)); } }; segment_tree<int> sgt[4]; struct segment { int from, to, no; segment(int a = 0, int b = 0, int no1 = 0) { from = min(a, b); to = max(a, b); no = no1; } }; vector<segment> orig[2][maxn]; pair<int, int> orig1[maxn * 2], origto[maxn]; long long origdir[maxn * 2], origstep[maxn]; struct vec { int no, loc; vec(int a = 0, int b = 0) : no(a), loc(b) {} }; vector<vec> she[4][maxn]; map<pair<int, int>, int> trans; long long narrow, b, nq; struct node { int fa, delta; long long dis; node(int a = 0, int b = 0, int c = 0) { fa = a, dis = b, delta = c; } } fa[60][maxn * 2]; inline int finddir(pair<int, int> from, pair<int, int> to) { to.first -= from.first; to.second -= from.second; if (to.first) to.first /= abs(to.first); if (to.second) to.second /= abs(to.second); for (int j = 0; j < 4; j++) if (to.first == step[j][0] && to.second == step[j][1]) return j; return 0; } inline long long cntdis(pair<int, int> co, pair<int, int> co1) { return abs(co.first - co1.first) + abs(co.second - co1.second); } inline pair<long long, long long> finddis(int from, int dir, int to) { pair<long long, long long> ans; if (to == -1) { ans = orig1[from]; if (dir > 1) swap(ans.first, ans.second); if (!(dir & 1)) ans.first = b - ans.first; ans.second = 0; return ans; } if ((origdir[to] ^ dir) < 2) { long long dis1 = orig1[from].first - orig1[to].first, dis2 = orig1[from].first - origto[to].first; if (orig1[to].first == orig1[from].first && orig1[to].first == origto[to].first) dis1 = orig1[from].second - orig1[to].second, dis2 = orig1[from].second - origto[to].second; if (dis1 * dis2 < 0) { ans.first = 0; ans.second = abs1(dis1); } else { ans.first = min(abs1(dis1), abs1(dis2)); ans.second = 0; if (abs1(dis1) > abs1(dis2)) ans.second = abs1(dis1) - abs1(dis2); } return ans; } ans = orig1[to]; ans.first -= orig1[from].first; ans.second -= orig1[from].second; if (dir > 1) swap(ans.first, ans.second); ans.first = abs1(ans.first); ans.second = abs1(ans.second); return ans; } int main() { scanf("%I64d%I64d", &narrow, &b); for (int i = 0; i < narrow; i++) { pair<int, int> from, to; scanf("%d%d%d%d", &from.first, &from.second, &to.first, &to.second); trans[from] = i; orig1[i] = from; origto[i] = to; if (from.first == to.first) { orig[0][from.first].push_back(segment(from.second, to.second, i)); orig[1][max(from.second, to.second)].push_back( segment(from.first, from.first, i)); orig[1][min(from.second, to.second)].push_back( segment(from.first, from.first, i)); } else { orig[1][from.second].push_back(segment(from.first, to.first, i)); orig[0][max(from.first, to.first)].push_back( segment(from.second, from.second, i)); orig[0][min(from.first, to.first)].push_back( segment(from.second, from.second, i)); } int randir = finddir(from, to); origdir[i] = randir; if (randir > 1) swap(to.first, to.second); she[randir][to.first].push_back(vec(i, to.second)); } scanf("%I64d", &nq); for (int i = 0; i < nq; i++) { pair<int, int> st; char randirc; long long step1, randir; scanf("%d %d %c %I64d", &st.first, &st.second, &randirc, &step1); orig1[i + narrow] = st; trans[st] = i + narrow; if (randirc == 'L') randir = 1; if (randirc == 'R') randir = 0; if (randirc == 'U') randir = 2; if (randirc == 'D') randir = 3; origdir[i + narrow] = randir, origstep[i] = step1; if (randir > 1) swap(st.first, st.second); she[randir][st.first].push_back(vec(i + narrow, st.second)); } for (int i = 0; i < 4; i++) { sgt[i].build(1, 0, b + 1); int from = 0, add = 1, to = b + 1; if (!(i & 1)) from = b, add = -1, to = -1; int biao = i / 2; for (int j = from; j != to; j += add) { for (int t = 0; t < (int)she[i][j].size(); t++) if (she[i][j][t].no < narrow) { fa[0][she[i][j][t].no].fa = sgt[i].getnum(she[i][j][t].loc, 1); pair<long long, long long> randis = finddis(she[i][j][t].no, i, fa[0][she[i][j][t].no].fa); fa[0][she[i][j][t].no].dis = randis.first; fa[0][she[i][j][t].no].delta = randis.second; } for (int t = 0; t < (int)orig[biao][j].size(); t++) sgt[i].down(orig[biao][j][t].from, orig[biao][j][t].to, 1, orig[biao][j][t].no); for (int t = 0; t < (int)she[i][j].size(); t++) if (she[i][j][t].no >= narrow) { fa[0][she[i][j][t].no].fa = sgt[i].getnum(she[i][j][t].loc, 1); pair<long long, long long> randis = finddis(she[i][j][t].no, i, fa[0][she[i][j][t].no].fa); fa[0][she[i][j][t].no].dis = randis.first; fa[0][she[i][j][t].no].delta = randis.second; } } } for (int i = 1; i < 60; i++) { for (int j = 0; j < narrow + nq; j++) if (fa[i - 1][j].fa != -1) { fa[i][j].fa = fa[i - 1][fa[i - 1][j].fa].fa; fa[i][j].delta = fa[i - 1][fa[i - 1][j].fa].delta; fa[i][j].dis = fa[i - 1][fa[i - 1][j].fa].dis + fa[i - 1][j].dis - fa[i - 1][j].delta; } else fa[i][j] = fa[i - 1][j]; } for (long long i1 = 0; i1 < nq; i1++) { long long be = 0, en = 1000000000000000ll; long long no, dis, delta; while (be < en) { long long mid = (be + en + 1) / 2, ranmid = mid; no = i1 + narrow, dis = 0, delta = 0; for (int i = 0; mid && no != -1; i++, mid >>= 1) if (mid & 1) { dis += fa[i][no].dis; dis -= delta; delta = fa[i][no].delta; no = fa[i][no].fa; } if (no == -1 || dis > origstep[i1]) en = ranmid - 1ll; else be = ranmid; } no = i1 + narrow, dis = 0, delta = 0; for (int i = 0; be && no != -1; i++, be >>= 1) if (be & 1) { dis += fa[i][no].dis; dis -= delta; delta = fa[i][no].delta; no = fa[i][no].fa; } origstep[i1] -= dis - delta; printf("%I64d %I64d\n", max1(0ll, min1(b, orig1[no].first + origstep[i1] * step[origdir[no]][0])), max1(0ll, min1(b, orig1[no].second + origstep[i1] * step[origdir[no]][1]))); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int inf = 0x3f3f3f3f; const long long linf = 1LL << 60; const long double eps = 1e-9; int read() { int x = 0, f = 1, c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f ^= 1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 48; return f ? x : -x; } void write(int x) { if (!x) { putchar('0'); return; } if (x < 0) putchar('-'), x = -x; int stk[10], tp = 0; for (; x; x /= 10) stk[tp++] = x - x / 10 * 10; for (; tp; putchar(48 + stk[--tp])) ; } const int N = 1 << 17; const char *Dir = "UDLR"; const int dx[] = {0, 0, -1, 1}; const int dy[] = {1, -1, 0, 0}; struct SegT { pair<int, int> mn[N << 1]; void init() { for (int i = 0; i < (N << 1); ++i) mn[i] = make_pair(inf, 0); } void chkmin(pair<int, int> &x, pair<int, int> y) { if (y < x) x = y; } void upd(int l, int r, pair<int, int> v) { for (l += N, r += N; l < r; l >>= 1, r >>= 1) { if (l & 1) chkmin(mn[l++], v); if (r & 1) chkmin(mn[--r], v); } } pair<int, int> qry(int x) { pair<int, int> ans = make_pair(inf, 0); for (x += N; x; x >>= 1) { chkmin(ans, mn[x]); } return ans; } } segt; int n, b, q; vector<pair<pair<int, int>, pair<int, int> > > arrow; vector<pair<pair<int, int>, pair<int, long long> > > qry; struct Arrow { int x1, y1; int x2, y2; int dir, id; int getLowCol() { return min(y1, y2); } int getHighCol() { return max(y1, y2); } int getLowRow() { return min(x1, x2); } int getHighRow() { return max(x1, x2); } } a[N + N]; bool cmp1(Arrow a, Arrow b) { return a.getHighCol() > b.getHighCol() || (a.getHighCol() == b.getHighCol() && a.id < b.id); } bool cmp2(Arrow a, Arrow b) { return a.getLowCol() < b.getLowCol() || (a.getLowCol() == b.getLowCol() && a.id < b.id); } bool cmp3(Arrow a, Arrow b) { return a.getHighRow() > b.getHighRow() || (a.getHighRow() == b.getHighRow() && a.id < b.id); } bool cmp4(Arrow a, Arrow b) { return a.getLowRow() < b.getLowRow() || (a.getLowRow() == b.getLowRow() && a.id < b.id); } map<pair<int, int>, pair<pair<int, int>, long long> > go; int dir[N]; map<pair<int, int>, int> belong; vector<pair<int, int> > vec; vector<pair<int, long long> > Go[50]; vector<int> has[N]; pair<pair<int, int>, long long> fstGo[N]; void print(int a, int b) { write(a); putchar(' '); write(b); putchar('\n'); } void mov(int x, int y, int tp, long long t) { if (tp < 2) { if (tp == 0) { if (y + t >= b) print(x, b); else print(x, y + t); } else { if (y - t <= 0) print(x, 0); else print(x, y - t); } } else { if (tp == 2) { if (x - t <= 0) print(0, y); else print(x - t, y); } else { if (x + t >= b) print(b, y); else print(x + t, y); } } } int main() { n = read(), b = read(); for (int i = 0; i < n; ++i) { int x1 = read(), y1 = read(); int x2 = read(), y2 = read(); arrow.push_back(make_pair(make_pair(x1, y1), make_pair(x2, y2))); } q = read(); for (int i = 0; i < q; ++i) { int x = read(), y = read(); char c = getchar(); int dir = strchr(Dir, c) - Dir; long long t; scanf("%lld", &t); qry.push_back(make_pair(make_pair(x, y), make_pair(dir, t))); } for (int i = 0; i < n; ++i) { int x1, x2, y1, y2, tp; x1 = arrow[i].first.first, y1 = arrow[i].first.second; x2 = arrow[i].second.first, y2 = arrow[i].second.second; tp = (x1 == x2 ? y1 < y2 ? 0 : 1 : x1 < x2 ? 3 : 2); dir[i] = tp; a[i] = (Arrow){x1, y1, x2, y2, tp, i}; } for (int i = 0; i < q; ++i) { int x, y, dir; x = qry[i].first.first, y = qry[i].first.second; dir = qry[i].second.first; a[i + n] = (Arrow){x, y, x, y, dir, i + n}; fstGo[i] = make_pair(make_pair(inf, inf), linf); } sort(a, a + n + q, cmp1); segt.init(); for (int i = 0; i < n + q; ++i) { if (a[i].dir == 0) { pair<int, int> p = segt.qry(a[i].x2); if (p.first != inf) { pair<pair<int, int>, long long> np = make_pair(make_pair(a[i].x2, max(a[i].y2, p.first)), max(0, p.first - a[i].y2)); if (a[i].id < n) { go[make_pair(a[i].x2, a[i].y2)] = np; belong[make_pair(a[i].x2, a[i].y2)] = a[i].id; } else { fstGo[a[i].id - n] = np; } belong[make_pair(a[i].x2, max(a[i].y2, p.first))] = p.second; } } if (a[i].id < n) segt.upd(min(a[i].x1, a[i].x2), max(a[i].x1, a[i].x2) + 1, make_pair(a[i].getLowCol(), a[i].id)); } sort(a, a + n + q, cmp2); segt.init(); for (int i = 0; i < n + q; ++i) { if (a[i].dir == 1) { pair<int, int> p = segt.qry(a[i].x2); if (p.first != inf) { pair<pair<int, int>, long long> np = make_pair(make_pair(a[i].x2, min(a[i].y2, -p.first)), max(0, a[i].y2 + p.first)); if (a[i].id < n) { go[make_pair(a[i].x2, a[i].y2)] = np; belong[make_pair(a[i].x2, a[i].y2)] = a[i].id; } else { fstGo[a[i].id - n] = np; } belong[make_pair(a[i].x2, min(a[i].y2, -p.first))] = p.second; } } if (a[i].id < n) segt.upd(min(a[i].x1, a[i].x2), max(a[i].x1, a[i].x2) + 1, make_pair(-a[i].getHighCol(), a[i].id)); } sort(a, a + n + q, cmp3); segt.init(); for (int i = 0; i < n + q; ++i) { if (a[i].dir == 3) { pair<int, int> p = segt.qry(a[i].y2); if (p.first != inf) { pair<pair<int, int>, long long> np = make_pair(make_pair(max(a[i].x2, p.first), a[i].y2), max(0, p.first - a[i].x2)); if (a[i].id < n) { go[make_pair(a[i].x2, a[i].y2)] = np; belong[make_pair(a[i].x2, a[i].y2)] = a[i].id; } else { fstGo[a[i].id - n] = np; } belong[make_pair(max(a[i].x2, p.first), a[i].y2)] = p.second; } } if (a[i].id < n) segt.upd(min(a[i].y1, a[i].y2), max(a[i].y1, a[i].y2) + 1, make_pair(a[i].getLowRow(), a[i].id)); } sort(a, a + n + q, cmp4); segt.init(); for (int i = 0; i < n + q; ++i) { if (a[i].dir == 2) { pair<int, int> p = segt.qry(a[i].y2); if (p.first != inf) { pair<pair<int, int>, long long> np = make_pair(make_pair(min(a[i].x2, -p.first), a[i].y2), max(0, a[i].x2 + p.first)); if (a[i].id < n) { go[make_pair(a[i].x2, a[i].y2)] = np; belong[make_pair(a[i].x2, a[i].y2)] = a[i].id; } else { fstGo[a[i].id - n] = np; } belong[make_pair(min(a[i].x2, -p.first), a[i].y2)] = p.second; } } if (a[i].id < n) segt.upd(min(a[i].y1, a[i].y2), max(a[i].y1, a[i].y2) + 1, make_pair(-a[i].getHighRow(), a[i].id)); } for (int i = 0; i < n; ++i) belong[arrow[i].second] = i; for (auto p : belong) vec.push_back(p.first); for (int i = 0; i < vec.size(); ++i) has[belong[vec[i]]].push_back(i); for (int i = 0; i < 50; ++i) Go[i].resize(vec.size()); for (int i = 0; i < vec.size(); ++i) Go[0][i] = make_pair(i, 0); for (int i = 0; i < n; ++i) { int x1 = arrow[i].first.first, y1 = arrow[i].first.second; int x2 = arrow[i].second.first, y2 = arrow[i].second.second; int tp = (x1 == x2 ? y1 < y2 ? 0 : 1 : x1 < x2 ? 3 : 2); sort(has[i].begin(), has[i].end(), [&](int a, int b) { return (vec[a].first - vec[b].first + vec[a].second - vec[b].second) / (dx[tp] + dy[tp]) < 0; }); for (int j = 0; j + 1 < has[i].size(); ++j) { Go[0][has[i][j]] = make_pair(has[i][j + 1], abs(vec[has[i][j]].first - vec[has[i][j + 1]].first + vec[has[i][j]].second - vec[has[i][j + 1]].second)); } if (!go.count(vec[has[i].back()])) continue; pair<pair<int, int>, long long> p = go[vec[has[i].back()]]; Go[0][has[i].back()] = make_pair( lower_bound(vec.begin(), vec.end(), p.first) - vec.begin(), p.second); } for (int j = 1; j < 50; ++j) { for (int i = 0; i < vec.size(); ++i) { pair<int, long long> p = Go[j - 1][i]; pair<int, long long> np = Go[j - 1][p.first]; Go[j][i] = make_pair(np.first, np.second + p.second); if (Go[j][i].second > linf) Go[j][i].second = linf; } } for (int i = 0; i < q; ++i) { pair<pair<int, int>, long long> p = fstGo[i]; int x = qry[i].first.first, y = qry[i].first.second; int tp = qry[i].second.first; long long t = qry[i].second.second; if (p.first.first == inf || t < p.second) { mov(x, y, tp, t); } else { int nw = lower_bound(vec.begin(), vec.end(), p.first) - vec.begin(); t -= p.second; for (int j = 50 - 1; ~j; --j) { pair<int, long long> P = Go[j][nw]; if (P.second <= t) { t -= P.second; nw = P.first; } } mov(vec[nw].first, vec[nw].second, dir[belong[vec[nw]]], t); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int DX[] = {-1, 0, +1, 0}, DY[] = {0, +1, 0, -1}; const char DC[] = {'L', 'U', 'R', 'D'}; int size; int np; int px[100000]; int py[100000]; int ppx[100000]; int ppy[100000]; int pd[100000]; int nq; int qx[100000]; int qy[100000]; int qd[100000]; long long qt[100000]; int pnextnode[100000]; int pnextdist[100000]; int pnextpos[100000]; int qnextnode[100000]; int qnextdist[100000]; int qnextpos[100000]; int stepnode[50][100000]; long long steppos[50][100000]; void simulate(int &at, long long &pos, long long cnt, int maxstep) { pos += cnt; while (pnextnode[at] != -1 && pos >= pnextdist[at]) { while (maxstep >= 0 && (1LL << maxstep) > pos) --maxstep; int to; long long npos; char type; if (maxstep >= 0 && (1LL << maxstep) >= pnextdist[at]) { to = stepnode[maxstep][at], npos = pos - (1LL << maxstep) + steppos[maxstep][at]; type = 'A'; } else { to = pnextnode[at], npos = pos - pnextdist[at] + pnextpos[at]; type = 'B'; } at = to, pos = npos; } } int gnode[1001][1001]; long long gpos[1001][1001]; void calcnext(int x, int y, int d, int &nextnode, int &nextdist, int &nextpos, int skip) { nextdist = 0; while (x >= 0 && x <= size && y >= 0 && y <= size) { if (gnode[x][y] != -1 && gnode[x][y] != skip) { nextnode = gnode[x][y]; nextpos = gpos[x][y]; return; } x += DX[d], y += DY[d], ++nextdist; } nextnode = -1; } void run(int casenr) { cin >> np >> size; for (int i = (0); i < (np); ++i) cin >> ppx[i] >> ppy[i] >> px[i] >> py[i]; for (int i = (0); i < (np); ++i) { int dx = px[i] - ppx[i], dy = py[i] - ppy[i]; if (dx < 0) dx = -1; if (dx > 0) dx = +1; if (dy < 0) dy = -1; if (dy > 0) dy = +1; pd[i] = -1; for (int k = (0); k < (4); ++k) if (DX[k] == dx && DY[k] == dy) pd[i] = k; } cin >> nq; for (int i = (0); i < (nq); ++i) { char tmp, dc; cin >> qx[i] >> qy[i] >> dc >> qt[i]; qd[i] = -1; for (int k = (0); k < (4); ++k) if (DC[k] == dc) qd[i] = k; } memset(gnode, -1, sizeof(gnode)); memset(gpos, -1, sizeof(gpos)); for (int i = (0); i < (np); ++i) for (int x = ppx[i], y = ppy[i];; x += DX[pd[i]], y += DY[pd[i]]) { gnode[x][y] = i; gpos[x][y] = -abs(x - px[i]) - abs(y - py[i]); if (x == px[i] && y == py[i]) break; } for (int i = (0); i < (np); ++i) calcnext(px[i], py[i], pd[i], pnextnode[i], pnextdist[i], pnextpos[i], i); for (int i = (0); i < (nq); ++i) calcnext(qx[i], qy[i], qd[i], qnextnode[i], qnextdist[i], qnextpos[i], -1); for (int j = (0); j < (np); ++j) { stepnode[0][j] = j; steppos[0][j] = 0; simulate(stepnode[0][j], steppos[0][j], 1, -1); } for (int i = (1); i < (50); ++i) for (int j = (0); j < (np); ++j) { stepnode[i][j] = stepnode[i - 1][j]; steppos[i][j] = steppos[i - 1][j]; simulate(stepnode[i][j], steppos[i][j], 1LL << (i - 1), i - 1); } for (int qi = (0); qi < (nq); ++qi) { long long x, y; if (qnextnode[qi] != -1 && qt[qi] >= qnextdist[qi]) { int at = qnextnode[qi]; long long pos = qnextpos[qi]; simulate(at, pos, qt[qi] - qnextdist[qi], 50); x = px[at] + pos * DX[pd[at]], y = py[at] + pos * DY[pd[at]]; } else { x = qx[qi] + qt[qi] * DX[qd[qi]], y = qy[qi] + qt[qi] * DY[qd[qi]]; } if (x < 0) x = 0; if (x > size) x = size; if (y < 0) y = 0; if (y > size) y = size; cout << x << " " << y << endl; } } int main() { run(1); return 0; }
#include <bits/stdc++.h> using namespace std; struct res { int x, y, z; } xun[200010], arr[200010]; int i, j, k, n, m, K, t, TY, num; int fa[200010][20], id[200010], tr[200010 * 4], f[200010], Id[200010], Se[200010], dep[200010]; long long ti[200010], len[200010][20], sum[200010]; vector<pair<int, long long> > c[200010], hu[200010]; set<pair<int, int> > Set[200010]; struct wen { int x, y, ty, di, id; inline void read() { char p[3]; scanf("%d%d%s", &x, &y, p); if (p[0] == 'U' || p[0] == 'D') ty = 1; else ty = 2; if (p[0] == 'U' || p[0] == 'R') di = 1; else di = -1; id = i; } } b[200010], q[200010], w[200010]; struct ww { int x, l, r, di, ty; inline void read() { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { x = x1, ty = 1; l = y1, r = y2; } else { x = y1, ty = 2; l = x1, r = x2; } if (l > r) swap(l, r), di = -1; else di = 1; if (ty == 1) y2 += di; else x2 += di; b[++t] = (wen){x2, y2, ty, di, -i}; w[i] = (wen){x1, y1, ty, di}; } } a[200010]; inline bool cc(const wen &A, const wen &B) { if (TY == 1) return A.y > B.y; if (TY == 2) return A.y < B.y; } inline bool cc1(const int &A, const int &B) { if (TY == 1) return a[A].x > a[B].x; if (TY == 2) return a[A].x < a[B].x; } void cover(int q, int x, int y, int l, int r, int k) { if (l <= x && y <= r) { tr[q] = k; return; } int mid = (x + y) / 2; if (l <= mid) cover(q * 2, x, mid, l, r, k); if (mid < r) cover(q * 2 + 1, mid + 1, y, l, r, k); } int cal(int q, int x, int y, int k) { if (x == y) return tr[q]; int mid = (x + y) / 2, an = tr[q], re; if (k <= mid) re = cal(q * 2, x, mid, k); else re = cal(q * 2 + 1, mid + 1, y, k); return max(an, re); } inline void get(wen O, int x, int Ty) { int s1, s2; s1 = s2 = 0; if (!Ty) { if (!x) s1 = m - O.y; else if (a[x].ty == 1) s1 = max(O.y, a[x].l) - O.y, s2 = a[x].di > 0 ? max(O.y - a[x].l, 0) : a[x].r - max(O.y, a[x].l); else s1 = a[x].x - O.y, s2 = abs(O.x - (a[x].di > 0 ? a[x].l : a[x].r)); } else { if (!x) s1 = O.y; else if (a[x].ty == 1) s1 = O.y - min(O.y, a[x].r), s2 = a[x].di > 0 ? min(a[x].r, O.y) - a[x].l : max(a[x].r - O.y, 0); else s1 = O.y - a[x].x, s2 = abs(O.x - (a[x].di > 0 ? a[x].l : a[x].r)); } int A = O.id; if (A > 0) xun[A] = (res){x, s1, s2}; else arr[-A] = (res){x, s1 + a[-A].r - a[-A].l + 1, s2}; } inline void work() { for (i = 0; i <= m; i++) Set[i].clear(); int s = 0; for (i = 1; i <= n; i++) if (a[i].ty == 1) { Set[a[i].x].insert(make_pair(a[i].l, i)); Set[a[i].x].insert(make_pair(a[i].r, i)); } else id[++s] = i; { memset(tr, 0, sizeof(tr)); TY = 1; sort(b + 1, b + t + 1, cc); sort(id + 1, id + s + 1, cc1); j = 1; for (i = 1; i <= t; i++) if (b[i].ty == 1 && b[i].di == 1) { wen O = b[i]; for (; j <= s; j++) { int A = id[j]; if (a[A].x < O.y) break; cover(1, 0, m, a[A].l, a[A].r, j); } int ans = 0; { set<pair<int, int> >::iterator A = Set[O.x].lower_bound(make_pair(O.y, 0)); if (A != Set[O.x].end()) ans = A->second; } { int A = id[cal(1, 0, m, O.x)]; if (!ans || A && a[A].x < a[ans].l) ans = A; } get(O, ans, 0); } } { memset(tr, 0, sizeof(tr)); TY = 2; sort(b + 1, b + t + 1, cc); sort(id + 1, id + s + 1, cc1); j = 1; for (i = 1; i <= t; i++) if (b[i].ty == 1 && b[i].di == -1) { wen O = b[i]; for (; j <= s; j++) { int A = id[j]; if (a[A].x > O.y) break; cover(1, 0, m, a[A].l, a[A].r, j); } int ans = 0; { set<pair<int, int> >::iterator A = Set[O.x].lower_bound(make_pair(O.y, 200010)); if (A != Set[O.x].begin()) ans = (--A)->second; } { int A = id[cal(1, 0, m, O.x)]; if (!ans || A && a[A].x > a[ans].r) ans = A; } get(O, ans, 1); } } } void dfs(int x) { res A = arr[x]; int B = A.x; if (!B) { hu[num].push_back(make_pair(x, 0)); return; } f[x] = 1; if (f[B] == 1) { int s = 0; for (k = x;;) { s++; Id[k] = num; Se[k] = s; sum[num] += len[k][0]; hu[num].push_back(make_pair(k, sum[num])); k = arr[k].x; if (k == x) break; } } else dfs(B); } void Dfs(int x) { int i, j; f[x] = 1; for (i = 0; i < c[x].size(); i++) { int A = c[x][i].first; if (Id[x] && Id[x] == Id[A]) continue; dep[A] = dep[x] + 1; fa[A][0] = x; for (j = 1; j <= 19; j++) { fa[A][j] = fa[fa[A][j - 1]][j - 1]; len[A][j] = len[A][j - 1] + len[fa[A][j - 1]][j - 1]; } Dfs(A); } } inline void Work() { for (i = 1; i <= n; i++) { res A = arr[i]; if (A.x) { int L = arr[A.x].y - arr[i].z; c[A.x].push_back(make_pair(i, L)); len[i][0] = L; } } for (i = 1; i <= n; i++) if (!f[i]) { num++; dfs(i); for (j = 0; j < hu[num].size(); j++) { int A = hu[num][j].first; Dfs(A); } } } inline void print(wen A, long long ti) { long long x = A.x, y = A.y; if (A.ty == 1) y += ti * A.di; else x += ti * A.di; if (x < 0) x = 0; if (y < 0) y = 0; if (x > m) x = m; if (y > m) y = m; printf("%d %d\n", (int)x, (int)y); } inline void suan() { for (i = 1; i <= K; i++) { res A = xun[i]; long long tim = ti[i]; if (!A.x || tim <= A.y) print(q[i], tim); else { tim -= A.y - A.z; int B = A.x; if (tim <= arr[B].y) { print(w[B], tim); continue; } tim -= arr[B].y; for (j = 19; j >= 0; j--) if (fa[B][j] && tim >= len[B][j]) { tim -= len[B][j]; B = fa[B][j]; } if (Id[B]) { int C = Id[B]; tim %= sum[C]; int D = Se[B] - 1, nn = hu[C].size(); long long st = !D ? 0 : hu[C][D - 1].second; if (tim >= hu[C][nn - 1].second - st) { tim -= hu[C][nn - 1].second - st; D = 0; } st = !D ? 0 : hu[C][D - 1].second; int l = D, r = nn - 1, mid; for (; l <= r;) { mid = (l + r) / 2; if (hu[C][mid].second - st <= tim) l = mid + 1; else r = mid - 1; } if (l) tim -= hu[C][l - 1].second - st; B = hu[C][l].first; } if (arr[B].x) tim += arr[B].z, B = arr[B].x; else tim += arr[B].y; print(w[B], tim); } } } int main() { scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) a[i].read(); scanf("%d", &K); for (i = 1; i <= K; i++) b[++t].read(), scanf("%I64d", &ti[i]), q[i] = b[t]; for (i = 1; i <= t; i++) b[i].ty = 3 - b[i].ty, swap(b[i].x, b[i].y); for (i = 1; i <= n; i++) a[i].ty = 3 - a[i].ty; work(); for (i = 1; i <= t; i++) b[i].ty = 3 - b[i].ty, swap(b[i].x, b[i].y); for (i = 1; i <= n; i++) a[i].ty = 3 - a[i].ty; work(); Work(); suan(); return 0; }
#include <bits/stdc++.h> using std::swap; const int N = 222222; const long long inf = (long long)1e16; int n, q, b; int fi[N], sl[N], id[N], sr[N]; int xa[N], ya[N], xb[N], yb[N]; int ex[N], ey[N], ed[N], help[N]; int go[N], arrd[N], who[55][N]; long long v[N], f[55][N], next[N], dist[N]; bool good[N], swapx[N], swapy[N], col[4 * N]; int tree[4 * N]; int dx[] = {0, 1, 0, -1}; int dy[] = {1, 0, -1, 0}; const char dir[4] = {'U', 'R', 'D', 'L'}; int tot; void mysort(int l, int r) { int i = l, j = r; int x = fi[(l + r) >> 1]; int y = sl[(l + r) >> 1]; do { while (fi[i] < x || fi[i] == x && sl[i] > y) i++; while (fi[j] > x || fi[j] == x && y > sl[j]) j--; if (i <= j) { swap(fi[i], fi[j]); swap(sl[i], sl[j]); swap(sr[i], sr[j]); swap(id[i], id[j]); i++; j--; } } while (i <= j); if (l < j) mysort(l, j); if (i < r) mysort(i, r); } void build(int l, int r, int x) { col[x] = false; tree[x] = 0; if (l < r) { int m = l + r >> 1; build(l, m, x << 1); build(m + 1, r, x << 1 | 1); } } void update(int L, int R, int v, int l, int r, int x) { if (L <= l && r <= R) { col[x] = true; tree[x] = v; return; } if (col[x]) { tree[x + x] = tree[x + x + 1] = tree[x]; col[x + x] = col[x + x + 1] = true; col[x] = false; } int m = l + r >> 1; if (L <= m) update(L, R, v, l, m, x << 1); if (R > m) update(L, R, v, m + 1, r, x << 1 | 1); } int query(int p, int l, int r, int x) { if (l == r || col[x]) return tree[x]; int m = l + r >> 1; if (p <= m) return query(p, l, m, x << 1); else return query(p, m + 1, r, x << 1 | 1); } bool valid(int x, int y) { return x >= 0 && x <= b && y >= 0 && y <= b; } void _X_X_long_long_gao() { for (int i = 1; i <= n + q; i++) { go[i] = 0; swapx[i] = swapy[i] = false; if (!valid(ex[i], ey[i])) good[i] = false; else good[i] = true; if (xa[i] > xb[i]) { swap(xa[i], xb[i]); swapx[i] = true; } if (ya[i] > yb[i]) { swap(ya[i], yb[i]); swapy[i] = true; } } tot = 0; for (int i = 1; i <= n; i++) { fi[i - 1] = -yb[i]; sl[i - 1] = xa[i]; sr[i - 1] = xb[i]; id[i - 1] = i; help[i] = -ya[i]; } tot = n; for (int i = 1; i <= n + q; i++) { if (ed[i] == 0 && good[i]) { fi[tot] = -ey[i]; sl[tot] = ~ex[i]; id[tot] = i; tot++; } } mysort(0, tot - 1); build(0, b, 1); for (int i = 0; i < tot; i++) { if (sl[i] >= 0) { update(sl[i], sr[i], id[i], 0, b, 1); } else { int num = query(~sl[i], 0, b, 1); go[id[i]] = num; next[id[i]] = fi[i] - help[num]; } } tot = 0; for (int i = 1; i <= n; i++) { fi[i - 1] = ya[i]; sl[i - 1] = xa[i]; sr[i - 1] = xb[i]; id[i - 1] = i; help[i] = yb[i]; } tot = n; for (int i = 1; i <= n + q; i++) { if (ed[i] == 2 && good[i]) { fi[tot] = ey[i]; sl[tot] = ~ex[i]; id[tot] = i; tot++; } } mysort(0, tot - 1); build(0, b, 1); for (int i = 0; i < tot; i++) { if (sl[i] >= 0) { update(sl[i], sr[i], id[i], 0, b, 1); } else { int num = query(~sl[i], 0, b, 1); go[id[i]] = num; next[id[i]] = fi[i] - help[num]; } } tot = 0; for (int i = 1; i <= n; i++) { fi[i - 1] = -xb[i]; sl[i - 1] = ya[i]; sr[i - 1] = yb[i]; id[i - 1] = i; help[i] = -xa[i]; } tot = n; for (int i = 1; i <= n + q; i++) { if (ed[i] == 1 && good[i]) { fi[tot] = -ex[i]; sl[tot] = ~ey[i]; id[tot] = i; tot++; } } mysort(0, tot - 1); build(0, b, 1); for (int i = 0; i < tot; i++) { if (sl[i] >= 0) { update(sl[i], sr[i], id[i], 0, b, 1); } else { int num = query(~sl[i], 0, b, 1); go[id[i]] = num; next[id[i]] = fi[i] - help[num]; } } tot = 0; for (int i = 1; i <= n; i++) { fi[i - 1] = xa[i]; sl[i - 1] = ya[i]; sr[i - 1] = yb[i]; id[i - 1] = i; help[i] = xb[i]; } tot = n; for (int i = 1; i <= n + q; i++) { if (ed[i] == 3 && good[i]) { fi[tot] = ex[i]; sl[tot] = ~ey[i]; id[tot] = i; tot++; } } mysort(0, tot - 1); build(0, b, 1); for (int i = 0; i < tot; i++) { if (sl[i] >= 0) { update(sl[i], sr[i], id[i], 0, b, 1); } else { int num = query(~sl[i], 0, b, 1); go[id[i]] = num; next[id[i]] = fi[i] - help[num]; } } for (int i = 1; i <= n + q; i++) { if (go[i] == 0) next[i] = inf; if (next[i] < 0) next[i] = 0; } for (int i = 1; i <= n; i++) { if (swapx[i]) swap(xa[i], xb[i]); if (swapy[i]) swap(ya[i], yb[i]); } } int main() { scanf("%d%d", &n, &b); for (int i = 1; i <= n; i++) { scanf("%d%d%d%d", xa + i, ya + i, xb + i, yb + i); if (xa[i] < xb[i]) ed[i] = 1; if (xa[i] > xb[i]) ed[i] = 3; if (ya[i] > yb[i]) ed[i] = 2; if (ya[i] < yb[i]) ed[i] = 0; ex[i] = xb[i] + dx[ed[i]]; ey[i] = yb[i] + dy[ed[i]]; } scanf("%d", &q); char ch; for (int i = n + 1; i <= n + q; i++) { scanf("%d %d %c %I64d", ex + i, ey + i, &ch, v + i); for (int j = 0; j < 4; j++) if (dir[j] == ch) ed[i] = j; } _X_X_long_long_gao(); for (int i = 1; i <= n + q; i++) { if (go[i] == 0) dist[i] = inf; else dist[i] = abs(ex[i] - xb[go[i]]) + abs(ey[i] - yb[go[i]]); } go[0] = 0; dist[0] = inf; next[0] = inf; for (int i = 0; i <= n; i++) { who[0][i] = go[i]; f[0][i] = dist[i] + 1; } for (int j = 1; j <= 50; j++) { for (int i = 0; i <= n; i++) { who[j][i] = who[j - 1][who[j - 1][i]]; f[j][i] = f[j - 1][i] + f[j - 1][who[j - 1][i]]; if (f[j][i] > inf) f[j][i] = inf; } } long long ansx, ansy; for (int i = n + 1; i <= n + q; i++) { if (v[i] <= dist[i]) { ansx = ex[i]; ansy = ey[i]; if (v[i] <= next[i]) { ansx += v[i] * dx[ed[i]]; ansy += v[i] * dy[ed[i]]; if (ansx < 0) ansx = 0; if (ansx > b) ansx = b; if (ansy < 0) ansy = 0; if (ansy > b) ansy = b; printf("%I64d %I64d\n", ansx, ansy); continue; } v[i] -= next[i]; ansx += next[i] * dx[ed[i]]; ansy += next[i] * dy[ed[i]]; ansx += dx[ed[go[i]]] * v[i]; ansy += dy[ed[go[i]]] * v[i]; printf("%I64d %I64d\n", ansx, ansy); continue; } v[i] -= dist[i]; int now = go[i]; for (int j = 50; j >= 0; j--) { if (f[j][now] <= v[i]) { v[i] -= f[j][now]; now = who[j][now]; } } if (go[now] == 0 || v[i] <= next[now] + 1) { ansx = xb[now] + v[i] * dx[ed[now]]; ansy = yb[now] + v[i] * dy[ed[now]]; if (ansx < 0) ansx = 0; if (ansx > b) ansx = b; if (ansy < 0) ansy = 0; if (ansy > b) ansy = b; printf("%I64d %I64d\n", ansx, ansy); continue; } v[i] -= next[now] + 1; ansx = xb[now] + dx[ed[now]] * (next[now] + 1); ansy = yb[now] + dy[ed[now]] * (next[now] + 1); ansx += v[i] * dx[ed[go[now]]]; ansy += v[i] * dy[ed[go[now]]]; printf("%I64d %I64d\n", ansx, ansy); } return 0; }
#include <bits/stdc++.h> const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const char dz[4] = {'R', 'U', 'L', 'D'}; int n, m, bs, i, j, xa, ya, xb, yb, xx, yy, dd, d[1010], a[1010][1010], g[1010], c[1010], x[1010], y[1010], u[1010], f[1010], it; long long cnt, cur; char s[5]; int main() { scanf("%d%d", &n, &bs); for (i = 1; i <= n; i++) { scanf("%d%d%d%d", &xa, &ya, &xb, &yb); x[i] = xb; y[i] = yb; if (xa == xb) { if (ya > yb) { d[i] = 3; for (j = yb; j <= ya; j++) a[xa][j] = i; } else { d[i] = 1; for (j = ya; j <= yb; j++) a[xa][j] = i; } } else { if (xa > xb) { d[i] = 2; for (j = xb; j <= xa; j++) a[j][ya] = i; } else { d[i] = 0; for (j = xa; j <= xb; j++) a[j][ya] = i; } } } for (i = 1; i <= n; i++) { for (xx = x[i], yy = y[i], dd = d[i]; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; xx += dx[dd], yy += dy[dd], c[i]++) { j = a[xx][yy]; if (j > 0) { dd = d[j]; if (c[i] > 0 && x[j] == xx && y[j] == yy) break; } } if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) g[i] = j; } scanf("%d", &m); while (m--) { scanf("%d%d", &xx, &yy); scanf("%s", s); for (dd = 0; dd < 4; dd++) if (dz[dd] == s[0]) break; scanf("%I64d", &cnt); for (; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; xx += dx[dd], yy += dy[dd], cnt--) { if (cnt == 0) break; j = a[xx][yy]; if (j > 0) { dd = d[j]; if (x[j] == xx && y[j] == yy) break; } } if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) { if (cnt == 0) { printf("%d %d\n", xx, yy); continue; } } else { printf("%d %d\n", xx - dx[dd], yy - dy[dd]); continue; } for (cur = 0, ++it;; j = g[j]) { if (u[j] == it) { cnt -= f[j]; cnt %= cur - f[j]; cur = 0; ++it; } xx = x[j]; yy = y[j]; dd = d[j]; if (g[j] == 0 || cur + c[j] > cnt) { cnt -= cur; break; } u[j] = it; f[j] = cur; cur += c[j]; } for (; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; xx += dx[dd], yy += dy[dd], cnt--) { if (cnt == 0) break; j = a[xx][yy]; if (j > 0) dd = d[j]; } if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) printf("%d %d\n", xx, yy); else printf("%d %d\n", xx - dx[dd], yy - dy[dd]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; int dx[5] = {0, 1, 0, -1, 0}; int dy[5] = {0, 0, 1, 0, -1}; int n, b, q; int a[N + 5][N + 5]; int nx[N * N * 5 + 55][2]; int ans[111111]; long long t[111111]; int hash1(int x, int y, int d) { if (a[x][y]) d = a[x][y]; return d * N * N + x * N + y; } int main() { int i, j, k, x1, y1, x2, y2, d, h; char ch[5]; scanf("%d%d", &n, &b); for (i = 1; i <= n; i = i + 1) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { if (y1 > y2) d = 4; else d = 2; } else { if (x1 > x2) d = 3; else d = 1; } while (x1 != x2 || y1 != y2) { a[x1][y1] = d; x1 += dx[d]; y1 += dy[d]; } a[x2][y2] = d; } for (i = 0; i <= b; i = i + 1) for (j = 0; j <= b; j = j + 1) for (k = 1; k <= 4; k = k + 1) { if (a[i][j] && a[i][j] != k) continue; h = hash1(i, j, k); x1 = i + dx[k]; y1 = j + dy[k]; if (x1 < 0 || x1 > b || y1 < 0 || y1 > b) nx[h][0] = h; else nx[h][0] = hash1(x1, y1, k); } scanf("%d", &q); for (i = 1; i <= q; i = i + 1) { scanf("%d%d%s%lld", &x1, &x2, &ch, t + i); if (ch[0] == 'R') d = 1; if (ch[0] == 'U') d = 2; if (ch[0] == 'L') d = 3; if (ch[0] == 'D') d = 4; ans[i] = hash1(x1, x2, d); } for (i = 0; i <= 60; i = i + 1) { h = i & 1; for (j = 1; j <= q; j = j + 1) if (t[j] & ((long long)1 << i)) ans[j] = nx[ans[j]][h]; for (j = 1; j < N * N * 5; j = j + 1) nx[j][h ^ 1] = nx[nx[j][h]][h]; } for (i = 1; i <= q; i = i + 1) { y1 = ans[i] % N; ans[i] /= N; x1 = ans[i] % N; cout << x1 << ' ' << y1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> bool get_int(T &x) { char t = getchar(); bool neg = false; x = 0; for (; (t > '9' || t < '0') && t != '-' && t != EOF; t = getchar()) ; if (t == '-') neg = true, t = getchar(); if (t == EOF) return false; for (; t <= '9' && t >= '0'; t = getchar()) x = x * 10 + t - '0'; if (neg) x = -x; return true; } template <typename T> void print_int(T x) { if (x < 0) putchar('-'), x = -x; short a[20] = {}, sz = 0; while (x > 0) a[sz++] = x % 10, x /= 10; if (sz == 0) putchar('0'); for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]); } const int inf = 0x3f3f3f3f; const long long Linf = 1ll << 61; const double pi = acos(-1.0); struct arrow { int type; int x, y, yy, i; void in(int i) { this->i = i; int x, y, xx, yy; (get_int(x) && get_int(y)); (get_int(xx) && get_int(yy)); if (x == xx) { this->x = x; this->y = y; this->yy = yy; type = 0; } else { this->x = y; this->y = x; this->yy = xx; type = 1; } } }; arrow a[100111]; int n, q, qnxt[100111]; int b, x[100111], y[100111], dir[100111]; long long t[100111]; int nxt[50][100111]; long long len[50][100111]; double tlen[50][100111]; void work(int tp) { vector<pair<int, pair<int, int> > > v; set<pair<int, int> > st; for (int i = 1; i <= n; i++) { if (a[i].type == tp) v.push_back(make_pair(a[i].x, make_pair(1, i))); else if (a[i].y <= a[i].yy) { v.push_back(make_pair(a[i].y, make_pair(0, i))); v.push_back(make_pair(a[i].yy, make_pair(3, i))); } else { v.push_back(make_pair(a[i].yy, make_pair(0, i))); v.push_back(make_pair(a[i].y, make_pair(3, i))); } } for (int i = 1; i <= q; i++) { if (tp == 0 && dir[i] <= 1) v.push_back(make_pair(x[i], make_pair(2, i))); else if (tp == 1 && dir[i] >= 2) v.push_back(make_pair(y[i], make_pair(2, i))); } sort(v.begin(), v.end()); for (int id = 0, nt = 0; id < (int)v.size(); id = nt) { for (; nt < (int)v.size() && v[id].first == v[nt].first; nt++) { if (v[nt].second.first == 1) { int i = v[nt].second.second; st.insert(make_pair(a[i].y, i)); st.insert(make_pair(a[i].yy, i)); } } for (int j = id; j < nt; j++) { pair<int, pair<int, int> > &now = v[j]; int i = now.second.second; if (now.second.first == 0) st.insert(make_pair(a[i].x, i)); else if (now.second.first == 3) st.erase(make_pair(a[i].x, i)); else if (now.second.first == 1) { st.erase(make_pair(a[i].y, i)); st.erase(make_pair(a[i].yy, i)); auto p = st.lower_bound(make_pair(a[i].y, 0)); if (a[i].y <= a[i].yy && p != st.end()) nxt[0][i] = p->second; else if (a[i].y > a[i].yy && p != st.begin()) { --p; nxt[0][i] = p->second; } st.insert(make_pair(a[i].y, i)); st.insert(make_pair(a[i].yy, i)); } else { int pos; if (tp == 0) pos = y[i]; else pos = x[i]; if (dir[i] % 2 == 0) { auto p = st.lower_bound(make_pair(pos, 0)); if (p != st.end()) qnxt[i] = p->second; } else if (dir[i] % 2) { auto p = st.lower_bound(make_pair(pos, inf)); if (p != st.begin()) { --p; qnxt[i] = p->second; } } } } for (int j = id; j < nt; j++) { if (v[j].second.first == 1) { int i = v[j].second.second; st.erase(make_pair(a[i].y, i)); st.erase(make_pair(a[i].yy, i)); } } } } const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; void find_pos(int x, int y, int d, int i, long long len) { int ansx, ansy; if (i == 0) { ansx = max(min(b + 0ll, x + len * dx[d]), 0ll); ansy = max(min(b + 0ll, y + len * dy[d]), 0ll); } else if (a[i].type == (d >> 1)) { if (a[i].type == 0) { ansx = a[i].x; if (a[i].yy > y) ansy = y + len; else ansy = y - len; } else { if (a[i].yy > x) ansx = x + len; else ansx = x - len; ansy = a[i].x; } } else { int l1; if (a[i].type == 0) l1 = abs(x - a[i].x); else l1 = abs(y - a[i].x); if (len <= l1) { if (a[i].type == 0) { if (x < a[i].x) ansx = x + len; else ansx = x - len; ansy = y; } else { ansx = x; if (y < a[i].x) ansy = y + len; else ansy = y - len; } } else { len -= l1; if (a[i].type == 0) { ansx = a[i].x; if (y < a[i].yy) ansy = y + len; else ansy = y - len; } else { if (x < a[i].yy) ansx = x + len; else ansx = x - len; ansy = a[i].x; } } } printf("%d %d\n", ansx, ansy); } int main() { (get_int(n) && get_int(b)); for (int i = 1; i <= n; i++) a[i].in(i); get_int(q); char op[3]; for (int i = 1; i <= q; i++) { (get_int(x[i]) && get_int(y[i])); scanf("%s", op); get_int(t[i]); if (op[0] == 'U') dir[i] = 0; else if (op[0] == 'D') dir[i] = 1; else if (op[0] == 'R') dir[i] = 2; else dir[i] = 3; } work(0); work(1); for (int i = 1; i <= n; i++) { int &j = nxt[0][i]; if (!j) continue; if (a[i].type == a[j].type) len[0][i] = abs(a[i].yy - a[j].yy); else len[0][i] = abs(a[i].yy - a[j].x) + abs(a[j].yy - a[i].x); tlen[0][i] = len[0][i]; } for (int i = 1; i < 50; i++) for (int j = 1; j <= n; j++) { nxt[i][j] = nxt[i - 1][nxt[i - 1][j]]; len[i][j] = len[i - 1][j] + len[i - 1][nxt[i - 1][j]]; tlen[i][j] = tlen[i - 1][j] + tlen[i - 1][nxt[i - 1][j]]; } for (int i = 1; i <= q; i++) { int l1, j = qnxt[i]; if (a[j].type == 0) l1 = abs(a[j].x - x[i]) + abs(a[j].yy - y[i]); else l1 = abs(a[j].x - y[i]) + abs(a[j].yy - x[i]); if (!j || t[i] <= l1) find_pos(x[i], y[i], dir[i], j, t[i]); else { t[i] -= l1; for (int d = 49; d >= 0; d--) if (nxt[d][j] && len[d][j] <= t[i] && tlen[d][j] <= 1e17) { t[i] -= len[d][j]; j = nxt[d][j]; } int tdir = a[j].type * 2 + (a[j].yy < a[j].y); if (a[j].type == 0) find_pos(a[j].x, a[j].yy, tdir, nxt[0][j], t[i]); else find_pos(a[j].yy, a[j].x, tdir, nxt[0][j], t[i]); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; const int Q = ((int)1e5) + 5; const int INF = 0x7F7F7F7F; const long long LINF = 0x7F7F7F7F7F7F7F7Fll; const double eps = 1e-6; const int mod = 1e9 + 7; const double pi = acos(-1); int B, q, b, n; long long t[Q]; int ans[Q]; int dx[4] = {0, 0, -1, 1}; int dy[4] = {1, -1, 0, 0}; struct Node { int x, y, k; Node(int _x = 0, int _y = 0, int _k = 0) : x(_x), y(_y), k(_k) { if (k == 'U') k = 0; if (k == 'D') k = 1; if (k == 'L') k = 2; if (k == 'R') k = 3; } Node nxt() { int _x = x + dx[k]; _x = max(0, _x); _x = min(_x, b); int _y = y + dy[k]; _y = max(0, _y); _y = min(_y, b); return Node(_x, _y, k); } int id() { return (x * B + y) * 4 + k; } } nxt[N * N * 4]; int to[2][N * N * 4]; int main() { scanf("%d%d", &n, &b); B = b + 1; for (int i = 0; i < B; ++i) for (int j = 0; j < B; ++j) for (int k = 0; k < 4; ++k) { Node a = Node(i, j, k); nxt[a.id()] = a.nxt(); } for (int i = 0; i < n; ++i) { int x1, x2, y1, y2, d; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) if (y2 > y1) d = 0; else d = 1; else if (x2 < x1) d = 2; else d = 3; int len = abs(y2 - y1) + abs(x2 - x1); for (int j = 0; j < len + 1; ++j) { int x, y; x = x1 + dx[d] * j; y = y1 + dy[d] * j; for (int k = 0; k < 4; ++k) nxt[Node(x, y, k).id()] = nxt[Node(x, y, d).id()]; } } scanf("%d", &q); for (int i = 0; i < q; ++i) { int x, y; char ch; long long t0; scanf("%d %d %c %lld", &x, &y, &ch, t + i); ans[i] = Node(x, y, ch).id(); } int ALL = 4 * B * B; for (int i = 0; i < ALL; ++i) to[0][i] = nxt[i].id(); for (int i = 0; i < 50; ++i) { for (int j = 0; j < q; ++j) if (t[j] & (1ll << i)) ans[j] = to[0][ans[j]]; for (int j = 0; j < ALL; ++j) to[1][j] = to[0][to[0][j]]; for (int j = 0; j < ALL; ++j) to[0][j] = to[1][j]; } for (int i = 0; i < q; ++i) { int x, y; x = ans[i] / 4 / B; y = ans[i] / 4 % B; printf("%d %d\n", x, y); } }
#include <bits/stdc++.h> using namespace std; inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; return (s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin), (s == t ? -1 : *s++) : *s++); } template <class T> inline void read(T &x) { static bool iosig; static char c; for (iosig = false, c = read(); !isdigit(c); c = read()) { if (c == '-') iosig = true; if (c == -1) return; } for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^ '0'); if (iosig) x = -x; } const int OUT_LEN = 10000000; char obuf[OUT_LEN], *ooh = obuf; inline void print(char c) { if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf; *ooh++ = c; } template <class T> inline void print(T x) { static int buf[30], cnt; if (x == 0) print('0'); else { if (x < 0) print('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) print((char)buf[cnt--]); } } inline void print(pair<int, int> x) { print(x.first), print(' '), print(x.second), print('\n'); } inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); } const int N = 100005, M = 52; const long long inf = 1e16; int n, m, q, x[N], y[N], xx[N], yy[N], X[N], Y[N], fa[N], fa1[N], f1[N], s[N << 2], f[N][M]; long long T[N], ga[N], g[N][M]; char opt[N], op[N]; vector<int> a[N], b[N], c[N]; void build(int l, int r, int t) { s[t] = 0; if (l == r) return; int mid = (l + r) >> 1, k = t << 1; build(l, mid, k), build(mid + 1, r, k | 1); } void change(int l, int r, int t, int L, int R, int x) { if (L <= l && r <= R) return (void)(s[t] = x); int mid = (l + r) >> 1, k = t << 1; if (s[t]) s[k] = s[k | 1] = s[t], s[t] = 0; if (L <= mid) change(l, mid, k, L, R, x); if (R > mid) change(mid + 1, r, k | 1, L, R, x); } int query(int l, int r, int t, int x) { if (l == r || s[t]) return s[t]; int mid = (l + r) >> 1, k = t << 1; return (x <= mid ? query(l, mid, k, x) : query(mid + 1, r, k | 1, x)); } inline pair<int, int> solve(pair<int, int> x, char o, long long t) { int a = x.first, b = x.second; if (o == 'L') a = max(0ll, a - t); if (o == 'R') a = min((long long)m, a + t); if (o == 'D') b = max(0ll, b - t); if (o == 'U') b = min((long long)m, b + t); return make_pair(a, b); } signed main() { read(n), read(m); for (int i = 1; i <= n; ++i) { read(x[i]), read(y[i]), read(xx[i]), read(yy[i]); if (x[i] == xx[i]) a[x[i]].push_back(i), op[i] = (y[i] > yy[i] ? 'D' : 'U'); else b[x[i]].push_back(i), b[xx[i]].push_back(i), op[i] = (x[i] > xx[i] ? 'L' : 'R'); } read(q); for (int i = 1; i <= q; ++i) { read(X[i]), read(Y[i]); while (isspace(opt[i] = read())) ; read(T[i]); } for (int i = 1; i <= q; ++i) if (opt[i] == 'R') c[X[i]].push_back(i); build(0, m, 1); for (int i = m; ~i; --i) { for (int j : a[i]) change(0, m, 1, min(y[j], yy[j]), max(y[j], yy[j]), j); for (int j : b[i]) if (x[j] > xx[j]) change(0, m, 1, y[j], y[j], j); else f[j][0] = query(0, m, 1, y[j]), g[j][0] = (f1[j] = abs(xx[f[j][0]] - xx[j])) + abs(yy[f[j][0]] - y[j]); for (int j : c[i]) { fa[j] = query(0, m, 1, Y[j]), ga[j] = abs(xx[fa[j]] - X[j]) + abs(yy[fa[j]] - Y[j]); fa1[j] = (x[fa[j]] == xx[fa[j]] ? abs(xx[fa[j]] - X[j]) : max(xx[fa[j]] - X[j], 0)); } } for (int i = 0; i <= m; ++i) c[i].clear(); for (int i = 1; i <= q; ++i) if (opt[i] == 'L') c[X[i]].push_back(i); build(0, m, 1); for (int i = 0; i <= m; ++i) { for (int j : a[i]) change(0, m, 1, min(y[j], yy[j]), max(y[j], yy[j]), j); for (int j : b[i]) if (x[j] < xx[j]) change(0, m, 1, y[j], y[j], j); else f[j][0] = query(0, m, 1, y[j]), g[j][0] = (f1[j] = abs(xx[j] - xx[f[j][0]])) + abs(yy[f[j][0]] - y[j]); for (int j : c[i]) { fa[j] = query(0, m, 1, Y[j]), ga[j] = abs(X[j] - xx[fa[j]]) + abs(yy[fa[j]] - Y[j]); fa1[j] = (x[fa[j]] == xx[fa[j]] ? abs(xx[fa[j]] - X[j]) : max(X[j] - xx[fa[j]], 0)); } } for (int i = 0; i <= m; ++i) c[i].clear(), a[i].clear(), b[i].clear(); for (int i = 1; i <= n; ++i) if (y[i] == yy[i]) a[y[i]].push_back(i); else b[y[i]].push_back(i), b[yy[i]].push_back(i); for (int i = 1; i <= q; ++i) if (opt[i] == 'U') c[Y[i]].push_back(i); build(0, m, 1); for (int i = m; ~i; --i) { for (int j : a[i]) change(0, m, 1, min(x[j], xx[j]), max(x[j], xx[j]), j); for (int j : b[i]) if (y[j] > yy[j]) change(0, m, 1, x[j], x[j], j); else f[j][0] = query(0, m, 1, x[j]), g[j][0] = (f1[j] = abs(yy[f[j][0]] - yy[j])) + abs(xx[f[j][0]] - x[j]); for (int j : c[i]) { fa[j] = query(0, m, 1, X[j]), ga[j] = (fa1[j] = abs(yy[fa[j]] - Y[j])) + abs(xx[fa[j]] - X[j]); fa1[j] = (x[fa[j]] == xx[fa[j]] ? max(yy[fa[j]] - Y[j], 0) : abs(yy[fa[j]] - Y[j])); } } for (int i = 0; i <= m; ++i) c[i].clear(); for (int i = 1; i <= q; ++i) if (opt[i] == 'D') c[Y[i]].push_back(i); build(0, m, 1); for (int i = 0; i <= m; ++i) { for (int j : a[i]) change(0, m, 1, min(x[j], xx[j]), max(x[j], xx[j]), j); for (int j : b[i]) if (y[j] < yy[j]) change(0, m, 1, x[j], x[j], j); else f[j][0] = query(0, m, 1, x[j]), g[j][0] = (f1[j] = abs(yy[j] - yy[f[j][0]])) + abs(xx[f[j][0]] - x[j]); for (int j : c[i]) { fa[j] = query(0, m, 1, X[j]), ga[j] = (fa1[j] = abs(Y[j] - yy[fa[j]])) + abs(xx[fa[j]] - X[j]); fa1[j] = (x[fa[j]] == xx[fa[j]] ? max(Y[j] - yy[fa[j]], 0) : abs(yy[fa[j]] - Y[j])); } } for (int j = 1; j <= 50; ++j) for (int i = 1; i <= n; ++i) f[i][j] = f[f[i][j - 1]][j - 1], g[i][j] = min(inf, g[i][j - 1] + g[f[i][j - 1]][j - 1]); for (int i = 1; i <= q; ++i) { if (!fa[i] || T[i] < fa1[i]) print(solve(make_pair(X[i], Y[i]), opt[i], T[i])); else if (T[i] < ga[i]) print(solve(solve(make_pair(X[i], Y[i]), opt[i], fa1[i]), op[fa[i]], T[i] - fa1[i])); else { T[i] -= ga[i]; int now = fa[i]; for (int j = 50; ~j; --j) if (f[now][j] && g[now][j] <= T[i]) T[i] -= g[now][j], now = f[now][j]; if (!f[now][0] || T[i] < f1[now]) print(solve(make_pair(xx[now], yy[now]), op[now], T[i])); else print(solve(solve(make_pair(xx[now], yy[now]), op[now], f1[now]), op[f[now][0]], T[i] - f1[now])); } } return flush(), 0; }
#include <bits/stdc++.h> #pragma G++ optimize(3) using namespace std; set<pair<int, int> > fnd[2][1010]; int tp[1010]; int des[1010][2]; int n, b; int q; int dir[1010]; int typ, dr; int x, y; bool vis[1010]; int nxt[1010][61]; long long len[1010][61]; int lst; int myabs(int x) { if (x < 0) return -x; return x; } void build() { int cur, tm; while (1) { if (vis[lst]) return; vis[lst] = true; if (!typ) { set<pair<int, int> >::iterator it = fnd[0][x].lower_bound(make_pair(y, 0)); if (dr == -1 && (*it).first != y) it--; tm = myabs((*it).first - y); y = (*it).first; if (y == -1 || y == b + 1) { nxt[lst][0] = 0; len[lst][0] = tm; return; } cur = (*it).second; typ = tp[cur]; dr = dir[cur]; } else { set<pair<int, int> >::iterator it = fnd[1][y].lower_bound(make_pair(x, 0)); if (dr == -1 && (*it).first != x) it--; tm = myabs((*it).first - x); x = (*it).first; if (x == -1 || x == b + 1) { nxt[lst][0] = 0; len[lst][0] = tm; return; } cur = (*it).second; typ = tp[cur]; dr = dir[cur]; } if (!typ) { int nd = (des[cur][1] - y) / dr + 1; y += dr * nd; tm += nd; } else { int nd = (des[cur][0] - x) / dr + 1; x += dr * nd; tm += nd; } nxt[lst][0] = cur; len[lst][0] = tm; lst = cur; } } int gt(long long &t, int cur) { int w = 0; for (int i = 0; i <= 60; i++) { if (len[cur][i] > t || !nxt[cur][i]) { w = i; break; } } if (!w) return cur; t -= len[cur][w - 1]; return gt(t, nxt[cur][w - 1]); } void solve(long long t) { int cur; if (!typ) { set<pair<int, int> >::iterator it = fnd[0][x].lower_bound(make_pair(y, 0)); if (dr == -1 && (*it).first != y) it--; if (myabs((*it).first - y) > t) { y += t * dr; return; } t -= myabs((*it).first - y); y = (*it).first; if (y == -1 || y == b + 1) { if (y == -1) y = 0; else y = b; return; } cur = (*it).second; typ = tp[cur]; dr = dir[cur]; } else { set<pair<int, int> >::iterator it = fnd[1][y].lower_bound(make_pair(x, 0)); if (dr == -1 && (*it).first != x) it--; if (myabs((*it).first - x) > t) { x += t * dr; return; } t -= myabs((*it).first - x); x = (*it).first; if (x == -1 || x == b + 1) { if (x == -1) x = 0; else x = b; return; } cur = (*it).second; typ = tp[cur]; dr = dir[cur]; } if (!typ) { int nd = (des[cur][1] - y) / dr; if (nd > t) { y += t * dr; return; } t -= nd; y += dr * nd; } else { int nd = (des[cur][0] - x) / dr; if (nd > t) { x += t * dr; return; } t -= nd; x += dr * nd; } cur = gt(t, cur); x = des[cur][0]; y = des[cur][1]; if (!t) return; dr = dir[cur]; typ = tp[cur]; if (!typ) y += dr; else x += dr; t--; if (!typ) { set<pair<int, int> >::iterator it = fnd[0][x].lower_bound(make_pair(y, 0)); if (dr == -1 && (*it).first != y) it--; if (myabs((*it).first - y) > t) { y += t * dr; return; } t -= myabs((*it).first - y); y = (*it).first; if (y == -1 || y == b + 1) { if (y == -1) y = 0; else y = b; return; } cur = (*it).second; typ = tp[cur]; dr = dir[cur]; } else { set<pair<int, int> >::iterator it = fnd[1][y].lower_bound(make_pair(x, 0)); if (dr == -1 && (*it).first != x) it--; if (myabs((*it).first - x) > t) { x += t * dr; return; } t -= myabs((*it).first - x); x = (*it).first; if (x == -1 || x == b + 1) { if (x == -1) x = 0; else x = b; return; } cur = (*it).second; typ = tp[cur]; dr = dir[cur]; } if (!typ) { int nd = (des[cur][1] - y) / dr; if (nd > t) { y += t * dr; return; } t -= nd; y += dr * nd; } else { int nd = (des[cur][0] - x) / dr; if (nd > t) { x += t * dr; return; } t -= nd; x += dr * nd; } } int main() { scanf("%d%d", &n, &b); for (int i = 1; i <= n; i++) { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); des[i][0] = x2; des[i][1] = y2; if (x1 == x2) { tp[i] = 0; if (y1 < y2) dir[i] = 1; else dir[i] = -1; for (int j = y1; j != y2 + dir[i]; j += dir[i]) fnd[0][x1].insert(make_pair(j, i)), fnd[1][j].insert(make_pair(x1, i)); } else { tp[i] = 1; if (x1 < x2) dir[i] = 1; else dir[i] = -1; for (int j = x1; j != x2 + dir[i]; j += dir[i]) fnd[0][j].insert(make_pair(y1, i)), fnd[1][y1].insert(make_pair(j, i)); } } for (int i = 0; i <= b; i++) fnd[0][i].insert(make_pair(-1, 0)), fnd[0][i].insert(make_pair(b + 1, 0)), fnd[1][i].insert(make_pair(-1, 0)), fnd[1][i].insert(make_pair(b + 1, 0)); for (int i = 1; i <= n; i++) { if (vis[i]) continue; lst = i; x = des[i][0]; y = des[i][1]; dr = dir[i]; typ = tp[i]; if (!tp[i]) y += dir[i]; else x += dir[i]; build(); } for (int i = 1; i <= 60; i++) { for (int j = 1; j <= n; j++) { nxt[j][i] = nxt[nxt[j][i - 1]][i - 1]; len[j][i] = len[j][i - 1] + len[nxt[j][i - 1]][i - 1]; } } scanf("%d", &q); while (q--) { char D; long long t; scanf("%d%d%c%c%lld", &x, &y, &D, &D, &t); if (D == 'L' || D == 'R') { typ = 1; if (D == 'L') dr = -1; else dr = 1; } else { typ = 0; if (D == 'U') dr = 1; else dr = -1; } solve(t); printf("%d %d\n", x, y); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const unsigned gen_seed = std::chrono::system_clock::now().time_since_epoch().count(); std::mt19937_64 gen(gen_seed); std::vector<int> to; int b; int A; int encode(int x, int y, int d) { return d * A * A + x * A + y; } std::vector<int> dx, dy; int main() { dx.push_back(1); dx.push_back(0); dx.push_back(-1); dx.push_back(0); dy.push_back(0); dy.push_back(1); dy.push_back(0); dy.push_back(-1); int n; scanf("%d %d", &n, &b); A = b + 1; to.resize(4 * (b + 1) * (b + 1)); std::vector<std::vector<int> > f(b + 1, std::vector<int>(b + 1, -1)); for (int i = 0; i < n; i++) { int x0, y0, x1, y1; scanf("%d %d %d %d", &x0, &y0, &x1, &y1); int dir = 0; if (x1 > x0) dir = 0; else if (y1 > y0) dir = 1; else if (x1 < x0) dir = 2; else if (y1 < y0) dir = 3; if (x0 > x1) swap(x0, x1); if (y0 > y1) swap(y0, y1); for (int x = x0; x < x1 + 1; x++) for (int y = y0; y < y1 + 1; y++) f[x][y] = dir; } int q; scanf("%d", &q); std::vector<int> pos(q); std::vector<long long> len(q); for (int i = 0; i < q; i++) { int x, y; char c; scanf("%d %d %c %lld", &x, &y, &c, &len[i]); int dir = 0; if (c == 'U') dir = 1; else if (c == 'L') dir = 2; else if (c == 'D') dir = 3; pos[i] = encode(x, y, dir); } for (int x = 0; x < b + 1; x++) for (int y = 0; y < b + 1; y++) for (int dir = 0; dir < 4; dir++) { int top = dir; if (f[x][y] != -1) top = f[x][y]; int nx = x + dx[top]; int ny = y + dy[top]; int cur = encode(x, y, dir); if (nx < 0 || nx > b || ny < 0 || ny > b) to[cur] = cur; else to[cur] = encode(nx, ny, top); } for (int it = 0; it < 60; it++) { for (int i = 0; i < q; i++) { if (len[i] % 2) pos[i] = to[pos[i]]; len[i] /= 2; } std::vector<int> nto(4 * A * A); for (int i = 0; i < 4 * A * A; i++) nto[i] = to[to[i]]; to = std::move(nto); nto.clear(); } for (int i = 0; i < q; i++) { printf("%d %d\n", (pos[i] / A) % A, pos[i] % A); } }
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e17; const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; const char dir[4] = {'U', 'R', 'D', 'L'}; inline int mabs(int x) { return (x > 0 ? x : -x); } const int N = 400010; int arr[N]; long long when[N], dist[N], qt[N]; int pv[55][N]; long long pt[55][N]; int xa[N], ya[N], xb[N], yb[N], arrd[N]; int wx[N], wy[N], wd[N]; int n, b, q; bool swapx[N], swapy[N]; int ke; int e[N], el[N], er[N], en[N], rght[N]; bool good[N]; void Sort(int l, int r) { int i = l, j = r; int x = e[(l + r) >> 1]; int xx = el[(l + r) >> 1]; do { while (e[i] < x || e[i] == x && el[i] > xx) i++; while (x < e[j] || x == e[j] && xx > el[j]) j--; if (i <= j) { int tmp = e[i]; e[i] = e[j]; e[j] = tmp; tmp = el[i]; el[i] = el[j]; el[j] = tmp; tmp = er[i]; er[i] = er[j]; er[j] = tmp; tmp = en[i]; en[i] = en[j]; en[j] = tmp; i++; j--; } } while (i <= j); if (l < j) Sort(l, j); if (i < r) Sort(i, r); } bool push[N]; int tree[N]; void build(int x, int l, int r) { push[x] = false; tree[x] = 0; if (l < r) { build(x + x, l, (l + r) >> 1); build(x + x + 1, ((l + r) >> 1) + 1, r); } } void put(int x, int l, int r, int ll, int rr, int v) { if (l >= ll && r <= rr) { push[x] = true; tree[x] = v; return; } if (push[x]) { push[x + x] = true; push[x + x + 1] = true; tree[x + x] = tree[x]; tree[x + x + 1] = tree[x]; push[x] = false; } int y = (l + r) >> 1; if (ll <= y) put(x + x, l, y, ll, rr, v); if (rr > y) put(x + x + 1, y + 1, r, ll, rr, v); } int get(int x, int l, int r, int p) { if (l == r || push[x]) return tree[x]; int y = (l + r) >> 1; if (p <= y) return get(x + x, l, y, p); return get(x + x + 1, y + 1, r, p); } void get_next_all() { for (int i = 1; i <= n; i++) { swapx[i] = false; swapy[i] = false; if (xa[i] > xb[i]) { int tmp = xa[i]; xa[i] = xb[i]; xb[i] = tmp; swapx[i] = true; } if (ya[i] > yb[i]) { int tmp = ya[i]; ya[i] = yb[i]; yb[i] = tmp; swapy[i] = true; } } for (int i = 1; i <= n + q; i++) { if (wx[i] < 0 || wy[i] < 0 || wx[i] > b || wy[i] > b) { good[i] = false; arr[i] = 0; } else good[i] = true; } { for (int i = 1; i <= n; i++) { e[i - 1] = xa[i]; el[i - 1] = ya[i]; er[i - 1] = yb[i]; en[i - 1] = i; rght[i] = xb[i]; } ke = n; for (int i = 1; i <= n + q; i++) { if (wd[i] == 3 && good[i]) { e[ke] = wx[i]; el[ke] = ~wy[i]; en[ke] = i; ke++; } } Sort(0, ke - 1); build(1, 0, b); for (int i = 0; i < ke; i++) if (el[i] >= 0) { put(1, 0, b, el[i], er[i], en[i]); } else { int num = get(1, 0, b, ~el[i]); arr[en[i]] = num; when[en[i]] = e[i] - rght[num]; } } { for (int i = 1; i <= n; i++) { e[i - 1] = -xb[i]; el[i - 1] = ya[i]; er[i - 1] = yb[i]; en[i - 1] = i; rght[i] = -xa[i]; } ke = n; for (int i = 1; i <= n + q; i++) { if (wd[i] == 1 && good[i]) { e[ke] = -wx[i]; el[ke] = ~wy[i]; en[ke] = i; ke++; } } Sort(0, ke - 1); build(1, 0, b); for (int i = 0; i < ke; i++) if (el[i] >= 0) { put(1, 0, b, el[i], er[i], en[i]); } else { int num = get(1, 0, b, ~el[i]); arr[en[i]] = num; when[en[i]] = e[i] - rght[num]; } } { for (int i = 1; i <= n; i++) { e[i - 1] = ya[i]; el[i - 1] = xa[i]; er[i - 1] = xb[i]; en[i - 1] = i; rght[i] = yb[i]; } ke = n; for (int i = 1; i <= n + q; i++) { if (wd[i] == 2 && good[i]) { e[ke] = wy[i]; el[ke] = ~wx[i]; en[ke] = i; ke++; } } Sort(0, ke - 1); build(1, 0, b); for (int i = 0; i < ke; i++) if (el[i] >= 0) { put(1, 0, b, el[i], er[i], en[i]); } else { int num = get(1, 0, b, ~el[i]); arr[en[i]] = num; when[en[i]] = e[i] - rght[num]; } } { for (int i = 1; i <= n; i++) { e[i - 1] = -yb[i]; el[i - 1] = xa[i]; er[i - 1] = xb[i]; en[i - 1] = i; rght[i] = -ya[i]; } ke = n; for (int i = 1; i <= n + q; i++) { if (wd[i] == 0 && good[i]) { e[ke] = -wy[i]; el[ke] = ~wx[i]; en[ke] = i; ke++; } } Sort(0, ke - 1); build(1, 0, b); for (int i = 0; i < ke; i++) if (el[i] >= 0) { put(1, 0, b, el[i], er[i], en[i]); } else { int num = get(1, 0, b, ~el[i]); arr[en[i]] = num; when[en[i]] = e[i] - rght[num]; } } for (int i = 1; i <= n + q; i++) { if (arr[i] == 0) when[i] = inf; if (when[i] < 0) when[i] = 0; } for (int i = 1; i <= n; i++) { if (swapx[i]) { int tmp = xa[i]; xa[i] = xb[i]; xb[i] = tmp; } if (swapy[i]) { int tmp = ya[i]; ya[i] = yb[i]; yb[i] = tmp; } } } int main() { scanf("%d %d", &n, &b); for (int i = 1; i <= n; i++) { scanf("%d %d %d %d", xa + i, ya + i, xb + i, yb + i); if (yb[i] > ya[i]) arrd[i] = 0; else if (xb[i] > xa[i]) arrd[i] = 1; else if (yb[i] < ya[i]) arrd[i] = 2; else arrd[i] = 3; wx[i] = xb[i] + dx[arrd[i]]; wy[i] = yb[i] + dy[arrd[i]]; wd[i] = arrd[i]; } scanf("%d", &q); for (int i = n + 1; i <= n + q; i++) { char foo; scanf("%d %d %c %I64d", wx + i, wy + i, &foo, qt + i); wd[i] = 0; for (int j = 0; j < 4; j++) if (foo == dir[j]) wd[i] = j; } get_next_all(); for (int i = 1; i <= n + q; i++) if (arr[i] == 0) dist[i] = inf; else dist[i] = mabs(wx[i] - xb[arr[i]]) + mabs(wy[i] - yb[arr[i]]); arr[0] = 0; when[0] = inf; dist[0] = inf; for (int i = 0; i <= n; i++) pv[0][i] = arr[i], pt[0][i] = dist[i] + 1; for (int j = 1; j <= 50; j++) for (int i = 0; i <= n; i++) { pv[j][i] = pv[j - 1][pv[j - 1][i]]; pt[j][i] = pt[j - 1][i] + pt[j - 1][pv[j - 1][i]]; if (pt[j][i] > inf) pt[j][i] = inf; } for (int i = n + 1; i <= n + q; i++) { if (qt[i] <= dist[i]) { if (qt[i] <= when[i]) { long long ax = wx[i] + dx[wd[i]] * qt[i]; long long ay = wy[i] + dy[wd[i]] * qt[i]; if (ax < 0) ax = 0; if (ay < 0) ay = 0; if (ax > b) ax = b; if (ay > b) ay = b; printf("%d %d\n", (int)ax, (int)ay); continue; } long long ax = wx[i] + dx[wd[i]] * when[i]; long long ay = wy[i] + dy[wd[i]] * when[i]; ax += dx[arrd[arr[i]]] * (qt[i] - when[i]); ay += dy[arrd[arr[i]]] * (qt[i] - when[i]); if (ax < 0) ax = 0; if (ay < 0) ay = 0; if (ax > b) ax = b; if (ay > b) ay = b; printf("%d %d\n", (int)ax, (int)ay); continue; } qt[i] -= dist[i]; int where = arr[i]; for (int j = 50; j >= 0; j--) if (pt[j][where] <= qt[i]) { qt[i] -= pt[j][where]; where = pv[j][where]; } if (arr[where] == 0 || qt[i] <= when[where] + 1) { long long ax = xb[where] + dx[arrd[where]] * qt[i]; long long ay = yb[where] + dy[arrd[where]] * qt[i]; if (ax < 0) ax = 0; if (ay < 0) ay = 0; if (ax > b) ax = b; if (ay > b) ay = b; printf("%d %d\n", (int)ax, (int)ay); continue; } int ax = xb[where] + dx[arrd[where]] * (when[where] + 1); int ay = yb[where] + dy[arrd[where]] * (when[where] + 1); ax += dx[arrd[arr[where]]] * (qt[i] - (when[where] + 1)); ay += dy[arrd[arr[where]]] * (qt[i] - (when[where] + 1)); printf("%d %d\n", ax, ay); } return 0; }
#include <bits/stdc++.h> using namespace std; const unsigned _mod = 998244353; const unsigned mod = 1e9 + 7; const long long infi = 0x3f3f3f3f3f3f3f3fll; const int inf = 0x3f3f3f3f; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long ksm(long long x, long long y) { long long ret = 1; while (y) { if (y & 1ll) ret = ret * x % mod; y >>= 1ll; x = x * x % mod; } return ret; } long long qpow(long long x, long long y) { long long ret = 1; while (y) { if (y & 1ll) ret = ret * x; y >>= 1ll; x = x * x; } return ret; } long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; } long long dx[5] = {0, 1, 0, -1, 0}, dy[5] = {0, 0, 1, 0, -1}, n, b, q, a[1010][1010], x[5100010][2], ans[1200010], t[1200010], x1, suibiandefineyixiajiuhaole, x2, y2, d, h; char ch; long long hash1(long long x, long long y, long long d) { if (a[x][y]) d = a[x][y]; return d * 1001 * 1001 + x * 1001 + y; } int main() { cin >> n >> b; for (int i = 1; i <= n; ++i) { x1 = read(), suibiandefineyixiajiuhaole = read(), x2 = read(), y2 = read(); if (x1 == x2) d = 2 + (suibiandefineyixiajiuhaole > y2) * 2; else d = 1 + (x1 > x2) * 2; while (x1 != x2 || suibiandefineyixiajiuhaole != y2) a[x1][suibiandefineyixiajiuhaole] = d, x1 += dx[d], suibiandefineyixiajiuhaole += dy[d]; a[x2][y2] = d; } for (register int i = 0; i <= b; ++i) for (register int j = 0; j <= b; ++j) for (register int k = 1; k <= 4; ++k) if (!a[i][j] || a[i][j] == k) { h = hash1(i, j, k); x1 = i + dx[k]; suibiandefineyixiajiuhaole = j + dy[k]; if (x1 < 0 || x1 > b || suibiandefineyixiajiuhaole < 0 || suibiandefineyixiajiuhaole > b) x[h][0] = h; else x[h][0] = hash1(x1, suibiandefineyixiajiuhaole, k); } cin >> q; for (register int i = 1; i <= q; ++i) { x1 = read(), x2 = read(); cin >> ch; t[i] = read(); if (ch == 'R') d = 1; if (ch == 'U') d = 2; if (ch == 'L') d = 3; if (ch == 'D') d = 4; ans[i] = hash1(x1, x2, d); } for (register int i = 0; i <= 60; ++i) { h = i & 1; for (register int j = 1; j <= q; ++j) if (t[j] & (1ll << i)) ans[j] = x[ans[j]][h]; for (register int j = 1; j <= 5100000; ++j) x[j][h ^ 1] = x[x[j][h]][h]; } for (register int i = 1; i <= q; ++i) { suibiandefineyixiajiuhaole = ans[i] % 1001; x1 = (ans[i] / 1001) % 1001; cout << x1 << ' ' << suibiandefineyixiajiuhaole << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; const unsigned _mod = 998244353; const unsigned mod = 1e9 + 7; const long long infi = 0x3f3f3f3f3f3f3f3fll; const int inf = 0x3f3f3f3f; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long ksm(long long x, long long y) { long long ret = 1; while (y) { if (y & 1ll) ret = ret * x % mod; y >>= 1ll; x = x * x % mod; } return ret; } long long qpow(long long x, long long y) { long long ret = 1; while (y) { if (y & 1ll) ret = ret * x; y >>= 1ll; x = x * x; } return ret; } long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; } long long dx[5] = {0, 1, 0, -1, 0}, dy[5] = {0, 0, 1, 0, -1}, n, b, q, a[1010][1010], x[5100010][2], ans[1200010], t[1200010], x1, suibiandefineyixiajiuhaole, x2, y2, d, h; char ch; long long hash1(long long x, long long y, long long d) { if (a[x][y]) d = a[x][y]; return d * 1001 * 1001 + x * 1001 + y; } int main() { cin >> n >> b; for (int i = 1; i <= n; ++i) { x1 = read(), suibiandefineyixiajiuhaole = read(), x2 = read(), y2 = read(); if (x1 == x2) d = 2 + (suibiandefineyixiajiuhaole > y2) * 2; else d = 1 + (x1 > x2) * 2; while (x1 != x2 || suibiandefineyixiajiuhaole != y2) a[x1][suibiandefineyixiajiuhaole] = d, x1 += dx[d], suibiandefineyixiajiuhaole += dy[d]; a[x2][y2] = d; } for (register int i = 0; i <= b; ++i) for (register int j = 0; j <= b; ++j) for (register int k = 1; k <= 4; ++k) if (!a[i][j] || a[i][j] == k) { h = hash1(i, j, k); x1 = i + dx[k]; suibiandefineyixiajiuhaole = j + dy[k]; if (x1 < 0 || x1 > b || suibiandefineyixiajiuhaole < 0 || suibiandefineyixiajiuhaole > b) x[h][0] = h; else x[h][0] = hash1(x1, suibiandefineyixiajiuhaole, k); } cin >> q; for (register int i = 1; i <= q; ++i) { x1 = read(), x2 = read(); cin >> ch; t[i] = read(); if (ch == 'R') d = 1; if (ch == 'U') d = 2; if (ch == 'L') d = 3; if (ch == 'D') d = 4; ans[i] = hash1(x1, x2, d); } for (register int i = 0; i <= 60; ++i) { h = i & 1; for (register int j = 1; j <= q; ++j) if (t[j] & (1ll << i)) ans[j] = x[ans[j]][h]; for (register int j = 1; j <= 5100000; ++j) x[j][h ^ 1] = x[x[j][h]][h]; } for (register int i = 1; i <= q; ++i) { suibiandefineyixiajiuhaole = ans[i] % 1001; x1 = (ans[i] / 1001) % 1001; cout << x1 << ' ' << suibiandefineyixiajiuhaole << '\n'; } return 0; }
#include <bits/stdc++.h> const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; const char dz[4] = {'R', 'U', 'L', 'D'}; int n, m, bs, i, j, xa, ya, xb, yb, xx, yy, dd, d[1010], a[1010][1010], g[1010], c[1010], x[1010], y[1010], u[1010], f[1010], it; long long cnt, cur; char s[5]; int main() { scanf("%d%d", &n, &bs); for (i = 1; i <= n; i++) { scanf("%d%d%d%d", &xa, &ya, &xb, &yb); x[i] = xb; y[i] = yb; if (xa == xb) { if (ya > yb) { d[i] = 3; for (j = yb; j <= ya; j++) a[xa][j] = i; } else { d[i] = 1; for (j = ya; j <= yb; j++) a[xa][j] = i; } } else { if (xa > xb) { d[i] = 2; for (j = xb; j <= xa; j++) a[j][ya] = i; } else { d[i] = 0; for (j = xa; j <= xb; j++) a[j][ya] = i; } } } for (i = 1; i <= n; i++) { for (xx = x[i], yy = y[i], dd = d[i]; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; xx += dx[dd], yy += dy[dd], c[i]++) { j = a[xx][yy]; if (j > 0) { dd = d[j]; if (c[i] > 0 && x[j] == xx && y[j] == yy) break; } } if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) g[i] = j; } scanf("%d", &m); while (m--) { scanf("%d%d", &xx, &yy); scanf("%s", s); for (dd = 0; dd < 4; dd++) if (dz[dd] == s[0]) break; scanf("%I64d", &cnt); for (; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; xx += dx[dd], yy += dy[dd], cnt--) { if (cnt == 0) break; j = a[xx][yy]; if (j > 0) { dd = d[j]; if (x[j] == xx && y[j] == yy) break; } } if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) { if (cnt == 0) { printf("%d %d\n", xx, yy); continue; } } else { printf("%d %d\n", xx - dx[dd], yy - dy[dd]); continue; } for (cur = 0, ++it;; j = g[j]) { if (u[j] == it) { cnt -= f[j]; cnt %= cur - f[j]; cur = 0; ++it; } xx = x[j]; yy = y[j]; dd = d[j]; if (g[j] == 0 || cur + c[j] > cnt) { cnt -= cur; break; } u[j] = it; f[j] = cur; cur += c[j]; } for (; xx >= 0 && yy >= 0 && xx <= bs && yy <= bs; xx += dx[dd], yy += dy[dd], cnt--) { if (cnt == 0) break; j = a[xx][yy]; if (j > 0) dd = d[j]; } if (xx >= 0 && yy >= 0 && xx <= bs && yy <= bs) printf("%d %d\n", xx, yy); else printf("%d %d\n", xx - dx[dd], yy - dy[dd]); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 300010; struct NN { int x1, y1, x2, y2; int x, y; int len; } nn[1100]; int b; int bel[1010][1010]; int tox[1010][1010], toy[1010][1010]; void init(int n) { int i, j, ii, jj; for (i = 1; i <= n; i++) { for (ii = nn[i].x1, jj = nn[i].y1, j = 0; j < nn[i].len + 1; j++, ii += nn[i].x, jj += nn[i].y) bel[ii][jj] = i; } for (i = 1; i <= n; i++) { ii = nn[i].x2 + nn[i].x; jj = nn[i].y2 + nn[i].y; while (ii >= 0 && ii <= b && jj >= 0 && jj <= b && !bel[ii][jj]) ii += nn[i].x, jj += nn[i].y; for (j = 0; j < nn[i].len + 1; j++) { tox[nn[i].x1 + nn[i].x * j][nn[i].y1 + nn[i].y * j] = ii; toy[nn[i].x1 + nn[i].x * j][nn[i].y1 + nn[i].y * j] = jj; } } } int vis[1010][1010]; long long tim[1010][1010]; int main() { int n, i, j, k; cin >> n >> b; for (i = 1; i <= n; i++) { scanf("%d%d%d%d", &nn[i].x1, &nn[i].y1, &nn[i].x2, &nn[i].y2); nn[i].len = abs(nn[i].x2 - nn[i].x1) + abs(nn[i].y2 - nn[i].y1); if (nn[i].x1 < nn[i].x2) nn[i].x = 1; else if (nn[i].x1 > nn[i].x2) nn[i].x = -1; if (nn[i].y1 < nn[i].y2) nn[i].y = 1; else if (nn[i].y1 > nn[i].y2) nn[i].y = -1; } init(n); int m; cin >> m; int x, y; long long t; char s[10]; int xx, yy; int woca = 1; while (m--) { scanf("%d%d%s%I64d", &x, &y, s, &t); if (!t) { printf("%d %d\n", x, y); continue; } if (s[0] == 'U') xx = 0, yy = 1; else if (s[0] == 'D') xx = 0, yy = -1; else if (s[0] == 'L') xx = -1, yy = 0; else xx = 1, yy = 0; while (x >= 0 && x <= b && y >= 0 && y <= b && !bel[x][y] && t) { x += xx, y += yy; t--; } if (x < 0 || y < 0 || x > b || y > b) { x -= xx, y -= yy; printf("%d %d\n", x, y); continue; } if (t == 0) { printf("%d %d\n", x, y); continue; } woca++; int dx = xx, dy = yy; while (t) { if (x < 0 || y < 0 || x > b || y > b) break; if (vis[x][y] == woca) t %= tim[x][y] - t; else vis[x][y] = woca, tim[x][y] = t; xx = tox[x][y], yy = toy[x][y]; dx = dy = 0; if (xx > x) dx = 1; else if (xx < x) dx = -1; if (yy > y) dy = 1; else if (yy < y) dy = -1; if (abs(xx - x) + abs(yy - y) > t) { break; } t -= abs(xx - x) + abs(yy - y); x = xx, y = yy; } if (x < 0 || y < 0 || x > b || y > b) { if (x < 0) x++; if (y < 0) y++; if (x > b) x--; if (y > b) y--; t = 0; } int tt = t; printf("%d %d\n", x + tt * dx, y + tt * dy); } }
#include <bits/stdc++.h> using namespace std; const int inf = ~0U >> 1; const long long INF = ~0ULL >> 1; const int maxn = 120000; int x2[maxn], y2[maxn], nxtid[maxn]; int Dir[maxn]; int M, N; int f[maxn][52]; long long g[maxn][52]; set<pair<int, int> > S[4][maxn]; const int dx[4] = {0, 0, 1, -1}; const int dy[4] = {1, -1, 0, 0}; inline int inside(int x, int y) { return x > 0 && y > 0 && x <= N && y <= N; } struct Segment_Tree { struct node { int l, r; set<pair<int, int> > s; } t[maxn * 5]; void build(int p, int l, int r) { t[p].l = l; t[p].r = r; if (l != r) { build(p << 1, l, l + r >> 1); build(p << 1 | 1, (l + r >> 1) + 1, r); } } pair<int, int> query(int p, int target, int now, int Dir) { pair<int, int> ret; if (Dir == 0 || Dir == 2) { ret = (make_pair(inf, -1)); set<pair<int, int> >::iterator it = t[p].s.lower_bound((make_pair(now, 0))); if (it != t[p].s.end()) ret = min(ret, *it); } else { ret = (make_pair(-inf, -1)); set<pair<int, int> >::iterator it = t[p].s.upper_bound((make_pair(now, inf))); if (it != t[p].s.begin()) { --it; ret = max(ret, *it); } } if (t[p].l == t[p].r) return ret; int m = t[p].l + t[p].r >> 1; if (target <= m) { pair<int, int> tmp = query(p << 1, target, now, Dir); if (Dir == 0 || Dir == 2) return min(tmp, ret); else return max(tmp, ret); } else { pair<int, int> tmp = query(p << 1 | 1, target, now, Dir); if (Dir == 0 || Dir == 2) return min(tmp, ret); else return max(tmp, ret); } } void modify(int p, int l, int r, int x, int y) { if (t[p].l == l && t[p].r == r) { t[p].s.insert((make_pair(x, y))); return; } int m = t[p].l + t[p].r >> 1; if (r <= m) modify(p << 1, l, r, x, y); else if (l > m) modify(p << 1 | 1, l, r, x, y); else modify(p << 1, l, m, x, y), modify(p << 1 | 1, m + 1, r, x, y); } } T[2]; pair<int, int> walk(int cur, long long maxt) { for (int i = (50); i >= (0); --i) if (f[cur][i] && maxt >= g[cur][i]) { maxt -= g[cur][i]; cur = f[cur][i]; } int curx = x2[cur], cury = y2[cur]; if (Dir[cur] == 0) { pair<int, int> tmp = T[1].query(1, x2[cur], y2[cur], Dir[cur]); set<pair<int, int> >::iterator it = S[1][curx].lower_bound((make_pair(cury, 0))); if (it != S[1][curx].end() && max(y2[it->second], cury) < tmp.first) { long long step = max(y2[it->second], cury) - cury; if (step >= maxt) return (make_pair(int(curx + dx[Dir[cur]] * maxt), int(cury + dy[Dir[cur]] * maxt))); cury += step; maxt -= step; step = cury - y2[it->second]; if (step >= maxt) return (make_pair(int(curx + dx[Dir[it->second]] * maxt), int(cury + dy[Dir[it->second]] * maxt))); } if (tmp.second == -1) { if (maxt <= N - y2[cur]) return (make_pair(x2[cur], y2[cur] + maxt)); else return (make_pair(x2[cur], N)); } else { if (maxt <= tmp.first - y2[cur]) return (make_pair(x2[cur], y2[cur] + maxt)); maxt -= (tmp.first - y2[cur]); return (make_pair(x2[cur] + dx[Dir[tmp.second]] * maxt, tmp.first + dy[Dir[tmp.second]] * maxt)); } } else if (Dir[cur] == 1) { pair<int, int> tmp = T[1].query(1, x2[cur], y2[cur], Dir[cur]); set<pair<int, int> >::iterator it = S[0][curx].upper_bound((make_pair(cury, inf))); if (it != S[0][curx].begin() && min(y2[(--it)->second], cury) > tmp.first) { long long step = cury - min(y2[it->second], cury); if (step >= maxt) return (make_pair(int(curx + dx[Dir[cur]] * maxt), int(cury + dy[Dir[cur]] * maxt))); cury -= step; maxt -= step; step = y2[it->second] - cury; if (step >= maxt) return (make_pair(int(curx + dx[Dir[it->second]] * maxt), int(cury + dy[Dir[it->second]] * maxt))); } if (tmp.second == -1) { if (maxt <= y2[cur]) return (make_pair(x2[cur], y2[cur] - maxt)); else return (make_pair(x2[cur], 0)); } else { if (maxt <= y2[cur] - tmp.first) return (make_pair(x2[cur], y2[cur] - maxt)); maxt -= (y2[cur] - tmp.first); return (make_pair(x2[cur] + dx[Dir[tmp.second]] * maxt, tmp.first + dy[Dir[tmp.second]] * maxt)); } } else if (Dir[cur] == 2) { pair<int, int> tmp = T[0].query(1, y2[cur], x2[cur], Dir[cur]); set<pair<int, int> >::iterator it = S[3][cury].lower_bound((make_pair(curx, 0))); if (it != S[3][cury].end() && max(x2[it->second], curx) < tmp.first) { long long step = abs(curx - max(curx, x2[it->second])); if (step >= maxt) return (make_pair(int(curx + dx[Dir[cur]] * maxt), int(cury + dy[Dir[cur]] * maxt))); curx += step; maxt -= step; step = abs(x2[it->second] - curx); if (step >= maxt) return (make_pair(int(curx + dx[Dir[it->second]] * maxt), int(cury + dy[Dir[it->second]] * maxt))); } if (tmp.second == -1) { if (maxt <= N - x2[cur]) return (make_pair(x2[cur] + maxt, y2[cur])); else return (make_pair(N, y2[cur])); } else { if (maxt <= tmp.first - x2[cur]) return (make_pair(x2[cur] + maxt, y2[cur])); maxt -= (tmp.first - x2[cur]); return (make_pair(tmp.first + dx[Dir[tmp.second]] * maxt, y2[cur] + dy[Dir[tmp.second]] * maxt)); } } else { pair<int, int> tmp = T[0].query(1, y2[cur], x2[cur], Dir[cur]); set<pair<int, int> >::iterator it = S[2][cury].upper_bound((make_pair(curx, inf))); if (it != S[2][cury].begin() && min(x2[(--it)->second], curx) > tmp.first) { long long step = abs(curx - min(x2[it->second], curx)); if (step >= maxt) return (make_pair(int(curx + dx[Dir[cur]] * maxt), int(cury + dy[Dir[cur]] * maxt))); curx -= step; maxt -= step; step = abs(x2[it->second] - curx); if (step >= maxt) return (make_pair(int(curx + dx[Dir[it->second]] * maxt), int(cury + dy[Dir[it->second]] * maxt))); } if (tmp.second == -1) { if (maxt <= x2[cur]) return (make_pair(x2[cur] - maxt, y2[cur])); else return (make_pair(0, y2[cur])); } else { if (maxt <= x2[cur] - tmp.first) return (make_pair(x2[cur] - maxt, y2[cur])); maxt -= (x2[cur] - tmp.first); return (make_pair(tmp.first + dx[Dir[tmp.second]] * maxt, y2[cur] + dy[Dir[tmp.second]] * maxt)); } } } int main() { scanf("%d%d", &M, &N); T[0].build(1, 0, N); T[1].build(1, 0, N); for (int i = (1); i <= (M); ++i) { int x1, y1; scanf("%d%d%d%d", &x1, &y1, &x2[i], &y2[i]); if (x1 == x2[i]) { if (y1 < y2[i]) { T[0].modify(1, y1, y2[i], x1, i); Dir[i] = 0; S[0][x1].insert((make_pair(y1, i))); } else { T[0].modify(1, y2[i], y1, x2[i], i); Dir[i] = 1; S[1][x1].insert((make_pair(y1, i))); } } else if (x1 < x2[i]) { T[1].modify(1, x1, x2[i], y1, i); Dir[i] = 2; S[2][y1].insert((make_pair(x1, i))); } else { T[1].modify(1, x2[i], x1, y1, i); Dir[i] = 3; S[3][y1].insert((make_pair(x1, i))); } } for (int i = (1); i <= (M); ++i) { int curx = x2[i], cury = y2[i]; if (Dir[i] == 0) { pair<int, int> tmp = T[1].query(1, x2[i], y2[i], Dir[i]); set<pair<int, int> >::iterator it = S[1][curx].lower_bound((make_pair(cury, 0))); if (it != S[1][curx].end() && max(y2[it->second], cury) < tmp.first) { long long step = max(y2[it->second], cury) - cury; g[i][0] += step; cury += step; step = cury - y2[it->second]; curx += dx[Dir[it->second]] * step; cury += dy[Dir[it->second]] * step; g[i][0] += step; f[i][0] = it->second; continue; } if (tmp.second == -1) f[i][0] = 0, g[i][0] = N - y2[i]; else { f[i][0] = tmp.second; g[i][0] = tmp.first - y2[i] + abs(x2[tmp.second] - x2[i]); } } else if (Dir[i] == 1) { pair<int, int> tmp = T[1].query(1, x2[i], y2[i], Dir[i]); set<pair<int, int> >::iterator it = S[0][curx].upper_bound((make_pair(cury, inf))); if (it != S[0][curx].begin() && min(y2[(--it)->second], cury) > tmp.first) { long long step = cury - min(y2[it->second], cury); g[i][0] += step; cury -= step; step = y2[it->second] - cury; g[i][0] += step; curx += dx[Dir[it->second]] * step; cury += dy[Dir[it->second]] * step; f[i][0] = it->second; continue; } if (tmp.second == -1) f[i][0] = 0, g[i][0] = y2[i]; else { f[i][0] = tmp.second; g[i][0] = y2[i] - tmp.first + abs(x2[tmp.second] - x2[i]); } } else if (Dir[i] == 2) { pair<int, int> tmp = T[0].query(1, y2[i], x2[i], Dir[i]); set<pair<int, int> >::iterator it = S[3][cury].lower_bound((make_pair(curx, 0))); if (it != S[3][cury].end() && max(x2[it->second], curx) < tmp.first) { long long step = abs(curx - max(curx, x2[it->second])); g[i][0] += step; curx += step; step = abs(x2[it->second] - curx); g[i][0] += step; curx += dx[Dir[it->second]] * step; cury += dy[Dir[it->second]] * step; f[i][0] = it->second; continue; } if (tmp.second == -1) f[i][0] = 0, g[i][0] = N - x2[i]; else { f[i][0] = tmp.second; g[i][0] = tmp.first - x2[i] + abs(y2[tmp.second] - y2[i]); } } else { pair<int, int> tmp = T[0].query(1, y2[i], x2[i], Dir[i]); set<pair<int, int> >::iterator it = S[2][cury].upper_bound((make_pair(curx, inf))); if (it != S[2][cury].begin() && min(x2[(--it)->second], curx) > tmp.first) { long long step = abs(curx - min(x2[it->second], curx)); g[i][0] += step; curx -= step; step = abs(x2[it->second] - curx); g[i][0] += step; curx += dx[Dir[it->second]] * step; cury += dy[Dir[it->second]] * step; f[i][0] = it->second; continue; } if (tmp.second == -1) f[i][0] = 0, g[i][0] = x2[i]; else { f[i][0] = tmp.second; g[i][0] = x2[i] - tmp.first + abs(y2[tmp.second] - y2[i]); } } } for (int j = (1); j <= (50); ++j) for (int i = (1); i <= (M); ++i) { if (g[i][j - 1] < INF && g[f[i][j - 1]][j - 1] < INF) g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1]; else g[i][j] = INF; if (g[i][j] > (long long)1e16) g[i][j] = INF; f[i][j] = f[f[i][j - 1]][j - 1]; } int Q; scanf("%d", &Q); for (int _ = (1); _ <= (Q); ++_) { char is[5]; int curx, cury, dir; long long maxt; scanf("%d%d%s%lld", &curx, &cury, is, &maxt); if (is[0] == 'U') dir = 0; else if (is[0] == 'D') dir = 1; else if (is[0] == 'R') dir = 2; else dir = 3; if (dir == 0) { pair<int, int> tmp = T[1].query(1, curx, cury, dir); set<pair<int, int> >::iterator it = S[1][curx].lower_bound((make_pair(cury, 0))); if (it != S[1][curx].end() && max(y2[it->second], cury) < tmp.first) { long long step = max(y2[it->second], cury) - cury; if (step >= maxt) { printf("%d %d\n", int(curx + dx[dir] * maxt), int(cury + dy[dir] * maxt)); continue; } cury += step; maxt -= step; step = cury - y2[it->second]; if (step >= maxt) { printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt), int(cury + dy[Dir[it->second]] * maxt)); continue; } curx += dx[Dir[it->second]] * step; cury += dy[Dir[it->second]] * step; maxt -= step; pair<int, int> ans = walk(it->second, maxt); printf("%d %d\n", int(ans.first), int(ans.second)); continue; } if (tmp.second == -1) { long long step = min((long long)N - cury, maxt); printf("%d %d\n", int(curx + dx[dir] * step), int(cury + dy[dir] * step)); continue; } long long step = tmp.first - cury; if (step >= maxt) { printf("%d %d\n", int(curx + dx[dir] * maxt), int(cury + dy[dir] * maxt)); continue; } curx += dx[dir] * step; cury += dy[dir] * step; maxt -= step; step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury); if (step >= maxt) { printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt), int(cury + dy[Dir[tmp.second]] * maxt)); continue; } curx += dx[Dir[tmp.second]] * step; cury += dy[Dir[tmp.second]] * step; maxt -= step; pair<int, int> ans = walk(tmp.second, maxt); printf("%d %d\n", int(ans.first), int(ans.second)); } else if (dir == 1) { pair<int, int> tmp = T[1].query(1, curx, cury, dir); set<pair<int, int> >::iterator it = S[0][curx].upper_bound((make_pair(cury, inf))); if (it != S[0][curx].begin() && min(y2[(--it)->second], cury) > tmp.first) { long long step = cury - min(y2[it->second], cury); if (step >= maxt) { printf("%d %d\n", int(curx + dx[dir] * maxt), int(cury + dy[dir] * maxt)); continue; } cury -= step; maxt -= step; step = y2[it->second] - cury; if (step >= maxt) { printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt), int(cury + dy[Dir[it->second]] * maxt)); continue; } curx += dx[Dir[it->second]] * step; cury += dy[Dir[it->second]] * step; maxt -= step; pair<int, int> ans = walk(it->second, maxt); printf("%d %d\n", int(ans.first), int(ans.second)); continue; } if (tmp.second == -1) { long long step = min((long long)cury, maxt); printf("%d %d\n", int(curx + dx[dir] * step), int(cury + dy[dir] * step)); continue; } long long step = cury - tmp.first; if (step >= maxt) { printf("%d %d\n", int(curx + dx[dir] * maxt), int(cury + dy[dir] * maxt)); continue; } curx += dx[dir] * step; cury += dy[dir] * step; maxt -= step; step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury); if (step >= maxt) { printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt), int(cury + dy[Dir[tmp.second]] * maxt)); continue; } curx += dx[Dir[tmp.second]] * step; cury += dy[Dir[tmp.second]] * step; maxt -= step; pair<int, int> ans = walk(tmp.second, maxt); printf("%d %d\n", int(ans.first), int(ans.second)); } else if (dir == 2) { pair<int, int> tmp = T[0].query(1, cury, curx, dir); set<pair<int, int> >::iterator it = S[3][cury].lower_bound((make_pair(curx, 0))); if (it != S[3][cury].end() && max(x2[it->second], curx) < tmp.first) { long long step = abs(curx - max(curx, x2[it->second])); if (step >= maxt) { printf("%d %d\n", int(curx + dx[dir] * maxt), int(cury + dy[dir] * maxt)); continue; } curx += step; maxt -= step; step = abs(x2[it->second] - curx); if (step >= maxt) { printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt), int(cury + dy[Dir[it->second]] * maxt)); continue; } curx += dx[Dir[it->second]] * step; cury += dy[Dir[it->second]] * step; maxt -= step; pair<int, int> ans = walk(it->second, maxt); printf("%d %d\n", int(ans.first), int(ans.second)); continue; } if (tmp.second == -1) { long long step = min((long long)N - curx, maxt); printf("%d %d\n", int(curx + dx[dir] * step), int(cury + dy[dir] * step)); continue; } long long step = tmp.first - curx; if (step >= maxt) { printf("%d %d\n", int(curx + dx[dir] * maxt), int(cury + dy[dir] * maxt)); continue; } curx += dx[dir] * step; cury += dy[dir] * step; maxt -= step; step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury); if (step >= maxt) { printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt), int(cury + dy[Dir[tmp.second]] * maxt)); continue; } curx += dx[Dir[tmp.second]] * step; cury += dy[Dir[tmp.second]] * step; maxt -= step; pair<int, int> ans = walk(tmp.second, maxt); printf("%d %d\n", int(ans.first), int(ans.second)); } else { pair<int, int> tmp = T[0].query(1, cury, curx, dir); set<pair<int, int> >::iterator it = S[2][cury].upper_bound((make_pair(curx, inf))); if (it != S[2][cury].begin() && min(x2[(--it)->second], curx) > tmp.first) { long long step = abs(curx - min(x2[it->second], curx)); if (step >= maxt) { printf("%d %d\n", int(curx + dx[dir] * maxt), int(cury + dy[dir] * maxt)); continue; } curx -= step; maxt -= step; step = abs(x2[it->second] - curx); if (step >= maxt) { printf("%d %d\n", int(curx + dx[Dir[it->second]] * maxt), int(cury + dy[Dir[it->second]] * maxt)); continue; } curx += dx[Dir[it->second]] * step; cury += dy[Dir[it->second]] * step; maxt -= step; pair<int, int> ans = walk(it->second, maxt); printf("%d %d\n", int(ans.first), int(ans.second)); continue; } if (tmp.second == -1) { long long step = min((long long)curx, maxt); printf("%d %d\n", int(curx + dx[dir] * step), int(cury + dy[dir] * step)); continue; } long long step = curx - tmp.first; if (step >= maxt) { printf("%d %d\n", int(curx + dx[dir] * maxt), int(cury + dy[dir] * maxt)); continue; } curx += dx[dir] * step; cury += dy[dir] * step; maxt -= step; step = abs(x2[tmp.second] - curx) + abs(y2[tmp.second] - cury); if (step >= maxt) { printf("%d %d\n", int(curx + dx[Dir[tmp.second]] * maxt), int(cury + dy[Dir[tmp.second]] * maxt)); continue; } curx += dx[Dir[tmp.second]] * step; cury += dy[Dir[tmp.second]] * step; maxt -= step; pair<int, int> ans = walk(tmp.second, maxt); printf("%d %d\n", int(ans.first), int(ans.second)); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; int dx[5] = {0, 1, 0, -1, 0}; int dy[5] = {0, 0, 1, 0, -1}; int n, b, q; int a[N + 5][N + 5]; int nx[N * N * 5 + 55][2]; int ans[111111]; long long t[111111]; int hash1(int x, int y, int d) { if (a[x][y]) d = a[x][y]; return d * N * N + x * N + y; } int main() { int i, j, k, x1, y1, x2, y2, d, h; char ch[5]; scanf("%d%d", &n, &b); for (i = 1; i <= n; i = i + 1) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 == x2) { if (y1 > y2) d = 4; else d = 2; } else { if (x1 > x2) d = 3; else d = 1; } while (x1 != x2 || y1 != y2) { a[x1][y1] = d; x1 += dx[d]; y1 += dy[d]; } a[x2][y2] = d; } for (i = 0; i <= b; i = i + 1) for (j = 0; j <= b; j = j + 1) for (k = 1; k <= 4; k = k + 1) { if (a[i][j] && a[i][j] != k) continue; h = hash1(i, j, k); x1 = i + dx[k]; y1 = j + dy[k]; if (x1 < 0 || x1 > b || y1 < 0 || y1 > b) nx[h][0] = h; else nx[h][0] = hash1(x1, y1, k); } scanf("%d", &q); for (i = 1; i <= q; i = i + 1) { scanf("%d%d%s%lld", &x1, &x2, &ch, t + i); if (ch[0] == 'R') d = 1; if (ch[0] == 'U') d = 2; if (ch[0] == 'L') d = 3; if (ch[0] == 'D') d = 4; ans[i] = hash1(x1, x2, d); } for (i = 0; i <= 60; i = i + 1) { h = i & 1; for (j = 1; j <= q; j = j + 1) if (t[j] & ((long long)1 << i)) ans[j] = nx[ans[j]][h]; for (j = 1; j < N * N * 5; j = j + 1) nx[j][h ^ 1] = nx[nx[j][h]][h]; } for (i = 1; i <= q; i = i + 1) { y1 = ans[i] % N; ans[i] /= N; x1 = ans[i] % N; cout << x1 << ' ' << y1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; struct coord { int x, y, dir; }; struct node { int cycle, cyclepos; int len; int visited; }; int main() { int N, B; scanf("%d %d", &N, &B); static struct node a[1005][1005][4]; static int d[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}}; int x, y, dir; static int ch[1005][1005]; for (x = 0; x <= B; x++) { for (y = 0; y <= B; y++) { ch[x][y] = -1; for (dir = 0; dir < 4; dir++) { a[x][y][dir].visited = -1; } } } static struct coord q[1005 * 1005 * 4]; int X0, Y0, X1, Y1; int i, temp; for (i = 0; i < N; i++) { scanf("%d %d %d %d", &X0, &Y0, &X1, &Y1); int ndir = -1; if (X0 == X1) { if (Y0 < Y1) ndir = 0; else ndir = 1; } else { if (X0 < X1) ndir = 3; else ndir = 2; } if (X0 > X1) { temp = X0; X0 = X1; X1 = temp; } if (Y0 > Y1) { temp = Y0; Y0 = Y1; Y1 = temp; } if (X0 == X1) { x = X0; for (y = Y0; y <= Y1; y++) { ch[x][y] = ndir; } } else { y = Y0; for (x = X0; x <= X1; x++) { ch[x][y] = ndir; } } } static int cycles[1005 * 1005 * 4][2]; int cyclesc = 0; int qw, z, st; qw = 0; int x7, y7, dir7; for (x7 = 0; x7 <= B; x7++) { for (y7 = 0; y7 <= B; y7++) { for (dir7 = 0; dir7 < 4; dir7++) { if (a[x7][y7][dir7].visited == -1) { x = x7; y = y7; dir = dir7; st = qw; while (1) { if (a[x][y][dir].visited == -2) { int cycle, cyclepos, len; cycle = a[x][y][dir].cycle; cyclepos = a[x][y][dir].cyclepos; len = a[x][y][dir].len; for (z = qw - 1; z >= st; z--) { a[q[z].x][q[z].y][q[z].dir].cycle = cycle; a[q[z].x][q[z].y][q[z].dir].cyclepos = cyclepos; len++; a[q[z].x][q[z].y][q[z].dir].len = len; a[q[z].x][q[z].y][q[z].dir].visited = -2; } break; } else if (a[x][y][dir].visited != -1) { int cycle, cyclepos, len; len = qw - a[x][y][dir].visited; cycles[cyclesc][0] = a[x][y][dir].visited; cycles[cyclesc][1] = qw; cycle = cyclesc; cyclepos = len; len = 0; int saved = a[x][y][dir].visited; cyclesc++; for (z = qw - 1; z >= saved; z--) { a[q[z].x][q[z].y][q[z].dir].cycle = cycle; cyclepos--; a[q[z].x][q[z].y][q[z].dir].cyclepos = cyclepos; a[q[z].x][q[z].y][q[z].dir].len = 0; a[q[z].x][q[z].y][q[z].dir].visited = -2; } for (z = saved - 1; z >= st; z--) { a[q[z].x][q[z].y][q[z].dir].cycle = cycle; a[q[z].x][q[z].y][q[z].dir].cyclepos = cyclepos; len++; a[q[z].x][q[z].y][q[z].dir].len = len; a[q[z].x][q[z].y][q[z].dir].visited = -2; } break; } a[x][y][dir].visited = qw; q[qw].x = x; q[qw].y = y; q[qw].dir = dir; qw++; if (ch[x][y] != -1) dir = ch[x][y]; x += d[dir][0]; y += d[dir][1]; if ((x < 0) || (x > B) || (y < 0) || (y > B)) { x -= d[dir][0]; y -= d[dir][1]; } } } } } } int Q; scanf("%d", &Q); int iQ; for (iQ = 0; iQ < Q; iQ++) { long long T; char c; scanf("%d %d %c %I64i", &x, &y, &c, &T); if (c == 'U') dir = 0; else if (c == 'D') dir = 1; else if (c == 'L') dir = 2; else dir = 3; if (T < (long long)(a[x][y][dir].len)) { while (1) { if (T == 0LL) break; if (ch[x][y] != -1) dir = ch[x][y]; x += d[dir][0]; y += d[dir][1]; T--; } printf("%d %d\n", x, y); } else { T -= (long long)(a[x][y][dir].len); int clen = cycles[a[x][y][dir].cycle][1] - cycles[a[x][y][dir].cycle][0]; T %= (long long)(clen); int pos = cycles[a[x][y][dir].cycle][0] + ((a[x][y][dir].cyclepos + (int)(T)) % clen); printf("%d %d\n", q[pos].x, q[pos].y); } } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void setmin(T &a, T b) { if (b < a) a = b; } template <class T> void setmax(T &a, T b) { if (b > a) a = b; } template <class T> T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } const int dx[4] = {0, 1, 0, -1}; const int dy[4] = {1, 0, -1, 0}; const char dc[4] = {'U', 'R', 'D', 'L'}; const int MAXN = 100010; const int MAXQ = 100010; const int MAXLog = 20; int ax0[MAXN], ay0[MAXN], ax1[MAXN], ay1[MAXN], avx[MAXN], avy[MAXN]; int nxt[MAXN]; long long dis[MAXN]; int qx[MAXN], qy[MAXN], qd[MAXN]; long long qt[MAXN]; int qnxt[MAXN]; int n, sz, qs; int sgn(int x) { if (x == 0) return 0; else return x > 0 ? 1 : -1; } pair<int, int> lst[MAXN + MAXQ]; int color[MAXN * 4]; void paint(int x, int s, int t, int le, int ri, int c) { if (le <= s && t <= ri) { color[x] = c; return; } int mid = (s + t) / 2; if (color[x] >= 0) { color[x * 2] = color[x * 2 + 1] = color[x]; color[x] = -1; } if (le <= mid) paint(x * 2, s, mid, le, ri, c); if (mid < ri) paint(x * 2 + 1, mid + 1, t, le, ri, c); } int getcolor(int x, int s, int t, int p) { if (color[x] >= 0) return color[x]; int mid = (s + t) / 2; if (p <= mid) return getcolor(x * 2, s, mid, p); else return getcolor(x * 2 + 1, mid + 1, t, p); } void init() { scanf("%d%d", &n, &sz); for (int i = (int)(1); i <= (int)(n); ++i) { scanf("%d%d%d%d", ax0 + i, ay0 + i, ax1 + i, ay1 + i); avx[i] = sgn(ax1[i] - ax0[i]); avy[i] = sgn(ay1[i] - ay0[i]); } scanf("%d", &qs); for (int i = (int)(1); i <= (int)(qs); ++i) { char c[9]; scanf("%d%d%s", qx + i, qy + i, c); cin >> qt[i]; qd[i] = 0; while (dc[qd[i]] != c[0]) ++qd[i]; } for (int dir = 0; dir < (int)(4); ++dir) { for (int i = (int)(1); i <= (int)(n); ++i) { int v0 = ax0[i] * dx[dir] + ay0[i] * dy[dir]; int v1 = ax1[i] * dx[dir] + ay1[i] * dy[dir]; lst[i].first = min(-v0, -v1); lst[i].second = -i; } for (int i = (int)(1); i <= (int)(qs); ++i) { lst[n + i].first = -(qx[i] * dx[dir] + qy[i] * dy[dir]); lst[n + i].second = i; } sort(lst + 1, lst + n + qs + 1); memset(color, 0, sizeof(color)); for (int k = (int)(1); k <= (int)(n + qs); ++k) { int i = -lst[k].second; if (i > 0) { if (avx[i] == dx[dir] && avy[i] == dy[dir]) { int s = ax1[i]; if (dx[dir] != 0) s = ay1[i]; int c = getcolor(1, 1, sz + 1, s + 1); nxt[i] = c; } int l = ax0[i], r = ax1[i]; if (dx[dir] != 0) l = ay0[i], r = ay1[i]; if (l > r) swap(l, r); paint(1, 1, sz + 1, l + 1, r + 1, i); } else { i = -i; if (dir == qd[i]) { int s = qx[i]; if (dx[dir] != 0) s = qy[i]; qnxt[i] = getcolor(1, 1, sz + 1, s + 1); } } } } for (int i = (int)(1); i <= (int)(n); ++i) { if (nxt[i] == 0) { int sid = 0; if (avx[i] + avy[i] > 0) sid = sz; dis[i] = abs(sid - (avx[i] != 0 ? ax1[i] : ay1[i])); } else { int j = nxt[i]; dis[i] = abs(ax1[i] - ax1[j]) + abs(ay1[i] - ay1[j]); } } } long long clen[MAXN]; bool vis[MAXN]; long long tmp[MAXN]; int plnk[MAXN][MAXLog]; long long plen[MAXN][MAXLog]; void getwalk(int t, int x1, int y1, int dx, int dy, int x2, int y2, int &x, int &y) { bool fl = false; if (dx == 0) fl = true, swap(x1, y1), swap(x2, y2), swap(dx, dy); int px = abs(x1 - x2); if (t <= px) { x = x1 + dx * t, y = y1; } else { x = x2, y = y1; t -= px; dy = sgn(y2 - y1); y += dy * t; } if (fl) swap(x, y); } bool onside(int x, int y, int x1, int y1, int x2, int y2) { if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); return x >= x1 && x <= x2 && y >= y1 && y <= y2; } void solve() { memset(clen, 0xff, sizeof(clen)); memset(tmp, 0xff, sizeof(tmp)); memset(vis, false, sizeof(vis)); for (int i = (int)(1); i <= (int)(n); ++i) if (!vis[i]) { tmp[i] = 0; for (int y = i, x = nxt[i];; y = x, x = nxt[x]) { if (x == 0) break; if (vis[x]) break; if (tmp[x] >= 0) { long long len = tmp[y] + dis[y] - tmp[x]; clen[x] = len; for (int k = nxt[x]; k != x; k = nxt[k]) clen[k] = len; break; } tmp[x] = tmp[y] + dis[y]; } for (int x = i; x > 0 && !vis[x]; x = nxt[x]) vis[x] = true; } for (int i = (int)(1); i <= (int)(n); ++i) if (nxt[i] > 0) { plnk[i][0] = nxt[i]; plen[i][0] = dis[i]; } else { plnk[i][0] = i; plen[i][0] = 0; } for (int j = (int)(1); j <= (int)(MAXLog - 1); ++j) for (int i = (int)(1); i <= (int)(n); ++i) { plnk[i][j] = plnk[plnk[i][j - 1]][j - 1]; plen[i][j] = plen[i][j - 1] + plen[plnk[i][j - 1]][j - 1]; } for (int cur = (int)(1); cur <= (int)(qs); ++cur) { int d = qd[cur]; if (qnxt[cur] == 0) { long long x = qx[cur] + dx[d] * qt[cur]; long long y = qy[cur] + dy[d] * qt[cur]; if (x < 0) x = 0; if (x > sz) x = sz; if (y < 0) y = 0; if (y > sz) y = sz; printf("%d %d\n", (int)x, (int)y); continue; } int i = qnxt[cur]; long long l = abs(qx[cur] - ax1[i]) + abs(qy[cur] - ay1[i]); long long t = qt[cur]; if (t <= l) { int x, y, u = dx[d], v = dy[d]; if (onside(qx[cur], qy[cur], ax0[i], ay0[i], ax1[i], ay1[i])) u = avx[i], v = avy[i]; getwalk((int)t, qx[cur], qy[cur], u, v, ax1[i], ay1[i], x, y); printf("%d %d\n", x, y); } else { t -= l; for (int j = MAXLog - 1; j >= 0; --j) if (plen[i][j] <= t) { t -= plen[i][j]; i = plnk[i][j]; } if (clen[i] > 0) t %= clen[i]; for (int j = MAXLog - 1; j >= 0; --j) if (plen[i][j] <= t) { t -= plen[i][j]; i = plnk[i][j]; } int j = nxt[i]; if (j == 0) { long long x = ax1[i] + avx[i] * t; long long y = ay1[i] + avy[i] * t; if (x < 0) x = 0; if (x > sz) x = sz; if (y < 0) y = 0; if (y > sz) y = sz; printf("%d %d\n", (int)x, (int)y); } else { int x, y; getwalk((int)t, ax1[i], ay1[i], avx[i], avy[i], ax1[j], ay1[j], x, y); printf("%d %d\n", x, y); } } } } int main() { init(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0'; } struct point { int x, y; point() {} point(int _x, int _y) : x(_x), y(_y) {} }; const int N = 310000; struct seg { int l, r, col, flag; } t[N << 2]; struct lj { int x1, y1, x2, y2, ty, id, ok; } P[N], QQ[N]; int n, m, nxt[N], rd[N], Q, Tree[N]; long long len[N], cirlen[N], g[N][22]; int f[N][22]; queue<int> Qu; inline int cmp1(const lj &a, const lj &b) { return min(a.x1, a.x2) < min(b.x1, b.x2) || (min(a.x1, a.x2) == min(b.x1, b.x2) && a.ok < b.ok); } inline int cmp2(const lj &a, const lj &b) { return max(a.x1, a.x2) > max(b.x1, b.x2) || (max(a.x1, a.x2) == max(b.x1, b.x2) && a.ok < b.ok); } inline int cmp3(const lj &a, const lj &b) { return min(a.y1, a.y2) < min(b.y1, b.y2) || (min(a.y1, a.y2) == min(b.y1, b.y2) && a.ok < b.ok); } inline int cmp4(const lj &a, const lj &b) { return max(a.y1, a.y2) > max(b.y1, b.y2) || (max(a.y1, a.y2) == max(b.y1, b.y2) && a.ok < b.ok); } inline int cmp5(const lj &a, const lj &b) { return a.id < b.id; } void build(int w, int l, int r) { t[w].l = l; t[w].r = r; t[w].col = t[w].flag = 0; if (l == r) return; int mid = l + r >> 1; build(w << 1, l, mid); build(w << 1 | 1, mid + 1, r); } void push(int w) { if (t[w].flag && t[w].l != t[w].r) { t[w << 1].col = t[w << 1 | 1].col = t[w << 1].flag = t[w << 1 | 1].flag = t[w].flag; t[w].flag = 0; } t[w].flag = 0; } void add(int w, int l, int r, int c) { int ll = l, rr = r; l = min(ll, rr); r = max(ll, rr); if (t[w].l == l && t[w].r == r) { t[w].col = t[w].flag = c; return; } push(w); int mid = t[w].l + t[w].r >> 1; if (r <= mid) add(w << 1, l, r, c); else if (l > mid) add(w << 1 | 1, l, r, c); else add(w << 1, l, mid, c), add(w << 1 | 1, mid + 1, r, c); } int ask(int w, int l) { push(w); if (t[w].l == t[w].r) return t[w].col; int mid = t[w].l + t[w].r >> 1; if (l <= mid) return ask(w << 1, l); else return ask(w << 1 | 1, l); } void Build_Tree() { sort(P + 1, P + m + 1, cmp5); for (int i = 1; i <= m; i++) if (!nxt[i]) nxt[i] = i; for (int i = 1; i <= m; i++) rd[nxt[i]]++; for (int i = 1; i <= m; i++) if (!rd[i]) Qu.push(i); while (Qu.size()) { int x = Qu.front(); Qu.pop(); Tree[x] = 1; rd[nxt[x]]--; if (rd[nxt[x]] == 0) Qu.push(nxt[x]); } for (int i = 1; i <= m; i++) if (!Tree[i]) { int x = i; long long sum = 0; while (nxt[x] != i) { sum += len[x]; x = nxt[x]; } sum += len[x]; x = i; cirlen[x] = sum; while (nxt[x] != i) { cirlen[x] = sum; x = nxt[x]; } } for (int i = 1; i <= m; i++) f[i][0] = nxt[i], g[i][0] = len[i]; for (int j = 1; j <= 20; j++) for (int i = 1; i <= m; i++) f[i][j] = f[f[i][j - 1]][j - 1], g[i][j] = g[i][j - 1] + g[f[i][j - 1]][j - 1]; } void Init() { int c; sort(P + 1, P + m + Q + 1, cmp1); build(1, 0, n); for (int i = 1; i <= m + Q; i++) { if (P[i].ok && P[i].ty == 0) { nxt[P[i].id] = c = ask(1, P[i].y1); if (c == 0) len[P[i].id] = 0; else len[P[i].id] = abs(QQ[c].x2 - P[i].x2) + abs(QQ[c].y2 - P[i].y2); } if (!P[i].ok && P[i].x1 > P[i].x2) { nxt[P[i].id] = c = ask(1, P[i].y1); if (c == 0) len[P[i].id] = 0; else len[P[i].id] = abs(QQ[c].x2 - P[i].x2) + abs(QQ[c].y2 - P[i].y2); } if (!P[i].ok) add(1, P[i].y1, P[i].y2, P[i].id); } sort(P + 1, P + m + Q + 1, cmp2); build(1, 0, n); for (int i = 1; i <= m + Q; i++) { if (P[i].ok && P[i].ty == 1) { nxt[P[i].id] = c = ask(1, P[i].y1); if (c == 0) len[P[i].id] = 0; else len[P[i].id] = abs(QQ[c].x2 - P[i].x2) + abs(QQ[c].y2 - P[i].y2); } if (!P[i].ok && P[i].x1 < P[i].x2) { nxt[P[i].id] = c = ask(1, P[i].y1); if (c == 0) len[P[i].id] = 0; else len[P[i].id] = abs(QQ[c].x2 - P[i].x2) + abs(QQ[c].y2 - P[i].y2); } if (!P[i].ok) add(1, P[i].y1, P[i].y2, P[i].id); } sort(P + 1, P + m + Q + 1, cmp3); build(1, 0, n); for (int i = 1; i <= m + Q; i++) { if (P[i].ok && P[i].ty == 2) { nxt[P[i].id] = c = ask(1, P[i].x1); if (c == 0) len[P[i].id] = 0; else len[P[i].id] = abs(QQ[c].x2 - P[i].x2) + abs(QQ[c].y2 - P[i].y2); } if (!P[i].ok && P[i].y1 > P[i].y2) { nxt[P[i].id] = c = ask(1, P[i].x1); if (c == 0) len[P[i].id] = 0; else len[P[i].id] = abs(QQ[c].x2 - P[i].x2) + abs(QQ[c].y2 - P[i].y2); } if (!P[i].ok) add(1, P[i].x1, P[i].x2, P[i].id); } sort(P + 1, P + m + Q + 1, cmp4); build(1, 0, n); for (int i = 1; i <= m + Q; i++) { if (P[i].ok && P[i].ty == 3) { nxt[P[i].id] = c = ask(1, P[i].x1); if (c == 0) len[P[i].id] = 0; else len[P[i].id] = abs(QQ[c].x2 - P[i].x2) + abs(QQ[c].y2 - P[i].y2); } if (!P[i].ok && P[i].y1 < P[i].y2) { nxt[P[i].id] = c = ask(1, P[i].x1); if (c == 0) len[P[i].id] = 0; else len[P[i].id] = abs(QQ[c].x2 - P[i].x2) + abs(QQ[c].y2 - P[i].y2); } if (!P[i].ok) add(1, P[i].x1, P[i].x2, P[i].id); } Build_Tree(); } int In(int x, int y, int x1, int y1, int x2, int y2) { if (x1 == x2) { if (x == x1 && y >= min(y1, y2) && y <= max(y1, y2)) return 1; else return 0; } if (y1 == y2) { if (y == y1 && x >= min(x1, x2) && x <= max(x1, x2)) return 1; else return 0; } } int solve(int x, int y, int Nxt, long long Len, long long t, int ty) { if (In(x, y, P[Nxt].x1, P[Nxt].y1, P[Nxt].x2, P[Nxt].y2)) ty = P[Nxt].ty; if (t <= Len) { if (ty == 0 || ty == 1) { if (t <= abs(x - P[Nxt].x2)) { if (ty == 0) x -= t; else x += t; printf("%d %d\n", x, y); return 1; } else { t -= abs(x - P[Nxt].x2); x = P[Nxt].x2; if (P[Nxt].ty == 2) y -= t; else y += t; printf("%d %d\n", x, y); return 1; } } if (ty == 2 || ty == 3) { if (t <= abs(y - P[Nxt].y2)) { if (ty == 2) y -= t; else y += t; printf("%d %d\n", x, y); return 1; } else { t -= abs(y - P[Nxt].y2); y = P[Nxt].y2; if (P[Nxt].ty == 0) x -= t; else x += t; printf("%d %d\n", x, y); return 1; } } } return 0; } char ch[10]; long long ti[N]; int main() { scanf("%d%d", &m, &n); for (int i = 1; i <= m; i++) { scanf("%d%d%d%d", &P[i].x1, &P[i].y1, &P[i].x2, &P[i].y2), P[i].id = i; if (P[i].x1 > P[i].x2) P[i].ty = 0; else if (P[i].x1 < P[i].x2) P[i].ty = 1; else if (P[i].y1 > P[i].y2) P[i].ty = 2; else P[i].ty = 3; QQ[i] = P[i]; } scanf("%d", &Q); for (int i = 1; i <= Q; i++) { scanf("%d%d%s%lld", &P[m + i].x1, &P[m + i].y1, ch, &ti[i]); P[m + i].id = m + i; P[m + i].x2 = P[m + i].x1; P[m + i].y2 = P[m + i].y1; P[m + i].ok = 1; if (ch[0] == 'L') P[m + i].ty = 0; else if (ch[0] == 'R') P[m + i].ty = 1; else if (ch[0] == 'U') P[m + i].ty = 3; else P[m + i].ty = 2; } Init(); sort(P + 1, P + m + Q + 1, cmp5); for (int i = 1; i <= Q; i++) { int x = P[m + i].x1, y = P[m + i].y1, ty = P[m + i].ty, Nxt = nxt[m + i]; long long t = ti[i], Len = len[m + i]; int id = m + i; if (Nxt == 0) { if (P[id].ty == 0) { if (t <= x) x -= t; else x = 0; } else if (P[id].ty == 1) { if (t <= n - x) x += t; else x = n; } else if (P[id].ty == 2) { if (t <= y) y -= t; else y = 0; } else { if (t <= n - y) y += t; else y = n; } printf("%d %d\n", x, y); continue; } if (solve(x, y, Nxt, Len, t, ty)) continue; t -= Len; id = Nxt; x = P[Nxt].x2, y = P[Nxt].y2; for (int j = 20; j >= 0; j--) if (Tree[f[id][j]] && g[id][j] <= t) { t -= g[id][j]; id = f[id][j]; } x = P[id].x2, y = P[id].y2; if (Tree[id]) { if (g[id][0] >= t) { solve(x, y, f[id][0], g[id][0], t, P[id].ty); continue; } t -= g[id][0]; id = f[id][0]; x = P[id].x2, y = P[id].y2; } if (cirlen[id] == 0) { if (P[id].ty == 0) { if (t <= x) x -= t; else x = 0; } else if (P[id].ty == 1) { if (t <= n - x) x += t; else x = n; } else if (P[id].ty == 2) { if (t <= y) y -= t; else y = 0; } else { if (t <= n - y) y += t; else y = n; } printf("%d %d\n", x, y); continue; } t %= cirlen[id]; for (int j = 20; j >= 0; j--) if (g[id][j] <= t) { t -= g[id][j]; id = f[id][j]; } x = P[id].x2, y = P[id].y2; solve(x, y, f[id][0], g[id][0], t, P[id].ty); } }
#include <bits/stdc++.h> using namespace std; long long rdtsc() { long long tmp; asm("rdtsc" : "=A"(tmp)); return tmp; } inline int myrand() { return abs((rand() << 15) ^ rand()); } inline int rnd(int x) { return myrand() % x; } const long long INF = (long long)1e16 + 1; const long double EPS = 1e-9; struct SegmTree { const static int maxn = (1 << 17); int a[maxn * 2]; int n; void build(int _n) { for (n = 1; n < _n; n <<= 1) ; assert(n <= maxn); for (int i = 1; i < 2 * n; ++i) a[i] = -1; } int get(int v) { int res = -1; for (v += n; v > 0; v >>= 1) if (a[v] != -1) res = a[v]; return res; } void make(int v, int l, int r, int l0, int r0, int nval) { if (l >= r0 || l0 >= r) return; if (l0 <= l && r <= r0) { a[v] = nval; return; } if (a[v] != -1) a[2 * v] = a[2 * v + 1] = a[v], a[v] = -1; int m = (l + r) / 2; make(2 * v, l, m, l0, r0, nval); make(2 * v + 1, m, r, l0, r0, nval); } void make(int l, int r, int nval) { make(1, 0, n, l, r, nval); } } tree; const int maxn = (int)1e5 + 10 + 1; struct Vertex { int x, y; int g; Vertex() {} Vertex(int _x, int _y, int _g) : x(_x), y(_y), g(_g) {} }; const int maxg = 4; const int go[maxg + 1][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}, {0, 0}}; const char chgo[maxg] = {'R', 'U', 'L', 'D'}; int dir[maxn][2][2]; int dirg[maxn]; struct Event { int type; int x; int l, r; int id; Event() {} Event(int _type, int _x, int _y, int _id) : type(_type), x(_x), l(_y), r(-1), id(_id) { assert(type == 1); } Event(int _type, int _x, int _l, int _r, int _id) : type(_type), x(_x), l(_l), r(_r), id(_id) { assert(type == 2); } inline bool operator<(const Event &e) const { if (x != e.x) return x < e.x; return type > e.type; } }; Event evs[3 * maxn]; int n, b; void rotate(int &x, int &y) { int nx = b - y, ny = x; x = nx, y = ny; } int gov[maxn * 2]; void fillGo(int cnt, Vertex *vs) { for (int rot = 0; rot < maxg; ++rot) { int m = 0; for (int i = 0; i < cnt; ++i) { if (vs[i].g != 2) continue; evs[m++] = Event(1, vs[i].x, vs[i].y, i); } for (int i = 0; i < n; ++i) { if (dir[i][0][0] == dir[i][1][0]) { int l = dir[i][0][1], r = dir[i][1][1]; if (l > r) swap(l, r); evs[m++] = Event(2, dir[i][0][0], l, r, i); } else { assert(dir[i][0][1] == dir[i][1][1]); int x = (dir[i][0][0] < dir[i][1][0]) ? dir[i][0][0] : (dir[i][1][0] + 1); evs[m++] = Event(2, x, dir[i][0][1], dir[i][1][1], i); } } sort(evs, evs + m); tree.build(b + 1); for (int i = 0; i < m; ++i) { if (evs[i].type == 1) { gov[evs[i].id] = tree.get(evs[i].l); } else { assert(evs[i].type == 2); tree.make(evs[i].l, evs[i].r + 1, evs[i].id); } } for (int i = 0; i < cnt; ++i) { rotate(vs[i].x, vs[i].y); vs[i].g += 1; if (vs[i].g == 4) vs[i].g = 0; } for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) { rotate(dir[i][j][0], dir[i][j][1]); } } } } Vertex vs[maxn * 2 * 2]; int start[maxn][2]; int startDir[maxn]; long long startTime[maxn]; int mynext[maxn * 4]; int price[maxn * 4]; const int maxl = 50; int pr[maxl][maxn * 4]; long long cs[maxl][maxn * 4]; int sign(int x) { if (x < 0) return -1; return !!x; } bool solve() { if (scanf("%d%d", &n, &b) < 2) return 0; int cnt = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < 2; ++j) for (int k = 0; k < 2; ++k) scanf("%d", &dir[i][j][k]); int dx = dir[i][1][0] - dir[i][0][0], dy = dir[i][1][1] - dir[i][0][1]; if (dx == 0) { assert(dy != 0); dy /= abs(dy); } else { assert(dy == 0); dx /= abs(dx); } dirg[i] = -1; for (int g = 0; g < maxg; ++g) { if (go[g][0] == dx && go[g][1] == dy) dirg[i] = g; } assert(dirg[i] != -1); vs[cnt++] = Vertex(dir[i][1][0], dir[i][1][1], dirg[i]); } int q; scanf("%d", &q); for (int iter = 0; iter < q; ++iter) { char ch; scanf( "%d%d %c" "%lld", &start[iter][0], &start[iter][1], &ch, &startTime[iter]); startDir[iter] = find(chgo, chgo + maxg, ch) - chgo; assert(startDir[iter] != maxg); vs[cnt++] = Vertex(start[iter][0], start[iter][1], startDir[iter]); } fillGo(cnt, vs); int rcnt = cnt; for (int i = 0; i < cnt; ++i) { if (gov[i] == -1) { mynext[i] = rcnt; if (vs[i].g == 0) { price[i] = b - vs[i].x; } else { if (vs[i].g == 1) price[i] = b - vs[i].y; else { if (vs[i].g == 2) price[i] = vs[i].x; else { assert(vs[i].g == 3); price[i] = vs[i].y; } } } vs[rcnt] = Vertex(vs[i].x + go[vs[i].g][0] * price[i], vs[i].y + go[vs[i].g][1] * price[i], 4); mynext[rcnt] = rcnt; price[rcnt] = 1; ++rcnt; continue; } int crash = gov[i]; mynext[i] = crash; price[i] = abs(vs[crash].x - vs[i].x) + abs(vs[crash].y - vs[i].y); } for (int i = 0; i < rcnt; ++i) { pr[0][i] = mynext[i], cs[0][i] = price[i]; } for (int it = 1; it < maxl; ++it) { for (int v = 0; v < rcnt; ++v) { pr[it][v] = pr[it - 1][pr[it - 1][v]]; cs[it][v] = cs[it - 1][v] + cs[it - 1][pr[it - 1][v]]; cs[it][v] = min(cs[it][v], INF); } } for (int iter = 0; iter < q; ++iter) { int v = iter + n; long long left = startTime[iter]; for (int it = maxl - 1; it >= 0; --it) { if (left >= cs[it][v]) { left -= cs[it][v]; v = pr[it][v]; } } { assert(left <= 3 * b + 5); int x = vs[v].x, y = vs[v].y; int nx = vs[pr[0][v]].x, ny = vs[pr[0][v]].y; if (x != nx && y != ny) { int dist1 = (vs[v].g & 1) ? (ny - y) : (nx - x); x += go[vs[v].g][0] * min((int)left, abs(dist1)); y += go[vs[v].g][1] * min((int)left, abs(dist1)); left -= min((int)left, abs(dist1)); } if (left > 0) { if (nx != x) x += sign(nx - x) * (int)left; if (ny != y) y += sign(ny - y) * (int)left; } printf("%d %d\n", x, y); } } return 1; } int main() { srand(rdtsc()); while (1) { if (!solve()) break; } return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int N = 1e5 + 10; const int inf = 110101010; char a[25][25]; int dp[40][1 << 20], n; bool vis[40][1 << 20]; int dfs(int x, int st) { if (vis[x][st]) return dp[x][st]; int &res = dp[x][st]; vis[x][st] = 1; if (x == n * 2 - 2) res = 0; else { if (x & 1) res = -inf; else res = inf; int mask[30]; memset(mask, 0, sizeof(mask)); int cnt = 0; for (int j = 0; j <= x + 1; j++) { int first = x + 1 - j; int second = j; if (first >= n || second >= n) continue; mask[a[first][second] - 'a'] |= (1 << cnt); cnt++; } for (int i = 0; i <= 25; i++) if (mask[i]) { int xt; if (x + 1 < n) xt = (st | (st << 1)) & mask[i]; else xt = (st | (st >> 1)) & mask[i]; if (!xt) continue; int tmp = 0; if (!i) tmp = 1; else if (i == 1) tmp = -1; if (x & 1) res = max(res, tmp + dfs(x + 1, xt)); else res = min(res, tmp + dfs(x + 1, xt)); } } if (!x) { if (a[0][0] == 'a') res++; else if (a[0][0] == 'b') res--; } return res; } int main() { n = read(); for (int i = 0; i <= n - 1; i++) scanf("%s", a[i]); int res = dfs(0, 1); if (!res) printf("DRAW\n"); else if (res > 0) printf("FIRST\n"); else printf("SECOND\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 25; bool vis[maxn * 2][1 << 20]; int f[maxn * 2][1 << 20], mask[maxn * 2][26], n; char mat[maxn][maxn]; int dfs(int i, int s, int now, bool flag) { if (vis[i][s]) return f[i][s]; vis[i][s] = true; int &ret = f[i][s]; ret = -1e9; if (i == 2 * n - 2) ret = 0; else { for (int(nxt) = (0); (nxt) <= ((26) - 1); (nxt)++) { int ns = mask[i + 1][nxt] & (s | (s << 1)); if (ns) ret = max(ret, -dfs(i + 1, ns, nxt, !flag)); } } if (flag) { if (now == 0) ret++; else if (now == 1) ret--; } else { if (now == 0) ret--; else if (now == 1) ret++; } return ret; } int main() { scanf("%d", &n); for (int(i) = (0); (i) <= ((n)-1); (i)++) scanf("%s", mat[i]); for (int(i) = (0); (i) <= ((2 * n) - 1); (i)++) for (int(j) = (0); (j) <= ((26) - 1); (j)++) for (int(k) = (0); (k) <= ((n)-1); (k)++) { int x = k, y = i - k; if (y >= 0 && y < n && mat[x][y] == j + 'a') mask[i][j] |= 1 << k; } int ans = dfs(0, 1, mat[0][0] - 'a', false); if (ans < 0) puts("FIRST"); else if (ans > 0) puts("SECOND"); else puts("DRAW"); }
#include <bits/stdc++.h> using namespace std; int n; char s[25][25]; int ans; bool vis[40][1 << 20]; int dp[40][1 << 20]; int work(int pos, int st) { if (vis[pos][st]) return dp[pos][st]; int &dq = dp[pos][st]; vis[pos][st] = true; if (pos == 2 * n - 2) { dq = 0; } else { if (pos % 2 == 0) dq = 19950920; else dq = -19950920; int flag[30]; memset(flag, 0, sizeof(flag)); int num = 0; for (int i = 0; i <= pos + 1; ++i) { int x = pos + 1 - i; int y = i; if (x >= n || x < 0 || y >= n || y < 0) continue; flag[s[x][y] - 'a'] |= (1 << num); num++; } for (int i = 0; i < 26; ++i) { if (flag[i]) { int next; if (pos + 1 < n) next = (st | (st << 1)) & flag[i]; else next = (st | (st >> 1)) & flag[i]; if (next == 0) continue; int now = 0; if (i == 0) now = 1; if (i == 1) now = -1; if (pos % 2 == 1) dq = max(dq, now + work(pos + 1, next)); else dq = min(dq, now + work(pos + 1, next)); } } } if (pos == 0) { if (s[0][0] == 'a') dq++; if (s[0][0] == 'b') dq--; } return dq; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%s", s[i]); ans = work(0, 1); if (ans > 0) { printf("FIRST\n"); } else if (ans < 0) { printf("SECOND\n"); } else printf("DRAW\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 20; const int INF = 16843009; int n; char a[N][N]; int memo[N + N][1 << N]; int dp(int d, int mask) { if (d == n + n - 1) return 0; int &ret = memo[d][mask]; if (ret != INF) return ret; ret = d & 1 ? INF : -INF; for (char c = 'a'; c <= 'z'; ++c) { int newMask = 0; for (int i = 0; i <= d; ++i) { int j = d - i; if (j >= n || a[i][j] != c || !((mask >> i) & 1)) continue; newMask |= 1 << i; newMask |= 1 << (i + 1); } if (!newMask) continue; int score = (c == 'a') - (c == 'b'); if (d & 1) ret = min(ret, dp(d + 1, newMask) + score); else ret = max(ret, dp(d + 1, newMask) + score); } return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { scanf(" %c", &a[i][j]); } } memset(memo, 1, sizeof memo); int ans = dp(0, 1); if (ans > 0) puts("FIRST"); else if (ans < 0) puts("SECOND"); else puts("DRAW"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1 << 20; int dp[41][N], mask[41][26], flag[41], pos = 0, b[21][21], n, val[41][21]; bool f[41][N]; int sol(int x, int st) { int &ret = dp[x][st]; if (f[x][st]) return ret; f[x][st] = 1; if (x & 1) ret = -100; else ret = 100; int i, nst = (st | (st << 1)) & flag[x + 1]; for (i = 0; i < 26; ++i) if (mask[x + 1][i] & nst) { if (x & 1) ret = max(ret, sol(x + 1, nst & mask[x + 1][i])); else ret = min(ret, sol(x + 1, nst & mask[x + 1][i])); } ret += val[x][__builtin_ctz(st)]; return ret; } int main() { int i, j, k; cin >> n; string s; for (i = 0; i < n; ++i) { cin >> s; for (j = 0; j < n; ++j) { flag[i + j] |= 1 << j; mask[i + j][s[j] - 'a'] |= 1 << j; val[i + j][j] = 0; if (s[j] == 'a') ++val[i + j][j]; if (s[j] == 'b') --val[i + j][j]; } } int x = 1 << n - 1; f[2 * n - 2][x] = 1; dp[2 * n - 2][x] = val[2 * n - 2][n - 1]; int ret = sol(0, 1); if (ret > 0) cout << "FIRST\n"; else if (ret < 0) cout << "SECOND\n"; else cout << "DRAW\n"; }
#include <bits/stdc++.h> using namespace std; struct Node { vector<Node *> parens; set<pair<int, int>> poslist; vector<Node *> children; int ch; bool filled = false; int ascore = 0; int bscore = 0; }; inline void fill_(char good, char bad, int &(*goodreader)(Node *), int &(*badreader)(Node *), Node *node) { int cfitness = (node->ch == good ? 1 : (node->ch == bad ? -1 : 0)); if (node->children.empty()) { goodreader(node) = cfitness; } else { int fitness = -1000000000; for (Node *child : node->children) { fitness = max(fitness, cfitness + (-badreader(child))); } goodreader(node) = fitness; } } void buildGraph(Node *curr, int n, const vector<vector<char>> &table, map<set<pair<int, int>>, Node *> &ghash) { map<char, set<pair<int, int>>> childPosSet; for (const auto &pos : curr->poslist) { const int &r = pos.first; const int &c = pos.second; if (r < n - 1) { childPosSet[table[r + 1][c]].insert(make_pair(r + 1, c)); } if (c < n - 1) { childPosSet[table[r][c + 1]].insert(make_pair(r, c + 1)); } } for (const auto &elem : childPosSet) { if (ghash.count(elem.second)) { Node *c = ghash.at(elem.second); curr->children.push_back(c); c->parens.push_back(curr); continue; } Node *a = new Node; a->parens.push_back(curr); a->ch = elem.first; a->poslist = elem.second; ghash.insert(make_pair(a->poslist, a)); buildGraph(a, n, table, ghash); curr->children.push_back(a); } } void getLeafs(Node *curr, set<Node *> &visited, set<Node *> &leafs) { if (visited.count(curr)) return; visited.insert(curr); if (curr->children.size()) { for (Node *child : curr->children) { getLeafs(child, visited, leafs); } } else { leafs.insert(curr); } } int &areader(Node *node) { return node->ascore; } int &breader(Node *node) { return node->bscore; } void fill(Node *node) { for (Node *child : node->children) { if (!child->filled) return; } fill_('a', 'b', areader, breader, node); fill_('b', 'a', breader, areader, node); node->filled = true; for (Node *paren : node->parens) { fill(paren); } } int main() { int n; cin >> n; vector<vector<char>> v(n, vector<char>(n, ' ')); for (int r = 0; r < n; ++r) { for (int c = 0; c < n; ++c) { cin >> v[r][c]; } } map<set<pair<int, int>>, Node *> ghash; Node g; g.poslist.insert(make_pair(0, 0)); g.ch = v[0][0]; ghash.insert(make_pair(g.poslist, &g)); buildGraph(&g, n, v, ghash); set<Node *> leafs; { set<Node *> visited; getLeafs(&g, visited, leafs); } for (Node *leaf : leafs) { ; fill(leaf); } if (0 == g.bscore) { cout << "DRAW" << endl; } else if (g.bscore > 0) { cout << "SECOND" << endl; } else { cout << "FIRST" << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n; char mat[40][40]; int dp[42][1 << 20]; int add(int x, int y) { if (mat[x][y] == 'a') return 1; if (mat[x][y] == 'b') return -1; return 0; } inline int solve(int diagonala, int masca) { if (dp[diagonala][masca] != -1) return dp[diagonala][masca]; if (diagonala == 2 * n - 1) return add(n, n); int diagonalaredusa = diagonala; if (diagonalaredusa >= n) diagonalaredusa = 2 * n - diagonala; int mastichar[30]; int xxx, yyy; memset(mastichar, 0, sizeof(mastichar)); for (int i = 0; i <= diagonalaredusa; ++i) if (masca & (1 << i)) { int xx = i + 1; if (diagonala >= n) xx = diagonala - n + i + 1; int yy = diagonala - xx + 1; xxx = xx; yyy = yy; if (xx + 1 <= n) mastichar[mat[xx + 1][yy] - 'a'] |= (1 << (diagonala < n ? i + 1 : i)); if (yy + 1 <= n) mastichar[mat[xx][yy + 1] - 'a'] |= (1 << (diagonala < n ? i : i - 1)); } int sol; if (diagonala & 1) sol = 1 << 30; else sol = -1 << 30; for (int i = 0; i <= 26; ++i) if (mastichar[i]) { if (diagonala & 1) sol = min(sol, solve(diagonala + 1, mastichar[i])); else sol = max(sol, solve(diagonala + 1, mastichar[i])); } sol += add(xxx, yyy); return dp[diagonala][masca] = sol; } int main() { ios ::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; ++i) cin >> (mat[i] + 1); memset(dp, -1, sizeof(dp)); int CET = solve(1, 1); if (CET == 0) cout << "DRAW\n"; else if (CET > 0) cout << "FIRST\n"; else cout << "SECOND\n"; return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0; char ch = getchar(); bool f = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = 1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return f ? -x : x; } void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } void writeln(long long x) { write(x); puts(""); } void writep(long long x) { write(x); putchar(' '); } const int N = 45, M = (1 << 20) + 5, inf = 1 << 30; int n, m, f[N][M], pos[N][N], a[N][N]; char s[N][N]; vector<pair<int, int> > v[N]; bool used[N][M]; int dfs(int u, int s) { if (u == 2 * n - 1) return 0; if (used[u][s]) return f[u][s]; int res = ((u & 1) ? inf : -inf); for (int k = (int)(0); k <= (int)(25); k++) { int t = 0, alb = 0; if (k == 0) t = 1; if (k == 1) t = -1; for (int i = (int)(0); i <= (int)((int)v[u].size() - 1); i++) { if ((s >> i) & 1) { int x = v[u][i].first, y = v[u][i].second; if (x + 1 <= n && a[x + 1][y] == k) alb |= (1 << pos[x + 1][y]); if (y + 1 <= n && a[x][y + 1] == k) alb |= (1 << pos[x][y + 1]); } } if (alb == 0) continue; if (u & 1) res = min(res, t + dfs(u + 1, alb)); else res = max(res, t + dfs(u + 1, alb)); } used[u][s] = 1; return f[u][s] = res; } int main() { n = read(); for (int i = (int)(1); i <= (int)(n); i++) { scanf("%s", s[i] + 1); for (int j = (int)(1); j <= (int)(n); j++) a[i][j] = s[i][j] - 'a'; } for (int i = (int)(1); i <= (int)(n); i++) for (int j = (int)(1); j <= (int)(n); j++) { pos[i][j] = v[i + j - 1].size(); v[i + j - 1].push_back(make_pair(i, j)); } int ans = dfs(1, 1); if (s[1][1] == 'a') ans++; if (s[1][1] == 'b') ans--; if (ans == 0) puts("DRAW"); else if (ans > 0) puts("FIRST"); else puts("SECOND"); }
#include <bits/stdc++.h> using namespace std; inline int myrand() { return ((rand() & ((1 << 15) - 1)) << 15) ^ (rand() & ((1 << 15) - 1)); } char dp[40][1 << 20]; char bio[40][1 << 20]; int n; char mat[21][21]; vector<char> diag[40]; inline int getVal(int x, char c) { if (x & 1) { if (c == 'b') return +1; if (c == 'a') return -1; return 0; } else { if (c == 'a') return +1; if (c == 'b') return -1; return 0; } } int solve(int x, int state) { if (x == 2 * n - 2) { assert(state == 1); return getVal(x, mat[n - 1][n - 1]); } char& ref = dp[x][state]; char& bio_ref = bio[x][state]; if (bio_ref) return ref; bio_ref = true; ref = -50; for (char c = 'a'; c <= 'z'; ++c) { int new_state = 0; int sz_ex = (int)diag[x].size(); int sz_nov = (int)diag[x + 1].size(); for (int i = 0; i < (sz_ex); ++i) { if ((state & (1 << i)) == 0) continue; if (diag[x][i] != c) continue; if (sz_ex < sz_nov) { new_state |= 1 << i; if (i + 1 < sz_nov) new_state |= 1 << (i + 1); } else { if (i < sz_nov) new_state |= 1 << i; if (i - 1 >= 0) new_state |= 1 << (i - 1); } } if (new_state != 0) { int val = getVal(x, c) - solve(x + 1, new_state); if (val >= ref) ref = val; } } return ref; } int main(void) { scanf("%d", &n); for (int i = 0; i < (n); ++i) scanf("%s", mat[i]); for (int i = 0; i < (n); ++i) for (int j = 0; j < (n); ++j) diag[i + j].push_back(mat[i][j]); memset(bio, 0, sizeof bio); int val = solve(0, 1 << 0); if (val > 0) puts("FIRST"); else if (val == 0) puts("DRAW"); else puts("SECOND"); return 0; }
#include <bits/stdc++.h> using namespace std; const int none = 100000; int n; int b[42][42]; int f[42][1 << 20]; string st[42]; void ready() { for (int i = 0; i < n + n; i++) for (int j = 0; j < (1 << n); j++) f[i][j] = none; for (int i = 0; i < n + n - 1; i++) for (int j = 0; j < 26; j++) for (int k = 0; k < n; k++) { int x = k, y = i - x; if (y < 0 || y >= n) continue; if (st[x][y] - 'a' != j) continue; b[i][j] |= 1 << k; } } int dfs(int dep, int set, int c, int first) { if (f[dep][set] != none) return f[dep][set]; int ans = -none; if (dep == 2 * n - 2) ans = 0; else { for (int i = 0; i < 26; i++) { int set2 = b[dep + 1][i] & (set | (set << 1)); if (set2 <= 0) continue; ans = max(ans, -dfs(dep + 1, set2, i, !first)); } } if (first) { if (c == 0) ans++; if (c == 1) ans--; } else { if (c == 0) ans--; if (c == 1) ans++; } return f[dep][set] = ans; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> st[i]; ready(); int ans = dfs(0, 1, st[0][0] - 'a', 0); if (ans < 0) puts("FIRST"); else if (ans == 0) puts("DRAW"); else puts("SECOND"); }
#include <bits/stdc++.h> using namespace std; const int one = 1048576; int n, inf = 1e9, f[40][one], g[30][30]; char s[30][30]; bool check(int x, int y) { return x >= 0 && y >= 0 && x < n && y < n; } int Get(int x, int y, int c) { if (c == 0) { if ((x + y) & 1) return -1; return 1; } if (c == 1) { if ((x + y) & 1) return 1; return -1; } return 0; } void read(int &x) { char ch = getchar(); int mark = 1; for (; ch != '-' && (ch < '0' || ch > '9'); ch = getchar()) ; if (ch == '-') mark = -1, ch = getchar(); for (x = 0; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - 48; x *= mark; } int Dp(int u, int sta) { if (u == n * 2 - 2) return g[n - 1][n - 1]; if (f[u][sta] != -inf) return f[u][sta]; int tmp; for (int j = 0; j < n; j++) if ((sta >> j) & 1) { tmp = g[j][u - j]; break; } for (int i = 0; i < 26; i++) { int New = 0; for (int j = 0; j < n; j++) if ((sta >> j) & 1) { int x = j, y = u - j; if (check(x + 1, y) && s[x + 1][y] == 'a' + i) New |= 1 << (x + 1); if (check(x, y + 1) && s[x][y + 1] == 'a' + i) New |= 1 << x; } if (New) f[u][sta] = max(f[u][sta], Dp(u + 1, New)); } return f[u][sta] = tmp - f[u][sta]; } int main() { read(n); for (int i = 0; i < n; i++) scanf("%s", s[i]); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g[i][j] = Get(i, j, s[i][j] - 'a'); for (int i = 0; i < n * 2; i++) for (int j = 0; j < (1 << n); j++) f[i][j] = -inf; int t = Dp(0, 1); if (t > 0) printf("FIRST\n"); else if (t < 0) printf("SECOND\n"); else printf("DRAW\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 20; char c[MAXN + 10][MAXN + 10]; char mem[2 * MAXN][1 << MAXN], dp[2 * MAXN][1 << MAXN]; int n; int lmask[2 * MAXN][30]; char val(char x) { if (x == 0) { return 1; } else if (x == 1) { return -1; } return 0; } char rec(int sum, int mask) { if (sum == 2 * (n - 1)) { return val(c[n - 1][n - 1]); } if (mem[sum][mask]) { return dp[sum][mask]; } mem[sum][mask] = 1; char add = 0; char &res = dp[sum][mask]; for (int i = 0; i < (n); ++i) { if (mask & (1 << i)) { add = val(c[i][sum - i]); break; } } int tm = mask | (mask << 1); if (sum % 2 == 0) { res = 100; for (char z = 0; z < 26; ++z) { int nmask = lmask[sum + 1][z] & tm; if (nmask != 0) { res = min(res, (char)(rec(sum + 1, nmask) + add)); } } } else { res = -100; for (char z = 0; z < 26; ++z) { int nmask = lmask[sum + 1][z] & tm; if (nmask != 0) { res = max(res, (char)(rec(sum + 1, nmask) + add)); } } } return res; } int main(void) { scanf("%d", &n); for (int i = 0; i < (n); ++i) { scanf("%s", c[i]); for (int j = 0; j < (n); ++j) { c[i][j] -= 'a'; } } for (int sum = 0; sum <= 2 * (n - 1); ++sum) { for (int i = 0; i < n; ++i) { int j = sum - i; if (j < 0 || j >= n) { continue; } lmask[sum][c[i][j]] |= (1 << i); } } char res = rec(0, 1); if (res > 0) { puts("FIRST"); } else if (res < 0) { puts("SECOND"); } else { puts("DRAW"); } return 0; }
#include <bits/stdc++.h> using namespace std; int n; string s[22]; int id[22][22]; vector<pair<int, int> > v[44]; map<int, int> r[44]; int go(int pos, int mask) { if (pos == n + n - 2) return 0; if (r[pos].count(mask)) return r[pos][mask]; vector<int> t; for (int i = (0); i < (20); i++) if (mask & (1 << i)) t.push_back(i); int m[26]; memset(m, 0, sizeof m); for (int i = (0); i < (t.size()); i++) { int px = v[pos][t[i]].first; int py = v[pos][t[i]].second; if (px + 1 < n) m[s[px + 1][py] - 'a'] |= (1 << id[px + 1][py]); if (py + 1 < n) m[s[px][py + 1] - 'a'] |= (1 << id[px][py + 1]); } int best = (pos % 2 == 0 ? 100 : -100); for (int i = (0); i < (26); i++) if (m[i]) { int diff = go(pos + 1, m[i]); if (i == 0) diff++; if (i == 1) diff--; if (pos % 2 == 0) best = min(best, diff); else best = max(best, diff); } return r[pos][mask] = best; } int main() { cin >> n; for (int i = (0); i < (n); i++) cin >> s[i]; for (int i = (0); i < (n); i++) for (int j = (0); j < (n); j++) { id[i][j] = v[i + j].size(); v[i + j].push_back(pair<int, int>(i, j)); } int d = go(0, 1) + (s[0][0] == 'a' ? 1 : (s[0][0] == 'b' ? -1 : 0)); if (d == 0) puts("DRAW"); else if (d > 0) puts("FIRST"); else puts("SECOND"); return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; int n; char s[22][22]; int rmh[22 * 2][30]; const int p = 107; int gg[p], ris[44], flag[44]; void prep() { int ff[22]; ff[0] = 1; for (int i = 1; i < 22; i++) { ff[i] = ff[i - 1] * 2 % p; } for (int i = 0; i < 22; i++) gg[ff[i]] = i; for (int i = 0; i < n; i++) ris[i] = (1 << (i + 1)) - 1; for (int i = n; i < 2 * n - 1; i++) ris[i] = (1 << n) - (1 << (i - n + 1)); flag[0] = 1; flag[1] = -1; } int func(const int x) { return gg[(x & -x) % p]; } void news(int &x) { x -= x & -x; } map<pair<int, int>, int> mp; int dfs(int k, int cur) { if (k == 2 * n - 2) return 0; if (mp.count(make_pair(k, cur))) return mp[make_pair(k, cur)]; int rlt, that = (cur | (cur << 1)); if (k % 2) { rlt = -INF; for (int c = 0, now; c < 26; c++) { now = that & rmh[k + 1][c]; if (!now) continue; rlt = max(rlt, flag[c] + dfs(k + 1, now)); } } else { rlt = INF; for (int c = 0, now; c < 26; c++) { now = that & rmh[k + 1][c]; if (!now) continue; rlt = min(rlt, flag[c] + dfs(k + 1, now)); } } mp[make_pair(k, cur)] = rlt; return rlt; } int main() { scanf("%d\n", &n); prep(); for (int i = 0; i < n; i++) gets(s[i]); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) s[i][j] -= 'a'; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) rmh[i + j][s[i][j]] |= (1 << i); int ans = flag[s[0][0]] + dfs(0, 1); if (ans > 0) puts("FIRST"); else if (ans < 0) puts("SECOND"); else puts("DRAW"); return 0; }
#include <bits/stdc++.h> using namespace std; int dfs(vector<map<pair<int, int>, int>> &dp, vector<string> &T, vector<vector<pair<int, int>>> &D, vector<vector<int>> &id, int d, int s, int c) { int N = T.size(); if (d == N * 2 - 2) { if (c > 0) { return 1; } if (c < 0) { return -1; } if (c == 0) { return 0; } } else { if (dp[d].count(make_pair(s, c)) == 1) { return dp[d][make_pair(s, c)]; } vector<int> nxt(26, 0); int cnt = D[d].size(); for (int i = 0; i < cnt; i++) { if ((s >> i & 1) == 1) { int x = D[d][i].first; int y = D[d][i].second; if (x < N - 1) { int ch = T[x + 1][y] - 'a'; nxt[ch] |= 1 << id[x + 1][y]; } if (y < N - 1) { int ch = T[x][y + 1] - 'a'; nxt[ch] |= 1 << id[x][y + 1]; } } } vector<int> p; for (int i = 0; i < 26; i++) { if (nxt[i] != 0) { if (i == 0) { p.push_back(dfs(dp, T, D, id, d + 1, nxt[i], c + 1)); } else if (i == 1) { p.push_back(dfs(dp, T, D, id, d + 1, nxt[i], c - 1)); } else { p.push_back(dfs(dp, T, D, id, d + 1, nxt[i], c)); } } } sort(p.begin(), p.end()); int ans; if (d % 2 == 1) { ans = p.back(); } else { ans = p.front(); } dp[d][make_pair(s, c)] = ans; return ans; } } int main() { int n; cin >> n; vector<string> T(n); for (int i = 0; i < n; i++) { cin >> T[i]; } vector<vector<pair<int, int>>> D(n * 2 - 1); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { D[i + j].push_back(make_pair(i, j)); } } vector<int> cnt(n * 2 - 1); for (int i = 0; i < n * 2 - 1; i++) { cnt[i] = D[i].size(); } vector<vector<int>> id(n, vector<int>(n)); for (int i = 0; i < n * 2 - 1; i++) { for (int j = 0; j < cnt[i]; j++) { int x = D[i][j].first; int y = D[i][j].second; id[x][y] = j; } } vector<map<pair<int, int>, int>> dp(n * 2 - 1); int ans; if (T[0][0] == 'a') { ans = dfs(dp, T, D, id, 0, 1, 1); } else if (T[0][0] == 'b') { ans = dfs(dp, T, D, id, 0, 1, -1); } else { ans = dfs(dp, T, D, id, 0, 1, 0); } if (ans == 1) { cout << "FIRST" << endl; } if (ans == -1) { cout << "SECOND" << endl; } if (ans == 0) { cout << "DRAW" << endl; } }
#include <bits/stdc++.h> using namespace std; map<pair<int, int>, int> mymap; int n; string s[100]; int ok(int i, int j, int c) { if (i < 0) return false; if (j < 0) return false; if (i >= n) return false; if (j >= n) return false; return s[i][j] == c; } int get(int i, int mask) { if (mymap.find(make_pair(i, mask)) != mymap.end()) return mymap[make_pair(i, mask)]; if (i == 2 * n - 2) return mymap[make_pair(i, mask)] = 0; int t = i % 2; int ret = (t ? -1000 : 1000); for (char c = 'a'; c <= 'z'; ++c) { int nm = 0; for (int j = 0; j < n; ++j) { int k = i - j; if (k >= n) continue; if (k < 0) continue; if ((1 << j) & mask) { if (ok(j + 1, k, c)) nm |= 1 << (j + 1); if (ok(j, k + 1, c)) nm |= 1 << j; } } if (nm) { int now = get(i + 1, nm) + (c == 'a') - (c == 'b'); if (t) ret = max(ret, now); else ret = min(ret, now); } } return mymap[make_pair(i, mask)] = ret; } int main() { while (cin >> n) { mymap.clear(); for (int i = 0; i < n; ++i) cin >> s[i]; int ret = get(0, 1) + (s[0][0] == 'a') - (s[0][0] == 'b'); if (ret > 0) cout << "FIRST" << endl; else if (ret < 0) cout << "SECOND" << endl; else cout << "DRAW" << endl; } }
#include <bits/stdc++.h> using namespace std; namespace mempool { char pool[300000000]; int ptr = 0; char *allocate(int size) { char *ret = &pool[ptr]; ptr += size; return ret; } } // namespace mempool char *dp[41], g[30][30]; int n; int solve(int level, int state, int lead) { if (level == n * 2 - 1) { if (lead < 0) return 3; if (lead == 0) return 2; if (lead > 0) return 1; } if (dp[level][state * 82 + lead + 40] != 0) return dp[level][state * 82 + lead + 40]; int ret = 1; int len; if (level >= n) len = n * 2 - level; else len = level; int d1, d2; if (level >= n) d1 = 0; else d1 = 1; if (level >= n) d2 = -1; else d2 = 0; int tt = 0; int next[26]; memset(next, 0, sizeof next); for (int i = (1); i <= (n); i++) { int j = level + 1 - i; if (j < 1 || j > n) continue; tt++; if (!(state & (1 << (tt - 1)))) continue; if (i + 1 <= n) next[g[i + 1][j] - 'a'] |= (1 << (tt + d1 - 1)); if (j + 1 <= n) next[g[i][j + 1] - 'a'] |= (1 << (tt + d2 - 1)); } for (int i = (0); i <= (25); i++) { int del = 0; if (i == 0) del = 1; if (i == 1) del = -1; if (next[i]) ret = max(ret, 4 - solve(level + 1, next[i], lead + del)); } dp[level][state * 82 + lead + 40] = ret; return ret; } void lemon() { scanf("%d", &n); for (int i = (1); i <= (n); i++) scanf("%s", g[i] + 1); for (int i = (1); i <= (n); i++) dp[i] = mempool::allocate((1 << i) * 82); for (int i = (n + 1); i <= (n * 2 - 1); i++) dp[i] = mempool::allocate((1 << (n * 2 - i)) * 82); int del = 0; if (g[1][1] == 'a') del = 1; if (g[1][1] == 'b') del = -1; int t = solve(1, 1, del); if (t == 3) printf("SECOND\n"); if (t == 2) printf("DRAW\n"); if (t == 1) printf("FIRST\n"); } int main() { ios::sync_with_stdio(true); lemon(); return 0; }
#include <bits/stdc++.h> using namespace std; const int max_n = 20, inf = 100; char* dp[max_n * 2 - 1]; bool* vis[max_n * 2 - 1]; char f[max_n][max_n]; string s[max_n * 2]; int n; void find_vis() { for (int i = 0; i + 1 < 2 * n; ++i) { dp[i] = new char[1 << s[i].size()]; vis[i] = new bool[1 << s[i].size()]; memset(vis[i], 0, 1 << s[i].size()); } vis[0][1] = 1; for (int i = 0; i + 2 < 2 * n; ++i) { int len = s[i].size(); int nlen = s[i + 1].size(); bool stepa = i % 2; for (int mask = 1; mask < (1 << len); ++mask) { if (!vis[i][mask]) continue; for (char ns : s[i + 1]) { int nxt_mask = 0; for (int k = 0; k < s[i].size(); ++k) { if ((mask & (1 << k)) == 0) continue; if (s[i + 1].size() > s[i].size()) { if (s[i + 1][k] == ns) { nxt_mask |= (1 << k); } if (s[i + 1][k + 1] == ns) { nxt_mask |= (1 << (k + 1)); } } else { if (k && s[i + 1][k - 1] == ns) { nxt_mask |= (1 << (k - 1)); } if (k + 1 < len && s[i + 1][k] == ns) { nxt_mask |= (1 << k); } } } if (nxt_mask == 0) continue; if (!vis[i + 1][nxt_mask]) { vis[i + 1][nxt_mask] = 1; } } } } } int main() { cin >> n; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> f[i][j]; } } for (int j = 0; j < n; ++j) { for (int i = 0; i < n; ++i) { s[i + j] += f[i][j]; } } find_vis(); if (f[n - 1][n - 1] == 'a') { dp[2 * n - 2][1] = 1; } else if (f[n - 1][n - 1] == 'b') { dp[2 * n - 2][1] = -1; } else { dp[2 * n - 2][1] = 0; } bool stepa = false; for (int i = 2 * n - 3; i >= 0; --i) { int len = s[i].size(); int nlen = s[i + 1].size(); stepa = !stepa; for (int mask = 1; mask < (1 << len); ++mask) { if (!vis[i][mask]) continue; dp[i][mask] = stepa ? -inf : inf; char cs; for (int k = 0; k < len; ++k) { if (mask & (1 << k)) { cs = s[i][k]; } } for (char ns : s[i + 1]) { int nxt_mask = 0; for (int k = 0; k < len; ++k) { if ((mask & (1 << k)) == 0) continue; if (s[i + 1].size() > s[i].size()) { if (s[i + 1][k] == ns) { nxt_mask |= (1 << k); } if (s[i + 1][k + 1] == ns) { nxt_mask |= (1 << (k + 1)); } } else { if (k && s[i + 1][k - 1] == ns) { nxt_mask |= (1 << (k - 1)); } if (k + 1 < len && s[i + 1][k] == ns) { nxt_mask |= (1 << k); } } } if (!vis[i + 1][nxt_mask]) continue; int val = dp[i + 1][nxt_mask]; if (cs == 'a') ++val; else if (cs == 'b') --val; if (stepa) { dp[i][mask] = max(dp[i][mask], (char)val); } else { dp[i][mask] = min(dp[i][mask], (char)val); } } } } if (dp[0][1] > 0) { cout << "FIRST"; } else if (dp[0][1] < 0) { cout << "SECOND"; } else { cout << "DRAW"; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T, typename S> ostream &operator<<(ostream &os, const pair<T, S> &p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type * = nullptr> ostream &operator<<(ostream &os, const C &c) { bool f = true; os << "["; for (const auto &x : c) { if (!f) os << ", "; f = false; os << x; } return os << "]"; } template <typename T> void debug(string s, T x) { cerr << s << " = " << x << "\n"; } template <typename T, typename... Args> void debug(string s, T x, Args... args) { for (int i = 0, b = 0; i < (int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) { cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break; } } const int MAXN = 20; const int INF = 1e9; int n, dp[2 * MAXN][1 << MAXN]; vector<pair<int, int>> pos[2 * MAXN][26]; int solve(int d, int mask, int cur) { int &ret = dp[d][mask]; if (ret != -1) return ret; if (d == 2 * n - 2) return ret = cur == 0 ? 1 : cur == 1 ? -1 : 0; ret = d % 2 ? -INF : INF; for (int nxt = 0; nxt < 26; nxt++) { int nmask = 0; for (auto [r, c] : pos[d + 1][nxt]) if ((r > 0 && mask & (1 << (r - 1))) || (c > 0 && mask & (1 << r))) nmask |= 1 << r; if (nmask != 0) { if (d % 2) ret = max(ret, solve(d + 1, nmask, nxt)); else ret = min(ret, solve(d + 1, nmask, nxt)); } } return ret += cur == 0 ? 1 : cur == 1 ? -1 : 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; int st = -1; for (int i = 0; i < n; i++) { string s; cin >> s; if (i == 0) st = s[0] - 'a'; for (int j = 0; j < n; j++) pos[i + j][s[j] - 'a'].emplace_back(i, j); } memset(dp, -1, sizeof(dp)); int ret = solve(0, 1, st); if (ret > 0) cout << "FIRST\n"; else if (ret == 0) cout << "DRAW\n"; else cout << "SECOND\n"; return 0; }
#include <bits/stdc++.h> using namespace std; char s[22][22]; int n; int dp[42][1 << 20], nxt[42][26]; bool v[42][1 << 20]; int dfs(int step, int state) { if (v[step][state]) return dp[step][state]; v[step][state] = 1; int add = 0; for (int i = 0; i < n; i++) { if (state & (1 << i)) { if (s[i][step - i] == 'a') add++; else if (s[i][step - i] == 'b') add--; break; } } if (step == (n - 1) * 2) return dp[step][state] = add; if (step % 2) dp[step][state] = -0x7fffffff; else dp[step][state] = 0x7fffffff; for (int i = 0; i < 26; i++) { int next = nxt[step + 1][i] & (state | (state << 1)); if (next == 0) continue; dp[step][state] = (step % 2) ? (max(dp[step][state], dfs(step + 1, next) + add)) : (min(dp[step][state], dfs(step + 1, next) + add)); } return dp[step][state]; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%s", s[i]); for (int i = 0; i < n * 2 - 1; i++) for (int j = 0; j < n; j++) if (i - j >= 0 && i - j < n) nxt[i][s[j][i - j] - 'a'] |= (1 << j); int ans = dfs(0, 1); puts((ans == 0) ? "DRAW" : ((ans > 0) ? "FIRST" : "SECOND")); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 22; const int M = 1000010; const int inf = 0x3f3f3f3f; const long long oo = 0x3f3f3f3f3f3f3f3fll; const int mod = 1e9 + 7; int n; char S[N][N]; int dp[42][1 << 20]; bool vis[42][1 << 20]; int dfs(int l, int s) { if (l == n + n - 2) return 0; int &ret = dp[l][s]; if (vis[l][s]) return ret; vis[l][s] = 1; int ns[26] = {}; if (l < n - 1) { for (int i = 0; i <= l; i++) { if (!(s & (1 << i))) continue; int x = l - i, y = i; if (x < n - 1) ns[S[x + 1][y] - 'a'] |= 1 << i; if (y < n - 1) ns[S[x][y + 1] - 'a'] |= 1 << (i + 1); } } else { for (int i = 0; i < 2 * n - 1 - l; i++) { if (!(s & (1 << i))) continue; int x = n - 1 - i, y = l - (n - 1) + i; if (x < n - 1) ns[S[x + 1][y] - 'a'] |= 1 << (i - 1); if (y < n - 1) ns[S[x][y + 1] - 'a'] |= 1 << i; } } if (l & 1) { ret = -inf; for (int i = 0; i < 26; i++) { if (ns[i] == 0) continue; ret = max(ret, dfs(l + 1, ns[i]) + (i == 0) - (i == 1)); } } else { ret = inf; for (int i = 0; i < 26; i++) { if (ns[i] == 0) continue; ret = min(ret, dfs(l + 1, ns[i]) + (i == 0) - (i == 1)); } } return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%s", S[i]); int sum = dfs(0, 1) + (S[0][0] == 'a') - (S[0][0] == 'b'); if (sum > 0) puts("FIRST"); else if (sum == 0) puts("DRAW"); else puts("SECOND"); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const double eps = 1e-9; const double INF = inf; const double EPS = eps; int D[42][(1 << 20) + 100]; int N[30]; char s[23][23]; int main() { int i, j, add, n, k, x, y; for (i = 0; i < 42; i++) for (j = 0; j < (1 << 20) + 100; j++) D[i][j] = inf; scanf("%d ", &n); for (i = 0; i < n; i++) gets(s[i]); D[0][1] = -inf; for (i = 0; i < 2 * n - 2; i++) { int plen = min(i + 1, 2 * n - 1 - i), poff = max(0, i - n + 1); int noff = max(0, i - n + 2); for (j = 1; j < (1 << plen); j++) if (D[i][j] == -inf) { memset(N, 0, sizeof(N)); for (k = 0; k < plen; k++) if (j & (1 << k)) { y = poff + k, x = i - y; if (x + 1 < n) N[(int)(s[x + 1][y] - 'a')] |= (1 << (y - noff)); if (y + 1 < n) N[(int)(s[x][y + 1] - 'a')] |= (1 << (y + 1 - noff)); } for (k = 0; k < 26; k++) { add = 0; if (k < 2) (k ^ (i & 1)) ? (add = 1) : (add = -1); if (N[k] != 0) D[i + 1][N[k]] = -inf; } } } if (s[n - 1][n - 1] == 'a') D[2 * n - 2][1] = 1; if (s[n - 1][n - 1] == 'b') D[2 * n - 2][1] = -1; if (s[n - 1][n - 1] != 'a' && s[n - 1][n - 1] != 'b') D[2 * n - 2][1] = 0; int tmp; for (i = 2 * n - 3; i >= 0; i--) { int plen = min(i + 1, 2 * n - 1 - i), poff = max(0, i - n + 1); int noff = max(0, i - n + 2); for (j = 1; j < (1 << plen); j++) if (D[i][j] == -inf) { add = -2; memset(N, 0, sizeof(N)); for (k = 0; k < plen; k++) if (j & (1 << k)) { y = poff + k, x = i - y; if (add == -2) { add = 0, tmp = s[x][y] - 'a'; if (tmp < 2) ((i & 1) ^ tmp) ? (add = 1) : (add = -1); } if (x + 1 < n) N[(int)(s[x + 1][y] - 'a')] |= (1 << (y - noff)); if (y + 1 < n) N[(int)(s[x][y + 1] - 'a')] |= (1 << (y + 1 - noff)); } for (k = 0; k < 26; k++) if (N[k] != 0) D[i][j] = max(D[i][j], D[i + 1][N[k]]); D[i][j] += add; D[i][j] *= -1; } } if (D[0][1] == 0) puts("DRAW"); if (D[0][1] > 0) puts("FIRST"); if (D[0][1] < 0) puts("SECOND"); ; return 0; }
#include <bits/stdc++.h> using namespace std; int n; string T[22]; int s[22][22]; int dy[] = {0, 1}; int score[128]; vector<int> dp[40]; vector<int> Y[40], X[40]; int f(int yx, int mask) { int& res = dp[yx][mask]; if (res != 1070000000LL) return res; if (yx == 2 * n - 2) return res = 0; if (yx & 1) res = -1070000000LL; else res = 1070000000LL; int convY[22]; fill(convY, convY + n, -1); for (int i = 0; i < (((int)(Y[yx]).size())); i++) { convY[Y[yx][i]] = i; } for (int ch = 'a'; ch < ('z' + 1); ch++) { int nmask = 0; for (int i = 0; i < (((int)(Y[yx + 1]).size())); i++) { int y = Y[yx + 1][i], x = X[yx + 1][i]; if (T[y][x] == ch) { int OK = 0; for (int dir = 0; dir < (2); dir++) { int py = y - dy[dir]; if (py >= 0 && convY[py] >= 0 && (mask >> convY[py] & 1)) { OK = 1; break; } } if (OK) nmask |= 1 << i; } } if (nmask != 0) { if (yx & 1) res = max(res, score[ch] + f(yx + 1, nmask)); else res = min(res, score[ch] + f(yx + 1, nmask)); } } return res; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < (n); i++) cin >> T[i]; for (int i = 0; i < (n); i++) for (int j = 0; j < (n); j++) { s[i][j] = (T[i][j] == 'a' ? 1 : (T[i][j] == 'b' ? -1 : 0)); } for (int i = 0; i < (n); i++) dp[i].resize(1 << (i + 1), 1070000000LL); for (int i = 0; i < (n - 1); i++) dp[2 * n - 2 - i].resize(1 << (i + 1), 1070000000LL); for (int i = 0; i < (n); i++) for (int j = 0; j < (n); j++) { Y[i + j].push_back(i); X[i + j].push_back(j); } score['a'] = 1; score['b'] = -1; int res = s[0][0] + f(0, 1); cerr << res << endl; cout << (res > 0 ? "FIRST" : (res < 0 ? "SECOND" : "DRAW")) << endl; }
#include <bits/stdc++.h> using namespace std; const double pi = 3.14159265358979323846264338327950288419716939937511; const double eps = 1e-9; char ch_ch_ch[1 << 20]; inline string gs() { scanf("%s", ch_ch_ch); return string(ch_ch_ch); } inline string gl() { gets(ch_ch_ch); return string(ch_ch_ch); } inline int gi() { int x; scanf("%d", &x); return x; } const int inf = 1000000000; int n; vector<string> a; signed char dp[41][1 << 20]; char was[41][1 << 20]; int newmask(int diag, int mask, char c) { int res = 0; if (diag < n) for (int x = diag, y = 0; x >= 0; --x, ++y) { if (mask & (1 << x)) { if (x + 1 < n && a[x + 1][y] == c) res |= (1 << (x + 1)); if (y + 1 < n && a[x][y + 1] == c) res |= (1 << (x)); } } else for (int x = n - 1, y = diag - n + 1; y < n; --x, ++y) { if (mask & (1 << x)) { if (x + 1 < n && a[x + 1][y] == c) res |= (1 << (x + 1)); if (y + 1 < n && a[x][y + 1] == c) res |= (1 << (x)); } } return res; } signed char R(int diag, int mask) { if (was[diag][mask]) return dp[diag][mask]; was[diag][mask] = 1; signed char& r = dp[diag][mask]; if (diag == 2 * n - 2) return r = 0; if (diag & 1) { r = -100; for (char c = 'a'; c <= 'z'; ++c) { int nmask = newmask(diag, mask, c); if (nmask) r = max((int)r, (c == 'a') - (c == 'b') + R(diag + 1, nmask)); } } else { r = 100; for (char c = 'a'; c <= 'z'; ++c) { int nmask = newmask(diag, mask, c); if (nmask) r = min((int)r, (c == 'a') - (c == 'b') + R(diag + 1, nmask)); } } return r; } void solution() { n = gi(); a.resize(n); for (int i = 0; i < (n); ++i) a[i] = gs(); memset(dp, -1, sizeof(dp)); signed char res = (a[0][0] == 'a') - (a[0][0] == 'b') + R(0, 1); if (res > 0) printf("FIRST"); if (res < 0) printf("SECOND"); if (res == 0) printf("DRAW"); cout << endl; } int main(int argc, char** argv) { solution(); return 0; }
#include <bits/stdc++.h> using namespace std; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; const int inf = 0x7f7f7f7f; const int mod = 1000000007; const double eps = 1e-8; const int R = 100007; char s[22][22]; int n; int dp[42][1 << 20]; int a[42][26]; int ans; int DP(int step, int state) { if (dp[step][state] != 1e9) return dp[step][state]; int add = 0; for (int i = 0; i < n; ++i) { if (state & (1 << i)) { if (s[i][step - i] == 'a') add++; else if (s[i][step - i] == 'b') add--; break; } } if (step == 2 * n - 2) return dp[step][state] = add; if (step % 2 == 0) { for (int i = 0; i < 26; ++i) { int next = a[step + 1][i] & (state | (state << 1)); if (next == 0) continue; dp[step][state] = min(dp[step][state], DP(step + 1, next) + add); } } else { dp[step][state] = -1e9; for (int i = 0; i < 26; ++i) { int next = a[step + 1][i] & (state | (state << 1)); if (next == 0) continue; dp[step][state] = max(dp[step][state], DP(step + 1, next) + add); } } return dp[step][state]; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%s", s[i]); memset(a, 0, sizeof(a)); for (int i = 0; i < 2 * n - 1; ++i) { for (int j = 0; j < n; ++j) { if (i - j < 0 || i - j >= n) continue; a[i][s[j][i - j] - 'a'] |= (1 << j); } } for (int i = 0; i < 2 * n - 1; ++i) for (int j = 0; j < (1 << 20); ++j) dp[i][j] = 1e9; int ans = DP(0, 1); if (ans == 0) printf("DRAW\n"); else if (ans > 0) printf("FIRST\n"); else printf("SECOND\n"); return 0; }
#include <bits/stdc++.h> using namespace std; char A[22][22], res[44][22]; int n, dp[44][1111111], appear[44][33]; int main() { cin >> n; for (int i = 1; i <= n; ++i) scanf("%s", A[i] + 1); memset(dp, -(0x3f3f3f3f), sizeof dp); dp[n + n - 1][1] = 1 - (A[n][n] != 'b') - (A[n][n] == 'a'); res[n + n - 1][1] = A[n][n]; appear[n + n - 1][A[n][n] - 'a'] = true; for (int i = n + n - 2; i >= 1; --i) { int sz = n - abs(i - (n)); int sx = n, sy = 1; if (i < n) sx = i; else sy = i - n + 1; for (int j = 1; j <= sz; ++j) { res[i][j] = A[sx][sy]; sx -= 1; sy += 1; appear[i][res[i][j] - 'a'] = true; } for (int S = 1; S <= (1 << sz) - 1; ++S) { dp[i][S] = -(0x3f3f3f3f); int pre = -1; bool valid = true; for (int j = 1; j <= sz; ++j) if (S >> (j - 1) & 1) { if (pre == -1) pre = res[i][j]; else if (pre != res[i][j]) valid = false; } int w = (pre == 'a') ? 1 : (pre == 'b' ? -1 : 0); if (i % 2) w *= -1; if (!valid) continue; for (int c = 0; c <= 25; ++c) if (appear[i + 1][c]) { char cc = 'a' + c; int nex_mask = 0; if (i < n) { for (int j = 1; j <= sz; ++j) { if ((S >> (j - 1) & 1) && res[i + 1][j] == cc) nex_mask |= 1 << j - 1; if ((S >> (j - 1) & 1) && res[i + 1][j + 1] == cc) nex_mask |= 1 << j; } } else { for (int j = 1; j <= sz; ++j) { if (j > 1 && (S >> (j - 1) & 1) && res[i + 1][j - 1] == cc) nex_mask |= 1 << j - 2; if (j < sz) if ((S >> (j - 1) & 1) && res[i + 1][j] == cc) nex_mask |= 1 << j - 1; } } if (nex_mask) dp[i][S] = max(dp[i][S], -dp[i + 1][nex_mask] + w); } } } int ans = -dp[1][1]; if (ans > 0) puts("FIRST"); else if (ans < 0) puts("SECOND"); else puts("DRAW"); return 0; }
#include <bits/stdc++.h> using namespace std; int n, dp[40][1 << 20]; char x[22][22]; int eval(int xx, int yy) { if (x[xx][yy] == 'a') return 1; if (x[xx][yy] == 'b') return -1; return 0; } int solve(int diag, int mask) { if (dp[diag][mask] != -1) return dp[diag][mask]; if (diag == 2 * n - 1) return eval(n, n); int res = 0; int cntDiag = diag; if (diag >= n) cntDiag = 2 * n - diag; int newMask[26], retx, rety; memset(newMask, 0, sizeof(newMask)); for (int i = 0; i < cntDiag; ++i) if (mask & (1 << i)) { int xx = i + 1; if (diag >= n) xx = diag - n + i + 1; int yy = diag - xx + 1; retx = xx; rety = yy; if (xx + 1 <= n) newMask[x[xx + 1][yy] - 'a'] |= (1 << (diag < n ? i + 1 : i)); if (yy + 1 <= n) newMask[x[xx][yy + 1] - 'a'] |= (1 << (diag < n ? i : i - 1)); } if (diag % 2) res = 1 << 30; else res = -(1 << 30); for (int i = 0; i < 26; ++i) if (newMask[i]) { if (diag % 2) res = min(res, solve(diag + 1, newMask[i])); else res = max(res, solve(diag + 1, newMask[i])); } res += eval(retx, rety); return dp[diag][mask] = res; } int main() { scanf("%d\n", &n); for (int i = 1; i <= n; ++i) gets(x[i] + 1); memset(dp, -1, sizeof(dp)); int res = solve(1, 1); if (res > 0) printf("FIRST"); if (res < 0) printf("SECOND"); if (res == 0) printf("DRAW"); return 0; }
#include <bits/stdc++.h> using namespace std; int n; string s[20]; int d[1 << 20], nd[1 << 20]; int getD(int sum, char ch) { if (ch != 'a' && ch != 'b') return 0; if ((sum & 1) ^ (ch == 'a')) return 1; return -1; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) cin >> s[i]; for (int i = 0; i < (1 << n); ++i) d[i] = -1000000000; d[1] = 0; vector<int> masks(26, 0), prevMasks(26, 0); masks[s[n - 1][n - 1] - 'a'] = 1; for (int sum = 2 * (n - 1) - 1; sum >= 0; --sum) { for (int i = 1; i < (1 << n); ++i) nd[i] = d[i]; prevMasks.swap(masks); masks.assign(26, 0); int num = 0; vector<char> ch; for (int i = n - 1; i >= 0; --i) { int j = sum - i; if (j < 0 || j >= n) continue; masks[s[i][j] - 'a'] += (1 << num); ch.push_back(s[i][j]); ++num; } int len = ch.size(); for (int i = 1; i < (1 << len); ++i) { int b = 0; while (!(i & (1 << b))) ++b; int c = ch[b] - 'a'; if ((i & masks[c]) != i) continue; int nmask; if (sum < n - 1) nmask = i | (2 * i); else nmask = i | (i / 2); d[i] = -1000000000; for (int j = 0; j < 26; ++j) if (nmask & prevMasks[j]) d[i] = max(d[i], getD(sum + 1, j + 'a') - nd[nmask & prevMasks[j]]); } } d[1] -= getD(0, s[0][0]); if (d[1] < 0) cout << "FIRST\n"; if (d[1] > 0) cout << "SECOND\n"; if (d[1] == 0) cout << "DRAW\n"; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100010; string v[21]; int mask[40][26]; int memo[40][1 << 20]; const int INF = 1 << 29; int n; int dfs(int len, int x, char c, bool A) { if (memo[len][x] != INF) return memo[len][x]; int ans = 0; if (len < n * 2 - 2) { if (A) { ans = -INF; for (int i = 0; i < (26); i++) { int next = (x | (x << 1)) & mask[len + 1][i]; if (!next) continue; ans = max(ans, dfs(len + 1, next, 'a' + i, !A)); } } else { ans = INF; for (int i = 0; i < (26); i++) { int next = (x | (x << 1)) & mask[len + 1][i]; if (!next) continue; ans = min(ans, dfs(len + 1, next, 'a' + i, !A)); } } } if (c == 'a') ans++; if (c == 'b') ans--; return memo[len][x] = ans; } int main() { cin >> n; for (int i = 0; i < (n); i++) cin >> v[i]; for (int i = 0; i < (40); i++) for (int j = 0; j < (1 << 20); j++) memo[i][j] = INF; for (int y = 0; y < (n * 2); y++) for (int x = 0; x < (n * 2); x++) { if (x < 0 || x >= n || y < 0 || y >= n) continue; mask[x + y][v[y][x] - 'a'] |= 1 << x; } int res = dfs(0, 1, v[0][0], false); if (res > 0) cout << "FIRST" << endl; else if (res < 0) cout << "SECOND" << endl; else cout << "DRAW" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int Inf = 1000000000; const int Maxn = 20; const int MaxN = 1 << Maxn; const int Maxl = 26; int n; char B[Maxn][Maxn]; int dp[2 * Maxn][MaxN]; int getRes(int r, int c, int len, int mask, char nd, char nnd) { if (dp[r + c][mask] == Inf) if (r == n - 1 && c == n - 1) dp[r + c][mask] = 0; else { int M[Maxl] = {}; int sub = 0; if (r || c == n - 1) sub = 1; for (int i = 0; i < len; i++) if (mask & 1 << i) { int nr = r + i, nc = c - i; if (nr + 1 < n) M[B[nr + 1][nc] - 'a'] |= 1 << i + 1 - sub; if (nc + 1 < n) M[B[nr][nc + 1] - 'a'] |= 1 << i - sub; } int mx = -Inf; if (sub) { for (int i = 0; i < Maxl; i++) if (M[i]) mx = max(mx, (i + 'a' == nd) - (i + 'a' == nnd) - getRes(r + 1, c, len - 1, M[i], nnd, nd)); } else for (int i = 0; i < Maxl; i++) if (M[i]) mx = max(mx, (i + 'a' == nd) - (i + 'a' == nnd) - getRes(r, c + 1, len + 1, M[i], nnd, nd)); dp[r + c][mask] = mx; } return dp[r + c][mask]; } int main() { fill((int*)dp, (int*)dp + (2 * Maxn) * MaxN, Inf); scanf("%d", &n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf(" %c", &B[i][j]); int res = (B[0][0] == 'a') - (B[0][0] == 'b') - getRes(0, 0, 1, 1, 'b', 'a'); if (res > 0) printf("FIRST\n"); else if (res == 0) printf("DRAW\n"); else printf("SECOND\n"); return 0; }
#include <bits/stdc++.h> using namespace std; int n; char T[25][25]; int V[25][25]; map<int, char> M[45]; char dp(int bit, int k) { if (k == 2 * n - 2) return 0; if (M[k].count(bit)) return M[k][bit]; char &r = M[k][bit]; vector<int> V(26); for (int i = (int)(0); i < (int)(n); ++i) if ((bit >> i) & 1) { int first = i, second = k - i; if (first != n - 1) { int nx = first + 1, ny = second; V[T[nx][ny]] |= (1 << nx); } if (second != n - 1) { int nx = first, ny = second + 1; V[T[nx][ny]] |= (1 << nx); } } if (k % 2 == 1) { r = -100; if (V[0]) r = max((int)r, 1 + dp(V[0], k + 1)); if (V[1]) r = max((int)r, -1 + dp(V[1], k + 1)); for (int i = (int)(2); i < (int)(26); ++i) if (V[i]) r = max(r, dp(V[i], k + 1)); } else { r = 100; if (V[0]) r = min((int)r, 1 + dp(V[0], k + 1)); if (V[1]) r = min((int)r, -1 + dp(V[1], k + 1)); for (int i = (int)(2); i < (int)(26); ++i) if (V[i]) r = min(r, dp(V[i], k + 1)); } return r; } int main() { scanf("%d", &n); for (int i = (int)(0); i < (int)(n); ++i) scanf("%s", T[i]); for (int i = (int)(0); i < (int)(n); ++i) for (int j = (int)(0); j < (int)(n); ++j) { if (T[i][j] == 'a') V[i][j] = 1; else if (T[i][j] == 'b') V[i][j] = -1; else V[i][j] = 0; T[i][j] -= 'a'; } int t = dp(1, 0) + V[0][0]; if (t > 0) printf("FIRST\n"); if (t == 0) printf("DRAW\n"); if (t < 0) printf("SECOND\n"); return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 1, sum = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return sum * f; } const int MAXN = 25; const int CHARSET = 26; int f[MAXN * 2][1 << 20], n; char s[MAXN][MAXN]; int dfs(int x, int S) { if (f[x][S] < 0x3f3f3f3f) return f[x][S]; int cnt = 0, ns = 0, a[CHARSET]; if (x < 2 * n - 2) { memset(a, 0, sizeof(a)); for (int i = 0; i <= x + 1; i++) { int nx = x + 1 - i, ny = i; if (nx >= n || ny >= n) continue; a[s[nx][ny] - 'a'] |= 1 << (cnt++); } for (int i = 0; i < CHARSET; i++) { if (x + 1 < n) ns = (S | (S << 1)) & a[i]; else ns = (S | (S >> 1)) & a[i]; if (!ns) continue; f[x][S] = min(f[x][S], -dfs(x + 1, ns)); } } else f[x][S] = 0; for (int i = 0; i <= x; i++) { if ((S >> i) & 1) { int nx = min(n - 1, x) - i, ny = x - nx; if (s[nx][ny] == 'a') f[x][S] += (x & 1) ? -1 : 1; else if (s[nx][ny] == 'b') f[x][S] += (x & 1) ? 1 : -1; break; } } return f[x][S]; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%s", s[i]); memset(f, 0x3f, sizeof(f)); int ans = dfs(0, 1); if (!ans) printf("DRAW"); else if (ans > 0) printf("FIRST"); else printf("SECOND"); return 0; }
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433832795l; string s[30]; int d[2][1 << 20]; int main() { int n; cin >> n; for (int i = 0; i < int(n); ++i) { cin >> s[i]; } for (int l = 2 * n - 1; l > 0; --l) { for (int msk0 = 0; msk0 < int(1 << (min(l, 2 * n - l))); ++msk0) { int msk = msk0; if (l > n) { msk <<= l - n; } if (msk == 0) { d[l & 1][msk] = 10000; continue; } if (l == 2 * n - 1) { d[l & 1][msk] = 0; continue; } int kuda[26]; memset(kuda, 0, sizeof kuda); for (int i = 0; i < int(n); ++i) { if (msk & (1 << i)) { int j = l - i - 1; if (i + 1 < n) { kuda[s[i + 1][j] - 'a'] |= 1 << (i + 1); } if (j + 1 < n) { kuda[s[i][j + 1] - 'a'] |= 1 << i; } } } int mx = -10000; for (int i = 0; i < int(26); ++i) { int cur = -d[(l + 1) & 1][kuda[i]]; if (i == 0) { if (l & 1) { cur--; } else { cur++; } } if (i == 1) { if (l & 1) { cur++; } else { cur--; } } mx = max(mx, cur); } d[l & 1][msk] = mx; } } int ans = -d[1][1]; if (s[0][0] == 'a') { ans++; } if (s[0][0] == 'b') { ans--; } if (ans > 0) { puts("FIRST"); } else if (ans < 0) { puts("SECOND"); } else { puts("DRAW"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long infLL = 0x3f3f3f3f3f3f3f3fLL; const int hash_mod = 1000037; const double pi = acos(-1); const double eps = 1e-10; const int maxn = 20 + 2; const int maxs = (1 << 20) + 2; int n; char g[maxn][maxn]; bool vis[maxn * 2][maxs]; int opt[maxn * 2][maxs]; void upmax(int &x, int y) { if (x == inf) x = y; else x = max(x, y); } void upmin(int &x, int y) { if (x == inf) x = y; else x = min(x, y); } int score(char ch) { if (ch > 'b') return 0; return ch == 'a' ? 1 : -1; } void dp(int d, int S) { if (vis[d][S]) return; vis[d][S] = true; if (d == n * 2 - 2) { opt[d][S] = 0; return; } opt[d][S] = inf; vector<pair<int, int> > point; for (int y = 0; y < n; ++y) if ((1 << y) & S) point.push_back(pair<int, int>(d - y, y)); for (char ch = 'a'; ch <= 'z'; ++ch) { int S0 = 0; for (int i = 0; i < point.size(); ++i) { int x = point[i].first, y = point[i].second; if (x < n - 1 && g[x + 1][y] == ch) S0 |= (1 << y); if (y < n - 1 && g[x][y + 1] == ch) S0 |= (1 << y + 1); } if (!S0) continue; dp(d + 1, S0); if (d % 2 == 0) upmin(opt[d][S], opt[d + 1][S0] + score(ch)); else upmax(opt[d][S], opt[d + 1][S0] + score(ch)); } } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%s", g[i]); dp(0, 1); int res = opt[0][1] + score(g[0][0]); if (res == 0) { puts("DRAW"); } else { puts(res > 0 ? "FIRST" : "SECOND"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 20 + 7; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template <class T, class S> inline void add(T& a, S b) { a += b; if (a >= mod) a -= mod; } template <class T, class S> inline void sub(T& a, S b) { a -= b; if (a < 0) a += mod; } template <class T, class S> inline bool chkmax(T& a, S b) { return a < b ? a = b, true : false; } template <class T, class S> inline bool chkmin(T& a, S b) { return a > b ? a = b, true : false; } int n; int dp[21 * 2][1 << 20]; bool vis[21 * 2][1 << 20]; char Map[30][30]; int id[30][30]; struct Point { int x, y; }; vector<Point> vc[50]; int dfs(int d, int mask, int op) { if (d == n * 2 - 2) return 0; int& ans = dp[d][mask]; if (vis[d][mask]) return ans; ans = op ? inf : -inf; int up = ((int)vc[d].size()); for (char c = 'a'; c <= 'z'; c++) { int nxtmask = 0, sco = 0; if (c == 'a') sco = 1; else if (c == 'b') sco = -1; for (int i = 0; i < up; i++) { if (mask >> i & 1) { int x = vc[d][i].x, y = vc[d][i].y + 1; if (y < n && Map[x][y] == c) { nxtmask |= 1 << id[x][y]; } x = vc[d][i].x + 1, y = vc[d][i].y; if (x < n && Map[x][y] == c) { nxtmask |= 1 << id[x][y]; } } } if (nxtmask) { if (op) chkmin(ans, dfs(d + 1, nxtmask, op ^ 1) + sco); else chkmax(ans, dfs(d + 1, nxtmask, op ^ 1) + sco); } } vis[d][mask] = true; return ans; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%s", Map[i]); for (int i = 0; i < n; i++) { int x = i, y = 0; while (x < n && y < n && x >= 0 && y >= 0) { id[x][y] = ((int)vc[i].size()); vc[i].push_back(Point{x, y}), x--, y++; } } for (int i = n; i < 2 * n; i++) { int x = n - 1, y = i - n + 1; while (x < n && y < n && x >= 0 && y >= 0) { id[x][y] = ((int)vc[i].size()); vc[i].push_back(Point{x, y}), x--, y++; } } int judge = dfs(0, 1, 1); if (Map[0][0] == 'a') judge++; else if (Map[0][0] == 'b') judge--; if (judge > 0) puts("FIRST"); else if (judge < 0) puts("SECOND"); else puts("DRAW"); return 0; }
#include <bits/stdc++.h> using namespace std; int n, i, j, k; char s[25]; int f[45][1200005], S[45][45]; bool g[45][1200005]; void dfs(int x, int y) { int i, j, k; if (g[x][y]) return; g[x][y] = true; if (x & 1) f[x][y] = 1000000000; else f[x][y] = -1000000000; if (x == 2 * n - 1) f[x][y] = 0; for (i = 1; i <= 26; ++i) { k = 0; for (j = 1; j <= n; ++j) if (y & (1 << j - 1)) { if (S[x + 1][j] == i) k |= 1 << j - 1; if (S[x + 1][j + 1] == i) k |= 1 << j; } if (k) { dfs(x + 1, k); if (x & 1) { if (f[x + 1][k] < f[x][y]) f[x][y] = f[x + 1][k]; } else { if (f[x + 1][k] > f[x][y]) f[x][y] = f[x + 1][k]; } } } for (i = 1; i <= n; ++i) if (y & (1 << i - 1)) break; if (S[x][i] == 1) ++f[x][y]; if (S[x][i] == 2) --f[x][y]; } int main() { scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%s", s + 1); for (j = 1; j <= n; ++j) S[i + j - 1][j] = s[j] - 'a' + 1; } dfs(1, 1); if (f[1][1] > 0) printf("FIRST\n"); if (f[1][1] == 0) printf("DRAW\n"); if (f[1][1] < 0) printf("SECOND\n"); }
#include <bits/stdc++.h> using namespace std; int N; char mat[21][21]; int getSize(int d) { return min(d + 1, 2 * N - 1 - d); } int getP(int i, int j, int d) { return min(j, N - 1 - i); } int getI(int p, int d) { return d < N ? d - p : (N - 1 - p); } int getJ(int p, int d) { return d - getI(p, d); } short mem[1 << 21][42]; bool cat[1 << 21][42]; int dp(int mask, int d) { if (d == 2 * N - 1) return 0; if (cat[mask][d]) return mem[mask][d]; int ans = 10000 * ((d & 1) ? 1 : -1); int SZ = getSize(d); for (int k = 0; k < SZ; ++k) { if (!(mask & (1 << k))) continue; char c = mat[getI(k, d)][getJ(k, d)]; int nm = 0; for (int l = 0; l < SZ; ++l) { int ni = getI(l, d), nj = getJ(l, d); if (c != mat[ni][nj]) continue; if (!(mask & (1 << l))) continue; if (ni + 1 < N) nm |= 1 << (getP(ni + 1, nj, d)); if (nj + 1 < N) nm |= 1 << (getP(ni, nj + 1, d)); } int ad = c == 'a' ? 1 : (c == 'b' ? -1 : 0); int tem = dp(nm, d + 1) + ad; ans = (d & 1) ? min(ans, tem) : max(ans, tem); } cat[mask][d] = 1; return mem[mask][d] = ans; } int main() { cin >> N; for (int i = 0; i < N; ++i) cin >> mat[i]; int t = dp(1, 0); cout << (t ? (t < 0 ? "SECOND" : "FIRST") : "DRAW") << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 20, M = 2 * N - 1, MXMSK = 1 << N; int sx[M], ex[M], lim[M], MSK, n, m; int dp[2][MXMSK], ch[M][26]; string s[N]; inline bool leg(int x, int y) { return (x >= 0 && y >= 0 && x < n && y < n); } inline int getv(char c) { return (c == 'a' ? 1 : c == 'b' ? -1 : 0); } int main() { ios::sync_with_stdio(false); cout.tie(0); cin.tie(0); cin >> n; MSK = 1 << n; m = 2 * n - 1; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < m; i++) sx[i] = max(i - n + 1, 0); for (int i = 0; i < m; i++) ex[i] = min(i + 1, n); for (int i = 0; i < m; i++) { lim[i] = (1 << ex[i]) - 1; if (sx[i]) lim[i] -= (1 << (sx[i] - 1)) - 1; } for (int i = 0; i < m; i++) for (int j = sx[i]; j < ex[i]; j++) ch[i][s[i - j][j] - 'a'] |= 1 << j; dp[(m - 1) % 2][1 << (n - 1)] = getv(s[n - 1][n - 1]); for (int i = m - 2; i >= 0; i--) { int cur = i % 2, pre = 1 - cur; if (i % 2 == 0) memset(dp[cur], 63, sizeof dp[cur]); else memset(dp[cur], -63, sizeof dp[cur]); for (int last = 0; last < 26; last++) for (int from = ch[i][last]; from; from = (from - 1) & ch[i][last]) { int posib = (from | (from << 1)) & lim[i + 1]; for (int next = 0; next < 26; next++) { int to = (posib & ch[i + 1][next]); if (!to) continue; if (i % 2) dp[cur][from] = max(dp[cur][from], dp[pre][to] + getv(last + 'a')); else dp[cur][from] = min(dp[cur][from], dp[pre][to] + getv(last + 'a')); } } } cout << (dp[0][1] > 0 ? "FIRST" : dp[0][1] < 0 ? "SECOND" : "DRAW") << endl; }
#include <bits/stdc++.h> using namespace std; int* dp[2][64]; int n, cou; string str[32]; int rec(int turn, int pos, int mask) { int& ret = dp[turn][pos][mask]; if (ret == -1000) { cou++; if (pos == 2 * n - 2) { if (str[n - 1][n - 1] == 'a') ret = 1; if (str[n - 1][n - 1] == 'b') ret = -1; ret = 0; } else { ret = (turn == 0) ? -100 : 100; int nmask[26], id = 0, j; fill(nmask, nmask + 26, 0); for (int i = 0; i < n; i++) { j = pos - i; if (j < n && j >= 0) { if ((mask >> id) & 1) { if (i + 1 < n) nmask[str[i + 1][j] - 'a'] |= (1 << (min(i + 1, n - 1 - j))); if (j + 1 < n) nmask[str[i][j + 1] - 'a'] |= (1 << (min(i, n - 1 - (j + 1)))); } id++; } } int val; for (char x = 'a'; x <= 'z'; x++) { val = 0; if (x == 'a') val = 1; if (x == 'b') val = -1; if (nmask[x - 'a'] != 0) { if (turn == 0) { ret = max(ret, val + rec(1 - turn, pos + 1, nmask[x - 'a'])); } else { ret = min(ret, val + rec(1 - turn, pos + 1, nmask[x - 'a'])); } } } } } return ret; } int main() { while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { cin >> str[i]; } for (int i = 0; i < 2 * n - 1; i++) { for (int j = 0; j < 2; j++) { dp[j][i] = (int*)malloc(sizeof(int) * (1 << min(i + 1, 2 * n - 1 - i))); for (int k = 0; k < (1 << min(i + 1, 2 * n - 1 - i)); k++) { dp[j][i][k] = -1000; } } } int x = 0; if (str[0][0] == 'a') x++; if (str[0][0] == 'b') x--; if (rec(1, 0, 1) + x > 0) { cout << "FIRST\n"; } else if (rec(1, 0, 1) + x < 0) { cout << "SECOND\n"; } else { cout << "DRAW\n"; } for (int i = 0; i < 2 * n - 1; i++) { for (int j = 0; j < 2; j++) { free(dp[j][i]); } } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, i, j, a[20][20], x; char f[20 * 2][1 << 20]; bool bo[20 * 2][1 << 20]; char c[20][20]; char dfs(int dep, int mask) { if (dep == 2 * n - 2) return a[n - 1][n - 1]; if (bo[dep][mask]) return f[dep][mask]; bo[dep][mask] = 1; int i, x, m, y, v; if (dep & 1) f[dep][mask] = -100; else f[dep][mask] = 100; for (i = 0; i < n; ++i) if ((mask >> i) & 1) break; v = a[i][dep - i]; for (i = 'a'; i <= 'z'; ++i) { m = 0; for (x = 0; x < n; ++x) if ((mask >> x) & 1) { y = dep - x; if (x + 1 < n && c[x + 1][y] == i) m |= 1 << (x + 1); if (y + 1 < n && c[x][y + 1] == i) m |= 1 << x; } if (m) { if (dep & 1) f[dep][mask] = max(f[dep][mask], char(dfs(dep + 1, m) + v)); else f[dep][mask] = min(f[dep][mask], char(dfs(dep + 1, m) + v)); } } return f[dep][mask]; } int main() { scanf("%d", &n); for (i = 0; i < n; ++i) { scanf("%s", c[i]); for (j = 0; j < n; ++j) if (c[i][j] == 'a') a[i][j] = 1; else if (c[i][j] == 'b') a[i][j] = -1; } x = dfs(0, 1); puts(x == 0 ? "DRAW" : x > 0 ? "FIRST" : "SECOND"); }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:60777216") using namespace std; int n; string s[22]; int id[22][22]; vector<pair<int, int> > v[44]; map<int, int> r[44]; int go(int pos, int mask) { if (pos == n + n - 2) return 0; if (r[pos].count(mask)) return r[pos][mask]; vector<int> t; for (int i = (0); i < (20); i++) if (mask & (1 << i)) t.push_back(i); int m[26]; memset(m, 0, sizeof m); for (int i = (0); i < (t.size()); i++) { int px = v[pos][t[i]].first; int py = v[pos][t[i]].second; if (px + 1 < n) m[s[px + 1][py] - 'a'] |= (1 << id[px + 1][py]); if (py + 1 < n) m[s[px][py + 1] - 'a'] |= (1 << id[px][py + 1]); } int best = (pos % 2 == 0 ? 100 : -100); for (int i = (0); i < (26); i++) if (m[i]) { int diff = go(pos + 1, m[i]); if (i == 0) diff++; if (i == 1) diff--; if (pos % 2 == 0) best = min(best, diff); else best = max(best, diff); } return r[pos][mask] = best; } int main() { cin >> n; for (int i = (0); i < (n); i++) cin >> s[i]; for (int i = (0); i < (n); i++) for (int j = (0); j < (n); j++) { id[i][j] = v[i + j].size(); v[i + j].push_back(pair<int, int>(i, j)); } int d = go(0, 1) + (s[0][0] == 'a' ? 1 : (s[0][0] == 'b' ? -1 : 0)); if (d == 0) puts("DRAW"); else if (d > 0) puts("FIRST"); else puts("SECOND"); return 0; }
#include <bits/stdc++.h> const int inf = (int)1e9 + 10; using namespace std; char s[110][110], e[110][110]; bool vis[40][1 << 20]; int g[40][1 << 20], d[1 << 20], si[110]; int n; int f(int x, int t) { if (vis[x][t]) return g[x][t]; vis[x][t] = 1; int &r = g[x][t], lt = 0; r = (x & 1 ? 1 : -1) * inf; for (int i = 0; i < si[x + 1]; ++i) { lt = 0; for (int j = 0; j < si[x + 1]; ++j) if (e[x + 1][j] == e[x + 1][i]) if (t & (1 << j) || t & (1 << (j + (x < n ? -1 : 1)))) lt += 1 << j; if (lt) if (x & 1) r = min(r, f(x + 1, lt)); else r = max(r, f(x + 1, lt)); } return r += (e[x][d[t & -t]] == 'a' ? 1 : (e[x][d[t & -t]] == 'b' ? -1 : 0)); } int main() { for (int i = 0; i < 20; ++i) d[1 << i] = i; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%s", s[i]); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) e[i + j + 1][si[i + j + 1]++] = s[i][j]; vis[n * 2 - 1][1] = 1; g[n * 2 - 1][1] += (s[n - 1][n - 1] == 'a' ? 1 : (s[n - 1][n - 1] == 'b' ? -1 : 0)); f(1, 1); if (g[1][1] < 0) printf("SECOND"); else if (g[1][1] == 0) printf("DRAW"); else printf("FIRST"); }
#include <bits/stdc++.h> using namespace std; const int N = 20; const int INF = 16843009; int n; char a[N][N]; int memo[N + N][1 << N]; int dp(int d, int mask) { if (d == n + n - 2) { if (a[n - 1][n - 1] == 'a') return 1; if (a[n - 1][n - 1] == 'b') return -1; return 0; } int &ret = memo[d][mask]; if (ret != INF) return ret; ret = d & 1 ? INF : -INF; for (char c = 'a'; c <= 'z'; ++c) { int newMask = 0; for (int i = 0; i <= d; ++i) { int j = d - i; if (j >= n || a[i][j] != c || !((mask >> i) & 1)) continue; if (j + 1 < n) newMask |= 1 << i; if (i + 1 < n) newMask |= 1 << (i + 1); } if (!newMask) continue; int score = (c == 'a') - (c == 'b'); if (d & 1) ret = min(ret, dp(d + 1, newMask) + score); else ret = max(ret, dp(d + 1, newMask) + score); } return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { scanf(" %c", &a[i][j]); } } memset(memo, 1, sizeof memo); int ans = dp(0, 1); if (ans > 0) puts("FIRST"); else if (ans < 0) puts("SECOND"); else puts("DRAW"); return 0; }
#include <bits/stdc++.h> using namespace std; const int oo = 27081993; int n, f[44][1 << 20]; string a[22]; int dp(int diagonal, int mask) { if (diagonal == n * 2 - 2) return 0; int &res = f[diagonal][mask]; if (res > -oo) return res; int maskChar[26] = {0}; for (int row = 0; row < n; row++) if (mask >> row & 1) { int col = diagonal - row; if (col < 0 || col >= n) continue; if (row + 1 < n) maskChar[a[row + 1][col] - 'a'] |= 1 << (row + 1); if (col + 1 < n) maskChar[a[row][col + 1] - 'a'] |= 1 << row; } for (int i = 0; i < 26; i++) if (maskChar[i]) { int score = 0; if (!i) score += (diagonal % 2 ? 1 : -1); if (i == 1) score += (diagonal % 2 ? -1 : 1); score -= dp(diagonal + 1, maskChar[i]); res = max(res, score); } return res; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n * 2 - 1; i++) for (int j = 0; j < 1 << n; j++) f[i][j] = -oo; int ans = dp(0, 1); if (a[0][0] == 'a') ans--; if (a[0][0] == 'b') ans++; if (!ans) puts("DRAW"); else puts(ans < 0 ? "FIRST" : "SECOND"); }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; int n, cnt; char g[25][25]; int dp[55][(1 << 20) + 10], M[50][26]; inline void ckmax(int &a, int b) { if (a < b) a = b; } inline void ckmin(int &a, int b) { if (a > b) a = b; } inline int getm(int k, int x, int y) { if (k <= n) return x - 1; return n - y; } inline int gnxtmask(int k, int mask) { if (k < n) return mask | (mask << 1); return mask | (mask >> 1); } int gao(int k, int mask) { int &res = dp[k][mask]; if (res != -inf) return res; if (k + 1 == n + n) { return res = 0; } int len = k <= n ? k : (n - (k - n)), sx = k <= n ? 1 : 1 + k - n, sy = k <= n ? k : n; if (~k & 1) res = -inf; else res = inf; for (int(c) = 0; (c) < (int)(26); ++(c)) { int m = gnxtmask(k, mask) & M[k + 1][c]; if (!m) continue; int t = (int)(c == 0) - (int)(c == 1); if (~k & 1) ckmax(res, t + gao(k + 1, m)); else ckmin(res, t + gao(k + 1, m)); } return res; } int main(int argc, char const *argv[]) { scanf("%d", &n); for (int(i) = (int)(1); (i) <= (int)(n); ++(i)) scanf("%s", g[i] + 1); for (int(k) = (int)(1); (k) <= (int)(n + n); ++(k)) for (int(j) = 0; (j) < (int)(26); ++(j)) { int len = k <= n ? k : (n - (k - n)), sx = k <= n ? 1 : 1 + k - n, sy = k <= n ? k : n; for (int i = 0; i < len; ++i, ++sx, --sy) { if (g[sx][sy] == 'a' + j) { M[k][j] |= 1 << i; } } } for (int(i) = (int)(1); (i) <= (int)(n + n); ++(i)) { int len = i <= n ? i : (n + n - i); for (int(j) = 0; (j) < (int)(1 << len); ++(j)) dp[i][j] = -inf; } int t = g[1][1] == 'a' ? 1 : g[1][1] == 'b' ? -1 : 0; int r = t + gao(1, 1); if (r < 0) puts("SECOND"); if (r > 0) puts("FIRST"); if (r == 0) puts("DRAW"); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:33554432") using namespace std; char a[1 << 5][1 << 5]; int to[1 << 6][26]; int dp[1 << 20][40]; int n; int go(int mask, int len) { if (len == 2 * n - 1) return 0; int& res = dp[mask][len]; if (res > -10000) return res; res = (len & 1) ? 1000 : -1000; for (int i = 0; i < 26; ++i) { if (to[len][i] == 0) continue; int next; if (len < n) next = (mask & to[len][i]) | ((mask << 1) & to[len][i]); else next = (mask & to[len][i]) | ((mask >> 1) & to[len][i]); if (!next) continue; int add = 0; if (i == 0) add = 1; if (i == 1) add = -1; if (len & 1) res = min(res, add + go(next, len + 1)); else res = max(res, add + go(next, len + 1)); } return res; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%s", a[i]); memset(to, 0, sizeof(to)); for (int i = 0; i < n; ++i) { for (int j = 0; j <= i; ++j) { int x = i - j; int y = j; to[i][a[x][y] - 'a'] |= 1 << j; } } for (int i = 1; i < n; ++i) { for (int j = 0; j < n - i; ++j) { int x = n - 1 - j; int y = i + j; to[n + i - 1][a[x][y] - 'a'] |= 1 << j; } } memset(dp, -100, sizeof(dp)); int res = go(1, 1); if (a[0][0] == 'a') res++; if (a[0][0] == 'b') res--; if (res == 0) printf("DRAW\n"); else if (res > 0) printf("FIRST\n"); else printf("SECOND\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 110; int n, t[26], dp[2][1 << 20], w[N]; int sx[N], sy[N], st[N], h; char s[N][N]; vector<int> p[N]; inline void ckmin(int &a, int b) { a = ((a < b) ? a : b); } inline void ckmax(int &a, int b) { a = ((a > b) ? a : b); } inline int read() { int f = 1, x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } inline int val(int k) { return (k == 0) ? 1 : (k == 1) ? -1 : 0; } inline void add(int x, int y) { if (x + 1 <= n) { int id = min(n - x - 1, y - 1), now = s[x + 1][y] - 'a'; if (!t[now]) st[++h] = now; t[now] |= (1 << id); } if (y + 1 <= n) { int id = min(n - x, y), now = s[x][y + 1] - 'a'; if (!t[now]) st[++h] = now; t[now] |= (1 << id); } } signed main() { n = read(); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); if (n == 1) { if (s[1][1] == 'a') printf("FIRST\n"); else if (s[1][1] == 'b') printf("SECOND\n"); else printf("DRAW\n"); return 0; } int cnt = 0; for (int i = 1; i <= n; i++) { memset(t, 0, sizeof(t)); cnt++; w[cnt] = i; sx[cnt] = i; sy[cnt] = 1; for (int j = 0; j < i; j++) { int x = i - j, y = 1 + j; t[s[x][y] - 'a'] |= (1 << j); } for (int j = 0; j < 26; j++) if (t[j] != 0) p[cnt].push_back(t[j]); } for (int i = 2; i <= n; i++) { memset(t, 0, sizeof(t)); cnt++; w[cnt] = n - i + 1; sx[cnt] = n; sy[cnt] = i; for (int j = 0; j < n - i + 1; j++) { int x = n - j, y = i + j; t[s[x][y] - 'a'] |= (1 << j); } for (int j = 0; j < 26; j++) if (t[j] != 0) p[cnt].push_back(t[j]); } int cur = 1; dp[0][1] = val(s[1][1] - 'a'); dp[0][0] = 0x3f3f3f3f; memset(t, 0, sizeof(t)); for (int i = cnt - 1; i >= 1; i--) { int v = (i & 1) ? 0x3f3f3f3f : -0x3f3f3f3f; int full = (1 << w[i]) - 1; fill(dp[cur], dp[cur] + full + 1, v); for (int j = 0; j < (int)p[i].size(); j++) { int s = p[i][j]; for (int mask = s; mask; mask = (mask - 1) & s) { h = 0; for (int k = 0; k < w[i]; k++) if ((mask >> k) & 1) { int x = sx[i] - k, y = sy[i] + k; add(x, y); } for (int k = 1; k <= h; k++) if (abs(dp[cur ^ 1][t[st[k]]]) != 0x3f3f3f3f) { int v = dp[cur ^ 1][t[st[k]]] + val(st[k]); t[st[k]] = 0; if (i & 1) ckmin(dp[cur][mask], v); else ckmax(dp[cur][mask], v); } } } cur ^= 1; } cur ^= 1; if (dp[cur][1] > 0) printf("FIRST\n"); else if (dp[cur][1] == 0) printf("DRAW\n"); else printf("SECOND\n"); }
#include <bits/stdc++.h> using namespace std; int N; string board[30]; int dp[42][(1 << 20)]; int mask[42][30]; int func(int i, int j, int c, bool first) { if (dp[i][j] != 10000) return dp[i][j]; int next; int ans = -(1 << 29); if (i == 2 * (N - 1)) { ans = 0; } else { for ((next) = 0; (next) < (int)(26); (next)++) { int i2 = i + 1; int j2 = (mask[i + 1][next] & (j | (j << 1))); if (j2 != 0) ans = max(ans, -func(i2, j2, next, !first)); } } if (first) { if (c == 0) ans++; else if (c == 1) ans--; } else { if (c == 0) ans--; else if (c == 1) ans++; } return dp[i][j] = ans; } int main(void) { int i, j, k; cin >> N; for ((i) = 0; (i) < (int)(N); (i)++) cin >> board[i]; for ((i) = 0; (i) < (int)(2 * N); (i)++) for ((j) = 0; (j) < (int)(26); (j)++) for ((k) = 0; (k) < (int)(N); (k)++) { int x = k; int y = i - x; if (y >= 0 && y < N && board[x][y] - 'a' == j) mask[i][j] |= (1 << k); } for ((i) = 0; (i) < (int)(2 * N); (i)++) for ((j) = 0; (j) < (int)((1 << N)); (j)++) dp[i][j] = 10000; int ans = func(0, 1, board[0][0] - 'a', false); if (ans < 0) cout << "FIRST" << endl; if (ans == 0) cout << "DRAW" << endl; if (ans > 0) cout << "SECOND" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void debug(T a, T b) { ; } template <class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } namespace std { template <class S, class T> ostream& operator<<(ostream& out, const pair<S, T>& a) { out << '(' << a.first << ',' << a.second << ')'; return out; } } // namespace std long long int readL() { long long int res; scanf("%I64d", &res); return res; } void printL(long long int res) { printf("%I64d", res); } const int INF = 5e8; int n; char buf[25][25]; int dp[45][1 << 20]; int score[300]; int main() { cin >> n; for (int i = 0; i < (n); ++i) scanf("%s", buf[i]); score[0] = 1; score[1] = -1; dp[n * 2 - 2][1] = 0; for (int i = n * 2 - 3; i >= 0; --i) { int wid = min(i + 1, 2 * n - 1 - i); for (int j = (1); j < ((1 << wid)); ++j) { int nbit[26] = {0}; for (int k = 0; k < (wid); ++k) if (j >> k & 1) { int y = k + max(0, i - (n - 1)), x = i - y; ; ; if (y + 1 < n) { nbit[buf[y + 1][x] - 'a'] |= (1 << (y + 1 - max(0, i + 1 - (n - 1)))); } if (x + 1 < n) { nbit[buf[y][x + 1] - 'a'] |= (1 << (y - max(0, i + 1 - (n - 1)))); } } if (i & 1) dp[i][j] = -INF; else dp[i][j] = INF; for (int k = 0; k < (26); ++k) if (nbit[k] > 0) { if (i & 1) { chmax(dp[i][j], dp[i + 1][nbit[k]] + score[k]); } else { chmin(dp[i][j], dp[i + 1][nbit[k]] + score[k]); } } } } int res = dp[0][1] + score[buf[0][0] - 'a']; ; ; puts(res > 0 ? "FIRST" : res < 0 ? "SECOND" : "DRAW"); return 0; }
#include <bits/stdc++.h> using namespace std; void solve(); int main() { int t = 1; while (t--) solve(); return 0; } int n; string s[50]; const int INF = 1000000000; map<vector<pair<int, int>>, int> dp; int cnt(vector<pair<int, int>> state) { if (dp.find(state) != dp.end()) return dp[state]; int w = (state[0].first + state[0].second) & 1; if (state.size() == 1 && state[0].first == n - 1 && state[0].second == n - 1) { if (!w) { if (s[n - 1][n - 1] == 'a') return 1; else if (s[n - 1][n - 1] == 'b') return -1; } else { if (s[n - 1][n - 1] == 'a') return -1; else if (s[n - 1][n - 1] == 'b') return 1; } return 0; } char cur = s[state[0].first][state[0].second]; if (!w) { int res = 0; if (cur == 'a') ++res; if (cur == 'b') --res; vector<pair<int, int>> go[30]; for (int i = 0; i < state.size(); ++i) { if (state[i].first < n - 1) go[s[state[i].first + 1][state[i].second] - 'a'].push_back( make_pair(state[i].first + 1, state[i].second)); if (state[i].second < n - 1) go[s[state[i].first][state[i].second + 1] - 'a'].push_back( make_pair(state[i].first, state[i].second + 1)); } int add = -INF; for (char c = 'a'; c <= 'z'; ++c) { if (go[c - 'a'].size() > 0) { sort(go[c - 'a'].begin(), go[c - 'a'].end()); go[c - 'a'].resize(unique(go[c - 'a'].begin(), go[c - 'a'].end()) - go[c - 'a'].begin()); add = max(add, cnt(go[c - 'a'])); } } res -= add; return dp[state] = res; } else { int res = 0; if (cur == 'b') ++res; if (cur == 'a') --res; vector<pair<int, int>> go[30]; for (int i = 0; i < state.size(); ++i) { if (state[i].first < n - 1) go[s[state[i].first + 1][state[i].second] - 'a'].push_back( make_pair(state[i].first + 1, state[i].second)); if (state[i].second < n - 1) go[s[state[i].first][state[i].second + 1] - 'a'].push_back( make_pair(state[i].first, state[i].second + 1)); } int add = -INF; for (char c = 'a'; c <= 'z'; ++c) { if (go[c - 'a'].size() > 0) { sort(go[c - 'a'].begin(), go[c - 'a'].end()); go[c - 'a'].resize(unique(go[c - 'a'].begin(), go[c - 'a'].end()) - go[c - 'a'].begin()); add = max(add, cnt(go[c - 'a'])); } } res -= add; return dp[state] = res; } } void solve() { cin >> n; for (int i = 0; i < n; ++i) cin >> s[i]; vector<pair<int, int>> start(1, make_pair(0, 0)); int res = cnt(start); if (res > 0) cout << "FIRST\n"; if (res < 0) cout << "SECOND\n"; if (res == 0) cout << "DRAW\n"; }
#include <bits/stdc++.h> using namespace std; int n; char s[20][21]; int a[40][26]; int dp[40][1 << 20]; int dfs(int x, int y) { int z = 0, i, j; if (dp[x][y] != 1e9) return dp[x][y]; for (i = 0; i < n; i++) { if ((y >> i) & 1) { if (s[i][x - i] == 'a') { z++; } else if (s[i][x - i] == 'b') { z--; } break; } } if (x == 2 * n - 2) return dp[x][y] = z; if (x % 2 == 1) { dp[x][y] = -1e9; for (i = 0; i < 26; i++) { int ny = a[x + 1][i] & (y | (y << 1)); if (ny == 0) continue; dp[x][y] = max(dp[x][y], dfs(x + 1, ny) + z); } } else { for (i = 0; i < 26; i++) { int ny = a[x + 1][i] & (y | (y << 1)); if (ny == 0) continue; dp[x][y] = min(dp[x][y], dfs(x + 1, ny) + z); } } return dp[x][y]; } int main() { int ans, i, j; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%s", s[i]); for (i = 0; i < n * 2 - 1; i++) { for (j = 0; j < n; j++) { if (i - j < 0 || i - j >= n) continue; a[i][s[j][i - j] - 'a'] |= (1 << j); } } for (i = 0; i < n * 2; i++) { for (j = 0; j < (1 << n); j++) { dp[i][j] = 1e9; } } ans = dfs(0, 1); if (ans > 0) { puts("FIRST"); } else if (ans == 0) { puts("DRAW"); } else { puts("SECOND"); } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 20; char a[maxn][maxn]; int dp[40][1 << 20]; int pos[40][30]; bool vis[40][1 << 20]; int n; void dfs(int i, int j) { if (vis[i][j]) return; vis[i][j] = 1; if (i == 2 * n - 2) { dp[i][j] = 0; return; } for (int k = 0; k < 26; k++) { int plus; if (k == 0) plus = 1; else if (k == 1) plus = -1; else plus = 0; int t = (j | (j << 1)) & pos[i + 1][k]; if (!t) continue; dfs(i + 1, t); if (i & 1) dp[i][j] = max(dp[i][j], dp[i + 1][t] + plus); else dp[i][j] = min(dp[i][j], dp[i + 1][t] + plus); } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < 2 * n - 1; i++) { for (int x = 0; x < n; x++) { int y = i - x; if (y < 0 || y >= n) continue; pos[i][a[x][y] - 'a'] |= 1 << x; } } for (int i = 0; i <= 2 * n - 2; i++) for (int j = 0; j < (1 << n); j++) if (i & 1) dp[i][j] = -inf; else dp[i][j] = inf; dp[2 * n - 2][1 << (n - 1)] = 0; dfs(0, 1); if (a[0][0] == 'a') dp[0][1]++; else if (a[0][0] == 'b') dp[0][1]--; if (dp[0][1] > 0) cout << "FIRST\n"; else if (dp[0][1] == 0) cout << "DRAW\n"; else cout << "SECOND\n"; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } template <typename T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T, class T1> inline void gn(T &first, T1 &second) { gn(first); gn(second); } template <class T, class T1, class T2> inline void gn(T &first, T1 &second, T2 &z) { gn(first); gn(second); gn(z); } template <typename T> inline void print(T first) { if (first < 0) { putchar('-'); return print(-first); } if (first < 10) { putchar('0' + first); return; } print(first / 10); putchar(first % 10 + '0'); } template <typename T> inline void println(T first) { print(first), putchar('\n'); } template <typename T> inline void printsp(T first) { print(first), putchar(' '); } template <class T, class T1> inline void print(T first, T1 second) { printsp(first), println(second); } template <class T, class T1, class T2> inline void print(T first, T1 second, T2 z) { printsp(first), printsp(second), println(z); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } int n; char s[22][22]; map<vector<pair<int, int> >, int> ans; int calc(vector<pair<int, int> > v) { if (ans.count(v)) return ans[v]; int val = (s[v[0].first][v[0].second] == 'a') - (s[v[0].first][v[0].second] == 'b'); if (v.size() == 1 && v[0].first == n && v[0].second == n) return val; vector<pair<int, int> > tmp[26]; for (int i = 0; i < v.size(); i++) { int first = v[i].first; int second = v[i].second; if (first < n) tmp[s[first + 1][second] - 'a'].push_back( pair<int, int>(first + 1, second)); if (second < n) tmp[s[first][second + 1] - 'a'].push_back( pair<int, int>(first, second + 1)); } int id = v[0].first + v[0].second & 1, ret; if (id) ret = -0x3f3f3f3f; else ret = 0x3f3f3f3f; for (int i = 0; i < 26; i++) if (tmp[i].size()) { tmp[i].resize(unique(tmp[i].begin(), tmp[i].end()) - tmp[i].begin()); int res = calc(tmp[i]); if (id) smax(ret, res); else smin(ret, res); } return ans[v] = ret + val; } int main() { gn(n); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); int ans = calc(vector<pair<int, int> >(1, pair<int, int>(1, 1))); if (ans > 0) puts("FIRST"); else if (ans == 0) puts("DRAW"); else puts("SECOND"); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1000 * 1000 * 1000; const int maxn = 20; int n, sz[maxn + maxn], ans, d[maxn + maxn][(1 << maxn)]; vector<int> g[maxn + maxn][26]; bool was[maxn + maxn][(1 << maxn)]; char a[maxn][maxn]; int calc(int v, int mask) { if (was[v][mask]) return d[v][mask]; was[v][mask] = true; if (v == n + n - 2) return 0; if (v % 2) d[v][mask] = -inf; else d[v][mask] = inf; int lmask, cur; for (int i = 0; i < 26; i++) { lmask = 0; for (int j = 0; j < (int)g[v + 1][i].size(); j++) { cur = min(v + 1, n - 1) - g[v + 1][i][j]; if (v > n - 2) { if ((mask & (1 << cur))) lmask |= (1 << cur); } else { if (cur && (mask & (1 << (cur - 1)))) lmask |= (1 << cur); } if (v > n - 2) { if ((mask & (1 << (cur + 1)))) lmask |= (1 << cur); } else { if (cur != sz[v + 1] - 1 && (mask & (1 << cur))) lmask |= (1 << cur); } } if (!lmask) continue; if (v % 2) d[v][mask] = max(d[v][mask], calc(v + 1, lmask) + (!i ? 1 : i == 1 ? -1 : 0)); else d[v][mask] = min(d[v][mask], calc(v + 1, lmask) + (!i ? 1 : i == 1 ? -1 : 0)); } return d[v][mask]; } int main() { cin >> n; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) cin >> a[i][j]; for (int i = 0; i < n + n - 1; ++i) { int l = min(i, n - 1), r = max(0, i - n + 1); while (l >= 0 && r < n) { g[i][a[l][r] - 'a'].push_back(l); l--; r++; } for (int j = 0; j < 26; j++) sz[i] += (int)g[i][j].size(); } ans = calc(0, 1) + (a[0][0] == 'a' ? 1 : a[0][0] == 'b' ? -1 : 0); if (ans) if (ans > 0) cout << "FIRST"; else cout << "SECOND"; else cout << "DRAW"; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; int n, val[26][26]; char s[26][26]; int pos[46][26], gv[1500010]; int f[46][(1 << 20) + 10]; bool v[46][(1 << 20) + 10]; void solve(int h, bool fs) { if (h == 2 * n) { v[h][1 << (n - 1)] = 1; f[h][1 << (n - 1)] = val[n][n]; return; } else solve(h + 1, !fs); int MAXS = 1 << min(h - 1, n); for (int S = 1; S < MAXS; S++) { if (gv[S & -S] < h - (n + 1)) continue; if (fs) f[h][S] = -inf; else f[h][S] = inf; int N = 0, i, j; for (int T = S, w; T; T -= w) { w = T & (-T), j = gv[w] + 1, i = h - j; if (i + 1 <= n) N |= (1 << (j - 1)); if (j + 1 <= n) N |= (1 << j); } for (int c = 0, M; c < 26; c++) { M = N & pos[h + 1][c]; if (M && v[h + 1][M]) { if (fs) f[h][S] = max(f[h][S], f[h + 1][M] + val[i][j]); else f[h][S] = min(f[h][S], f[h + 1][M] + val[i][j]); } } if (f[h][S] != inf && f[h][S] != -inf) v[h][S] = 1; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%s", s[i] + 1); for (int j = 1; j <= n; j++) { if (s[i][j] == 'a') val[i][j] = 1; if (s[i][j] == 'b') val[i][j] = -1; } } for (int t = 2 * n; t >= 2; t--) for (int i = 1, j; j = t - i, i <= n; i++) if (1 <= j && j <= n) pos[t][s[i][j] - 'a'] |= (1 << (j - 1)); for (int i = 0; i <= 20; i++) gv[1 << i] = i; solve(2, 0); if (f[2][1] > 0) printf("FIRST\n"); if (f[2][1] == 0) printf("DRAW\n"); if (f[2][1] < 0) printf("SECOND\n"); }
#include <bits/stdc++.h> using namespace std; int n, cch[40][1 << 20], sc[26] = {1, -1}; char gr[22][22], s[40][1 << 20]; int dp(int di, int msk) { if (di == 2 * n - 2) return 0; if (s[di][msk]) return cch[di][msk]; s[di][msk] = 1; int adv[26]; for (int i = 0; i < 26; i++) adv[i] = 0; for (int i = 0; i < 20; i++) if (msk & (1 << i)) { int y = min(di, n - 1) - i, x = max(0, di - n + 1) + i; if (y < n - 1) adv[gr[y + 1][x] - 'a'] |= (1 << (i - (di >= n - 1))); if (x < n - 1) adv[gr[y][x + 1] - 'a'] |= (1 << (i + (di < n - 1))); } int ans = di % 2 ? -999 : 999; for (int i = 0; i < 26; i++) if (adv[i]) { int mv = dp(di + 1, adv[i]) + sc[i]; if (di % 2) ans = max(ans, mv); else ans = min(ans, mv); } return cch[di][msk] = ans; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%s", gr[i]); int res = dp(0, 1) + sc[gr[0][0] - 'a']; printf(res > 0 ? "FIRST\n" : res < 0 ? "SECOND\n" : "DRAW\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const char leta = 'a'; const char letb = 'b'; const char win[] = "FIRST"; const char draw[] = "DRAW"; const char lose[] = "SECOND"; const int INF = 7e3; char board[23][23]; int n; int f[41][(1 << 20) + 7]; bool c[41][(1 << 20) + 7]; int dx[] = {0, 1}; int dy[] = {1, 0}; int diagsize(const int &id) { if (id < n) return (id + 1); else return (2 * n - 1 - id); } int diagpos(const int &first, const int &second) { if (first + second < n) return (first); else return (n - 1 - second); } pair<int, int> diagadd(const int &id, const int &pos) { assert(pos < diagsize(id)); if (id < n) return (pair<int, int>(pos, id - pos)); else return (pair<int, int>(id + pos + 1 - n, n - 1 - pos)); } void minimize(int &first, const int &second) { if (first > second) first = second; } void maximize(int &first, const int &second) { if (first < second) first = second; } void init(void) { scanf("%d", &n); int i; for (i = 0; i < n; i = i + 1) scanf("%s", board[i]); } int count(int id, int state) { assert(state > 0); if (c[id][state]) return (f[id][state]); if (id == 2 * n - 2) return (0); if (id % 2 == 0) f[id][state] = INF; else f[id][state] = -INF; int next[30]; memset(next, 0, sizeof next); int i, j, vx, vy; pair<int, int> p; for (i = 0; i < diagsize(id); i = i + 1) if ((state | (1 << i)) == state) { p = diagadd(id, i); for (j = 0; j < 2; j = j + 1) { vx = p.first + dx[j]; vy = p.second + dy[j]; next[board[vx][vy] - 'a'] = next[board[vx][vy] - 'a'] | (1 << diagpos(vx, vy)); } } for (i = 'a'; i <= 'z'; i = i + 1) if (next[i - 'a'] > 0) { if (id % 2 == 0) minimize(f[id][state], count(id + 1, next[i - 'a']) + (i == leta) - (i == letb)); else maximize(f[id][state], count(id + 1, next[i - 'a']) + (i == leta) - (i == letb)); } c[id][state] = true; assert(f[id][state] < INF); assert(f[id][state] > -INF); return (f[id][state]); } void process(void) { int res = count(0, 1) + (board[0][0] == leta) - (board[0][0] == letb); if (res > 0) printf("%s", win); if (res < 0) printf("%s", lose); if (res == 0) printf("%s", draw); } int main(void) { init(); process(); return 0; }
#include <bits/stdc++.h> using namespace std; int n, mp[30][30], dp[2][(1 << 20) + 5]; vector<pair<int, int> > p[30]; int num(int len, int x, int y) { if (len <= n) return x - 1; else return n - y; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { char c = getchar(); while (c < 'a' || c > 'z') c = getchar(); mp[i][j] = c - 'a'; } int m = (n << 1) - 1; int nw = 0, nx = 1; memset(dp, -127, sizeof dp); const int inf = -dp[0][0]; dp[nw][1] = 0; for (int i = 2 * n - 2; i > 0; i--) { swap(nw, nx); memset(dp[nw], -127, sizeof dp[nw]); int s = i <= n ? i : 2 * n - i; for (int j = 1; j < 1 << s; j++) { for (int c = 0; c < 26; c++) p[c].clear(); for (int x = 1; x <= n; x++) { int y = i + 2 - x; if (y >= 1 && y <= n) p[mp[x][y]].push_back(make_pair(x, y)); } for (int c = 0; c < 26; c++) { int v = (c == 0) - (c == 1); if (i & 1) v *= -1; int nj = 0; for (int k = 0; k < (int)p[c].size(); k++) { int x = p[c][k].first, y = p[c][k].second; if (x - 1 >= 1 && ((j >> num(i, x - 1, y)) & 1)) nj |= 1 << num(i + 1, x, y); if (y - 1 >= 1 && ((j >> num(i, x, y - 1)) & 1)) nj |= 1 << num(i + 1, x, y); } if (dp[nx][nj] != -inf) dp[nw][j] = max(dp[nw][j], v - dp[nx][nj]); } } } int ret = (mp[1][1] == 0) - (mp[1][1] == 1) - dp[nw][1]; if (ret > 0) printf("FIRST\n"); else if (ret == 0) printf("DRAW\n"); else printf("SECOND\n"); return 0; }
#include <bits/stdc++.h> using namespace std; const int MM = 45; char s[25][25]; int dp[41][1 << 20]; int f(char c) { if (c == 'a') return 1; if (c == 'b') return -1; return 0; } int main() { int two; int(n); scanf("%d", &n); two = (1 << n); for (int i = 0; i < (n); ++i) scanf("%s", (s[i])); dp[n + n - 2][1 << (n - 1)] = 0; for (int i = n + n - 3; i >= 0; i--) { int mask; if (i < n) mask = (1 << (i + 1)) - 1; else mask = (1 << n) - (1 << (i - n + 1)); for (int now = mask; now; now = (now - 1) & mask) { bool fail = false; char c = 0; int nxt[26] = {}; for (int j = 0; (1 << j) <= now; j++) { if ((now >> j) & 1) { if (!c) c = s[j][i - j]; else { if (c != s[j][i - j]) { fail = true; break; } } int y = i - j; if (j + 1 < n) nxt[s[j + 1][y] - 'a'] |= 1 << (j + 1); if (y + 1 < n) nxt[s[j][y + 1] - 'a'] |= 1 << j; } } if (fail) continue; int player, res; if (i & 1) { player = 0; res = -1000; } else { player = 1; res = 10000; } for (int j = 0; j < 26; j++) { if (!nxt[j]) continue; if (player == 0) res = max(res, dp[i + 1][nxt[j]] + f('a' + j)); else if (player == 1) res = min(res, dp[i + 1][nxt[j]] + f('a' + j)); } dp[i][now] = res; } } if (dp[0][1] + f(s[0][0]) > 0) puts("FIRST"); else if (dp[0][1] + f(s[0][0]) == 0) puts("DRAW"); else puts("SECOND"); return 0; }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:255000000") bool firstout = 1; template <class T> T &minn(T &a, T b) { if (b < a) a = b; return a; } template <class T> T &maxx(T &a, T b) { if (a < b) a = b; return a; } int &madd(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; return a; } int &msub(int &a, int b) { a -= b; if (a < 0) a += 1000000007; return a; } int &mmult(int &a, int b) { return a = (long long)a * b % 1000000007; } int mdiv(long long a, long long b, long long m) { a = (a % m + m) % m; b = (b % m + m) % m; if (a % b == 0) return a / b; return (a + m * mdiv(-a, m, b)) / b; } int n, m, q; char S[20][20 + 1]; int D[2 * 20][1 << 20]; int P[1012]; int fnd(int k, int d) { int &res = D[k][d]; if (res != 1012345678) return res; res = !(k & 1) ? 1012345678 : -1012345678; int r = 0; int i, j; char c; string moves = ""; for (i = (0); i < (n); ++i) if ((d >> i) & 1) { j = k - i; if (r == 0 && S[i][j] == 'a') r = 1; if (r == 0 && S[i][j] == 'b') r = -1; if (j < n - 1) { P[c = S[i][j + 1]] |= 1 << i; moves += c; } if (i < n - 1) { P[c = S[i + 1][j]] |= 1 << (i + 1); moves += c; } } if (k == 2 * n - 2) return res = r; vector<int> mm(moves.size()); for (i = 0; (moves)[i]; ++i) mm[i] = (P[c = moves[i]]), P[c] = 0; for (i = 0; (moves)[i]; ++i) if (mm[i] > 0) if (!(k & 1)) minn(res, fnd(k + 1, mm[i])); else maxx(res, fnd(k + 1, mm[i])); return res += r; } int main() { int i, j, k; char c; int a, d; int ts; for (ts = 1; scanf("%" "d", &(n)) > 0; ++ts) { for (i = (0); i < (n); ++i) scanf( "%" "s", &(S[i])); for (i = (0); i < (2 * n); ++i) for (j = (0); j < (1 << n); ++j) D[i][j] = 1012345678; for (i = (0); i < (1012); ++i) P[i] = 0; int res = fnd(0, 1); if (res > 0) printf( "%" "s", ("FIRST")); else if (res == 0) printf( "%" "s", ("DRAW")); else printf( "%" "s", ("SECOND")); printf("\n"), firstout = 1; } return 0; }