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
| #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define sqz main #define ll long long #define rep(i, a, b) for (int i = (a); i <= (b); i++) #define per(i, a, b) for (int i = (a); i >= (b); i--) #define Rep(i, a, b) for (int i = (a); i < (b); i++) #define travel(i, u) for (int i = head[u]; ~i; i = edge[i].next)
const ll INF = 1e9, Mo = 998244353; const int N = 50000; const double eps = 1e-6; namespace slow_IO { ll read() { ll x = 0; int zf = 1; char ch = getchar(); while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf; } void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0'); } } using namespace slow_IO;
int Belong[N + 5], Y[N + 5]; ll Ans[N + 5]; int cnt = 0; ll ans; struct node1 { int val, id; }X[N + 5]; int cmp1(node1 X, node1 Y) { return X.val < Y.val; } struct node2 { int l, r, id; }Q[N + 5]; int cmp2(node2 X, node2 Y) { return Belong[X.l] == Belong[Y.l] ? X.r < Y.r : Belong[X.l] < Belong[Y.l]; }
struct BinaryIndexedTree { int T[N + 5]; inline int lowbit(int x) { return ((x) & (-x)); } inline void Modify(int u, int v) { while (u <= cnt) { T[u] += v; u += lowbit(u); } } inline int Query(int u) { int ans = 0; while (u) { ans += T[u]; u -= lowbit(u); } return ans; } }BIT;
int sqz() { int n = read(), Size = sqrt(n); rep(i, 1, n) X[i].val = read(), X[i].id = i, Belong[i] = (i - 1) / Size + 1; sort(X + 1, X + n + 1, cmp1); rep(i, 1, n) { if (X[i].val != X[i - 1].val) cnt++; Y[X[i].id] = cnt; } int m = read(); rep(i, 1, m) Q[i].l = read(), Q[i].r = read(), Q[i].id = i; sort(Q + 1, Q + m + 1, cmp2); int L = 1, R = 0; rep(i, 1, m) { while (L < Q[i].l) BIT.Modify(Y[L], -1), ans -= BIT.Query(Y[L] - 1), L++; while (L > Q[i].l) --L, BIT.Modify(Y[L], 1), ans += BIT.Query(Y[L] - 1); while (R > Q[i].r) BIT.Modify(Y[R], -1), ans -= R - L - BIT.Query(Y[R]), R--; while (R < Q[i].r) ++R, BIT.Modify(Y[R], 1), ans += R - L + 1 - BIT.Query(Y[R]); Ans[Q[i].id] = ans; } rep(i, 1, m) printf("%lld\n", Ans[i]); return 0; }
|