Niuniu has learned prefix sum and he found an interesting about prefix sum.
Let's consider (k+1) arrays a[i] (0 <= i <= k)
The index of a[i] starts from 1.
a[i] is always the prefix sum of a[i-1].
"always" means a[i] will change when a[i-1] changes.
"prefix sum" means a[i][1] = a[i-1][1] and a[i][j] = a[i][j-1] + a[i-1][j] (j >= 2)
Initially, all elements in a[0] are 0.
There are two kinds of operations, which are modify and query.
For a modify operation, two integers x, y are given, and it means a[0][x] += y.
For a query operation, one integer x is given, and it means querying a[k][x].
As the result might be very large, you should output the result mod 1000000007.
The first line contains three integers, n, m, k. n is the length of each array. m is the number of operations. k is the number of prefix sum. In the following m lines, each line contains an operation. If the first number is 0, then this is a change operation. There will be two integers x, y after 0, which means a[0][x] += y; If the first number is 1, then this is a query operation. There will be one integer x after 1, which means querying a[k][x]. 1 <= n <= 100000 1 <= m <= 100000 1 <= k <= 40 1 <= x <= n 0 <= y < 1000000007
For each query, you should output an integer, which is the result.
4 11 3 0 1 1 0 3 1 1 1 1 2 1 3 1 4 0 3 1 1 1 1 2 1 3 1 4
1 3 7 13 1 3 8 16
题意:给你一个(k+1)*n的矩阵,初始全为0,m个操作
操作①0 x y表示将a[0][x] += y,并且计算∀(i∈[1,k],j∈[1,n]),a[i][j] = a[i-1][j]+a[i][j-1]
操作②1 x 表示询问a[k][x]的值
很容易算出:a[0][x]对a[k][y]的贡献为,因为k的范围很小所以可以O(1)预处理所有组合数
用上面的结论,直接暴力每个操作对后面所有询问的贡献,复杂度是O(n*m)的,肯定会超时
对于这种每次操作对后面询问产生特定贡献的题,考虑对询问时间进行CDQ分治
步骤如下:
#include
#include
#include
#include