概念
例题1、P3865 【模板】ST表
#include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define all(__vv__) (__vv__).begin(), (__vv__).end() #define endl "\n" #define pai pair<int, int> #define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__)) typedef long long ll; typedef unsigned long long ull; typedef long double ld; inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; } inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} }; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const int N = 1e5 + 7; int st[N][21], Log2[N]; void pre() { Log2[1] = 0; Log2[2] = 1; for (int i = 3; i < N; ++i) Log2[i] = Log2[i >> 1] + 1; } int main() { pre(); int T = 1; //T = read(); while (T--) { int n = read(), m = read(); for (int i = 1; i <= n; ++i) st[i][0] = read(); int t = Log2[n]; //区间最长的2次方 for (int j = 1; j <= t; ++j) for (int i = 1; i + (1 << j) - 1 <= n; ++i) st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); while (m--) { int l = read(), r = read(); int s = Log2[r - l + 1]; print(max(st[l][s], st[r - (1 << s) + 1][s])); } } return 0; }
例题2、P1890 gcd区间
#include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define all(__vv__) (__vv__).begin(), (__vv__).end() #define endl "\n" #define pai pair<int, int> #define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__)) typedef long long ll; typedef unsigned long long ull; typedef long double ld; inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; } inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} }; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const int N = 1e3 + 7; int st[N][21], Log2[N]; void pre() { Log2[1] = 0; Log2[2] = 1; for (int i = 3; i < N; ++i) Log2[i] = Log2[i >> 1] + 1; } int main() { pre(); int T = 1; //T = read(); while (T--) { int n = read(), m = read(); for (int i = 1; i <= n; ++i) st[i][0] = read(); int t = Log2[n]; //区间最长的2次方 for (int j = 1; j <= t; ++j) for (int i = 1; i + (1 << j) - 1 <= n; ++i) st[i][j] = gcd(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); while (m--) { int l = read(), r = read(); int s = Log2[r - l + 1]; print(gcd(st[l][s], st[r - (1 << s) + 1][s])); } } return 0; }
例题3、P2880 [USACO07JAN]Balanced Lineup G
#include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define all(__vv__) (__vv__).begin(), (__vv__).end() #define endl "\n" #define pai pair<int, int> #define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__)) typedef long long ll; typedef unsigned long long ull; typedef long double ld; inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; } inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} }; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const int N = 5e4 + 7; int st1[N][21], st2[N][21], Log2[N]; void pre() { Log2[1] = 0; Log2[2] = 1; for (int i = 3; i < N; ++i) Log2[i] = Log2[i >> 1] + 1; } int querymax(int l, int r) { int s = Log2[r - l + 1]; return max(st1[l][s], st1[r - (1 << s) + 1][s]); } int querymin(int l, int r) { int s = Log2[r - l + 1]; return min(st2[l][s], st2[r - (1 << s) + 1][s]); } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); pre(); int T = 1; //T = read(); while (T--) { int n = read(), m = read(); for (int i = 1; i <= n; ++i) st1[i][0] = st2[i][0] = read(); int t = Log2[n]; //区间最长的2次方 for (int j = 1; j <= t; ++j) for (int i = 1; i + (1 << j) - 1 <= n; ++i) st1[i][j] = max(st1[i][j - 1], st1[i + (1 << (j - 1))][j - 1]), st2[i][j] = min(st2[i][j - 1], st2[i + (1 << (j - 1))][j - 1]); while (m--) { int l = read(), r = read(); int ans = querymax(l, r) - querymin(l, r); print(ans); } } return 0; }
例题4、[bzoj1067] [SCOI2007]降雨量
#include <bits/stdc++.h> using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) #define all(__vv__) (__vv__).begin(), (__vv__).end() #define endl "\n" #define pai pair<int, int> #define ms(__x__,__val__) memset(__x__, __val__, sizeof(__x__)) typedef long long ll; typedef unsigned long long ull; typedef long double ld; inline ll read() { ll s = 0, w = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) s = (s << 1) + (s << 3) + (ch ^ 48); return s * w; } inline void print(ll x, int op = 10) { if (!x) { putchar('0'); if (op) putchar(op); return; } char F[40]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); if (op) putchar(op); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } const int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} }; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const int N = 5e4 + 7; int year[N]; int st[N][21], Log2[N]; void pre() { Log2[1] = 0; Log2[2] = 1; for (int i = 3; i < N; ++i) Log2[i] = Log2[i >> 1] + 1; } int query(int l, int r) { if (l > r) return -INF; int s = Log2[r - l + 1]; return max(st[l][s], st[r - (1 << s) + 1][s]); } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); pre(); int T = 1; //T = read(); while (T--) { int n = read(); for (int i = 1; i <= n; ++i) year[i] = read(), st[i][0] = read(); int t = Log2[n]; //区间最长的2次方 for (int j = 1; j <= t; ++j) for (int i = 1; i + (1 << j) - 1 <= n; ++i) st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); int m = read(); while (m--) { int a = read(), b = read(); int l = lower_bound(year + 1, year + 1 + n, a) - year; int r = lower_bound(year + 1, year + 1 + n, b) - year; if (year[l] != a and year[r] != b) puts("maybe"); else if (year[l] == a and year[r] != b) { if (query(l + 1, r - 1) >= st[l][0]) puts("false"); else puts("maybe"); } else if (year[l] != a and year[r] == b) { if (query(l, r - 1) >= st[r][0]) puts("false"); else puts("maybe"); } else if (st[l][0] >= st[r][0] and query(l + 1, r - 1) < st[r][0]) { if (r - l == b - a) puts("true"); else puts("maybe"); } else puts("false"); } } return 0; }
力扣:找出最具竞争力的子序列
给你长度为 的序列,你需要找到一个最小的长度为 的子序列。
处理办法是表预处理各个区间的最小值。
很显然,对于长度为的,我们要找到长度为的子序列,那么我们最低的要求就是在这个区间里面找到最小值,这样才有在最差的情况可以把后面全部的选上满足条件。并且满足了当前这个数是在这个区间中的最小数。每次都拿最小,我们就可以构成一个最小的子序列了。
注意枚举的起点和终点就可以了,查询区间最小值的过程使用表处理。
const int N = 1e5+7; int st[N][21], Log2[N]; void pre() { Log2[1] = 0; Log2[2] = 1; for (int i = 3; i < N; ++i) Log2[i] = Log2[i >> 1] + 1; } int query(int l, int r) { int s = Log2[r - l + 1]; return min(st[l][s], st[r - (1 << s) + 1][s]); } vector<int> ans; int cnt[N]; class Solution { public: int n; void dfs(vector<int>& nums, int start, int k) { if (k == 0) return; int mini = query(start, n - k + 1); ans.push_back(mini); for (; nums[start - 1] != mini; ++start); dfs(nums, start + 1, k - 1); } vector<int> mostCompetitive(vector<int>& nums, int k) { pre(); memset(cnt, 0, sizeof cnt); n = nums.size(); for (int i = 0; i < n; ++i) st[i + 1][0] = nums[i]; int t = Log2[n]; for (int j = 1; j <= t; ++j) for (int i = 1; i + (1 << j) - 1 <= n; ++i) st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); ans.clear(); dfs(nums, 1, k); return ans; } };