HDU 5419 Victor and Toys

Problem Description
Victor has n toys, numbered from 1 to n. The beauty of the i-th toy is wi.

Victor has a sense of math and he generates m intervals, the i-th interval is [li,ri]. He randomly picks 3 numbers i,j,k(1≤i< j < k ≤m), and selects all of the toys whose number are no less than max(li,lj,lk) and no larger than min(ri,rj,rk). Now he wants to know the expected sum of beauty of the selected toys, can you help him?

Input
The first line of the input contains an integer T, denoting the number of test cases.

In every test case, there are two integers n and m in the first line, denoting the number of the toys and intervals.

The second line contains n integers, the i-th integer wi denotes that the beauty of the i-th toy.

Then there are m lines, the i-th line contains two integers li and ri.

1≤T≤10.

1≤n,m≤50000.

1≤wi≤5.

1≤li≤ri≤n.

Output
Your program should print T lines : the i-th of these denotes the answer of the i-th case.

If the answer is an integer, just print a single interger, otherwise print an irreducible fraction like p/q.

Sample Input
1
3 4
1 1 5
2 3
1 3
3 3
1 1

Sample Output
5/4

Source
BestCoder Round #52 (div.2)

思路:树状数组,用查分前缀和来算出每个玩具被t个区间包括,则分子加上C(t,3),分母是C(m,3)。
注意:deno=(long long)m*(m-1)*(m-2)/6; 中的(long long)不加就会WA。

代码:c++

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>

using namespace std;

int c[50000+10],n;

int lowbit(int x)
{
    return x&-x;
}
void add(int x,int d)
{
    while(x<=n)
    {
        c[x]+=d;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
long long gcd(long long a,long long b)
{
    if(!b)
        return a;
    else
        return gcd(b,a%b);
}
int main()
{
    #ifdef LOCAL
        freopen("in.txt","rb",stdin);
        //freopen("out.txt","wb",stdout);
    #endif // LOCAL
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int m,w[50000+10];
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&w[i]);
        memset(c,0,sizeof(c));
        for(int i=1;i<=m;i++)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            add(l,1);
            add(r+1,-1);
        }
        long long numr=0,deno=0;
        for(int i=1;i<=n;i++)
        {
            long long temp=sum(i);
            if(temp>2) numr+=temp*(temp-1)*(temp-2)/6*w[i];
        }
        deno=(long long)m*(m-1)*(m-2)/6;
        if(!deno || !numr)
        {
            cout << 0 << endl;
            continue;
        }
        long long temp=gcd(numr,deno);
        if(temp==deno) printf("%I64d\n",numr/deno);
        else
            printf("%I64d/%I64d\n",numr/temp,deno/temp);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值