###什么是差分数组?
####定义:
对于已知有n个元素的数列d,建立记录它每项与前一项差值的差分数组f:显然,f[1]=d[1]-0=d[1];对于整数i∈[2,n],我们让f[i]=d[i]-d[i-1]。
####性质:
计算数列各项的值:观察d[2]=f[1]+f[2]=d[1]+d[2]-d[1]=d[2]可知,d[i]=f[i]的前缀和。
####演算
![](/image_editor_upload/20201218031045_10366.png)
![](/image_editor_upload/20201218031103_88862.png)
字有点丑!!!若有不足,请指正
####代码
```java
public class Case_分苹果 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int StudentNum = sc.nextInt();
int TeacherNum = sc.nextInt();
int AppleNum[] = new int[StudentNum+2];
for( int i = 0 ; i < TeacherNum ; i++ )
{
int Left = sc.nextInt();
int Right = sc.nextInt();
int C = sc.nextInt();
//区间首加,区间尾减
AppleNum[Left] += C;
AppleNum[Right+1] -= C;
}
//通过差分数组推导,原数组
for( int i = 1 ; i <= StudentNum ; i++ )
{
AppleNum[i] += AppleNum[i-1];
System.out.print(AppleNum[i]+" ");
}
}
}
```
0.0分
1 人评分