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. SharpCompress压缩和解压缩,并解决压缩的中文乱码问题

    一.下载SharpCompress库 二.解压缩 (1)不带密码 /// <summary> /// 解压缩(支持rar,zip) /// </summary> /// < ...

  2. SQLServer数据库,表内存,实例名分析SQL语句

    --数据库内存分析 USE master go DECLARE @insSize TABLE(dbName sysname,checkTime VARCHAR(19),dbSize VARCHAR(5 ...

  3. 抽象类(abstract class)和接口(Interface)的区别

    前言 抽象类(abstract class)和接口(Interface)是Java语言中对于抽象类定义进行支持的两种机制,赋予了Java强大的面向对象能力. 二者具有很大的相似性,甚至可以相互替换,因 ...

  4. Python3 中socket使用

    1.动态导入模块 在当前目录下有lib和test目录,在test中要想使用lib中的aa的C类: test中: 第一种方法:推荐 importlib.import_module('lib.aa') o ...

  5. ubuntu - 如何搜索文件?

    1.whereis 文件名 特点:快速,但是是模糊查找,例如 找 #whereis mysql 它会把mysql,mysql.ini,mysql.*所在的目录都找出来.我一般的查找都用这条命令. 2. ...

  6. ubuntu - 14.04,安装CDT(Eclipse开发C++和C的插件)

    我已经安装完Eclipse了,我想把CDT加入到我的Eclipse里面. 一,下载CDT:我下载的是64位的CDT,下载地址:http://eclipse.bluemix.net/packages/m ...

  7. 【图灵学院09】RPC底层通讯原理之Netty线程模型源码分析

    1. dubbo 2.5.3 netty 3.2.5.Final

  8. React-Native App启动页制作(安卓端)

    原文地址:React-Native App启动页制作(安卓端) 这篇文章是根据开源项目react-native-splash-screen来写的.在使用react-native-link命令安装该包后 ...

  9. P2866 [USACO06NOV]糟糕的一天Bad Hair Day

    题意:给你一个序列,问将序列倒过来后,对于每个点,在再碰到第一个比它大的点之前,有多少比它小的? 求出比它小的个数的和 样例: 610374122 output: 5 倒序后:2    12    4 ...

  10. 第一章:初识JAVA

    一:计算机语言发展史 机器语言:典型的二进制文件和计算机交流. 汇编语言: 通过大量的标识符表示一些基本操作来和计算机做交流. 高级语言:通过常见的英语指令来编写程序,完成沟通 常见高级语言 Java ...