【BZOJ5286】【HNOI2018】转盘

【题目链接】

【思路要点】

  • 首先有一个不是非常显然的结论,对于每个询问,一定有一种最优解是从某个点开始先等待一段时间,然后不停顿地走完一圈。关于这个结论的证明我们可以考虑我们现在有一个解的答案为\(Ans\),路径上最后一个点为\(x\),那么\(x\)之前的一个点被经过的最后时刻至多是\(Ans-1\),在这个点之后停顿会让这个点以及它之前的所有点最后被经过的时间变早,是不会更优的。
  • 把序列倍长,我们要求的即是\(Min_{i=1}^{N}\{Max_{j=i}^{i+N-1}\{T_j-j+i+N-1\}\}\)。
  • 上面的式子和这个式子实际上是等价的:\(Min_{i=1}^{N}\{Max_{j=i}^{2*N}\{T_j-j+i+N-1\}\}\)。
  • 令\(A_j=T_j-j\),上式被简化为\(Min_{i=1}^{N}\{Max_{j=i}^{2*N}\{A_j\}+i\}+N-1\)。
  • 考虑用线段树支持这样的询问。
  • 为每个节点\(root\)(对应区间\([L,R]\))记录区间最大值\(Max_{root}\)以及\(val_{root}=Min_{i=L}^{Mid}\{Max_{j=i}^{R}\{A_j\}+i\}\)。
  • 假设我们已经维护好了这样的一系列信息,我们现在要支持一种询问\(query(L,R,suf)\)表示\(Min_{i=L}^{R}\{Max\{Max_{j=i}^{R}\{A_j\},suf\}+i\}\)。
  • 若\(suf≤Max_{rightchild}\),那么\(val_{root}\)就代表了\([L,Mid]\)的答案,返回\(Min\{val_{root},query(Mid+1,R,suf)\}\)。
  • 否则,\([Mid+1,R]\)的答案就等于\(Mid+1+suf\),返回\(Min\{Mid+1+suf,query(L,Mid,suf)\}\)。
  • 可以发现,单次调用\(query\)的复杂度是\(O(LogN)\)的。
  • 因此,我们可以借助\(query\)在修改后维护线段树上的信息。
  • 时间复杂度\(O(NLogN+MLog^2N)\)。

【代码】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int INF = 1e9;
template <typename T> void chkmax(T &x, T y) {x = max(x, y); }
template <typename T> void chkmin(T &x, T y) {x = min(x, y); } 
template <typename T> void read(T &x) {
	x = 0; int f = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
	for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
	x *= f;
}
template <typename T> void write(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x > 9) write(x / 10);
	putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
	write(x);
	puts("");
}
struct SegmentTree {
	struct Node {
		int lc, rc;
		int val, Max;
	} a[MAXN * 2];
	int root, n, size;
	int query(int root, int l, int r, int suf) {
		if (l == r) return l + max(a[root].Max, suf);
		int mid = (l + r) / 2;
		if (a[a[root].rc].Max >= suf) {
			int ans = a[root].val;
			chkmin(ans, query(a[root].rc, mid + 1, r, suf));
			return ans;
		} else return min(query(a[root].lc, l, mid, suf), mid + 1 + suf);
	}
	void update(int root, int l, int r, int mid) {
		a[root].val = query(a[root].lc, l, mid, a[a[root].rc].Max);
		a[root].Max = max(a[a[root].lc].Max, a[a[root].rc].Max);
	}
	void build(int &root, int l, int r, int *x) {
		root = ++size;
		if (l == r) {
			int tmp;
			if (l <= n) tmp = x[l] - l;
			else tmp = x[l - n] - l;
			a[root].Max = tmp;
			a[root].val = tmp + l;
			return;
		}
		int mid = (l + r) / 2;
		build(a[root].lc, l, mid, x);
		build(a[root].rc, mid + 1, r, x);
		update(root, l, r, mid);
	}
	void init(int x, int *a) {
		n = x;
		root = size = 0;
		build(root, 1, n * 2, a);
	}
	void modify(int root, int l, int r, int pos, int val) {
		if (l == r) {
			a[root].Max = val;
			a[root].val = val + l;
			return;
		}
		int mid = (l + r) / 2;
		if (mid >= pos) modify(a[root].lc, l, mid, pos, val);
		else modify(a[root].rc, mid + 1, r, pos, val);
		update(root, l, r, mid);
	}
	void modify(int x, int val) {
		modify(root, 1, n * 2, x, val);
	}
	int query() {
		return a[root].val;
	}
} ST;
int n, m, type, a[MAXN];
int main() {
	read(n), read(m), read(type);
	for (int i = 1; i <= n; i++)
		read(a[i]);
	ST.init(n, a);
	int lastans = 0;
	writeln(lastans = ST.query() + n - 1);
	for (int i = 1; i <= m; i++) {
		int x, y;
		read(x), read(y);
		x ^= lastans * type;
		y ^= lastans * type;
		ST.modify(x, y - x);
		ST.modify(x + n, y - n - x);
		writeln(lastans = ST.query() + n - 1);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_39972971/article/details/79978647