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;
}