题目链接:传送门 维护路径期望值,带区间修改
看每条路径会被统计多少次贡献
非常不显然
是
方案数就是
上面的是分子下面的是分母
现在要把上面的展开看怎么维护
直接拆开得
所以要分别维护每个区间的,,
更新的时候用前缀和就可以做到了
注意我们要维护的相当于是两个点之间的边
所以要注意左右端点
不知道哪炸了
#define int ll就过了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define
using namespace std;
typedef long long ll;
#define
struct node {
int l, r; ll w[3], f;
}tree[A];
int n, m; ll a, b, c;
ll a1, a2, a3, s[A][3]; char opt;
ll gcd(ll a, ll b) {
return !b ? a : gcd(b, a % b);
}
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r; tree[k].f = 0;
for (int i = 0; i < 3; i++) tree[k].w[i] = 0;
if (l == r) return;
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
}
void down(int k) {
int m = (tree[k].l + tree[k].r) >> 1;
tree[k << 1].f += tree[k].f; tree[k << 1 | 1].f += tree[k].f;
for (int i = 0; i < 3; i++) {
tree[k << 1].w[i] += tree[k].f * (s[m][i] - s[tree[k].l - 1][i]);
tree[k << 1 | 1].w[i] += tree[k].f * (s[tree[k].r][i] - s[m][i]);
}
tree[k].f = 0;
}
void change(int k, ll l, ll r, ll val) {
if (tree[k].l >= l and tree[k].r <= r) {
for (int i = 0; i < 3; i++) tree[k].w[i] += val * (s[tree[k].r][i] - s[tree[k].l - 1][i]);
tree[k].f += val;
return;
}
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if (l <= m) change(k << 1, l, r, val);
if (r > m) change(k << 1 | 1, l, r, val);
for (int i = 0; i < 3; i++) tree[k].w[i] = tree[k << 1].w[i] + tree[k << 1 | 1].w[i];
}
void ask(int k, ll l, ll r) {
if (tree[k].l >= l and tree[k].r <= r) {
a1 += tree[k].w[0];
a2 += tree[k].w[1];
a3 += tree[k].w[2];
return;
}
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if (l <= m) ask(k << 1, l, r);
if (r > m) ask(k << 1 | 1, l, r);
}
signed main() {
cin >> n >> m; build(1, 1, n);
for (int i = 1; i <= n; i++) {
s[i][0] = s[i - 1][0] + 1;
s[i][1] = s[i - 1][1] + i;
s[i][2] = s[i - 1][2] + i * i;
}
while (m--) {
cin >> opt;
if (opt == 'C') {
cin >> a >> b >> c;
change(1, a, b - 1, c);
}
else {
cin >> a >> b; a1 = a2 = a3 = 0; ask(1, a, b - 1);
ll ans1 = (b + a - 1) * a2 - a3 + (b - a * b) * a1;
ll ans2 = (b - a + 1) * (b - a) / 2;
ll d = gcd(ans1, ans2);
cout << ans1 / d << "/" << ans2 / d << endl;
}
}
return 0;
}