Description
维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000.
Solution
好多年没写过CDQ分治了。。
CDQ分治模板题。
将操作顺序看做另一维,将矩阵询问用前缀和拆成4个,就变成了三维偏序问题了。
Code
/************************************************
* Au: Hany01
* Date: Sep 26th, 2018
* Prob: bzoj1176 Mokia
* Email: hany01dxx@gmail.com & hany01@foxmail.com
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define Rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define X first
#define Y second
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
static int _, __; static char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 2e5 + 5, maxm = 2e6 + 5;
struct Query {
int val, ty, x, y, id;
}Q[maxn << 2], tmp[maxn << 2];
int tot, ans[maxn], N, qs, n, S;
struct BIT {
int c[maxm];
inline void update(int x, int dt) { for ( ; x <= N; x += (x & -x)) c[x] += dt; }
inline int query(int x) { int ans = 0; for ( ; x; x -= (x & -x)) ans += c[x]; return ans; }
}bit;
void solve(int l, int r) {
if (l == r) return;
register int mid = (l + r) >> 1, cur1 = l, cur2 = mid + 1;
solve(l, mid), solve(mid + 1, r), tot = l - 1;
while (tot < r) {
if (cur1 > mid || (cur1 <= mid && cur2 <= r && Q[cur1].x > Q[cur2].x)) {
tmp[++ tot] = Q[cur2];
if (Q[cur2].id) ans[Q[cur2].id] += Q[cur2].ty * bit.query(Q[cur2].y);
++ cur2;
} else {
tmp[++ tot] = Q[cur1];
if (Q[cur1].val) bit.update(Q[cur1].y, Q[cur1].val);
++ cur1;
}
}
For(i, l, mid) if (Q[i].val) bit.update(Q[i].y, -Q[i].val);
For(i, l, r) Q[i] = tmp[i];
}
int main()
{
#ifdef hany01
freopen("bzoj1176.in", "r", stdin);
freopen("bzoj1176.out", "w", stdout);
#endif
static int ty, x1, y1, x2, y2;
for (S = read(), N = read(); ; ) {
if ((ty = read()) == 3) break;
else if (ty == 1) {
x1 = read(), y1 = read();
Q[++ n] = (Query){read(), 0, x1, y1};
} else {
x1 = read(), y1 = read(), x2 = read(), y2 = read();
ans[++ qs] = (x2 - x1 + 1) * (y2 - y1 + 1) * S;
Q[++ n] = (Query){0, 1, x2, y2, qs};
Q[++ n] = (Query){0, 1, x1 - 1, y1 - 1, qs};
Q[++ n] = (Query){0, -1, x2, y1 - 1, qs};
Q[++ n] = (Query){0, -1, x1 - 1, y2, qs};
}
}
solve(1, n);
For(i, 1, qs) printf("%d\n", ans[i]);
return 0;
}