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
| #include <cstdio> #include <algorithm> using namespace std; const int N = 3e5 + 10, M = 2e7 + 10; struct Node { int l, r, v; } tr[M]; int idx; int n, m, w[N], rt[N]; void modify(int &x, int l, int r, int t, int k) { tr[++idx] = tr[x]; x = idx; if (l == r) { tr[x].v += k; return; } int mid = l + r >> 1; if (t <= mid) modify(tr[x].l, l, mid, t, k); else modify(tr[x].r, mid + 1, r, t, k); } int query(int x1, int x2, int l, int r, int c) { if (l == r) return tr[x2].v - tr[x1].v; int mid = l + r >> 1; if (c <= mid) return query(tr[x1].l, tr[x2].l, l, mid, c); return query(tr[x1].r, tr[x2].r, mid + 1, r, c); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &w[i]); rt[i] = rt[i - 1]; modify(rt[i], 1, 300000, w[i], 1); } for (int op; m; m--) { scanf("%d", &op); if (op == 1) { int l, r, c; scanf("%d%d%d", &l, &r, &c); printf("%d\n", query(rt[l - 1], rt[r], 1, 300000, c)); } else if (op == 2) { int x; scanf("%d", &x); modify(rt[x], 1, 300000, w[x], -1); modify(rt[x], 1, 300000, w[x + 1], 1); swap(w[x], w[x + 1]); } } return 0; }
|