分母是一定的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

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
 

/* ***********************************************
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 树状数组的更多相关文章

  1. hdoj 1166 敌兵布阵(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...

  2. HDOJ 4455 Substrings 递推+树状数组

    pre[i]第i位数往前走多少位碰到和它同样的数 dp[i]表示长度为i的子串,dp[i]能够由dp[i-1]加上从i到n的pre[i]>i-1的数减去最后一段长度为i-1的断中的不同的数得到. ...

  3. HDOJ 4417 - Super Mario 线段树or树状数组离线处理..

    题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...

  4. 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

    pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...

  5. 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵

    题目传送门 /* 线段树基本功能:区间值的和,修改某个值 */ #include <cstdio> #include <cstring> #define lson l, m, ...

  6. 线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations

    题目传送门 /* 题意:不懂... 线段树+树状数组+贪心:贪心从第一位开始枚举,一个数可以是循环节的末尾或者在循环节中,循环节(循环节内部是后面的换到前面,最前面的换到最后面).线段树维护最大值,树 ...

  7. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  8. HDOJ/HDU 1556 Color the ball(树状数组)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3-.N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从 ...

  9. hdoj 1166 敌兵布阵 线段数和树状数组

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

随机推荐

  1. nyoj--1011--So Easy[II](数学几何水题)

    So Easy[II] 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 这是一道基础的计算几何问题(其实这不提示大家也都看的出).问题描述如下: 给你一个N边形.且N边形 ...

  2. 36.创建自定义的指令directive

    转自:https://www.cnblogs.com/best/tag/Angular/ 1. <html> <head> <meta charset="utf ...

  3. 状态压缩dp初学__$Corn Fields$

    明天计划上是要刷状压,但是作为现在还不会状压的\(ruoruo\)来说是一件非常苦逼的事情,所以提前学了一下状压\(dp\). 鸣谢\(hmq\ juju\)的友情帮助 状态压缩动态规划 本博文的大体 ...

  4. BZOJ 3110 线段树套线段树

    思路: 外围一个权值线段树 里面是个区间线段树 搞一个标记永久化 //By SiriusRen #include <cstdio> #include <cstring> #in ...

  5. POJ 3671 DP or 乱搞

    思路: 1.DP f[i][j]:前i个数 最后一个数是j的最小花费 f[i][j]=min(f[i][j],f[i-1][k]+(a[i]!=j));1<=k<=j 这种做法比较有普遍性 ...

  6. CUDA学习笔记(四)

    昨天一直在写ben的作业.总结一下周一的cuda情况. cuda程序需要用到一些设置的参数,如argv[],另外cuda读入文件特别苛刻,只能采用C的方式,而且对w+,r的使用只有试通才行. 卧底天外 ...

  7. Spring项目的配置文件们(web.xml context servlet springmvc)

    我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目的配置文件.我理解它是servlet的配置文件,也就是说,与spring无关.即使你开发的是一个纯粹jsp ...

  8. iOS 集成Protobuf,转换proto文件

    原文地址:http://blog.csdn.net/hyq4412/article/details/54891038 附加Homebrew安装地址:https://brew.sh/index_zh-c ...

  9. bash编程,while嵌套case语句, file不能判断文件存在与否

    写一个脚本, 完成如下要求 (1)脚本可接受参数 : start, stop, restart, status, (2)如果参数非非法, 提示使用格式后报错退出; (3)如果是start, 则创建/t ...

  10. BZOJ 3277/3473 广义后缀自动机

    说实话没啥难的. 建一棵广义后缀自动机,暴力自底向上更新即可. 时间复杂度非常玄学,但据说是可以过的. 要注意每个串中相同的子串的贡献是都要加进去的,开始因为这个被坑了好久 QAQ Code: #in ...