hdu5419 Victor and Toys
2015-09-07 22:30
169 查看
设数列s,位置为i的元素被区间覆盖的数目定义为∑s[j], j ≤ i。
则给定区间[l, r], 更新数列s,只需:
++s[l],--s[r + 1]。
http://acm.hdu.edu.cn/showproblem.php?pid=5419
View Code
则给定区间[l, r], 更新数列s,只需:
++s[l],--s[r + 1]。
http://acm.hdu.edu.cn/showproblem.php?pid=5419
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef __int64 LL; const int maxn = 5e4 + 10; int w[maxn], cnt[maxn]; LL A, B; int n, m; LL gcd(LL a, LL b){ if(!b) return a; return gcd(b, a % b); } LL f[maxn]; void init(){ for(int i = 0; i < 3; i++) f[i] = 0; f[3] = 1; for(int i = 3; i < maxn; i++) f[i + 1] = f[i] * (i + 1) / (i - 2); } int main(){ int T; scanf("%d", &T); init(); while(T--){ scanf("%d%d", &n, &m); memset(cnt, 0, sizeof cnt); for(int i = 1; i <= n; i++) scanf("%d", &w[i]); for(int i = 1, p, q; i <= m; i++){ scanf("%d%d", &p, &q); ++cnt[p], --cnt[q + 1]; } for(int i = 1; i <= n; i++) cnt[i] += cnt[i - 1]; B = f[m]; A = 0; for(int i = 1; i <= n; i++) A += (LL)w[i] * f[cnt[i]]; if(!A || !B){ printf("0\n"); continue; } LL D = gcd(A, B); A /= D, B /= D; if(B == 1) printf("%I64d\n", A); else printf("%I64d/%I64d\n", A, B); } return 0; }
View Code
相关文章推荐
- Java Web学习(一)
- 《资源革命:如何抓住一百年来最大的商机》:资源紧缺是一个巨大的商机,有许多创新的解决办法。三星推荐
- Linux内存管理之mmap详解
- jdk 环境变量配置
- 1072 Nightmare
- LeetCode Sum Root to Leaf Numbers
- 腾讯16算法 杂谈
- Android五大存储---内部存储(缓存)
- JavaScript之prototype
- LeetCode Sum Root to Leaf Numbers
- 学习 《一步步搭建自己的博客》 第一版 之异常 边理解边记录
- Objective-C( Category 分类,非正式协议,分类延展)
- 数据库连接和定义
- 欢迎使用CSDN-markdown编辑器
- Qt 信号槽机制解析一 理论篇
- HDOJ 1010 Tempter of the Bone
- 简单工厂、工厂方法、抽象工厂的区别
- Swift实现的快速排序及sorted方法的对比
- Android - 模块添加与编译
- URL cache