#include #pragma GCC optimize ("O3") #pragma GCC target ("avx") using namespace std; using int64 = long long; const int mod = 1e9 + 7; const int64 infll = (1LL << 62) - 1; const int inf = (1 << 30) - 1; struct IoSetup { IoSetup() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cerr << fixed << setprecision(10); } } iosetup; template< typename T1, typename T2 > ostream &operator<<(ostream &os, const pair< T1, T2 > &p) { os << p.first << " " << p.second; return os; } template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; } template< typename T > ostream &operator<<(ostream &os, const vector< T > &v) { for(int i = 0; i < (int) v.size(); i++) { os << v[i] << (i + 1 != v.size() ? " " : ""); } return os; } template< typename T > istream &operator>>(istream &is, vector< T > &v) { for(T &in : v) is >> in; return is; } template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template< typename T = int64 > vector< T > make_v(size_t a) { return vector< T >(a); } template< typename T, typename... Ts > auto make_v(size_t a, Ts... ts) { return vector< decltype(make_v< T >(ts...)) >(a, make_v< T >(ts...)); } template< typename T, typename V > typename enable_if< is_class< T >::value == 0 >::type fill_v(T &t, const V &v) { t = v; } template< typename T, typename V > typename enable_if< is_class< T >::value != 0 >::type fill_v(T &t, const V &v) { for(auto &e : t) fill_v(e, v); } template< typename F > struct FixPoint : F { FixPoint(F &&f) : F(forward< F >(f)) {} template< typename... Args > decltype(auto) operator()(Args &&... args) const { return F::operator()(*this, forward< Args >(args)...); } }; template< typename F > inline decltype(auto) MFP(F &&f) { return FixPoint< F >{forward< F >(f)}; } template< class Monoid > struct RandomizedBinarySearchTree { using F = function< Monoid(Monoid, Monoid) >; inline int xor128() { static int x = 123456789; static int y = 362436069; static int z = 521288629; static int w = 88675123; int t; t = x ^ (x << 11); x = y; y = z; z = w; return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)); } struct Node { Node *l, *r; int cnt; Monoid key, sum; Node() = default; Node(const Monoid &k) : cnt(1), key(k), sum(k), l(nullptr), r(nullptr) {} }; const Monoid M1; const F f; RandomizedBinarySearchTree(const F &f, const Monoid &M1) : f(f), M1(M1) {} inline Node *alloc(const Monoid &key) { return new Node(key); } virtual Node *clone(Node *t) { return t; } inline int count(const Node *t) { return t ? t->cnt : 0; } inline Monoid sum(const Node *t) { return t ? t->sum : M1; } inline Node *update(Node *t) { t->cnt = count(t->l) + count(t->r) + 1; t->sum = f(f(sum(t->l), t->key), sum(t->r)); return t; } Node *merge(Node *l, Node *r) { if(!l || !r) return l ? l : r; if(xor128() % (l->cnt + r->cnt) < l->cnt) { l->r = merge(l->r, r); return update(l); } else { r->l = merge(l, r->l); return update(r); } } pair< Node *, Node * > split(Node *t, int k) { if(!t) return {t, t}; if(k <= count(t->l)) { auto s = split(t->l, k); t->l = s.second; return {s.first, update(t)}; } else { auto s = split(t->r, k - count(t->l) - 1); t->r = s.first; return {update(t), s.second}; } } Node *build(int l, int r, const vector< Monoid > &v) { if(l + 1 >= r) return alloc(v[l]); return merge(build(l, (l + r) >> 1, v), build((l + r) >> 1, r, v)); } Node *build(const vector< Monoid > &v) { return build(0, (int) v.size(), v); } void dump(Node *r, typename vector< Monoid >::iterator &it) { if(!r) return; dump(r->l, it); *it = r->key; dump(r->r, ++it); } vector< Monoid > dump(Node *r) { vector< Monoid > v((size_t) count(r)); auto it = begin(v); dump(r, it); return v; } string to_string(Node *r) { auto s = dump(r); string ret; for(int i = 0; i < s.size(); i++) ret += ", "; return (ret); } void insert(Node *&t, int k, const Monoid &v) { auto x = split(t, k); t = merge(merge(x.first, alloc(v)), x.second); } void erase(Node *&t, int k) { auto x = split(t, k); t = merge(x.first, split(x.second, 1).second); } Monoid query(Node *&t, int a, int b) { auto x = split(t, a); auto y = split(x.second, b - a); auto ret = sum(y.first); t = merge(x.first, merge(y.first, y.second)); return ret; } void set_element(Node *&t, int k, const Monoid &x) { if(k < count(t->l)) set_element(t->l, k, x); else if(k == count(t->l)) t->key = t->sum = x; else set_element(t->r, k - count(t->l) - 1, x); t = update(t); } int size(Node *t) { return count(t); } bool empty(Node *t) { return !t; } Node *makeset() { return nullptr; } int lower_bound(Node *t, const Monoid x) { if(!t) return 0; if(x <= t->key) return lower_bound(t->l, x); return lower_bound(t->r, x) + count(t->l) + 1; } Monoid lower_bound_sum(Node *t, const Monoid x) { if(!t) return 0; if(x <= t->key) return lower_bound_sum(t->l, x); return lower_bound_sum(t->r, x) + sum(t->l) + t->key; } Monoid kth_element(Node *t, int k) { if(k < count(t->l)) return kth_element(t->l, k); if(k == count(t->l)) return t->key; return kth_element(t->r, k - count(t->l) - 1); } void insert_key(Node *&t, const Monoid &x) { insert(t, lower_bound(t, x), x); } void erase_key(Node *&t, const Monoid &x) { erase(t, lower_bound(t, x)); } }; template< class T > struct BinaryIndexedTree { vector< T > data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for(++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for(++k; k < data.size(); k += k & -k) data[k] += x; } }; int main() { auto f = [](const int64 &a, const int64 &b) { return a + b; }; using RBST = RandomizedBinarySearchTree< int64 >; RBST rbst(f, 0LL); int N, Q; cin >> N >> Q; BinaryIndexedTree< int64 > bit(N); int sz = 1; while(sz < N) sz <<= 1; vector< RBST::Node * > seg(2 * sz, nullptr); vector< int64 > A(N); cin >> A; auto add = [&](int k, int64 x) { bit.add(k, x); k += sz; rbst.insert_key(seg[k], x); while(k >>= 1) rbst.insert_key(seg[k], x); }; auto erase = [&](int k, int64 x) { bit.add(k, -x); k += sz; rbst.erase_key(seg[k], x); while(k >>= 1) rbst.erase_key(seg[k], x); }; auto query1 = [&](int a, int b, int64 x) { int64 ret = 0; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) ret += rbst.lower_bound(seg[a++], x); if(b & 1) ret += rbst.lower_bound(seg[--b], x); } return ret; }; auto query2 = [&](int a, int b, int64 x) { int64 sum = 0; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) sum += rbst.lower_bound_sum(seg[a++], x); if(b & 1) sum += rbst.lower_bound_sum(seg[--b], x); } return sum; }; for(int i = 0; i < N; i++) { add(i, A[i]); } int64 S = 0; while(Q--) { int T; cin >> T; if(T == 1) { int64 X, Y; cin >> X >> Y; X ^= S % (1LL << 16); Y ^= S % (1LL << 40); --X; erase(X, A[X]); A[X] = Y; add(X, A[X]); } else { int64 L, R; cin >> L >> R; L ^= S % (1LL << 16); R ^= S % (1LL << 16); if(L > R) swap(L, R); --L; int64 mid = (R - L) / 2; int64 mid_val = 0; for(int i = 39; i >= 0; i--) { if(query1(L, R, mid_val | (1LL << i)) <= mid) mid_val |= 1LL << i; } ++mid_val; int64 low_cnt = query1(L, R, mid_val); int64 low_sum = query2(L, R, mid_val); --mid_val; int64 high_cnt = R - L; int64 high_sum = bit.sum(R - 1) - bit.sum(L - 1); high_cnt -= low_cnt; high_sum -= low_sum; int64 ans = high_sum - high_cnt * mid_val + low_cnt * mid_val - low_sum; cout << ans << "\n"; S ^= ans; } } }