Victor and Toys
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 654 Accepted Submission(s): 219
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?
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.
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.
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
题目描述:
解题思路1:(差分前缀和)预处理出来s[i]数组,表示每个玩具在多少个区间内。E=sigma(xi*pi)。这里的xi就是有趣值,pi就是C(s[i],3)/C(m,3)。所以这道题关键是处理出来s[i]。同时注意姿势优美,别爆long long。至于差分前缀和,其实是处理离线区间问题的一个巧妙数组应用,对于m个区间,在区间左端点li的地方+1,在区间右端点ri的地方-1。最后前缀和处理, n 的复杂度就能得到第i个玩具在多少个区间内。
#include<bits/stdc++.h> using namespace std; typedef __int64 INT; const int maxn=55000; int a[maxn],s[maxn]; INT cal(INT nn){ if(nn<3) return 0; return (nn-2)*(nn-1)*nn/6; } INT GCD(INT a,INT b){ return b==0?a:GCD(b,a%b); } int main(){ int t,n,m,li,ri; scanf("%d",&t); while(t--){ memset(s,0,sizeof(s)); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=m;i++){ //差分 scanf("%d%d",&li,&ri); s[li]++;s[ri+1]--; } for(int i=1;i<=n;i++){ //前缀和。s数组中的值就是第i个玩具在多少个区间内。 s[i]+=s[i-1]; } INT fm,fz; fz=0; for(int i=1;i<=n;i++){ fz+=cal((INT)s[i])*a[i]; } if(m<3){ puts("0"); continue; } fm=cal(m); if(fz==0){ printf("0 ",fm); }else { INT gcd=GCD(fz,fm); fz/=gcd,fm/=gcd; if(fm==1) printf("%I64d ",fz); else printf("%I64d/%I64d ",fz,fm); } } return 0; }