Victor and Toys
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 156 Accepted Submission(s): 54
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?
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.
If the answer is an integer, just print a single interger, otherwise print an irreducible fraction likep/q.
考慮每一個點,對結果的影響,如果,一個點包括了x個區間,那麼,就有C(x,3)個w[i]在分子上,分母爲C(m,3);所以最終的問題就是要求每個點,所點的區間數,真接用區間的開頭加1,結尾減1,複雜度爲o(n ),如果用線段樹成段更新,複雜度爲o(n * log(n);注意不要爆了long long,所以分子分母先都除上6.
第一種方法
#define N 300005
#define M 100005
#define maxn 205
#define SZ 26
#define MOD 1000000000000000007
#define lson (now<<1)
#define rson (now<<1|1)
int n,T,m,w[N],a,b,num[N],pri[N][2];
int main()
{
while(S(T)!=EOF)
{
while(T--){
S2(n,m);
For(i,1,n+1){
scan_d(a);
w[i] = a;
num[i] = 0;
pri[i][0] = pri[i][1] = 0;
}
FI(m){
scan_d(a);scan_d(b);
pri[a][0]++;
pri[b][1]++;
}
int nn = 0;
for(int i = 1;i<=n;i++){
nn += pri[i][0];
num[i] = nn;
nn -= pri[i][1];
}
ll ans = 0,s2 = (ll)(m) * (ll)(m - 1) * (ll)(m -2) / 6;
For(i,1,n+1){
ll t = (ll) num[i];
if(t >= 3){
ans += (ll)t * (ll)(t - 1) * (ll)(t - 2) * (ll)w[i] / 6;
}
}
if(ans == 0){
printf("%lld\n",ans);
continue;
}
ll gc = gcd(ans,s2);
ans /= gc;s2 /= gc;
if(s2 == 1)
printf("%lld\n",ans);
else
printf("%lld/%lld\n",ans,s2);
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
第二種方法(線段樹)
#define N 300005
#define M 100005
#define maxn 205
#define SZ 26
#define MOD 1000000000000000007
#define lson (now<<1)
#define rson (now<<1|1)
int n,T,m,w[N],a,b;
struct node{
int sum;
};
node tree[N*4];
void pushDown(int now){
if(tree[now].sum > 0){
tree[lson].sum += tree[now].sum;
tree[rson].sum += tree[now].sum;
tree[now].sum = 0;
}
}
void buildTree(int l,int r,int now){
tree[now].sum = 0;
if(l >= r){
return ;
}
int mid = (l+r)>>1;
buildTree(l,mid,lson);
buildTree(mid+1,r,rson);
}
void updateTree(int l,int r,int now,int s,int e,int c){
if(s <= l && e>= r){
tree[now].sum += c;
return ;
}
pushDown(now);
int mid = (l+r)>>1;
if(s <= mid) updateTree(l,mid,lson,s,e,c);
if(e > mid) updateTree(mid+1,r,rson,s,e,c);
}
int queryTree(int l,int r,int now,int s,int e){
if(s <= l && e>= r){
return tree[now].sum;
}
pushDown(now);
int mid = (l+r)>>1;
if(s <= mid) return queryTree(l,mid,lson,s,e);
if(e > mid) return queryTree(mid+1,r,rson,s,e);
return 0;
}
int main()
{
while(S(T)!=EOF)
{
while(T--){
S2(n,m);
buildTree(1,n,1);
For(i,1,n+1){
scan_d(a);
w[i] = a;
}
FI(m){
scan_d(a);scan_d(b);
updateTree(1,n,1,a,b,1);
}
ll ans = 0,s2 = (ll)(m) * (ll)(m - 1) * (ll)(m -2) / 6;
For(i,1,n+1){
ll t = (ll) queryTree(1,n,1,i,i);
if(t >= 3){
ans += (ll)t * (ll)(t - 1) * (ll)(t - 2) * (ll)w[i] / 6;
}
}
if(ans == 0){
printf("%lld\n",ans);
continue;
}
ll gc = gcd(ans,s2);
ans /= gc;s2 /= gc;
if(s2 == 1)
printf("%lld\n",ans);
else
printf("%lld/%lld\n",ans,s2);
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}