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 敌兵布阵 线段树 点更新 // // 这道题裸的线段树的点更新,直接写就能够了 // // 一直以来想要进线段树的坑,结果一直没有跳进去,今天算是跳进去吧, // 尽管十分简单 ...
随机推荐
- ubuntu - 14.04,安装VirtualBox 5.0(虚拟机软件)!
VirtualBox是一款免费.开源的虚拟机软件,可以运行在多种操作系统上,真的是一款值得我们使用的虚拟机软件! 官方网址:https://www.virtualbox.org/ ubuntu14.0 ...
- Nodejs 文档概览
Node.js v8.11.1 Node.js v8.11.1 文档 今天大致浏览了一下Node.js的官方文档,走马观花的了解了大部分模块的api,对他们的使用场景做一个简单的笔记 assert 断 ...
- hdu1162(最小生成树 prim or kruscal模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 意义:给出一些点,用线问使所有点直接或间接连通,需要多长: 思路:裸最小生成树: 法1: pri ...
- OkHttp 3.x 源码解析之Interceptor 拦截器
拦截器 Java里的拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提 ...
- Tarjan缩点+LCA【洛谷P2416】 泡芙
P2416 泡芙 题目描述 火星猫经过一番努力终于到达了冥王星.他发现冥王星有 N 座城市,M 条无向边.火星猫准备出发去找冥王兔,他听说有若干泡芙掉落在一些边上,他准备采集一些去送给冥王兔.但是火星 ...
- [BZOJ4916]神犇和蒟蒻 杜教筛/Min_25筛
题目大意: 给定\(n\le 10^9\),求: 1.\(\sum_{i=1}^n\mu(i^2)\) 2.\(\sum_{i=1}^n\varphi(i^2)\) 解释 1.\(\sum_{i=1} ...
- kindedtor的基本使用
首先需要进入官网下载kineditor相关文件: 然后写代码: <!DOCTYPE html> <html> <head> <meta charset=&qu ...
- P1147 连续自然数和(思维题)
题目描述 对一个给定的自然数MM,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为MM. 例子:1998+1999+2000+2001+2002 = 100001998+1999+2000 ...
- php5 编译安装
#!/bin/bash######################################## File Name: php.sh# Version: V1.0# Author: sun yu ...
- Go语言基础之17--Redis基本操作
一.介绍与连接 1.1 介绍 使用第三方的redis库, github.com/garyburd/redigo/redis github地址:https://github.com/gomodule/r ...