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. WebStorm设置Themes

    1.首先去 http://www.phpstorm-themes.com/  选择你喜欢的主题,保存对应主题的xml文件到你本地 2.打开C:\Users\Administrator\.WebStor ...

  2. 正经学C#_判断[switch语句]:[c#入门经典]

    switch是一个和IF语句极其相似的语句.但是Switch允许条件可以有多个值. 程序的基本结构如下 switch(textVal) { case Val: 程序代码 break case Val2 ...

  3. How to extract pcd from a rosbag? 如何从rosbag中提取pcd

    4.1 bag_to_pcd Reads a bag file, saving all ROS point cloud messages on a specified topic as PCD fil ...

  4. hdu4658(广义五边形&分割函数2)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4658 题意:f(x) 为将 x 分成其他数和的形式的方案数.对于 t 组输入,输出 f(xi, k), ...

  5. luogu3380 树套树之线段树套线段树

    个人感觉可能是最不需要脑子写的方法 不过也不太好调 就是用一个普通的线段树维护这个序列,但是对于线段树的每一个区间,再开一个动态开点的权值线段树,里面存储这个区间所有元素值 单点修改只会涉及到log棵 ...

  6. shell操作数组

    #!/bin/bash nums=( ) echo ${#nums[*]} #向数组中添加元素 nums[]="http://c.biancheng.net/shell/" ech ...

  7. Unity 使用小技巧

    本文介绍我遇到过我Unity使用小技巧,有了这些技巧,项目做起来,溜得飞起 1.快速设置相机的位置 2.固定面板

  8. Mybatis学习笔记(二) —— mybatis入门程序

    一.mybatis下载 mybaits的代码由github.com管理,下载地址:https://github.com/mybatis/mybatis-3/releases 下载完后的目录结构: 二. ...

  9. java 反射 处理 空值

    package org.zkdg.utils.spring.annotations.impl; import java.lang.annotation.Annotation; import java. ...

  10. AD属性常量类

    参考:http://www.selfadsi.org/user-attributes.htm namespace Common { /// <summary> /// AD中的属性,没有出 ...