您的位置:首页 > 其它

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

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: