BZOJ 2752: [HAOI2012]高速公路(road)
看到期望就想期望的线性性
即求一条边在一个区间里经过多少次
把边权都转到左边的点上
查询和修改就都变成了对 \([l, r - 1]\) 的查询和修改
令 \(x=l,y=r-1\),那么一条边 \(i\) 在区间 \([x,y]\) 里的贡献为 $$(i - x + 1)(y- i + 1)=(x+y)i \times v_i- i^2 \times v_i+(y-x-xy+1)\times v_x$$
就维护 \(v_i\),\(i\times v_i\),\(i^2 \times v_i\) 即可
#include <bits/stdc++.h>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) >> 1)
#define ll long long
const int N = 1e5 + 7;
ll sum[N][3];
struct Seg {
ll s[N << 2][3];
int lazy[N << 2];
inline void tag(int p, int v, int l, int r) {
lazy[p] += v;
for (int i = 0; i < 3; i++)
s[p][i] += (sum[r][i] - sum[l - 1][i]) * v;
}
inline void pushdown(int p, int l, int r) {
if (!lazy[p]) return;
tag(lp, lazy[p], l, mid);
tag(rp, lazy[p], mid + 1, r);
lazy[p] = 0;
}
inline void pushup(int p) {
for (int i = 0; i < 3; i++)
s[p][i] = s[lp][i] + s[rp][i];
}
void update(int p, int l, int r, int x, int y, int v) {
if (x <= l && y >= r) return tag(p, v, l, r);
pushdown(p, l, r);
if (x <= mid) update(lp, l, mid, x, y, v);
if (y > mid) update(rp, mid + 1, r, x, y, v);
pushup(p);
}
ll query(int p, int l, int r, int x, int y) {
if (x <= l && y >= r) return 1LL * (x + y) * s[p][1] - 1LL * s[p][2] + (y - x - 1LL * y * x + 1LL) * s[p][0];
pushdown(p, l, r);
ll ans = 0;
if (x <= mid) ans += query(lp, l, mid, x, y);
if (y > mid) ans += query(rp, mid + 1, r, x, y);
return ans;
}
} seg;
inline ll C(int n) {
return 1LL * n * (n - 1) / 2;
}
ll gcd(ll a, ll b) {
while (b) {
ll t = a % b;
a = b;
b = t;
}
return a;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
sum[i][0] = sum[i - 1][0] + 1;
sum[i][1] = sum[i - 1][1] + i;
sum[i][2] = sum[i - 1][2] + 1LL * i * i;
}
n--;
for (int l, r, v; m--; ) {
char s[3];
scanf("%s%d%d", s, &l, &r);
r--;
if (s[0] == 'C') {
scanf("%d", &v);
seg.update(1, 1, n, l, r, v);
} else {
ll fz = seg.query(1, 1, n, l, r);
ll fm = C(r - l + 2);
ll g = gcd(fz, fm);
printf("%lld/%lld\n", fz / g, fm / g);
}
}
return 0;
}