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
| #include <cstdio> #include <algorithm> #include <cmath>
int a[50005], cnt[50005][2], Q[50005], ans, S, tot; struct Question { int op, id, l, r; inline bool operator < (const Question a) const { int x((l - 1) / S), y((a.l - 1) / S); return x == y ? (r - 1) / S < (a.r - 1) / S + 1 : x < y; } } q[200005];
inline void add(const int x, const int d) { ans += cnt[x][d ^ 1], ++ cnt[x][d]; } inline void del(const int x, const int d) { ans -= cnt[x][d ^ 1], -- cnt[x][d]; }
int main() { int n, m, l(0), r(0); scanf("%d", &n); for (int i(1); i <= n; ++ i) scanf("%d", a + i); scanf("%d", &m); S = n / sqrt(m); for (int i(1); i <= m; ++ i) { int l1, l2, r1, r2; scanf("%d%d%d%d", &l1, &r1, &l2, &r2); q[++ tot].op = 1, q[tot].id = i, q[tot].l = r1, q[tot].r = r2; q[++ tot].op = -1, q[tot].id = i, q[tot].l = l1 - 1, q[tot].r = r2; q[++ tot].op = -1, q[tot].id = i, q[tot].l = l2 - 1, q[tot].r = r1; q[++ tot].op = 1, q[tot].id = i, q[tot].l = l1 - 1, q[tot].r = l2 - 1; for (int j(tot - 3); j <= tot; ++ j) if (q[j].l > q[j].r) std::swap(q[j].l, q[j].r); } std::sort(q + 1, q + tot + 1); for (int i(1); i <= tot; ++ i) { while (l < q[i].l) add(a[++ l], 0); while (l > q[i].l) del(a[l --], 0); while (r < q[i].r) add(a[++ r], 1); while (r > q[i].r) del(a[r --], 1); Q[q[i].id] += q[i].op * ans; } for (int i(1); i <= m; ++ i) printf("%d\n", Q[i]); return 0; }
|