[HAOI2012]高速公路
嘟嘟嘟
涉及到概率的题,估计我就只会这种吧……
这一类概率问题比较好想,就是每一种情况/总方案数。而这两者一般都可以分别维护。
对于这道题,因为是一条链,于是把边看成点,那么总方案数就是\(C_{R - L + 1} ^ 2\),询问的时候直接搞出来就行。
关键是分子。很显然得先推推式子。有一个技巧就是式子中的下标尽量统一,也就是全局编号,这样就不用像我一样区间合并的时候还得考虑左右区间,然后分别维护从区间左右端点开始编号的一对乱七八糟的东西了。
考虑每一条边权的贡献,那么分子就是$$ans = \sum _ {i = l} ^ {r} a_i * (i - l + 1) * (r - i + 1)$$然后我们把式子拆开$$ans = (-l * r - l + r + 1) * \sum a_i + (l + r) \sum a_i * i + \sum a_i * i ^ 2$$到这就很简单了,发现我们只用维护这三个\(\sum\)。而这三个\(\sum\)都是极其好维护的。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
#include<assert.h>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
In ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
In void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
}
char s[2];
int n, m;
ll S[maxn];
In ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
struct Tree
{
int l, r, len;
ll lzy, sum1, sum2, sum3;
In Tree operator + (const Tree& oth)const
{
Tree ret;
ret.l = l, ret.r = oth.r;
ret.len = ret.r - ret.l + 1;
ret.lzy = 0;
ret.sum1 = sum1 + oth.sum1;
ret.sum2 = sum2 + oth.sum2;
ret.sum3 = sum3 + oth.sum3;
return ret;
}
}t[maxn << 2];
In void build(int L, int R, int now)
{
t[now].l = L, t[now].r = R;
t[now].len = R - L + 1;
t[now].lzy = t[now].sum1 = t[now].sum2 = t[now].sum3 = 0;
if(L == R) return;
int mid = (L + R) >> 1;
build(L, mid, now << 1);
build(mid + 1, R, now << 1 | 1);
}
In void change(int now, ll d)
{
t[now].lzy += d;
t[now].sum1 += d * t[now].len;
t[now].sum2 += d * t[now].len * (t[now].l + t[now].r) / 2;
t[now].sum3 += d * (S[t[now].r] - S[t[now].l - 1]);
}
In void pushdown(int now)
{
if(t[now].lzy)
{
change(now << 1, t[now].lzy), change(now << 1 | 1, t[now].lzy);
t[now].lzy = 0;
}
}
In void update(int L, int R, int now, ll d)
{
if(L > R) return;
if(t[now].l == L && t[now].r == R) {change(now, d); return;}
pushdown(now);
int mid = (t[now].l + t[now].r) >> 1;
if(R <= mid) update(L, R, now << 1, d);
else if(L > mid) update(L, R, now << 1 | 1, d);
else update(L, mid, now << 1, d), update(mid + 1, R, now << 1 | 1, d);
t[now] = t[now << 1] + t[now << 1 | 1];
}
In Tree query(int L, int R, int now)
{
if(t[now].l == L && t[now].r == R) return t[now];
pushdown(now);
int mid = (t[now].l + t[now].r) >> 1;
if(R <= mid) return query(L, R, now << 1);
else if(L > mid) return query(L, R, now << 1 | 1);
else return query(L, mid, now << 1) + query(mid + 1, R, now << 1 | 1);
}
int main()
{
MYFILE();
n = read(), m = read();
for(int i = 1; i <= n; ++i) S[i] = S[i - 1] + 1LL * i * i;
build(1, n - 1, 1);
for(int i = 1; i <= m; ++i)
{
scanf("%s", s); int L = read(), R = read() - 1;
if(s[0] == 'C')
{
int v = read();
update(L, R, 1, v);
}
else
{
if(L > R) {puts("0"); continue;}
int len = R - L + 1;
Tree tp = query(L, R, 1);
ll ans1 = tp.sum1 * (R - L - 1LL * L * R + 1) + tp.sum2 * (L + R) - tp.sum3;
ll ans2 = (1LL * (len + 1) * len) >> 1;
ll Gcd = gcd(ans1, ans2);
write(ans1 / Gcd), putchar('/'), write(ans2 / Gcd), enter;
}
}
return 0;
}