【模板】离散化

posted on 2022-07-22 15:16:41 | under 模板 | source

离散花

template<int N> struct flower{
    int b[N+10],cnt;
    flower():cnt(0){}
    void operator+=(int x){b[++cnt]=x;}
    void build(){sort(b+1,b+cnt+1),cnt=unique(b+1,b+cnt+1)-b-1;}
    int operator()(int x){return lower_bound(b+1,b+cnt+1,x)-b;}
    int operator[](int i){return b[i];}
};

vector

template<class T> struct flower {
    vector<T> b;
    int cnt;
    void add(T x) { b.push_back(x); }
    void build() {
        sort(b.begin(), b.end());
        b.erase(unique(b.begin(), b.end()), b.end());
        cnt = b.size();
    }
    int operator()(T x) {
        return lower_bound(b.begin(), b.end(), x) - b.begin() + 1;
    }
};

vector2

template<class T> struct flower {
    vector<T> b; int cnt;
    flower& operator<<(const T& x) { return b.push_back(x), *this; }
    void build() {
        sort(b.begin(), b.end());
        b.erase(unique(b.begin(),  b.end()), b.end());
        cnt = b.size();
    }
    T operator[](int x) { return b[x - 1]; }
    int operator()(const T& x) {
        return lower_bound(b.begin(), b.end(), x) - b.begin() + 1;
    }
};
posted @ 2022-11-06 19:17  caijianhong  阅读(34)  评论(0编辑  收藏  举报