structNode { int sum; Node() {} inline Node operator + (const Node & b) const { Node ans; ans.sum = sum + b.sum; return ans; } }; template<constint N > classPerSegTree { public : Node node[N]; int cntn; int lc[N], rc[N]; int rt[N];
inlineint& operator [] (int x) { return rt[x]; }
intBuild(int L, int R){ int root = ++cntn; if(L == R) return root; int mid = L + R >> 1; lc[root] = Build(L, mid); rc[root] = Build(mid + 1, R); return root; }
intUpdate(int p, int L, int R, int val){ int dir = ++cntn; lc[dir] = lc[p], rc[dir] = rc[p]; node[dir].sum = node[p].sum + 1; if(L == R) return dir; int mid = L + R >> 1; if(val <= mid) lc[dir] = Update(lc[dir], L, mid, val); else rc[dir] = Update(rc[dir], mid+1,R,val); return dir; }
intQuery(int u, int v, int L, int R, int val){ int mid = L + R >> 1; int x = node[lc[v]].sum - node[lc[u]].sum; if(L == R) return L; if(val <= x) returnQuery(lc[u], lc[v], L, mid, val); else returnQuery(rc[u], rc[v], mid+1, R, val - x); } };
constint N = 1e5 + 10;
int n, m; int a[N];
int uni[N], unilen;
inlinevoidInput(){ read(n, m); for(int i = 1; i <= n; i++) { read(a[i]); } }
namespace FastIO { template<typename T> inlinevoidread(T& x){ x = 0; int f = 1; char ch; while (!isdigit(ch = getchar())) if (ch == '-') f = -1; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x *= f; } template<typename T, typename... Args> inlinevoidread(T& x, Args &...x_){ read(x); read(x_...); } } usingnamespace FastIO;
constint N = 1e5 + 10, M = 1e5 + 10;
int n, m, tot, root[N]; ll ans = 1; int a[M], b[M], num; vector<int> be[N], ed[N];
structTree { ll sum; int cnt, l, r; } t[N << 6];
inlinevoidupdate(int &u, int l, int r, int pre, int pos, int v){ u = ++tot, t[u] = t[pre]; t[u].cnt += v, t[u].sum += 1LL * v * b[pos]; if (l == r) return; int mid = (l + r) >> 1; if (pos <= mid) update(t[u].l, l, mid, t[pre].l, pos, v); elseupdate(t[u].r, mid + 1, r, t[pre].r, pos, v); }
inline ll query(int u, int l, int r, int k){ int num = t[t[u].l].cnt; if (l == r) return1LL * t[u].sum / t[u].cnt * k; int mid = (l + r) >> 1; if (k <= num) returnquery(t[u].l, l, mid, k); returnquery(t[u].r, mid + 1, r, k - num) + t[t[u].l].sum; }
inlinevoidInput(){ read(m, n); int x, y; for (int i = 1; i <= m; i++) { read(x, y, a[i]); b[i] = a[i]; be[x].push_back(i), ed[y + 1].push_back(i); } }
inlinevoidInit(){ sort(b + 1, b + 1 + m); num = unique(b + 1, b + 1 + m) - b - 1; for (int i = 1; i <= n; i++) { root[i] = root[i - 1]; for (int j : be[i]) { int pos = lower_bound(b + 1, b + 1 + num, a[j]) - b; update(root[i], 1, num, root[i], pos, 1); } for (int j : ed[i]) { int pos = lower_bound(b + 1, b + 1 + num, a[j]) - b; update(root[i], 1, num, root[i], pos, -1); } } }
inlinevoidWork(){ Init(); int x, A, B, C; for (int i = 1; i <= n; i++) { read(x, A, B, C); int k = (1LL * A * ans + B) % C + 1; if (k > t[root[x]].cnt) ans = t[root[x]].sum; else ans = query(root[x], 1, num, k); printf("%lld\n", ans); } }
intmain(){ int T = 1; while(T--) { Input(); Work(); } return0; }
namespace BIT { constint M = N << 5; int c[M]; inlinevoidModify(int x, int val, int n){ for(int i = x; i <= n; i += lowbit(i)) c[i] += val; } inlineintPreSum(int x){ int ans = 0; for(int i = x; i > 0; i -= lowbit(i)) ans += c[i]; return ans; } inlineintQuery(int l, int r){ returnPreSum(r) - PreSum(l - 1); } };
namespace PerSegTree { constint M = N << 7; int cnt[M], lc[M], rc[M]; int cntn;
voidUpdate(int &p, int L, int R, int val, int f){ if(p == 0) p = ++cntn; cnt[p] += f; if(L == R) return ; int mid = L + R >> 1; if(val <= mid) Update(lc[p], L, mid, val, f); elseUpdate(rc[p], mid + 1, R, val, f); }
ll Query1(int p, int L, int R, int val){ if(p == 0) return0; if(L == R) { if(val < L) return cnt[p]; return0; } int mid = L + R >> 1; if(val > mid) returnQuery1(rc[p], mid + 1, R, val); return cnt[rc[p]] + Query1(lc[p], L, mid, val); } ll Query2(int p, int L, int R, int val){ if(p == 0) return0; if(L == R) { if(val > L) return cnt[p]; return0; } int mid = L + R >> 1; if(val <= mid) returnQuery2(lc[p], L, mid, val); return cnt[lc[p]] + Query2(rc[p], mid + 1, R, val); } };
int n, Q; int a[N], b[N];
ll ans;
int rt[N];
inlinevoidInput(){ read(n, Q); for(int i = 1; i <= n; i++) { read(a[i]), b[a[i]] = i; ans += BIT::Query(a[i] + 1, n); BIT::Modify(a[i], 1, n); for(int j = i-lowbit(i)+1; j <= i; j++) PerSegTree::Update(rt[i], 1, n, a[j], 1); } }
inline ll QueryBigger(int l, int r, int val){ if(l > r) return0; l--; ll ans = 0; for(int i = r; i > 0; i -= lowbit(i)) ans += PerSegTree::Query1(rt[i], 1, n, val); for(int i = l; i > 0; i -= lowbit(i)) ans -= PerSegTree::Query1(rt[i], 1, n, val); return ans; }
inline ll QueryLitter(int l, int r, int val){ if(l > r) return0; l--; ll ans = 0; for(int i = r; i > 0; i -= lowbit(i)) ans += PerSegTree::Query2(rt[i], 1, n, val); for(int i = l; i > 0; i -= lowbit(i)) ans -= PerSegTree::Query2(rt[i], 1, n, val); return ans; }
inline ll CountBackW(int del, int pos){ ll Bigger = QueryBigger(1, b[del]-1, del); ll Litter = QueryLitter(b[del]+1, n, del); return Bigger + Litter; }
inlinevoidDelete(int del, int pos){ for(int i = pos; i <= n; i += lowbit(i)) { PerSegTree::Update(rt[i], 1, n, del, -1); } }
inlinevoidWork(){ int del; while(Q--) { printf("%lld\n", ans); read(del); ans -= CountBackW(del, b[del]); Delete(del, b[del]); } }
intmain(){ int T = 1; while(T--) { Input(); Work(); } return0; }
int n, m, sz, totn, totx, toty; int a[N], b[N << 1], ca[N], cb[N], cc[N]; int xx[N], yy[N], rt[N], size[600 * N], ls[600 * N], rs[600 * N];
voidInsert(int& o, int l, int r, int x, int q, int v){ o = ++sz; size[o] = size[x] + v; ls[o] = ls[x]; rs[o] = rs[x]; if (l == r) return; int mid = (l + r) >> 1; if (q <= mid) Insert(ls[o], l, mid, ls[x], q, v); elseInsert(rs[o], mid + 1, r, rs[x], q, v); }
intQuery(int l, int r, int q){ if (l == r) return l; int sum = 0, mid = (l + r) >> 1; for (int i = 1; i <= totx; i++) sum -= size[ls[xx[i]]]; for (int i = 1; i <= toty; i++) sum += size[ls[yy[i]]]; if (q <= sum) { for (int i = 1; i <= totx; i++) xx[i] = ls[xx[i]]; for (int i = 1; i <= toty; i++) yy[i] = ls[yy[i]]; returnQuery(l, mid, q); } else { for (int i = 1; i <= totx; i++) xx[i] = rs[xx[i]]; for (int i = 1; i <= toty; i++) yy[i] = rs[yy[i]]; returnQuery(mid + 1, r, q - sum); } }
voidAdd(int x, int v){ int k = lower_bound(b + 1, b + totn + 1, a[x]) - b; for (int i = x; i <= n; i += lowbit(i)) Insert(rt[i], 1, totn, rt[i], k, v); }
voidInput(){ read(n, m); for (int i = 1; i <= n; i++) read(a[i]), b[++totn] = a[i]; for (int i = 1; i <= m; i++) { char s[20]; scanf("%s", s); read(ca[i], cb[i]); if (s[0] == 'Q') read(cc[i]); else b[++totn] = cb[i]; } sort(b + 1, b + totn + 1); totn = unique(b + 1, b + totn + 1) - b - 1; for (int i = 1; i <= n; i++) Add(i, 1); }
voidWork(){ for (int i = 1; i <= m; i++) { if (cc[i]) { totx = toty = 0; for (int j = ca[i] - 1; j; j -= lowbit(j)) xx[++totx] = rt[j]; for (int j = cb[i]; j; j -= lowbit(j)) yy[++toty] = rt[j]; printf("%d\n", b[Query(1, totn, cc[i])]); } else { Add(ca[i], -1); a[ca[i]] = cb[i]; Add(ca[i], 1); } } }
intmain(){ int T = 1; while (T--) { Input(); Work(); } return0; }