import java.io.*; import java.util.*; public class Main { static class FastInput { private final InputStream in; private final byte[] buf = new byte[1 << 16]; private int ptr = 0, len = 0; FastInput(InputStream is) { this.in = is; } private int read() throws IOException { if (ptr >= len) { len = in.read(buf); ptr = 0; if (len <= 0) return -1; } return buf[ptr++]; } int nextInt() throws IOException { int c, sgn = 1, x = 0; do { c = read(); } while (c <= 32 && c != -1); if (c == -1) return Integer.MIN_VALUE; if (c == '-') { sgn = -1; c = read(); } while (c > 32) { x = x * 10 + (c - '0'); c = read(); } return x * sgn; } } static final Random RNG = new Random(712367123); static class Node { int val; int pr; int sz; long sum; boolean hasAssign; int assignVal; int addVal; Node l, r; Node(int v) { val = v; pr = RNG.nextInt(); sz = 1; sum = v; } } static int size(Node t) { return t == null ? 0 : t.sz; } static long sum(Node t) { return t == null ? 0L : t.sum; } static void applyAssign(Node t, int x) { if (t == null) return; t.hasAssign = true; t.assignVal = x; t.addVal = 0; t.val = x; t.sum = (long) x * t.sz; } static void applyAdd(Node t, int x) { if (t == null) return; if (t.hasAssign) { t.assignVal += x; t.val = t.assignVal; } else { t.val += x; } t.addVal += x; t.sum += (long) x * t.sz; } static void push(Node t) { if (t == null) return; if (t.hasAssign) { applyAssign(t.l, t.assignVal); applyAssign(t.r, t.assignVal); t.hasAssign = false; } if (t.addVal != 0) { applyAdd(t.l, t.addVal); applyAdd(t.r, t.addVal); t.addVal = 0; } } static void pull(Node t) { if (t == null) return; t.sz = 1 + size(t.l) + size(t.r); t.sum = (long) t.val + sum(t.l) + sum(t.r); } static Node[] split(Node t, int k) { if (t == null) return new Node[] {null, null}; push(t); int ls = size(t.l); if (ls >= k) { Node[] res = split(t.l, k); t.l = res[1]; pull(t); return new Node[] {res[0], t}; } else { Node[] res = split(t.r, k - ls - 1); t.r = res[0]; pull(t); return new Node[] {t, res[1]}; } } static Node merge(Node a, Node b) { if (a == null) return b; if (b == null) return a; if (a.pr > b.pr) { push(a); a.r = merge(a.r, b); pull(a); return a; } else { push(b); b.l = merge(a, b.l); pull(b); return b; } } static long rangeSum(Node[] rootRef, int L, int R) { Node root = rootRef[0]; Node[] t1 = split(root, L); Node[] t2 = split(t1[1], R - L + 1); long ans = sum(t2[0]); rootRef[0] = merge(t1[0], merge(t2[0], t2[1])); return ans; } static void rangeAssign(Node[] rootRef, int L, int R, int x) { Node root = rootRef[0]; Node[] t1 = split(root, L); Node[] t2 = split(t1[1], R - L + 1); applyAssign(t2[0], x); rootRef[0] = merge(t1[0], merge(t2[0], t2[1])); } static void rangeAdd(Node[] rootRef, int L, int R, int x) { Node root = rootRef[0]; Node[] t1 = split(root, L); Node[] t2 = split(t1[1], R - L + 1); applyAdd(t2[0], x); rootRef[0] = merge(t1[0], merge(t2[0], t2[1])); } static void insertAt(Node[] rootRef, int pos, int x) { Node root = rootRef[0]; Node[] t = split(root, pos); rootRef[0] = merge(t[0], merge(new Node(x), t[1])); } static void eraseAt(Node[] rootRef, int pos) { Node root = rootRef[0]; Node[] t1 = split(root, pos); Node[] t2 = split(t1[1], 1); rootRef[0] = merge(t1[0], t2[1]); } static void collect(Node t, int[] out, int[] idx) { if (t == null) return; push(t); collect(t.l, out, idx); out[idx[0]++] = t.val; collect(t.r, out, idx); } static int writeBack(Node t, int[] vals, int idx) { if (t == null) return idx; push(t); idx = writeBack(t.l, vals, idx); t.val = vals[idx++]; idx = writeBack(t.r, vals, idx); pull(t); return idx; } static boolean nextPermutation(int[] a, int n) { int i = n - 2; while (i >= 0 && a[i] >= a[i + 1]) i--; if (i < 0) { for (int l = 0, r = n - 1; l < r; l++, r--) { int tmp = a[l]; a[l] = a[r]; a[r] = tmp; } return false; } int j = n - 1; while (a[j] <= a[i]) j--; int tmp = a[i]; a[i] = a[j]; a[j] = tmp; for (int l = i + 1, r = n - 1; l < r; l++, r--) { tmp = a[l]; a[l] = a[r]; a[r] = tmp; } return true; } static boolean prevPermutation(int[] a, int n) { int i = n - 2; while (i >= 0 && a[i] <= a[i + 1]) i--; if (i < 0) { for (int l = 0, r = n - 1; l < r; l++, r--) { int tmp = a[l]; a[l] = a[r]; a[r] = tmp; } return false; } int j = n - 1; while (a[j] >= a[i]) j--; int tmp = a[i]; a[i] = a[j]; a[j] = tmp; for (int l = i + 1, r = n - 1; l < r; l++, r--) { tmp = a[l]; a[l] = a[r]; a[r] = tmp; } return true; } static void doNextPermutation(Node[] rootRef, int L, int R) { Node root = rootRef[0]; Node[] t1 = split(root, L); Node[] t2 = split(t1[1], R - L + 1); int len = R - L + 1; int[] vals = new int[len]; collect(t2[0], vals, new int[] {0}); nextPermutation(vals, len); writeBack(t2[0], vals, 0); rootRef[0] = merge(t1[0], merge(t2[0], t2[1])); } static void doPrevPermutation(Node[] rootRef, int L, int R) { Node root = rootRef[0]; Node[] t1 = split(root, L); Node[] t2 = split(t1[1], R - L + 1); int len = R - L + 1; int[] vals = new int[len]; collect(t2[0], vals, new int[] {0}); prevPermutation(vals, len); writeBack(t2[0], vals, 0); rootRef[0] = merge(t1[0], merge(t2[0], t2[1])); } static Node build(int[] a) { Node root = null; for (int v : a) { root = merge(root, new Node(v)); } return root; } public static void main(String[] args) throws Exception { FastInput fs = new FastInput(System.in); PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out, 1 << 16)); int n = fs.nextInt(); if (n == Integer.MIN_VALUE) { pw.close(); return; } int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = fs.nextInt(); } Node root = build(arr); Node[] rootRef = new Node[] {root}; int q = fs.nextInt(); for (int qi = 0; qi < q; qi++) { int type = fs.nextInt(); if (type == 1) { int L = fs.nextInt(); int R = fs.nextInt(); pw.println(rangeSum(rootRef, L, R)); } else if (type == 2) { int x = fs.nextInt(); int pos = fs.nextInt(); insertAt(rootRef, pos, x); } else if (type == 3) { int pos = fs.nextInt(); eraseAt(rootRef, pos); } else if (type == 4) { int x = fs.nextInt(); int L = fs.nextInt(); int R = fs.nextInt(); rangeAssign(rootRef, L, R, x); } else if (type == 5) { int x = fs.nextInt(); int L = fs.nextInt(); int R = fs.nextInt(); rangeAdd(rootRef, L, R, x); } else if (type == 6) { int L = fs.nextInt(); int R = fs.nextInt(); doNextPermutation(rootRef, L, R); } else if (type == 7) { int L = fs.nextInt(); int R = fs.nextInt(); doPrevPermutation(rootRef, L, R); } } int sz = size(rootRef[0]); int[] finalVals = new int[sz]; collect(rootRef[0], finalVals, new int[] {0}); for (int i = 0; i < sz; i++) { if (i > 0) pw.print(' '); pw.print(finalVals[i]); } pw.println(); pw.close(); } }