前缀和的用法 Problem(M33):IQ Test

Judge Info

  • Memory Limit: 65537KB
  • Case Time Limit: 5000MS
  • Time Limit: 5000MS
  • Judger: Normal

Description

Given an array A of N elements , define an operation that consists of two parameters L and R such that Ai=Ai+i-L+1,L<=i<=R. You are asked to calculate the value of all elements in A after conducting all the operations. All initial Ai is 0.

Input

The first line is a number T(1<=T<=10) which indicates the number of test cases. Next line is a number N(1<=N<=100000) which indicates the number of elements in the array and a number M(1<=M<=100000) which indicates the number of operations, following M lines, each line contains two number L, R(1<=L<=R<=N). Proceed to the end of file.

Output

For each test case output N numbers in a line, which are the value of elements mod 1000,000,007 in the array.

Sample Input

2
2 1
1 1
3 3
1 2
2 3
1 3

Sample Output

1 0
2 5 5

 



#include <iostream>



#include <cstdio>
#include <cstring>
#include <string.h>
#include <algorithm>


#define mod 1000000007;




#include <map>
using namespace std;
//题目要求每次对区间两端范围内进行Ai=Ai-L+1的操作
//如果暴力必然超时。。n,m都是10^5 因此采用前缀和的方法!
long long f[100500];      //i的数组              //写int在这个会WA..估计得每次mod一下就可以用int了
long long l[100500];          //L的数组




int main()
{
int i,j,k,t,n,m,x,y;

scanf("%d",&t);
for (i=1;i<=t;i++)
{
memset(f,0,sizeof(f));
memset(l,0,sizeof(l));


scanf("%d%d",&n,&m);
for (j=1;j<=m;j++)
{

scanf("%d%d",&x,&y);

f[x]++;   l[x]-=x;    //每次在L处+1,在R+1处-1,
f[y+1]--;      l[y+1]+=x; 
//每次在L处-X,在R+1处+X, 
}
for (k=1;k<=n;k++)  //对f求前缀和
{
f[k]+=f[k-1];
}

for (k=1;k<=n;k++)  //对l求前缀和
{
l[k]+=l[k-1];
}

for (k=1;k<=n;k++)  //计算
{
f[k]=    ((k+1)*f[k]  +l[k] )%mod  ;      //这里得mod一下。忘记了之前,导致wa
}

for (k=1;k<=n;k++)
{
if (k!=1) printf(" ");
printf("%d",f[k]);
}
printf("\n");

/* for (k=1;k<=n;k++)   //清零 
{
l[k]=f[k]=0;
}*/
}

return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值