CF1924B
Solution
考虑维护任意两个相邻码头之间的费用总和,设
此时计算
用 set 来维护码头的位置,方便在新建码头时快速找到前驱和后继的位置。
每次新建一个码头,
对于查询造作:我们不妨先把答案记录成
在左边,我们是没有处理从
而在右边,我们多算了从
这样查询和修改我们都做到了
总复杂度
写法较丑。
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 3e5 + 5, INF = 0x3f3f3f3f;
const LL mod = 998244353;
int n, m, q;
set<int> s;
int val[N], w[N], g[N];
struct BIT {
int N;
vector<LL> c;
#define lbt(x) x & -x
BIT(int n) {
N = n;
c.resize(n + 1);
}
void modify(int x, LL v) {
for(; x <= N; x += lbt(x)) c[x] += v;
}
LL query(int x) {
LL res = 0;
for(; x; x -= lbt(x)) res += c[x];
return res;
}
LL query(int l, int r) {
return query(r) - query(l - 1);
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> m >> q;
BIT bit(n);
for(int i = 1; i <= m; i ++) {
int x;
cin >> x;
g[i] = x;
s.insert(x);
}
for(int i = 1; i <= m; i ++) {
int v;
cin >> v;
w[g[i]] = v;
}
for(auto it = s.begin(); it != s.end(); it ++) {
auto it2 = it;
it2 ++;
int p = *it, nxt = *it2;
if(p == n) continue;
int d = nxt - p - 1;
val[p] = d * (d + 1ll) / 2ll * w[p];
bit.modify(p, val[p]);
}
while(q --) {
int op, x, y;
cin >> op >> x >> y;
if(op == 1) {
w[x] = y;
auto it = s.lower_bound(x);
int nxt = *it;
it --;
int pre = *it;
bit.modify(pre, -val[pre]);
int d = x - pre - 1;
val[pre] = d * (d + 1ll) / 2ll * w[pre];
bit.modify(pre, val[pre]);
d = nxt - x - 1;
val[x] = d * (d + 1ll) / 2ll * w[x];
bit.modify(x, val[x]);
s.insert(x);
}
else {
int ans = bit.query(x, y);
int l = *--s.upper_bound(x), ll = *s.lower_bound(x), r = *s.upper_bound(y), rr = *--s.upper_bound(y);
int d = ll - x;
ans += d * (d + 1ll) / 2ll * w[l];
if(y != r && y != n) {
d = r - y - 1;
ans -= d * (d + 1ll) / 2ll * w[rr];
}
cout << ans << '\n';
}
}
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现