跳到正文

题解

题解:P17593 「CZOI-R8」Prefix Min Query

违规用户名1764483约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年10月4日
收录
2026年10月5日
更新
2026年11月22日
篇幅
约 10 分钟
题目
P17593

版本

  1. v1当前

题目大意

有一个长度为 nn 的的排列 aa。定义排列的权值为 min⁡i=1n{i×min⁡j=1iaj}\min_{i = 1}^n \{i \times \min_{j = 1}^i a_j\},我们需要求从初始到当前所有时刻排列权值的最小值,即求历史最值。

题目解法

首先我们可以将这个序列的权值转化为 min⁡i=1n{ai×i}\min_{i = 1}^n \{a_i \times i\},因为我们可以想到,如果存在一组 x,yx, y 满足 axa_x 与 min⁡i=1yai\min_{i = 1}^y a_i 相等,那么最值取在 xx 处一定比取在 yy 处更优。

所以我们就可以直接用线段树维护 i×aii \times a_i 的最小值即可。

那么写个线段树即可,注意要手写快读卡常,不然容易超时。

代码

代码
C++
#include <bits/stdc++.h>
#ifdef __linux__
    #include <bits/extc++.h>
    #define getchar getchar_unlocked
    using namespace __gnu_cxx;
    using namespace __gnu_pbds;
#endif
#define int long long
#define lc (p << 1)
#define rc (p << 1 | 1)
#define pii pair<int, int>
#define pdd pair<double, double>
#define pss pair<string, string>
#define ull unsigned int
#define push_back emplace_back
#define push emplace
#define min basic_operation :: my_min
#define max basic_operation :: my_max
#define abs basic_operation :: my_abs
#define chkmin basic_operation :: my_chkmin
#define chkmax basic_operation :: my_chkmax
#define sqrt sqrtl
#define double long double
#define all(x) (x).begin(), (x).end()
#define exall(x, n) (x) + 1, (x) + 1 + (n)
#define siz(x) ((int)(x).size())
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define rp(i, a, b) for (register int i = (a); i < (b); ++i)
#define pr(i, a, b) for (register int i = (a); i > (b); --i)
#define rtep(i, a, b, c) for (register int i = (a); i <= (b); i += (c))
#define pter(i, a, b, c) for (register int i = (a); i >= (b); i -= (c))
#define inl_force __attribute__((always_inline)) inline
#define fir first
#define sec second
using namespace std;
namespace basic_operation {
    template<typename Tp> inl_force Tp my_min(const Tp& x, const Tp& y) { return (x < y ? x : y); }
    template<typename Tp> inl_force Tp my_max(const Tp& x, const Tp& y) { return (x > y ? x : y); }
    template<typename Tp> inl_force Tp my_abs(const Tp& x) { return (x >= 0 ? x : -x); }
    template<typename Tp> inl_force void my_chkmin(Tp& x, const Tp& y) { x = min(x, y); }
    template<typename Tp> inl_force void my_chkmax(Tp& x, const Tp& y) { x = max(x, y); }
}
namespace FastIO {
    template<typename Tp> inl_force Tp input() {
        register Tp x = 0;
        register int f = 1;
        register char ch = getchar();
        while (!isdigit(ch)) {
            if (ch == EOF)  return 0;
            if (ch == '-')  f = -1;
            ch = getchar();
        }
        while (isdigit(ch)) {
            x = (x << 3) + (x << 1) + (ch ^ 48);
            ch = getchar();
        }
        return x * f;
    }
    template<typename Tp> inline void output(Tp x) {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        if (x > 9)  output(x / 10);
        putchar((x % 10) ^ 48);
    }
    template<typename Tp> inl_force void outputll(Tp x) { output(x); putchar(' '); }
    template<typename Tp> inl_force void outputln(Tp x) { output(x); putchar('\n'); }
    inl_force double double_input() { double x; scanf("%Lf", &x); return x;}
    inl_force void double_output(const double& x) { printf("%Lf", x); }
    inl_force char char_input() { return getchar(); }
    inl_force void char_output(const char& x) { putchar(x); }
    inl_force string string_input() { static char buf[1000005]; scanf("%s", buf); return string(buf); }
    inl_force void string_output(const string& s) { fputs(s.c_str(), stdout); }
    #define input() (input<int>())
} using namespace FastIO;
constexpr int N = 1e6 + 5;
int T, n, q, x, y, his, a[N], pos[N], Min[N << 2];
inl_force void push_up(int p) {
    Min[p] = min(Min[lc], Min[rc]);
}
inline void build(int p, int l, int r) {
    if (l == r) {
        Min[p] = pos[l] * l;
        return ;
    }
    register int mid = (l + r) >> 1;
    build(lc, l, mid); build(rc, mid + 1, r);
    push_up(p);
}
inline int query(int p, int l, int r, int x, int y) {
    if (x <= l && r <= y)    return Min[p];
    register int mid = (l + r) >> 1, res = 1e18;
    if (x <= mid)    res = min(res, query(lc, l, mid, x, y));
    else    res = min(res, query(rc, mid + 1, r, x, y));
    return res;
}
inline void update(int p, int l, int r, int x) {
    if (l == r) {
        Min[p] = l * pos[l];
        return ;
    }
    register int mid = (l + r) >> 1;
    if (x <= mid)    update(lc, l, mid, x);
    else    update(rc, mid + 1, r, x);
    push_up(p);
}
inl_force void solve() {
    n = input(), q = input(); his = 1e18;
    rep(i, 1, n)    a[i] = input(), pos[a[i]] = i;
    build(1, 1, n);
    outputll(Min[1]); his = Min[1];
    while (q--) {
        x = input(), y = input();
        swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]);
        update(1, 1, n, a[x]), update(1, 1, n, a[y]);
        his = min(his, Min[1]); outputll(his);
    }
    putchar('\n');
}
signed main() {
#ifndef ONLINE_JUDGE
    freopen("problem.in", "r", stdin);
    freopen("problem.out", "w", stdout);
#endif
    register clock_t time = clock();
    T = input();
    while (T--)    solve();
#ifndef ONLINE_JUDGE
    cerr << "\ntime: " << clock() - time << "ms\n";
#endif
    return 0;
}
原文
存档评论2 条抓取于