HDU 5419——Victor and Toys——————【线段树|差分前缀和】
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
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 like p/q.

#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——————【线段树|差分前缀和】的更多相关文章
- 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,求物 ...
- HDU 3016 Man Down (线段树+dp)
HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- hdu 1754 I Hate It 线段树 点改动
// hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...
- hdu 1166 敌兵布阵 线段树 点更新
// hdu 1166 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...
随机推荐
- C# 链表 --增 -删-反转-删除最小值
1. Node.cs namespace 链表 { public class Node<T> { public T Data; //这个就是地址 public Node<T> ...
- 搭建TensorFlow
网上有许多在线安装TensorFlow框架的,我试了好多,结果安装时间长先不说,还总是出现一些问题,然后我就想着离线安装,成功了,与大家分享! (1)首先,需要下载离线安装的TensorFlow包,可 ...
- 温故而知新_C语言_递归
递归. 是的,差不多就是这种感觉.上面就是类似递归的显示表现. 2017 10 24更新: 递归这个问题放了很久.也没有写.大概是自己还没有好好理解吧. 在这里写下自己理解的全部. 一 何为递归. 字 ...
- ubuntu - 14.04,常用PPA源(第三方提供的deb格式安装文件)!!
说明: 1,下面所有PPA源的执行命令,均为在shell中执行的命令,需要依次执行! 2,下面所有测试方法,均为在shell中执行的命令!! PPA源: 一,Oracle JDK:Oracle公司提供 ...
- [SinGuLaRiTy] 分治题目复习
[SInGuLaRiTy-1025] Copyrights (c) SinGuLaRiTy 2017. All Rights Reserved. [POJ 1905] 棍的膨胀 (Expanding ...
- History命令用法15例
以下内容为转载: 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTT ...
- C#引号中用@报错
如SQL = " INSERT INTO A SELECT * FROM B@DBLINK "会报“Parameter '@DBLINK' specified but none o ...
- linux下redis的安装与django-redis使用方法
Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. Redis支持五种数据类型:string(字符串),hash(哈希),list(列表),set( ...
- loj #2024. 「JLOI / SHOI2016」侦查守卫
#2024. 「JLOI / SHOI2016」侦查守卫 题目描述 小 R 和 B 神正在玩一款游戏.这款游戏的地图由 nnn 个点和 n−1n - 1n−1 条无向边组成,每条无向边连接两个点, ...
- ORACLE 中dbms_stats的使用
dbms_stats能良好地估计统计数据(尤其是针对较大的分区表),并能获得更好的统计结果,最终制定出速度更快的SQL执行计划. exec dbms_stats.gather_schema_stats ...