【BZOJ1176】【Balkan2007】Mokia(CDQ分治)

本文深入探讨了CDQ分治算法的应用,通过一个具体的题目示例,详细讲解了如何利用CDQ分治解决大规模矩阵操作问题。文章通过将操作顺序视为另一维度,将矩阵查询分解为四个部分,巧妙地转化为三维偏序问题,展示了CDQ分治的强大能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值