`count = (count + B[i+1])%MOD;`

Seems to me number of times common X will be executed, ONLY considering the commands executed from end till now. (i.e. not including command X in list right now)

`B[i+1] = (count + 1)%MOD;`

Including X in list, hence increasing count by 1.

```
if(t[i] == 1)
{
A[l[i] - 1] = (A[l[i] - 1] + B[i+1])%MOD;
A[r[i]] = (A[r[i]] + MOD - B[i+1])%MOD;
//printf("A[%d]=%d A[%d]=%d\n",l[i]-1,A[l[i]-1],r[i],A[r[i]]);
}
```

Standard difference Array. Say your array is {5,10,11,2}. Then difference array arr[i+1]-arr[i] is {5,1,-9}. You know you know arr[1]=5. You can give arr[i] as-

`arr[i]=arr[i-1]+diff[i-1];//Eg- arr[1]==arr[0]+diff[0]=5+5=10`

```
B[l[i]-1] = (B[l[i]-1] + MOD - B[i+1])%MOD;
B[r[i]] = (B[r[i]] + B[i+1])%MOD;
```

Same technique for Type 2 commands as well.

And I think this was pretty much it of this code. In case your doubt persists, please check editorial, he has explained this approach there.

answered
**20 Sep '17, 18:56**

5★vijju123 ♦♦

15.5k●1●20●66

accept rate:
18%