蓝桥杯1458双向排序

这道题暴力解能拿60%的分数,如果想要满分得用到线性树,(如果不熟练考场上很容易写错)

60%:

java 复制代码
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s = br.readLine().split(" ");
        int n=Integer.parseInt(s[0]),m=Integer.parseInt(s[1]);
        Integer num[]=new Integer[n+1];
        for (int i=1;i<=n;i++){
            num[i]=i;
        }
        for (int i=0;i<m;i++){
            String[] s1 = br.readLine().split(" ");
            int p=Integer.parseInt(s1[0]),q=Integer.parseInt(s1[1]);
            if (p==0){
                Arrays.sort(num,1,q+1,(a,b)->b-a);
            }else {
                Arrays.sort(num,q,n+1);
            }
        }
        for (int i=1;i<=n;i++){
            System.out.print(num[i]+" ");
        }
    }
}

100%:

java 复制代码
import java.io.IOException;
import java.util.*;
import java.io.*;

class InputReader {
    private final static int BUF_SZ = 65536;
    BufferedReader in;
    StringTokenizer tokenizer;

    public InputReader(InputStream in) {
        super();
        this.in = new BufferedReader(new InputStreamReader(in), BUF_SZ);
        tokenizer = new StringTokenizer("");
    }

    private String next() {
        while (!tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }
}

class Tree {
    public int l = 0;
    public int r = 0;
    public int sum = 0;
    public int lazy = -1;
}

public class Main {
    public static final int N = 100010;
    public static int n;
    public static int m;
    public static int op;
    public static int pos;
    public static Tree[] tree = new Tree[N << 2];

    public static void push_up(int rt) {
        tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
    }

    public static void push_down(int rt) {
        int x = tree[rt].lazy;
        if (x != -1) {
            int len1 = tree[rt << 1].r - tree[rt << 1].l + 1;
            int len2 = tree[rt << 1 | 1].r - tree[rt << 1 | 1].l + 1;
            tree[rt << 1].sum = len1 * x;
            tree[rt << 1 | 1].sum = len2 * x;
            tree[rt << 1].lazy = tree[rt << 1 | 1].lazy = x;
            tree[rt].lazy = -1;
        }
    }

    public static void build(int l, int r, int rt) {
        tree[rt].l = l;
        tree[rt].r = r;
        tree[rt].lazy = -1;
        if (l == r) {
            tree[rt].sum = 1;
            return;
        }
        int mid = l + r >> 1;
        build(l, mid, rt << 1);
        build(mid + 1, r, rt << 1 | 1);
        push_up(rt);
    }

    public static void update1(int L, int R, int rt, int cnt) {
        if (cnt == 0) {
            return;
        }
        if (tree[rt].sum == cnt) {
            tree[rt].sum = tree[rt].lazy = 0;
            return;
        }
        push_down(rt);
        if (cnt < tree[rt << 1].sum) {
            update1(L, R, rt << 1, cnt);
        } else {
            update1(L, R, rt << 1 | 1, cnt - tree[rt << 1].sum);
            tree[rt << 1].sum = 0;
            tree[rt << 1].lazy = 0;
        }
        push_up(rt);
    }

    public static void update2(int L, int R, int rt, int cnt) {
        if (cnt == 0) {
            return;
        }
        int l = tree[rt].l;
        int r = tree[rt].r;
        int len = r - l + 1;
        if (len - tree[rt].sum == cnt) {
            tree[rt].sum = len;
            tree[rt].lazy = 1;
            return;
        }
        push_down(rt);
        int cnt1 = tree[rt << 1].r - tree[rt << 1].l + 1 - tree[rt << 1].sum;
        if (cnt < cnt1) {
            update2(L, R, rt << 1, cnt);
        } else {
            update2(L, R, rt << 1 | 1, cnt - cnt1);
            tree[rt << 1].sum = tree[rt << 1].r - tree[rt << 1].l + 1;
            tree[rt << 1].lazy = 1;
        }
        push_up(rt);
    }

    public static int query(int L, int R, int rt) {
        int l = tree[rt].l;
        int r = tree[rt].r;
        if (L <= l && r <= R) {
            return tree[rt].sum;
        }
        push_down(rt);

        int mid = l + r >> 1;
        int res = 0;
        if (L <= mid) {
            res += query(L, R, rt << 1);
        }
        if (R > mid) {
            res += query(L, R, rt << 1 | 1);
        }
        return res;
    }

    public static void main(String[] args) {
        for (int i = 0; i < (N << 2); i++) tree[i] = new Tree();
        InputReader cin = new InputReader(System.in);
        n = cin.nextInt();
        m = cin.nextInt();
        build(1, n, 1);
        while ((m--) != 0) {
            op = cin.nextInt();
            pos = cin.nextInt();
            if (op == 0) {
                int cnt0 = n - tree[1].sum;
                int cnt = Math.max(0, pos - cnt0);
                update1(1, n, 1, cnt);
            } else {
                int cnt1 = tree[1].sum;
                int cnt = Math.max(0, n - pos + 1 - cnt1);
                update2(1, n, 1, cnt);
            }
        }
        int vv1 = 0;
        int vv2 = 0;
        for (int i = n; i >= 1; i--) {
            if (query(i, i, 1) == 0) {
                System.out.print(i + " ");
            }
        }
        for (int i = 1; i <= n; i++) {
            if (query(i, i, 1) == 1) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }
}
相关推荐
To_OC2 小时前
从一次栈溢出报错说起,我把递归彻底扒明白了
javascript·算法·程序员
千纸鹤安安7 小时前
千问Qwen-AgentWorld来了:一个语言模型搞定七大Agent场景,GPT-5.4都输了
算法
白鲸开源9 小时前
Apache SeaTunnel Zeta Engine 的 Basic Auth 是怎么工作的?
java·vue.js·github
白鲸开源9 小时前
一文读懂DolphinScheduler插件机制:如何轻松扩展任务类型与数据源
java·架构·github
七牛开发者9 小时前
MCP 到底是什么?为什么 Agent 都想接上它
算法·aigc·agent
北域码匠13 小时前
冒泡排序太慢?鸡尾酒排序双向优化,原生 C# 零第三方库完整代码
数据结构·排序算法·泛型·c# 算法·鸡尾酒排序·原生 c# 开发·冒泡排序优化·嵌入式算法
用户2986985301414 小时前
Java 实现 Word 文档文本查找与高亮标注
java·后端
宇宙之一粟15 小时前
乐企版式文件生成平台
java·后端·python