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
| #include <cstdio> #include <algorithm> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } typedef long long LL; const int N = 2e5 + 10, B = 450; int n, m, w[N], p[N], tot, id[N], L[N], R[N], tr[B][N]; void add (int x, int y, int k) { for (int i = x; i <= tot; i += i & -i) for (int j = y; j <= n; j += j & -j) tr[i][j] += k; } int query (int x, int y) { int res = 0; for (int i = x; i; i -= i & -i) for (int j = y; j; j -= j & -j) res += tr[i][j]; return res; } int main () { read(n, m); for (int i = 1; i <= n; ++i) id[i] = (i - 1) / B + 1; tot = id[n]; for (int i = 1; i <= tot; ++i) L[i] = R[i - 1] + 1, R[i] = R[i - 1] + B; R[tot] = n; for (int i = 1, a; i <= n; ++i) read(a), p[a] = i; for (int i = 1; i <= n; ++i) read(w[i]), add(id[i], w[i] = p[w[i]], 1); for (int op; m; --m) { read(op); if (op == 1) { int a, b, l, r; read(a, b, l, r); if (id[l] == id[r]) { int res = 0; for (int i = l; i <= r; ++i) res += w[i] >= a && w[i] <= b; write(res), puts(""); } else { int res = 0; for (int i = l; i <= R[id[l]]; ++i) res += w[i] >= a && w[i] <= b; res += query(id[r] - 1, b) - query(id[r] - 1, a - 1) - query(id[l], b) + query(id[l], a - 1); for (int i = L[id[r]]; i <= r; ++i) res += w[i] >= a && w[i] <= b; write(res), puts(""); } } else if (op == 2) { int a, b; read(a, b); add(id[a], w[a], -1), add(id[b], w[b], -1); swap(w[a], w[b]); add(id[a], w[a], 1), add(id[b], w[b], 1); } } return 0; }
|