跳到正文

题解

Range Modify Point Query

pystraf11约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年4月26日
收录
2026年5月23日
更新
2026年11月2日
篇幅
约 7 分钟
题目
P7881

版本

  1. v2当前
  2. v1

Description

给定平面,每个点 (x,y)(x,y) 有权值 w(x,y)w(x,y),nn 次操作分两种:

  • update(p,dim,d0,d1)\text{update}(p,\text{dim},d_0,d_1):对每个点 (x,y)(x,y),若 dim=0\text{dim}=0 则 w(x,y)w(x,y) 乘上 d[x≥p]d_{[x\ge p]},若 dim=1\text{dim}=1 则乘上 d[y≥p]d_{[y\ge p]}。

  • query(x,y)\text{query}(x,y):查询 w(x,y)w(x,y) 的值。

操作强制在线。其中 w(x,y)w(x,y) 为含幺半群 DD,有运算 D×DD\times D,有结合律但无交换律。

Limitations

1≤n≤1051\le n\le 10^5

1≤x,y,p≤109,dim∈{0,1}1\le x,y,p\le 10^9,\text{dim}\in \{0,1\}

最多使用 2×1072\times 10^7 次运算。

2s,512MB2\text{s},512\text{MB}

Solution

维护划分,则 BB 次操作会把 x,yx,y 轴分别划成 O(B)O(B) 段,把平面划成 O(B2)O(B^2) 格矩形,并且每格内 w(x,y)w(x,y) 相同可以直接存。考虑合并两个结构,把 x,yx,y 轴的划分点归并起来,容易把格子对应到新划分上,直接合并出新的 ww。

由于强制在线,考虑二进制分组,update\text{update} 时新建一个划分,不断合并直到大小超过 BB,那么只会出现 O(nB)O(\frac{n}{B}) 个划分,query\text{query} 时在每个划分上二分定位并合并即可。取 B=O(n)B=O(\sqrt n),运算次数 O(nn)O(n\sqrt n)。

由于要二分,复杂度多一个 log⁡B\log B,可以分散层叠优化掉,不过没必要。

code
C++
#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;

template<class T>
bool chmax(T &a, const T &b){
	if(a < b){ a = b; return true; }
	return false;
}

template<class T>
bool chmin(T &a, const T &b){
	if(a > b){ a = b; return true; }
	return false;
}

// Header
struct Data {
	unsigned short a, b, c, d;
	void operator*=(const Data &x);
	void clr();
};

constexpr int inf = 2e9, B = 256;
struct Grid {
	int n, m;
	vector<int> X, Y;
	vector<Data> dat;

	Grid() {}
	Grid(int x, int dim, const Data& d1, const Data& d2) {
		if (dim) {
			resize(1, 2);
			Y[0] = x, X[0] = Y[1] = inf;
		}
		else {
			resize(2, 1);
			X[0] = x, X[1] = Y[0] = inf;
		}
		dat[0] = d1, dat[1] = d2;
	}

	void resize(int _n, int _m) {
		n = _n, m = _m;
		X.resize(n);
		Y.resize(m);
		Data e; e.clr();
		dat.resize(n * m, e);
	}

	int size() { return max(0, n + m - 2); }
	Data get(int x, int y) const { return dat[x * m + y]; }
	void set(int x, int y, const Data& d) { dat[x * m + y] = d; }

	Data query(int x, int y) {
		return get(upper_bound(X.begin(), X.end(), x) - X.begin(),
				   upper_bound(Y.begin(), Y.end(), y) - Y.begin());
	}
};

using pii = pair<int, int>;
array<pii, B + 5> p, q;

Grid operator+(const Grid& a, const Grid& b) {
	Grid res;
	res.resize(a.n + b.n - 1, a.m + b.m - 1);

	int j = 0, k = 0;
	for (int i = 0; i < a.n; i++) {
		while (j < b.n && b.X[j] < a.X[i]) {
			p[k] = pii(i, j);
			res.X[k] = b.X[j];
			j++, k++;
		}
		p[k] = pii(i, j);
		res.X[k] = a.X[i];
		k++;
	}

	j = 0, k = 0;
	for (int i = 0; i < a.m; i++) {
		while (j < b.m && b.Y[j] < a.Y[i]) {
			q[k] = pii(i, j);
			res.Y[k] = b.Y[j];
			j++, k++;
		}
		q[k] = pii(i, j);
		res.Y[k] = a.Y[i];
		k++;
	}

	for (int i = 0; i < res.n; i++)
		for (int j = 0; j < res.m; j++) {
			Data tmp = a.get(p[i].first, q[j].first);
			tmp *= b.get(p[i].second, q[j].second);
			res.set(i, j, tmp);
		}
	return res;
}

vector<Grid> grid;
int siz = 0;

void update(int x, int dim, Data d1, Data d2) {
	grid.emplace_back(x, dim, d1, d2);
	siz++;
	while (siz > 1 && grid[siz - 1].size() < B &&
		   grid[siz - 2].size() <= grid[siz - 1].size()) {

		grid[siz - 2] = grid[siz - 2] + grid[siz - 1];
		siz--;
		grid.pop_back();
	}
}

Data query(int x, int y) {
	Data res;
	res.clr();
	for (int i = 0; i < siz; i++) res *= grid[i].query(x, y);
	return res;
}
原文