ST表的cache优好代码?

P2880 [USACO07JAN] Balanced Lineup G

@[zcqzwy](/user/645208) 把你的完整代码发一下。
by Goedel_Machine @ 2023-02-28 20:00:01


@[Goedel_Machine](/user/908068) ```cpp #include <algorithm> #include <array> #include <bitset> #include <climits> #include <cmath> #include <cstring> #include <iostream> #include <map> #include <queue> #include <random> #include <set> #include <vector> #include <functional> #include <chrono> using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); using ul = unsigned long; using ull = unsigned long long; using ll = long long; using db = long double; // 如果时间超过用double using str = string; // python! using hash_t = uint64_t; // When P = 2^32 - 13337, both P and (P - 1) / 2 are prime. Also, we avoid multiplication bases near 0 or P - 1. const int HASH_COUNT = 2; const ull HASH_P = (uint32_t) -13337; // pairs using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; #define mp make_pair #define f first #define s second // vectors // oops size(x), rbegin(x), rend(x) need C++17 using words = vector<string>; using vi = vector<int>; #define sz(x) int((x).size()) #define bg(x) begin(x) #define all(x) bg(x), end(x) #define rall(x) x.rbegin(), x.rend() #define sor(x) sort(all(x)) #define rsz resize #define ins insert #define pb push_back #define eb emplace_back #define ft front() #define bk back() #define lb lower_bound #define ub upper_bound // loops #define FOR(i, a, b) for (int i = (a); i <= (b); ++i) #define F1R(i, a) FOR(i, 1, a) #define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i) #define R0F(i, a) ROF(i, 0, a) #define rep(a) F0R(_, a) #define each(a, x) for (auto &a : x) // 输入输出 #define endl '\n'; #define cdebug cout << "---------debug--------" << endl; // bitwise ops // also see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html constexpr int pct(int x) { return __builtin_popcount(x); // # 1的个数 } constexpr int bits( int x) { // assert(x >= 0); // 一般不需要 return x == 0 ? 0 : 31 - __builtin_clz(x); } // floor(log2(x)) 返回第一个1的位数 constexpr int p2(int x) { return 1 << x; } constexpr int msk2(int x) { return p2(x) - 1; } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } // divide a by b rounded down const int MOD = 998244353; const int MX = (int)2e5 + 5; const ll BIG = 1e18 + 7; // 不要太接近 LLONG_MAX const db PI = 3.141592653589793238462643383279502884; const int dx[4] {1, 0, -1, 0}, dy[4] {0, 1, 0, -1}; // 网格问题 inline int read() { // 快读 int x = 0, w = 1; char ch = 0; while (ch < '0' || ch > '9') { // ch 不是数字时 if (ch == '-') w = -1; // 判断是否为负 ch = getchar(); // 继续读入 } while (ch >= '0' && ch <= '9') { // ch 是数字时 x = x * 10 + (ch - '0'); // 将新读入的数字’加’在 x 的后面 // x 是 int 类型,char 类型的 ch 和 ’0’ 会被自动转为其对应的 // ASCII 码,相当于将 ch 转化为对应数字 // 此处也可以使用 (x<<3)+(x<<1) 的写法来代替 x*10 ch = getchar(); // 继续读入 } return x * w; // 数字 * 正负号 = 实际数值 } inline void write(int x) { static int sta[35]; int top = 0; do { sta[top++] = x % 10, x /= 10; } while (x); while (top) putchar(sta[--top] + 48); // 48 是 '0' } const double eps = 1e-6; //默认精度 const bool io = false; int Log2[MX]; // log2初始化 void log2_init() { for (int i = 2; i <= MX; ++i) Log2[i] = Log2[i >> 1] + 1; } int n, q; int maxcow[MX][22], mincow[MX][22]; int main() { if (io) { ios::sync_with_stdio(false); cin.tie(nullptr); } log2_init(); //预处理log2数组; int n = read(), q = read(); int x; memset(maxcow,60,sizeof(maxcow)); F1R(i, n) { x = read(); maxcow[0][i] = x; mincow[0][i] = x; } F1R(i, 16) { for (int j = 1; j + (1 << i) - 1 <= n; ++j) { maxcow[i][j] = max(maxcow[i - 1][j + (1 << (i - 1))], maxcow[i - 1][j]); mincow[i][j] = min(mincow[i - 1][j + (1 << (i - 1))], mincow[i - 1][j]); } } F1R(i, q) { int l = read(), r = read(); int k = Log2[r - l + 1]; int mx = max(maxcow[k][l], maxcow[k][r - (1 << k) + 1]); int mi = min(mincow[k][l], mincow[k][r - (1 << k) + 1]); write(mx - mi), puts(""); } return 0; } ```
by zcqzwy @ 2023-02-28 21:09:59


@[Goedel_Machine](/user/908068) 已找到原因,st的大小问题maxcow[MX][22], mincow[MX][22] ==》maxcow[22][MX], mincow[22][MX]
by zcqzwy @ 2023-02-28 21:47:39


|