1. 程式人生 > 實用技巧 >(vector水平衡樹)【模板】普通平衡樹 Luogu P3369

(vector水平衡樹)【模板】普通平衡樹 Luogu P3369

20行寫完極其害怕

只能跑1e5的資料,那個1e6強制線上的開o2只有20pts QAQ

不用reserve也可以過,不過開了之後200ms的點只要130-140ms

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fastio ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
const int maxn = 2e5 + 10;
const ll inf = 1e17;
ll mod = 1e9 + 7;

vector<int>a;

int main()
{
    //freopen("C:\\1.in", "r", stdin);
    fastio;
    int n;
    cin >> n;
    a.reserve(100010);//提前開空間跑得更快
    while (n--)
    {
        int o, x;
        cin >> o >> x;
        if (o == 1)
            a.insert(lower_bound(a.begin(), a.end(), x), x);
        else if (o == 2)
            a.erase(lower_bound(a.begin(), a.end(), x));
        else if (o == 3)
            cout << lower_bound(a.begin(), a.end(), x) - a.begin() + 1 << endl;
        else if (o == 4)
            cout << a[x - 1] << endl;
        else if (o == 5)
            cout << *(lower_bound(a.begin(), a.end(), x) - 1) << endl;
        else
            cout << *upper_bound(a.begin(), a.end(), x) << endl;
    }
    return 0;

}