Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <fstream>
- #include <iostream>
- #include <vector>
- using namespace std;
- long long f[1001][1001][4];
- int n;
- long long x, y;
- long long get(long long x1, long long y1)
- {
- int i = x1;
- long long ans = 0;
- while (i > 0)
- {
- int j = y1;
- while (j > 0)
- {
- ans += f[i][j][0] * x1 * y1;
- // cout << f[i][j][0] << " 0 " << i << " get " << j << endl;
- ans += f[i][j][1] * y1;
- // cout << f[i][j][1] << " 1 " << i << " get " << j << endl;
- ans += f[i][j][2] * x1;
- // cout << f[i][j][2] << " 2 " << i << " get " << j << endl;
- ans += f[i][j][3];
- // cout << f[i][j][3] << " 3 " << i << " get " << j << endl;
- j = j & (j - 1);
- }
- i = i & (i - 1);
- }
- return ans;
- }
- void add(int x1, int y1, long long v)
- {
- int i = x1;
- while (i <= x)
- {
- // cout << "1";
- int j = y1;
- while (j <= y)
- {
- f[i][j][0] += v;
- // cout << f[i][j][0] << " 0 " << i << " " << j << endl;
- f[i][j][1] += v * (1 - x1);
- // cout << f[i][j][1] << " 1 " << i << " " << j << endl;
- f[i][j][2] += v * (1 - y1);
- // cout << f[i][j][2] << " 2 " << i << " " << j << endl;
- f[i][j][3] += v * (x1 - 1) * (y1 - 1);
- // cout << f[i][j][3] << " 3 " << i << " " << j << endl;
- j = (j | (j - 1)) + 1;
- // cout << "2";
- }
- i = (i | (i - 1)) + 1;
- }
- return;
- }
- int main()
- {
- freopen("segtree2d.in", "r", stdin);
- freopen("segtree2d.out", "w", stdout);
- int i;
- cin >> x >> y;
- cin >> n;
- for (i = 0; i < n; i++)
- {
- int q;
- long long x1, y1, x2, y2, w;
- cin >> q >> x1 >> y1 >> x2 >> y2;
- if (q == 1)
- {
- cin >> w;
- add(x1, y1, w);
- // cout << "1";
- add(x2 + 1, y1, -w);
- // cout << "1";
- add(x1, y2 + 1, -w);
- // cout << "1";
- add(x2 + 1, y2 + 1, w);
- // cout << "1";
- }
- else
- {
- long long ans = get(x2, y2) - get(x1 - 1, y2) - get(x2, y1 - 1) + get(x1 - 1, y1 - 1);
- cout << ans << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement