Descriptioninputoutputsample Input
Sample Output1
2HINT
A simple application of a two-dimensional tree array, where the first-dimensional coordinates of the C array correspond to hashes. If it is a modification, the number of the current value and the ancestors are reduced by 1 before the modification, and the number is added 1.
#include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set># Include <stack> #include <cctype> #include <algorithm> #define Lson o<<1, L, M#define Rson o< <1|1, M+1, rusing namespace std;typedef long long ll;const int mod = 99999997;const int MAX = 1000000000;const int MAXN = 100005;int N, m, op, Q;int in[305][305], c[105][305][305];void Add (int i, int j, int x, int v) {while (I <= N) { int J1 = j; while (J1 <= m) {c[x][i][j1] + = V; J1 + = j1&-j1; } i + = i&-i; }}int query (int i, int j, int v) {int ans = 0; while (i > 0) {int j1 = j; while (J1 > 0) {ans + = c[v][i][j1]; J1-= j1&-j1; } I-= i&-i; } return ans; int main () {//Freopen ("In.txt", "R", stdin); CIN >> n >> m; for (int i = 1, i <= N; i++) for (int j = 1; j <= M; j + +) { CIN >> In[i][j]; Add (I, J, In[i][j], 1); } cin >> Q; while (q--) {scanf ("%d", &op); if (op = = 1) {int A, B, V; scanf ("%d%d%d", &a, &b, &v); Add (A, B, In[a][b],-1); In[a][b] = v; Add (A, B, In[a][b], 1); } else {int x1, y1, x2, y2, V; scanf ("%d%d%d%d%d", &x1, &x2, &y1, &y2, &v); int ans = query (x2, y2, v) + query (x1-1, Y1-1, v)-Query (x2, Y1-1, v)-query (X1-1, y2, v); printf ("%d\n", ans); }} return 0;}