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
| #include <algorithm> #include <array> #include <cmath> #include <iostream> #include <tuple> #include <vector> using namespace std; using ll = long long; const int N = 1e6 + 5, Q = 1e6 + 10;
namespace IO { inline char _getchar() { static const int BUFFER_SIZE = 1 << 15; static char *fs, *ft, fbuf[BUFFER_SIZE]; if (fs == ft) { ft = (fs = fbuf) + fread(fbuf, 1, BUFFER_SIZE, stdin); if (fs == ft) return EOF; } return *fs++; } template <typename T> void read(T &x) { x = 0; int f = 1; static char ch; while (!isdigit(ch = _getchar())) { if (ch == '-') f = -1; } for (; ch >= '0' && ch <= '9'; ch = _getchar()) x = x * 10 + ch - '0'; x *= f; } template <typename T, typename... Args> void read(T &x, Args &... args) { read(x); read(args...); } template <typename T> inline void write(T x) { if (x < 0) x = ~x + 1, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T, typename... Args> void write(T &x, Args &... args) { write(x); putchar(' '); write(args...); } } using namespace IO; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } pair<ll, ll> simply(ll x, ll y) { ll g = gcd(x, y); return make_pair(x / g, y / g); } int n, m, q; array<int, N> a, b; struct Data { int l, r, opt, id; }; vector<Data> op; array<int, N> len, belong, cnta, cntb; array<ll, Q> ans; int main() { read(n, m); for (int i = 1; i <= n; i++) { read(a[i]); } for (int i = 1; i <= m; i++) { read(b[i]); } read(q); op.resize(4 * q); int tot = 0; for (int i = 1; i <= q; i++) { int l, r, x, y; read(l, r, x, y); op[tot++] = (Data{r, y, 1, i}); op[tot++] = (Data{l - 1, y, -1, i}); op[tot++] = (Data{r, x - 1, -1, i}); op[tot++] = (Data{l - 1, x - 1, 1, i}); len[i] = y - x + 1; } int BLOCK_SIZE = (n / sqrt(4 * q) + 1) + 1; for (int i = 1; i <= max(n, m); i++) { belong[i] = (i - 1) / BLOCK_SIZE + 1; } sort(op.begin(), op.end(), [&](const Data &A, const Data &B) -> bool { if (belong[A.l] == belong[B.l]) { if (belong[A.l] & 1) { return belong[A.r] > belong[B.r]; } return belong[A.r] < belong[B.r]; } return A.l < B.l; }); ll now(0ll); int l = 0, r = 0; auto change_a = [&](int x, int v) -> void { now += cntb[a[x]] * v; cnta[a[x]] += v; }; auto change_b = [&](int x, int v) -> void { now += cnta[b[x]] * v; cntb[b[x]] += v; }; for_each(op.begin(), op.end(), [&](const Data &x) -> void { while (l < x.l) { l++; change_a(l, 1); } while (l > x.l) { change_a(l, -1); l--; } while (r < x.r) { ++r; change_b(r, 1); } while (r > x.r) { change_b(r, -1); r--; } ans[x.id] += now * x.opt; }); for (int i = 1; i <= q; i++) { auto res = simply(ans[i], len[i]); write(res.first, res.second); puts(""); } }
|