跳到正文

题解

P10401 「XSOI-R1」区间操作 题解

2023gdgz01约 4 分钟
原文
文章信息与版本

文章信息

发布
2024年5月4日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 4 分钟
获赞
1
题目
P10401

版本

  1. v1当前

一道很有意思的预处理题目。

先将所有数据读入,然后处理出 aa 数组的前缀和 sumsum,其中 sumi=a1+a2+a3+⋯+aisum_i=a_1+a_2+a_3+\cdots+a_i,接下来递推求出询问的答案:双重循环顺序遍历出所有可能的询问,循环变量为 i,j(1≤n≤104)i,j(1\le n\le10^4),用 temptemp 记录 l=il=i 且 r=jr=j 的询问的答案,则 l=il=i 且 r=j+1r=j+1 的询问的答案为 temp⊕(sumj+1−sumi)temp\oplus(sum_{j+1}-sum_i),若 j=1j=1,则 temptemp 为 aia_i,若测试数据中有 l=il=i 且 r=jr=j 的询问,则将当前的 temptemp 计入答案,具体实现见代码。代码如下:

C++
#include <cstdio>
#include <cctype>
#include <utility>
#include <map>

namespace quick_output { //快速输出
	int len = 0;
	char out[(1 << 24) + 1];
	inline void putc(char ch) {
		out[len] = ch;
		++len;
	}
	template <typename T> inline void write(T x) {
		if (x < 0) {
			putc('-');
			x = -x;
		}
		if (x > 9)
			write(x / 10);
		out[len] = x % 10 + 48;
		++len;
	}
	inline void flush() {
		fwrite(out, 1, len, stdout);
		len = 0;
	}
}

using namespace std;
using namespace quick_output;

bool q[10005][10005]; //q[i][j] 表示是否存在 l=i 且 r=j 的询问
int n, Q, l[1000005], r[1000005];
long long temp;
long long a[10005], sum[10005];
map<pair<int, int>, long long> ans; //ans[{i,j}] 记录 l=i 且 r=j 的询问的答案

int main() {
	scanf("%d%d", &n, &Q);
	for (register int i = 1; i <= n; ++i)
		scanf("%lld", a + i);
	for (register int i = 1; i <= Q; ++i) {
		scanf("%d%d", l + i, r + i);
		q[l[i]][r[i]] = true;
	}
	for (register int i = 1; i <= n; ++i) //求 a 数组的前缀和
		sum[i] = sum[i - 1] + a[i];
	for (register int i = 1; i <= n; ++i) {
		temp = a[i];
		if (q[i][i]) //不要忽略 l=i 且 r=i 的询问
			ans[make_pair(i, i)] = temp;
		for (register int j = i + 1; j <= n; ++j) {
			temp ^= (sum[j] - sum[i - 1]); //更新 temp
			if (q[i][j]) //如果有询问 i,j
				ans[make_pair(i, j)] = temp; //记录答案
		}
	}
	for (register int i = 1; i <= Q; ++i) {
		write(ans[make_pair(l[i], r[i])]);
		putc('\n');
	}
	flush();
	return 0;
}

时间复杂度为 O(n2log⁡2n+q)O(n^2\log_2n+q),但实际上,其语句频度为 T(n×2+q×2+n2log⁡2n2)T(n\times2+q\times2+\dfrac{n^2\log_2n}{2}),带入极限数据为 T(104×2+106×2+(104)2×log⁡2(104)2)≈T(6.66405619×108)T(10^4\times2+10^6\times2+\dfrac{(10^4)^2\times\log_2(10^4)}{2})\approx T(6.66405619\times10^8),由于计算机性能不可避免地提升,所以略微卡卡常是可以过的。AC 链接

原文
存档评论3 条抓取于