import java.util.*;

public class Main {
    static int n, m, block_sz, block_n;
    static int[] a = new int[100050];
    static int[] belong = new int[100050];
    static List<Integer>[] g = new ArrayList[100050];

    public static void spliceBlock() {
        block_sz = (int) (Math.sqrt(n) * Math.log(n + 1) / 2);
        block_n = (n + block_sz - 1) / block_sz;

        for (int i = 1; i <= n; i++) {
            belong[i] = (i - 1) / block_sz + 1;
            if (g[belong[i]] == null) {
                g[belong[i]] = new ArrayList<>();
            }
            g[belong[i]].add(a[i]);
        }

        for (int i = 1; i <= block_n; i++) {
            Collections.sort(g[i]);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = scanner.nextInt();
        }

        spliceBlock();

        m = scanner.nextInt();
        for (int i = 1; i <= m; i++) {
            int op = scanner.nextInt();
            int x = scanner.nextInt();
            int y = scanner.nextInt();

            if (op == 2) {
                int p = scanner.nextInt();
                int l = belong[x], r = belong[y], ans = 1;

                if (l + 1 >= r) {
                    for (int j = x; j <= y; j++) {
                        if (a[j] < a[p]) ans++;
                    }
                } else {
                    for (int j = x; j <= l * block_sz; j++) {
                        if (a[j] < a[p]) ans++;
                    }
                    for (int j = (r - 1) * block_sz + 1; j <= y; j++) {
                        if (a[j] < a[p]) ans++;
                    }
                    for (int j = l + 1; j < r; j++) {
                        ans += lowerBound(g[j], a[p]);
                    }
                }
                System.out.print(ans + " ");
            } else {
                int idx = belong[x];
                List<Integer> block = g[idx];
                block.remove((Integer) a[x]);
                int pos = lowerBound(block, y);
                block.add(pos, a[x] = y);
                Collections.sort(block);
            }
        }
        scanner.close();
    }


    public static int lowerBound(List<Integer> list, int value) {
        int left = 0, right = list.size();
        while (left < right) {
            int mid = (left + right) / 2;
            if (list.get(mid) < value) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }
        return left;
    }
}

 

posted on 2025-06-08 09:39  fafrkvit  阅读(12)  评论(0)    收藏  举报