【模板】ST 表 STable

posted on 2022-07-22 19:15:58 | under 模板 | source

template<int N,class T=int,int logN=20> struct STable{
	int tot,lg[N+10];T f[logN+1][N+10];
	STable():tot(0){lg[0]=-1;for(int i=1;i<=N;i++) lg[i]=lg[i>>1]+1;}
	void insert(T x){f[0][++tot]=x;for(int j=0,i=tot-1;i>=1;i-=1<<++j) f[j+1][i]=min(f[j][i],f[j][i+(1<<j)]);}
	T query(int l,int r){int k=lg[r-l+1];return min(f[k][l],f[k][r-(1<<k)+1]);}
};
template <class T>
struct STable {
  vector<T> f[21];
  vector<int> lg = {-1};
  int insert(T x) {
    f[0].push_back(x);
    int n = f[0].size();
    lg.push_back(lg[n >> 1] + 1);
    for (int j = 1; 1 << j <= n; j++) {
      int i = n - (1 << j);
      f[j].push_back(min(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]));
    }
    return n - 1;
  }
  T query(int l, int r) {
    //[l, r)
    int k = lg[r - l];
    return min(f[k][l], f[k][r - (1 << k)]);
  }
};
template <class T, int N>
struct STable {
  T f[21][N + 10];
  int lg[N + 10], cnt;
  STable() : cnt(0) { lg[0] = -1; }
  void insert(T x) {
    f[0][++cnt] = x;
    lg[cnt] = lg[cnt >> 1] + 1;
    for (int j = 1; 1 << j <= cnt; j++) {
      int i = cnt - (1 << j) + 1;
      f[j][i] = min(f[j - 1][i], f[j - 1][i + (1 << (j - 1))]);
    }
  }
  T query(int l, int r) {
    int k = lg[r - l + 1];
    return min(f[k][l], f[k][r - (1 << k) + 1]);
  }
};
template <class T, class Compare>
struct STable {
  Compare cmp;
  vector<T> f[21];
  STable(const Compare& cmp = Compare{}, const vector<T>& vec = vector<T>{})
      : cmp(cmp) {
    for (auto&& x : vec) insert(x);
  }
  int insert(const T& x) {
    f[0].push_back(x);
    int n = f[0].size();
    for (int j = 1; 1 << j <= n; j++) {
      int i = n - (1 << j);
      f[j].push_back(min(f[j - 1][i], f[j - 1][i + (1 << (j - 1))], cmp));
    }
    return n - 1;
  }
  T query(int l, int r) {  // [l, r]
    int k = 31 - __builtin_clz(r - l + 1);
    return min(f[k][l], f[k][r - (1 << k) + 1], cmp);
  }
};
const auto cmp = [](int u, int v) { return dfn[u] < dfn[v]; };
STable<int, decltype(cmp)> ST{cmp};
bool cmp(int u, int v) { return dfn[u] < dfn[v]; };
STable<int, decltype(&cmp)> ST{&cmp};
posted @ 2022-11-06 19:12  caijianhong  阅读(19)  评论(0编辑  收藏  举报