- Preprefix sum
code
- @ 2026-2-8 11:58:12
template<class T>
struct Fen {
int n;
vector<T> t;
Fen (int n_) {
init(n_);
}
Fen () {};
void init(int n_) {
n = n_;
t.assign(n + 1, {});
}
void change(int u, T x) {
for(; u <= n; u += u & -u) t[u] += x;
}
T sum(int u) {
T res = 0;
for(; u; u -= u & -u) res += t[u];
return res;
}
T sum(int l, int r) {
return sum(r) - sum(l - 1);
}
int kth(T sum) {
int i = 0;
T tot = {};
for (int j = 1 << __lg(n); j > 0; j /= 2) {
if (i + j <= n && t[i + j] + tot < sum) {
i += j;
tot += t[i];
}
}
return i + 1;
}
};
void solve() {
int n, m;
cin >> n >> m;
Fen<ll> t1(n + 1), t2(n + 1);
auto add = [&] (int i, int x) {
t1.change(i, x);
t2.change(i, 1ll * x * (n + 1 - i));
};
auto query = [&] (int i) {
return t2.sum(i) - (n - i) * t1.sum(i);
};
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
int x; cin >> x; add(i, x); a[i] = x;
}
while (m--) {
string o; int x, y;
cin >> o >> x;
if (o[0] == 'Q') {
cout << query(x) << '\n';
} else {
cin >> y;
add(x, y - a[x]);
a[x] = y;
}
}
}
0 条评论
目前还没有评论...
信息
- ID
- 433
- 时间
- ms
- 内存
- MiB
- 难度
- 8
- 标签
- 递交数
- 15
- 已通过
- 7
- 上传者