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?

 
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
 
 
题目描述:
 
解题思路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\n",fm);
}else {
INT gcd=GCD(fz,fm);
fz/=gcd,fm/=gcd;
if(fm==1)
printf("%I64d\n",fz);
else
printf("%I64d/%I64d\n",fz,fm);
}
}
return 0;
}

  

HDU 5419——Victor and Toys——————【线段树|差分前缀和】的更多相关文章

  1. HDU - 5419 Victor and Toys(组合计数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5419 题意 n个物品,标号1-n,物品i有权值wi.现在有m个区间[l,r],从中任意选三个区间i,j,k,求物 ...

  2. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  3. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  4. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  5. HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)

    HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...

  6. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  7. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  8. hdu 1754 I Hate It 线段树 点改动

    // hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...

  9. hdu 1166 敌兵布阵 线段树 点更新

    // hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...

随机推荐

  1. FileInfo类和DirectoryInfo类

    FileInfo类和DirectoryInfo类可以方便地对文件和文件夹进行操作. 1. FileInfo类(非静态类) FileInfo类和File类之间许多方法调用都是相同的,但是FileInfo ...

  2. 【转】php通过curl跨域向asp.net服务器上传文件及参数

    转:http://blog.sina.com.cn/s/blog_13331dce50102vq32.html 这是一个由php通过调用asp.net接口向asp.net服务器post上传文件及参数并 ...

  3. 13、OpenCV Python canny边缘提取

    __author__ = "WSX" import cv2 as cv import numpy as np def lapalian_demo(image): #拉普拉斯算子 # ...

  4. 2.Valid Parentheses (括号匹配)

    Level: ​  Easy 题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  5. springboot整合activemq(二),消费均匀分析

    问题分析:当如果多个消费者是什么情况呢 topic消费是友多个消费者的,是支持的,但是queue是支持,但是不能保证多个消费均匀消费,在分布式环境下怎么操作呢: 看案例: 在前面整合代码执行: 浏览器 ...

  6. php 常见递归实例

    //计算数组{1,1,2,3,5,8.......} 第n位值 function Process1($i){ if ($i == 0) return 0; if ($i == 1) return 1; ...

  7. SprimgMVC学习笔记(八)—— SpringMVC与前台json数据交互

    一.两种交互形式 可以看出,前台传过来的方式有两种,一种是传json格式的数据过来,另一种就是在url的末尾传普通的key/value串过来,针对这两种方式,在Controller类中会有不同的解析, ...

  8. Js 处理 错误图片...(不用jquery)

    document.addEventListener("error", function (e) { var elem = e.target; if (elem.tagName.to ...

  9. IP 分段 子网掩码

    子网掩码分网段 例如 200台机器分成4个子网 [ ip 段 ] 200台机器,4个子网,那么就是每个子网50台机器,设定为192.168.10.0,C类的IP,大子网掩码应为255.255.255. ...

  10. Unix shell判断和比较

    1.  shell 的$! ,$?, $$,$@ $n        $1 the first parameter,$2 the second... $#        The number of c ...