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 ...
随机推荐
- PostgreSQL Replication之第四章 设置异步复制(3)
4.3 slave到master的切换 如果您想扩展读或您想做一个数据备份,一个 slave是件美好的事情.但是,slave可能不会一直是slave.在有些时候,您可能需要把slave转换为maste ...
- Python开发注意事项
仅为记录自己在使用python过程的的一些心得! 1.服务器上运行脚本: windows服务器: 显式运行:在cmd中直接用python xxxx.py 运行一个py脚本文件. 后台运行:在cm ...
- MPP的进化 - 深入理解Batch和MPP优缺点
https://mp.weixin.qq.com/s/scXNfkpjktCZxBg3pYEUUA?utm_medium=hao.caibaojian.com&utm_source=hao.c ...
- CSS 预处理语言之 less 篇
less 前言 Less 是一门 CSS 预处理语言,它扩充了 CSS 语言,增加了诸如变量.混合(mixin).函数等功能,让 CSS 更易维护.方便制作主题.扩充. 安装 客户端使用 // 引入 ...
- 2019 前端面试题汇总(主要为 Vue)
原文链接:点我 由于我的技术栈主要为Vue,所以大部分题目都是Vue开发相关的. 1. 谈谈你对MVVM开发模式的理解 MVVM分为Model.View.ViewModel三者. Model:代表数据 ...
- Python对象的循环引用问题
目录 Python对象循环引用 循环引用垃圾回收算法 容器对象 生成容器对象 追踪容器对象 结束追踪容器对象 分代容器对象链表 何时执行循环引用垃圾回收 循环引用的垃圾回收 循环引用中的终结器 pyt ...
- 我的CSDN原创高质量免积分下载资源列表(持续更新)
最近几个月,我在CSDN平台,发表了大量原创高质量的项目,并给出了相应的源码.文档等相关资源. 为了方便CSDN用户或潜在需求者,下载到自己想要的资源,特分类整理出来,欢迎大家下载. 我的原则:原创高 ...
- 【ICM Technex 2018 and Codeforces Round #463 (Div. 1 + Div. 2, combined) C】 Permutation Cycle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] p[i] = p[p[i]]一直进行下去 在1..n的排列下肯定会回到原位置的. 即最后会形成若干个环. g[i]显然等于那个环的大 ...
- sql暂时表的创建
create table #simple /*仅仅对当前用户有效.其它用户无法使用,断掉连接后马上销毁该表*/ ( id int not null ) select * from #simple c ...
- poj1014 hdu1059 Dividing 多重背包
有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...