1 条题解

  • 0
    @ 2025-12-26 9:12:56

    题解

    对于 20%20\% 的数据,可以直接暴力模拟。

    其实对于每次询问可以进行二分,但是二分的前提需要先排序,排序后直接使用 upper_bound 查找。

    所以对于另外 30%30\% 的数据,最多添加 20002000 个数字,每次添加一个数后,进行 sort 排序即可。

    对于 100%100\% 的数据,我们使用 set 维护数列即可。

    标程

    #include<bits/stdc++.h>
    using namespace std;
    set <int> st;
    int main() {
        int n, q;
        scanf("%d", & n);
        for (int i = 1; i <= n; i++) {
            int x;
            scanf("%d", & x);
            st.insert(x);
        }
        scanf("%d", & q);
        while (q--) {
            int op, x;
            scanf("%d%d", & op, & x);
            if (op == 1) {
                st.insert(x);
            } else {
                set <int> ::iterator it = st.upper_bound(x);
                if (it != st.end()) {
                    printf("%d\n", *it);
                } else {
                    printf("-1\n");
                }
            }
        }
        return 0;
    }
    • 1

    信息

    ID
    47
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    20
    已通过
    6
    上传者