HDOJ 5419 Victor and Toys 树状数组
分母是一定的C(m,3) 树状数组求每一个数能够在那些段中出现,若x出如今了s段中,分子加上w[x]*C(s,3)
Victor and Toys
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 331 Accepted Submission(s): 118
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?
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.
: 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.
1
3 4
1 1 5
2 3
1 3
3 3
1 1
5/4
/* ***********************************************
Author :CKboss
Created Time :2015年08月23日 星期日 14时23分47秒
File Name :HDOJ5419.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef unsigned long long int LL; const int maxn=50500; /****************BIT***********************/ int n,m;
int w[maxn];
int l[maxn],r[maxn]; inline int lowbit(int x) { return x&(-x); } int tree[maxn]; void add(int p,int v)
{
for(int i=p;i<maxn;i+=lowbit(i)) tree[i]+=v;
} int sum(int p)
{
int ret=0;
for(int i=p;i;i-=lowbit(i)) ret+=tree[i];
return ret;
} LL getC(LL x)
{
return x*(x-1)/2LL*(x-2)/3LL;
} LL gcd(LL a,LL b)
{
if(b==0) return a;
return gcd(b,a%b);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",w+i);
memset(tree,0,sizeof(tree));
for(int i=0;i<m;i++)
{
scanf("%d%d",l+i,r+i);
add(l[i],1); add(r[i]+1,-1);
} if(m<3) { puts("0"); continue; } LL up=0,down=getC(m);
for(int i=1;i<=n;i++)
{
LL x=sum(i);
if(x>=3)
{
up=up+w[i]*getC(x);
}
}
if(up==0) { puts("0"); continue; } LL g=gcd(up,down);
if(g==down) cout<<up/g<<endl;
else cout<<up/g<<"/"<<down/g<<endl;
} return 0;
}
HDOJ 5419 Victor and Toys 树状数组的更多相关文章
- hdoj 1166 敌兵布阵(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...
- HDOJ 4455 Substrings 递推+树状数组
pre[i]第i位数往前走多少位碰到和它同样的数 dp[i]表示长度为i的子串,dp[i]能够由dp[i-1]加上从i到n的pre[i]>i-1的数减去最后一段长度为i-1的断中的不同的数得到. ...
- HDOJ 4417 - Super Mario 线段树or树状数组离线处理..
题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...
- 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵
题目传送门 /* 线段树基本功能:区间值的和,修改某个值 */ #include <cstdio> #include <cstring> #define lson l, m, ...
- 线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations
题目传送门 /* 题意:不懂... 线段树+树状数组+贪心:贪心从第一位开始枚举,一个数可以是循环节的末尾或者在循环节中,循环节(循环节内部是后面的换到前面,最前面的换到最后面).线段树维护最大值,树 ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- HDOJ/HDU 1556 Color the ball(树状数组)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...
- hdoj 1166 敌兵布阵 线段数和树状数组
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
随机推荐
- windows电脑空间清理
最近电脑空间又快满了,想下载一些好电影音频资源都要先临时清理一些文件才行,今天有时间就彻底整理一下,将整理过程及用到的好工具都记录一下,方面下次再遇到问题时可以很方面的参考执行. 1.分析磁盘空间占用 ...
- [BZOJ3670] [NOI2014] 动物园 解题报告 (KMP)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3670 Description 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅, ...
- C# App.config 自定义 配置节 出现的问题:配置系统未能初始化
C# 读取app.config配置文件 节点键值,提示 "配置系统未能初始化" 错误的解决方案 新建C#项目,在app.config中添加了appSettings项,运行时出现&q ...
- UVa 1599 Ideal Path【BFS】
题意:给出n个点,m条边,每条边上涂有一个颜色,求从节点1到节点n的最短路径,如果最短路径有多条,要求经过的边上的颜色的字典序最小 紫书的思路:第一次从终点bfs,求出各个节点到终点的最短距离, 第二 ...
- MyEclipse for mac 快捷键
原文出处:http://blog.csdn.net/ray_seu/article/details/17384463 一直比较欣赏myeclipse的快捷键,网上搜索了一圈,发现windows平台下面 ...
- UI Framework-1: Ash Color Chooser
Ash Color Chooser Overview This document describes how to achieve <input type=”color”> UI in C ...
- [JSOI2018]潜入行动 树形DP_复杂计数
code #include <cstdio> #include <algorithm> #include <cstring> #include <string ...
- Java导出csv修正时间格式
处理前导出的日期格式为:yyyy-M-d HH:mm 正确的应该是:yyyy-MM-dd HH:mm:ss 处理方案是在 时间数据两边增加 "\t" 有问题欢迎交流. thanks ...
- 使用 Beego 搭建 Restful API 项目
1 环境准备 首先你需要在你的环境安装以下软件: go:编程语言运行环境 git:版本控制工具 beego:go 语言流行的开发框架 bee:beego 配套的快速搭建工具 你喜欢的数据库:这里以 M ...
- 【Henu ACM Round#20 B】Contest
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 根据时间和原分数. 算出对应的分数就可以了. [代码] #include <bits/stdc++.h> using n ...