跳到正文

个人记录

12.5总结

EnderWitherX约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年12月8日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

T3

思路

枚举所有区间,使用结构体记录其异或前缀和 O(n2)O(n^2),然后按异或和大小排序,接着使用前缀最小值 lastlast 数组记录前缀最小长度,这样就满足了单调性。对于每次询问,使用二分即可.

代码

C++
#include <bits/stdc++.h>
using namespace std;
int sum[10000005];
int n, m;
struct node {
 int sum, len;
}a[10000005];
int last[10000005];
bool operator<(const node& a, const node& b) {
	return a.sum < b.sum;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		int x;
		cin >> x;
		sum[i] = sum[i - 1] ^ x;
	}
		int k = 0;
	for(int i = 1; i <= n; i++)
		for(int j = i; j <= n; j++)
			a[++k] = (node){sum[j] ^ sum[i - 1], j - i + 1};
	sort(a + 1, a + 1 + k);
    last[k] = a[k].len;

   	for(int i = k - 1; i >= 1; i--) {
		last[i] = min(last[i + 1], a[i].len);
	}
	while(m--) {
		int x;
		cin >> x;
 		int l = 0, r = k + 1;
		while(l + 1 < r) {
			int mid = (l + r) / 2;
			if (a[mid].sum >= x) r = mid;
			else l = mid;
		}
		cout << (r == k + 1?-1:last[r]) << "\n"; // 需要判断无解情况
	}
	return 0;
}

T4

思路

xorl,k=a[l]⊕a[l+1]⊕...⊕a[k]=s[k]⊕s[l−1]xork+1,r=a[k+1]⊕a[k+2]⊕...⊕a[r]=s[r]⊕s[k]xorl,k=xork+1,r根据异或的性质得:xorl,k⊕xork+1,r=0所以,xor1,l−1=xor1,r,可以证明,k没有影响再使用桶优化即可。xor_{l, k} = a[l]\oplus a[l + 1]\oplus...\oplus a[k] = s[k] \oplus s[l - 1]\\ xor_{k + 1, r} = a[k + 1]\oplus a[k + 2]\oplus...\oplus a[r] = s[r] \oplus s[k]\\ xor_{l, k}=xor_{k + 1, r}\\ 根据异或的性质得:\\ xor_{l, k}\oplus xor_{k + 1, r} = 0\\ 所以,xor_{1,l - 1} = xor_{1, r},可以证明,k没有影响\\ 再使用桶优化即可。

代码

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10, mod = 1e9;
int a[N], sum[N];
map<int, int>cnt, tot;
signed main() {
	int n;
	cin >> n;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i];
		sum[i] = sum[i - 1] ^ a[i];
	}
	cnt[sum[0]] ++;
	tot[sum[0]] = 0;
	int ans = 0;
	for(int i = 1; i <= n; i ++) {
		if(cnt.count(sum[i])) {
			ans += i * cnt[sum[i]] - (tot[sum[i]] + cnt[sum[i]]);
		}
		cnt[sum[i]] ++;
		tot[sum[i]] += i;
	}
	cout << ans;
	return 0;
}

T5

思路

只有完全平方数有奇数个因子!!!

  1. 计算异或前缀和和所有⼦区间数量 cnt=n∗(n+1)/2cnt = n*(n + 1) / 2 ;

  2. 枚举右端点,统计每个前缀值的出现次数。 mp[sum[i]]++mp[sum[i]]++;

  3. 枚举右端点 i 的同时,枚举 ≤2×n≤2\times n 的完全平⽅数,由 可以得到左端点异或 值 sum[i]⊕sum[?]=j2sum[i] \oplus sum[?]= j^2 相当于 sum[?]=sum[i]⊕(j2)sum[?] = sum[i] \oplus (j^2) ,减去 左端点异或值的出现次数 cnt−=mp[sum[i](j∗j)] cnt-=mp[sum[i] ^ (j * j)] 。

  4. 初始值全一

代码

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 4e5 + 10;
long long n, m, a[N], sum[N], cnt, mp[N];
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		sum[i] = sum[i - 1] ^ a[i];
	}
	memset(mp, 0, sizeof(mp))
	cnt = n * (n + 1) / 2;
	mp[0] = 1;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; (sum[i] ^ (j * j)) <= 2*n; j++) {
			if (mp[sum[i] ^ (j * j)]) {
				cnt -= mp[sum[i] ^ (j * j)];
			}
		}
		mp[sum[i]]++;
	}
	cout << cnt << "\n";
}
int main() {
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
return 0;
}
原文