hdu 5419 Victor and Toys 线段树成段更新
来源:互联网 发布:linux ntpclient 编辑:程序博客网 时间:2024/05/26 08:43
Victor and Toys
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 156 Accepted Submission(s): 54
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 generatesm 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?
Victor has a sense of math and he generates
Input
The first line of the input contains an integer T , denoting the number of test cases.
In every test case, there are two integersn and m in the first line, denoting the number of the toys and intervals.
The second line containsn integers, the i -th integer wi denotes that the beauty of the i -th toy.
Then there arem 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 .
In every test case, there are two integers
The second line contains
Then there are
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 likep/q .
If the answer is an integer, just print a single interger, otherwise print an irreducible fraction like
Sample Input
13 41 1 52 31 33 31 1
Sample Output
5/4
Source
BestCoder Round #52 (div.2)
Recommend
hujie | We have carefully selected several similar problems for you: 5421 5420 5418 5417 5416
题意,给出n个点,m个区间,先出三个区间,左值取最大,右值取最小,区间和的期望。考虑每一个点,对结果的影响,如果,一个点包括了x个区间,那么,就有C(x,3)个w[i]在分子上,分母为C(m,3);所以最终的问题就是要求每个点,所点的区间数,真接用区间的开头加1,结尾减1,复杂度为o(n ),如果用线段树成段更新,复杂度为o(n * log(n);注意不要爆了long long,所以分子分母先都除上6.
第一种方法
#define N 300005#define M 100005#define maxn 205#define SZ 26#define MOD 1000000000000000007#define lson (now<<1)#define rson (now<<1|1)int n,T,m,w[N],a,b,num[N],pri[N][2];int main(){ while(S(T)!=EOF) { while(T--){ S2(n,m); For(i,1,n+1){ scan_d(a); w[i] = a; num[i] = 0; pri[i][0] = pri[i][1] = 0; } FI(m){ scan_d(a);scan_d(b); pri[a][0]++; pri[b][1]++; } int nn = 0; for(int i = 1;i<=n;i++){ nn += pri[i][0]; num[i] = nn; nn -= pri[i][1]; } ll ans = 0,s2 = (ll)(m) * (ll)(m - 1) * (ll)(m -2) / 6; For(i,1,n+1){ ll t = (ll) num[i]; if(t >= 3){ ans += (ll)t * (ll)(t - 1) * (ll)(t - 2) * (ll)w[i] / 6; } } if(ans == 0){ printf("%lld\n",ans); continue; } ll gc = gcd(ans,s2); ans /= gc;s2 /= gc; if(s2 == 1) printf("%lld\n",ans); else printf("%lld/%lld\n",ans,s2); } } //fclose(stdin); //fclose(stdout); return 0;}
第二种方法(线段树)
#define N 300005#define M 100005#define maxn 205#define SZ 26#define MOD 1000000000000000007#define lson (now<<1)#define rson (now<<1|1)int n,T,m,w[N],a,b;struct node{ int sum;};node tree[N*4];void pushDown(int now){ if(tree[now].sum > 0){ tree[lson].sum += tree[now].sum; tree[rson].sum += tree[now].sum; tree[now].sum = 0; }}void buildTree(int l,int r,int now){ tree[now].sum = 0; if(l >= r){ return ; } int mid = (l+r)>>1; buildTree(l,mid,lson); buildTree(mid+1,r,rson);}void updateTree(int l,int r,int now,int s,int e,int c){ if(s <= l && e>= r){ tree[now].sum += c; return ; } pushDown(now); int mid = (l+r)>>1; if(s <= mid) updateTree(l,mid,lson,s,e,c); if(e > mid) updateTree(mid+1,r,rson,s,e,c);}int queryTree(int l,int r,int now,int s,int e){ if(s <= l && e>= r){ return tree[now].sum; } pushDown(now); int mid = (l+r)>>1; if(s <= mid) return queryTree(l,mid,lson,s,e); if(e > mid) return queryTree(mid+1,r,rson,s,e); return 0;}int main(){ while(S(T)!=EOF) { while(T--){ S2(n,m); buildTree(1,n,1); For(i,1,n+1){ scan_d(a); w[i] = a; } FI(m){ scan_d(a);scan_d(b); updateTree(1,n,1,a,b,1); } ll ans = 0,s2 = (ll)(m) * (ll)(m - 1) * (ll)(m -2) / 6; For(i,1,n+1){ ll t = (ll) queryTree(1,n,1,i,i); if(t >= 3){ ans += (ll)t * (ll)(t - 1) * (ll)(t - 2) * (ll)w[i] / 6; } } if(ans == 0){ printf("%lld\n",ans); continue; } ll gc = gcd(ans,s2); ans /= gc;s2 /= gc; if(s2 == 1) printf("%lld\n",ans); else printf("%lld/%lld\n",ans,s2); } } //fclose(stdin); //fclose(stdout); return 0;}
0 0
- hdu 5419 Victor and Toys 线段树成段更新
- hdu 5419 Victor and Toys
- HDU 5419 Victor and Toys
- HDU 5419 Victor and Toys ()
- hdu 5419 Victor and Toys(暴力+组合)
- 【HDOJ 5419】 Victor and Toys (排列组合)
- HDOJ 5419 Victor and Toys 树状数组
- 【hdu5419】Victor and Toys
- hdu 5419 Victor and Toys(期望+差分前缀和)
- BestCoder#52 Victor and Toys
- hdu5419--Victor and Toys(枚举)
- hdu 5420 Victor and Proposition 线段树建图+强连通分量
- hdu 5420 Victor and Proposition(强连通+线段树建图)
- 【HDU】5420 Victor and Proposition【SCC+线段树】
- hdu5419 Victor and Toys 期望,差分前缀和
- hdu 5417 Victor and Machine
- hdu 5417 Victor and Machine
- HDU 5417 Victor and Machine
- 最长回文子串
- ECMAScript6学习笔记《二》-----“const命令与let命令”
- POJ 1330 ---(线段树在线LCA 与 tarjan离线LCAs)
- TextView和EditText的总结 (收藏)
- Jetty学习(三)--jetty部署spring mvc项目
- hdu 5419 Victor and Toys 线段树成段更新
- Ubuntu下安装搜狗输入法
- android Bitmap内存优化(一) Bitmap 详解
- 找出二叉树中某两个结点的第一个公共祖先
- poj1080
- opencv学习(七)(opencv3.0.0+VS2012+win7)打开摄像头并且进行边缘检测加上了滑动条
- python二进制处理详述
- Java之旅hibernate(1)——初始
- 会说话的tom猫