题意:
Victor有n个玩具,编号为1到n,其中编号为ii的玩具有 wi 点有趣值。
Victor对数字特别敏感,他在头脑中生成了m个区间,其中第i个区间为 [li,ri] ,并随机选取了3个互不相同的数 i,j,k (1≤i<j<k≤m) ,将所有满足 max(li,lj,lk)≤x≤min(ri,rj,rk) 的编号为x的玩具取出,他想知道他取出的玩具的有趣值之和的期望是多少,你能帮帮他吗?
解析:
考虑每一个点,对结果的影响,如果,一个点包括了x个区间,那么,就有C(x,3)个w[i]在分子上,分母为C(m,3);所以最终的问题就是要求每个点,所点的区间数,真接用区间的开头加1,结尾减1,复杂度为 o(n) ,如果用线段树成段更新,复杂度为 o(n∗log(n) ;注意不要爆了long long。
my code
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef __int64 ll;
typedef __int64 type;
const int N = (int)5e5 + 10;
ll w[N], sum[N];
int n, m;
struct Frac {
type a, b;
Frac() {a = 0; b = 1;}
Frac(type a, type b) {this->a = a; this->b = b; deal();}
void init() {a = 0; b = 1;}
type gcd(type a, type b) {
while (b) {
type tmp = a % b;
a = b;
b = tmp;
}
return a;
}
void deal() {
type d = gcd(a, b);
a /= d; b /= d;
if (b < 0) {
a = -a;
b = -b;
}
}
void put() {
if (a == 0) printf("0");
else {
if (b == 1) printf("%I64d", a);
else printf("%I64d/%I64d", a, b);
}
}
};
inline void modify(int l, int r, ll val) {
sum[l] += val; sum[r+1] -= val;
}
inline void update() {
for(int i = 1; i <= n; i++) {
sum[i] += sum[i-1];
}
}
ll cn3(ll n) {
return n*(n-1)*(n-2)/6;
}
void cal() {
ll num = 0, den = cn3(m);
for(int i = 1; i <= n; i++) {
num += cn3(sum[i]) * w[i];
}
Frac ans = Frac(num, den);
ans.deal();
ans.put(); puts("");
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
memset(sum, 0, sizeof(sum));
for(int i = 1; i <= n; i++) {
scanf("%I64d", &w[i]);
}
int l, r;
for(int i = 1; i <= m; i++) {
scanf("%d%d", &l, &r);
modify(l, r, 1);
}
update();
if(m < 3) {
puts("0");
continue;
}
cal();
}
return 0;
}