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
| #include <bits/stdc++.h> #define STC static #define fi first #define se second #define IL inline namespace FIO { const int L = (1 << 20) + 5; char *E, *S, buf[L], out[L]; int l = 0; #define gh() (E == S ? E = (S = buf) + fread(buf, 1, L, stdin), (E == S ? EOF : *S++) : *S++) template<class T> IL void read(T &x) { char ch = gh(), t = 0; for ( ; ch < '0' || ch > '9'; ch = gh()) t |= ch == '-'; for (x = 0; ch >= '0' && ch <= '9'; ch = gh()) x = x * 10 + (ch ^ 48); if (t) x = -x; } IL void flus(){ fwrite(out, 1, l, stdout), l = 0; } IL void check(){ if (l >= L - 5) flus(); } IL void putc(char x){ out[l++] = x, check(); } template<class T> IL void write(T x) { if (x < 0) putc('-'), x = -x; if (x > 9) write(x / 10); out[l++] = x % 10 + 48, check(); } } using FIO::read; using FIO::flus; using FIO::putc; using FIO::write; using PII = std::pair<int, int>; using LL = long long; const int N = 1e5 + 100, B = 320, NB = 320;
int n, m, a[N], num = 0, bl[N], cnt[NB][N], pre[N], suf[N]; PII a2[N], a3[N]; LL as[NB][NB], ans; struct Block{ int l, r; } b[NB]; namespace BIT { #define lb(x) ((x) & (-(x))) int ct[N]; IL void add(int x, int d){ for (; x <= n; x += lb(x)) ct[x] += d; } IL int ask(int x) { int res = 0; for (; x; x ^= lb(x)) res += ct[x]; return res; } #undef lb } IL int merge(int x[], int y[], int nx, int ny) { int res = 0; for (int ix = 1, iy = 1; ix <= nx && iy <= ny; ) if (x[ix] < y[iy]) ++ix; else res += nx - ix + 1, ++iy; return res; } IL void ready(int id) { Block *x = &b[id]; int t = 0; for (int i = x->l; i <= x->r; ++i) bl[i] = id; for (int i = x->l; i <= x->r; ++i) { BIT::add(a[i], 1); t += BIT::ask(n) - BIT::ask(a[i]); pre[i] = t; } as[id][id] = t; for (int i = x->l; i <= x->r; ++i) { suf[i] = t; BIT::add(a[i], -1); t -= BIT::ask(a[i] - 1); } std::sort(a2 + x->l, a2 + x->r + 1); for (int i = 1, j = x->l; i <= n; ++i) { while (j <= x->r && a3[i].fi > a2[j].fi) ++j; if (a3[i].se < x->l) cnt[id][a3[i].se] = j - x->l; else if (a3[i].se > x->r) cnt[id][a3[i].se] = x->r - j + 1; } for (int i = 2; i <= n; ++i) cnt[id][i] += cnt[id][i - 1]; } IL int get(int l, int r, int id){ return cnt[id][r] - cnt[id][l - 1]; } IL void ready2() { for (int len = 2; len <= num; ++len) for (int i = 1, j; (j = i + len - 1) <= num; ++i) as[i][j] = as[i + 1][j] + as[i][j - 1] - as[i + 1][j - 1] + get(b[i].l, b[i].r, j); } IL void ask(int l, int r) { STC int tp1[N], tp2[N], ct1, ct2, ll, rr; ct1 = ct2 = 0; if ((ll = bl[l]) == (rr = bl[r])) { Block *x = &b[bl[l]]; for (int i = x->l; i <= x->r; ++i) if (l <= a2[i].se && r >= a2[i].se) tp2[++ct2] = a2[i].fi; else if (a2[i].se < l) tp1[++ct1] = a2[i].fi; ans = pre[r] - (l == x->l ? 0 : pre[l - 1]) - merge(tp1, tp2, ct1, ct2); } else { ans = as[ll + 1][rr - 1] + suf[l] + pre[r]; for (int i = ll + 1; i < rr; ++i) ans = ans + get(l, b[ll].r, i) + get(b[rr].l, r, i); for (int i = b[ll].l; i <= b[ll].r; ++i) if (a2[i].se >= l) tp1[++ct1] = a2[i].fi; for (int i = b[rr].l; i <= b[rr].r; ++i) if (a2[i].se <= r) tp2[++ct2] = a2[i].fi; ans = ans + merge(tp1, tp2, ct1, ct2); } } int main() { read(n), read(m); for (int i = 1; i <= n; ++i) read(a[i]), a2[i] = a3[i] = {a[i], i}; std::sort(a3 + 1, a3 + 1 + n); for (int i = 1; i <= n; i += B) b[++num].l = i, b[num].r = i + B - 1; b[num].r = n; for (int i = 1; i <= num; ++i) ready(i); ready2(); for (int l, r; m--; write(ans), putc('\n')) read(l), read(r), ask(l ^ ans, r ^ ans); return flus(), 0; }
|