「KTSC 2024 R2」跳跃游戏 题解
睡了一觉,打呼噜被老胡叫醒了/lh 睡醒场切,vector find 是
思路
考虑最终得到了
结论:必然存在一种方案,使得在
若
考虑有一个
代码
#include<bits/stdc++.h>
#define rep(i,l,r) for (int i(l), i##end(r); i <= i##end; ++i)
#define per(i,r,l) for (int i(r), i##end(l); i >= i##end; --i)
#define LL long long
#define fi first
#define se second
#define eb emplace_back
#define PII pair <int, int>
using namespace std;
const int N = 5e5 + 9;
LL n, K; int q;
vector <pair <LL, int> > seg;
map <LL, int> mp;
void cmax(LL &a, LL b) {if (a < b) a = b;}
LL ans0, ans1;
const LL inf = 1e18;
vector <LL> pos;
struct SegmentTree {
#define lc (p << 1)
#define rc (p << 1 | 1)
LL mx[N << 2], tag1[N << 2], tag2[N << 2];
void addtag1(int p, LL v) {
mx[p] += v;
tag1[p] += v;
tag2[p] += v;
}
void addtag2(int p, LL v) {
cmax(mx[p], v);
cmax(tag2[p], v);
}
void pushdown(int p) {
if (tag1[p]) {
addtag1(lc, tag1[p]);
addtag1(rc, tag1[p]);
tag1[p] = 0;
}
if (tag2[p]) {
addtag2(lc, tag2[p]);
addtag2(rc, tag2[p]);
tag2[p] = 0;
}
}
void add(int ql, int qr, LL v, int p = 1, int l = 0, int r = pos.size() - 1) {
if (ql <= l && r <= qr) {
addtag1(p, v);
return ;
}
if (qr < l || r < ql) return ;
pushdown(p);
int mid = (l + r) >> 1;
add(ql, qr, v, lc, l, mid);
add(ql, qr, v, rc, mid + 1, r);
mx[p] = max(mx[lc], mx[rc]);
}
void mdf(int ql, int qr, LL v, int p = 1, int l = 0, int r = pos.size() - 1) {
if (ql <= l && r <= qr) {
addtag2(p, v);
return ;
}
if (qr < l || r < ql) return ;
pushdown(p);
int mid = (l + r) >> 1;
mdf(ql, qr, v, lc, l, mid);
mdf(ql, qr, v, rc, mid + 1, r);
mx[p] = max(mx[lc], mx[rc]);
}
LL query(int loc, int p = 1, int l = 0, int r = pos.size() - 1) {
if (l == r) return mx[p];
pushdown(p);
int mid = (l + r) >> 1;
if (loc <= mid) return query(loc, lc, l, mid);
return query(loc, rc, mid + 1, r);
}
} tr;
void fAdd(LL l, LL r, int var) {
if (r < K) {
int L = lower_bound(pos.begin(), pos.end(), l) - pos.begin();
int R = upper_bound(pos.begin(), pos.end(), r) - pos.begin() - 1;
tr.add(L, R, var);
}
else {
fAdd(l, K - 1, var);
fAdd(0, r - K, var);
}
}
void fMax(LL l, LL r, LL var) {
if (r < K) {
int L = lower_bound(pos.begin(), pos.end(), l) - pos.begin();
int R = upper_bound(pos.begin(), pos.end(), r) - pos.begin() - 1;
tr.mdf(L, R, var);
}
else {
fMax(l, K - 1, var);
fMax(0, r - K, var);
}
}
long long play_game(long long N, int Q, long long K, vector<long long> L, vector<long long> R) {
n = N; q = Q; ::K = K;
rep (i, 0, q - 1) {
LL l = L[i], r = R[i];
++mp[l];
--mp[r + 1];
}
int sum = 0;
for (auto p : mp) {
if (next(mp.find(p.fi)) == mp.end()) break;
LL l = p.fi, r = next(mp.find(p.fi))->fi - 1;
sum += p.se;
LL len = r - l + 1, val = sum;
ans1 += len / K * val, len %= K;
if (len == 0) continue;
seg.eb(len, sum);
}
LL cur = 0; pos.eb(cur);
for (auto i : seg) {
LL len = i.fi;
cur = (cur + len) % K;
pos.eb(cur);
}
sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin(), pos.end()), pos.end());
// build
cur = 0;
for (auto i : seg) {
LL len = i.fi, val = i.se;
fAdd(cur + 1, cur + len, val);
fMax(cur + 1, cur + len, tr.query(lower_bound(pos.begin(), pos.end(), cur) - pos.begin()));
cur = (cur + len) % K;
}
return ans1 + tr.mx[1];
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗