hdu 5419 Victor and Toys(期望+差分前缀和)

题意:

Victor有n个玩具,编号为1到n,其中编号为ii的玩具有 wi 点有趣值。

Victor对数字特别敏感,他在头脑中生成了m个区间,其中第i个区间为 [li,ri] ,并随机选取了3个互不相同的数 i,j,k (1i<j<km) ,将所有满足 max(li,lj,lk)xmin(ri,rj,rk) 的编号为x的玩具取出,他想知道他取出的玩具的有趣值之和的期望是多少,你能帮帮他吗?

解析:

考虑每一个点,对结果的影响,如果,一个点包括了x个区间,那么,就有C(x,3)个w[i]在分子上,分母为C(m,3);所以最终的问题就是要求每个点,所点的区间数,真接用区间的开头加1,结尾减1,复杂度为 o(n) ,如果用线段树成段更新,复杂度为 o(nlog(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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值