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
|
#include <bits/stdc++.h> using namespace std; #define resetIO(x) \ freopen(#x ".in", "r", stdin), freopen(#x ".out", "w", stdout) #define debug(fmt, ...) \ fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) template <class _Tp> inline _Tp& read(_Tp& x) { bool sign = false; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) sign |= (ch == '-'); for (x = 0; isdigit(ch); ch = getchar()) x = x * 10 + (ch ^ 48); return sign ? (x = -x) : x; } template <class _Tp> inline void write(_Tp x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar((x % 10) ^ 48); } bool m_be; using ll = long long; const int MAXN = 5e5 + 10; const int MAXB = 710; const int INF = 0x3f3f3f3f; int n, m, q, a[MAXN], id[MAXN], val[MAXN], dp[MAXB][MAXB]; int blk, bn, bl[MAXB], br[MAXB], pos[MAXN]; vector<int> vec[MAXN]; inline bool inseq(int k, int p, int l, int r) { return p >= 0 && p < vec[k].size() && l <= vec[k][p] && vec[k][p] <= r; } bool m_ed; signed main() { read(n), read(q), blk = ceil(sqrt(n)); for (int i = 1; i <= n; ++i) read(a[i]), val[i] = a[i]; sort(val + 1, val + n + 1); m = unique(val + 1, val + n + 1) - val - 1; for (int i = 1; i <= n; ++i) { a[i] = lower_bound(val + 1, val + m + 1, a[i]) - val; vec[a[i]].push_back(i), id[i] = vec[a[i]].size() - 1; } for (int l = 1; l <= n; l += blk) { int r = min(n, l + blk - 1); ++bn, bl[bn] = l, br[bn] = r; for (int i = l; i <= r; ++i) pos[i] = bn; } for (int i = 1; i <= bn; ++i) for (int j = i; j <= bn; ++j) { dp[i][j] = dp[i][j - 1]; for (int k = bl[j]; k <= br[j]; ++k) while (inseq(a[k], id[k] - dp[i][j], bl[i], br[j])) ++dp[i][j]; } int ans = 0; while (q--) { int l, r; read(l), read(r); l ^= ans, r ^= ans, ans = 0; if (pos[l] == pos[r]) { for (int i = l; i <= r; ++i) while (inseq(a[i], id[i] - ans, l, r)) ++ans; } else { ans = dp[pos[l] + 1][pos[r] - 1]; for (int i = l; i <= br[pos[l]]; ++i) while (inseq(a[i], id[i] + ans, l, r)) ++ans; for (int i = bl[pos[r]]; i <= r; ++i) while (inseq(a[i], id[i] - ans, l, r)) ++ans; } write(ans), putchar('\n'); } return 0; }
|