# 【题解】CatOJ C0458C 滑动窗口定期重构

Code（这份代码 5e5 的包 WA 了，但大致思路可以参考一下）

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb emplace_back
#define rep(_, __, ___) for (int _ = (__); _ <= (___); _++)
#define per(_, __, ___) for (int _ = (__); _ >= (___); _--)
// #define int long long
using ll = long long; using pii = pair<int, int>;
char ch = getchar(); int s = 0, f = 1;
while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar();
while (isdigit(ch)) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
return s * f;
}
constexpr int N = 5e5 + 5, mod = 1e9 + 7;
int n, k, a[N], s1[N], t1, t2; ll pw[N]; tuple<int, int, int> s2[N];
#define ls id << 1
#define rs id << 1 | 1
ll mn[N << 2], tag[N << 2];
inline void maketag(int id, int x) {
tag[id] += x, mn[id] += x;
}
void upd(int a, int b, int x, int id = 1, int l = 1, int r = n) {
if (a <= l && b >= r) return maketag(id, x);
int mid = (l + r) >> 1; maketag(ls, tag[id]), maketag(rs, tag[id]), tag[id] = 0;
if (a <= mid) upd(a, b, x, ls, l, mid);
if (b > mid) upd(a, b, x, rs, mid + 1, r);
mn[id] = min(mn[ls], mn[rs]);
}
ll qry(int a, int b, int id = 1, int l = 1, int r = n) {
if (a <= l && b >= r) return mn[id];
int mid = (l + r) >> 1; ll res = 1e18; maketag(ls, tag[id]), maketag(rs, tag[id]), tag[id] = 0;
if (a <= mid) res = min(res, qry(a, b, ls, l, mid));
if (b > mid) res = min(res, qry(a, b, rs, mid + 1, r));
return res;
}
signed main() {
//    freopen("knight.in", "r", stdin);
//    freopen("knight.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
pw[0] = 1; rep (i, 1, n) a[i] = read(), pw[i] = pw[i - 1] * 23 % mod;
ll ans = 0, dp;
rep (i, 1, n) {
while (t1 && a[s1[t1]] < a[i]) t1--;
int x = s1[t1] + 1; s2[t2 + 1] = {get<1>(s2[t2]) + 1, i, 0}, t2++;
while (t2 && get<0>(s2[t2]) >= x) {
upd(get<0>(s2[t2]), get<1>(s2[t2]), a[i] - get<2>(s2[t2]));
t2--;
}
s2[++t2] = {x, i, a[i]}, s1[++t1] = i; dp = qry(max(i - k + 1, 1), i);
if (i < n) upd(i + 1, i + 1, dp);
ans += pw[n - i] * (dp % mod) % mod;
}
cout << ans % mod;
return 0;
}
/*
3 4 4 8 10 12
*/



Code

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb emplace_back
#define rep(_, __, ___) for (int _ = (__); _ <= (___); _++)
#define per(_, __, ___) for (int _ = (__); _ >= (___); _--)
#define int long long
using ll = long long; using pii = pair<int, int>;
char ch = getchar(); int s = 0, f = 1;
while (!isdigit(ch)) f = (ch == '-' ? -1 : 1), ch = getchar();
while (isdigit(ch)) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
return s * f;
}
constexpr int N = 1e7 + 5, mod = 1e9 + 7;
int n, k, st = 1, ed, mid = 1, dp[N], mn[N], pw[N];
struct node {signed l, r; int tag;} s[N];
inline int get(int i) {
return s[i].tag + dp[s[i].l - 1];
}
inline void rebuild() {
if (st > ed) return (void)(mid = st); mid = (st + ed) >> 1;
rep (i, st, ed) mn[i] = get(i);
per (i, mid - 1, st) mn[i] = min(mn[i], mn[i + 1]);
rep (i, mid + 2, ed) mn[i] = min(mn[i], mn[i - 1]);
}
signed main() {
// freopen("knight.in", "r", stdin);
// freopen("knight.out", "w", stdout);
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
pw[0] = 1; rep (i, 1, n) pw[i] = pw[i - 1] * 23 % mod;
rep (i, 1, n) {
// cerr << s[st].l << '*' << s[st].r << endl;
while (st <= ed && s[st].l < i - k + 1) {
if (s[st].r < i - k + 1) {
if (++st > mid) rebuild();
}
else {
s[st].l = i - k + 1; mn[st] = get(st);
if (st < mid) mn[st] = min(mn[st], mn[st + 1]);
}
}
// cerr << s[st].l << '\$' << s[st].r << endl;
while (st <= ed && s[ed].tag <= x) if (--ed <= mid) rebuild();
// cerr << s[st].l << '&' << s[st].r << endl;
if (st > ed) s[++ed] = (node){max(1ll, i - k + 1), i, x};
else s[ed + 1] = (node){s[ed].r + 1, i, x}, ed++;
mn[ed] = get(ed); if (ed - 1 > mid) mn[ed] = min(mn[ed], mn[ed - 1]);
dp[i] = min(mn[ed], mn[st]); ans += pw[n - i] * (dp[i] % mod) % mod;
// cerr << i << ' ' << dp[i] << '\n';
}
cout << ans % mod;
return 0;
}
/*
3 4 4 8 10 12
*/


posted @ 2024-01-11 16:34  Opuntia9622  阅读(61)  评论(1编辑  收藏  举报