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

来源:互联网 发布:淘宝详情页模板下载 编辑:程序博客网 时间:2024/06/04 19:48

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

22 11 13 31 22 31 3

Sample Output

1 02 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
原创粉丝点击