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
| #include <iostream> #include <cmath> #define maxn 100010 #define maxb 400 using namespace std;
const int N = 100000;
int n, m, a[maxn];
int fa[maxn]; void init_fa(int n) { for (int i = 0; i <= n; ++i) fa[i] = i; }
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void merge(int x, int y) { int fx = find(x), fy = find(y); if (fx == fy) return ; fa[fx] = fy; }
int blo, num, l[maxb], r[maxb], bl[maxn], d[maxb][maxn]; void init_blo(int n) { blo = sqrt(n); num = (n + blo - 1) / blo; for (int i = 1; i <= n; ++i) bl[i] = (i - 1) / blo + 1; for (int i = 1; i <= num; ++i) { l[i] = (i - 1) * blo + 1; r[i] = i * blo; } r[num] = n; for (int o = 1; o <= num; ++o) { int l = 0, r = N, k = 1, b = 0, v; init_fa(N); for (int i = ::l[o]; i <= ::r[o]; ++i) { v = k * a[i] + b; if (2 * v > l + r) { k = -1, b = v; for (int i = v; i <= r; ++i) merge(i, 2 * v - i); r = min(r, v); } else { k = 1, b = v; for (int i = l; i <= v; ++i) merge(i, 2 * v - i); l = max(l, v); } } for (int i = 0; i <= N; ++i) d[o][i] = k * (find(i) - b); } }
int query(int L, int R, int v) { int Bl = bl[L], Br = bl[R]; if (Bl == Br) { for (int i = L; i <= R; ++i) v = abs(v - a[i]); return v; } for (int i = L; i <= r[Bl]; ++i) v = abs(v - a[i]); for (int i = Bl + 1; i < Br; ++i) v = d[i][v]; for (int i = l[Br]; i <= R; ++i) v = abs(v - a[i]); return v; }
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; init_blo(n); for (int i = 1, lans = 0; i <= m; ++i) { int x, y, z; cin >> x >> y >> z; x ^= lans; y ^= lans; z ^= lans; cout << (lans = query(x, y, z)) << "\n"; } return 0; }
|