分母是一定的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. tf.nn.top_k(input, k, name=None)和tf.nn.in_top_k(predictions, targets, k, name=None)

    tf.nn.top_k(input, k, name=None) 这个函数的作用是返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引. input: 一个张量,数据类型必须是以下之一 ...

  2. bzoj1615 麻烦的干草打包机 BFS

    Description Farmer John新买的干草打包机的内部结构大概算世界上最混乱的了,它不象普通的机器一样有明确的内部传动装置,而是,N (2 <= N <= 1050)个齿轮互 ...

  3. 题解 P3243 【[HNOI2015]菜肴制作】

    这道题看起来就是个裸的拓扑排序,抄上模板就能AC. 上面这种想法一看就不现实,然鹅我第一次还真就这么写了,然后被随意hack. 我们需要注意一句话: 现在,酒店希望能求出一个最优的菜肴的制作顺序,使得 ...

  4. HttpService解析

    HttpServlet容器响应Web客户请求流程如下: 1)Web客户向Servlet容器发出Http请求: 2)Servlet容器解析Web客户的Http请求: 3)Servlet容器创建一个Htt ...

  5. 新一代企业即时通信系统 -- 傲瑞通(OrayTalk)

    傲瑞通(OrayTalk)是我们为企业专门打造的新一代企业即时通讯平台,功能强大丰富.像组织结构.文字/语音/视频会话.文件传送.远程协助.消息记录等功能都有,而且留有接口可与企业遗留系统进行集成. ...

  6. [Python] Normalize the data with Pandas

    import os import pandas as pd import matplotlib.pyplot as plt def test_run(): start_date='2017-01-01 ...

  7. Activity 中的Toast在Activity销毁后报错,解决方法,把context改成应用的

    ToastUtil.showShort(context, R.string.connection_fail); 改成 ToastUtil.showShort(BusinesslinkApplicati ...

  8. bzoj 2120 数颜色 题解

    转载请注明:http://blog.csdn.net/jiangshibiao/article/details/23990489 [原题] 2120: 数颜色 Time Limit: 6 Sec  M ...

  9. Android资源之图像资源(状态图像资源)

    在上一篇博文中.我主要解说了XML图像资源中的图层资源,在此图像资源博文中我会给大家陆续解说XMl图像资源的图像状态资源.图像级别资源.淡入淡出资源.嵌入图像资源.剪切图像资源和外形资源. 1.图像状 ...

  10. AES加密解密在JAVA和ANDROID下互通

    <span style="font-family: Arial, Helvetica, sans-serif;">昨天外包安卓的那个人说AES的加解密结果不一样.于是百 ...