1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| #include <iostream> #include <ctime> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <climits> #include <queue> #include <set> #include <cmath> #include <map> #include <bitset> #include <fstream> #include <tr1/unordered_map> #include <assert.h>
#define x first #define y second #define y0 Y0 #define y1 Y1 #define mp make_pair #define pb push_back #define DEBUG(x) cout << #x << " = " << x << endl;
using namespace std;
typedef long long LL; typedef pair <int, int> pii;
template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> inline T read () { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; }
inline void proc_status () { ifstream t ("/proc/self/status"); cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl; }
const int MAXN = 2e5;
int N, Q[MAXN + 5]; pii Ans[MAXN + 5];
int Pos[MAXN + 5]; pii A[MAXN + 5], stk[MAXN + 5];
inline int cmp1 (const pii &a, const pii &b) { return a.y == b.y ? a.x > b.x : a.y > b.y; } inline int cmp2 (const pii &a, const pii &b) { return a.y == b.y ? a.x < b.x : a.y < b.y; }
inline pii operator - (const pii &a, const pii &b) { return mp (a.x - b.x, a.y - b.y); }
inline LL cross (pii o, pii a, pii b) { a = a - o, b = b - o; return (LL) a.x * b.y - (LL) a.y * b.x; }
inline void solve1 () { sort (A + 1, A + N + 1, cmp1); for (int i = 1; i <= N; ++i) Pos[A[i].x] = i;
int top = 0; for (int i = 1; i <= N; ++i) { while (top && stk[top].x < A[i].x) --top; while (top > 1 && cross (A[i], stk[top - 1], stk[top]) > 0) --top; stk[++top] = A[i];
if (A[Pos[Q[A[i].x]]].y <= A[i].y) { pii to = mp (A[Pos[Q[A[i].x]]].x, A[Pos[Q[A[i].x]]].y); int l = 1, r = top, ans = 0; while (l <= r) { int mid = (l + r) >> 1; if (stk[mid].x > to.x && (mid == 1 || cross (to, stk[mid], stk[mid - 1]) >= 0)) ans = mid, l = mid + 1; else r = mid - 1; }
if (ans) Ans[A[i].x] = stk[ans] - to; } } }
inline void solve2 () { sort (A + 1, A + N + 1, cmp2); for (int i = 1; i <= N; ++i) Pos[A[i].x] = i;
int top = 0; for (int i = 1; i <= N; ++i) { while (top && stk[top].x > A[i].x) --top; while (top > 1 && cross (A[i], stk[top - 1], stk[top]) > 0) --top; stk[++top] = A[i];
if (A[Pos[Q[A[i].x]]].y > A[i].y) { pii to = mp (A[Pos[Q[A[i].x]]].x, A[Pos[Q[A[i].x]]].y); int l = 1, r = top, ans = 0; while (l <= r) { int mid = (l + r) >> 1; if (stk[mid].x < to.x && (mid == 1 || cross (to, stk[mid], stk[mid - 1]) >= 0)) ans = mid, l = mid + 1; else r = mid - 1; }
if (ans) Ans[A[i].x] = to - stk[ans]; } } }
inline void Solve () { for (int i = 1; i <= N; ++i) Ans[i].x = -1; solve1 (); solve2 ();
for (int i = 1; i <= N; ++i) if (Ans[i].x == -1) puts("-1"); else printf("%d/%d\n", Ans[i].y / __gcd (Ans[i].x, Ans[i].y), Ans[i].x / __gcd (Ans[i].x, Ans[i].y)); }
inline void Input () { N = read<int>(); for (int i = 1; i <= N; ++i) A[i] = mp (i, read<int>()); for (int i = 1; i <= N; ++i) Q[i] = read<int>(); }
int main () {
freopen("falling.in", "r", stdin); freopen("falling.out", "w", stdout);
Input (); Solve ();
return 0; }
|