跳到正文

个人记录

4.10 总结

Cabbage_Squirrel约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年4月13日
更新
2026年4月16日
篇幅
约 6 分钟

版本

  1. v2当前
  2. v1

bro 终于不是倒一了。


P6225 [eJOI 2019] 异或橙子

Description

给定 nn 个整数,有 22 种操作。

  • 修改一个数的值。
  • 选取一个区间从 ll 至 rr,求这个区间内所有子区间的异或和的异或和。

Solution

拿原题面中的例子举例。

a2⊕a3⊕a4⊕(a2⊕a3)⊕(a3⊕a4)⊕(a2⊕a3⊕a4)a_2 \oplus a_3 \oplus a_4 \oplus (a_2\oplus a_3)\oplus(a_3\oplus a_4)\oplus(a_2\oplus a_3 \oplus a_4)

看到这么长一坨式子,很容易想到暴力肯定会爆,看到这么多相同的数,很容易联想到异或的性质,即 a⊕a=0a \oplus a = 0 和 a⊕0=aa \oplus 0 = a。

这个式子化简后就是 a2⊕a4a_2 \oplus a_4,原本一坨大的变成了一丢丢,再举个例子,l=1,r=4l = 1,r = 4 就没了,l=1,r=5l = 1,r = 5 就是 a1⊕a3⊕a5a_1 \oplus a_3 \oplus a_5。

可以发现若 l,rl,r 奇偶性相异,则区间中每个数都会异或偶数次,最终结果为 00.

若 l,rl,r 奇偶性相同,区间中所有 l,rl,r 奇偶性相同的位置的值会对答案异或奇数次,其他位置的值则会贡献偶数次,那么答案就是 al⊕al+2⊕……⊕ara_{l} \oplus a_{l+2} \oplus …… \oplus a_{r}。

修改的操作没什么好讲的,遇到第二种判断奇偶性,然后看情况按上面说的做即可。

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e5 + 5,M = 5e6 + 5;
int n,q,ans,a[N];
signed main()
{
	cin >> n >> q;
	for(int i = 1;i <= n;i ++) cin >> a[i];
	while(q --)
	{
		int op,l,r;
		cin >> op >> l >> r;
		if(op == 1) a[l] = r;//操作1
		else
		{
			int cnt,ans = 0;
			for(int i = l;i <= r;i ++)//其实不判奇偶性相异也行,反正算出来也是0
			{
				cnt = (i - l + 1) * (r - i + 1) % 2;
				if(cnt) ans ^= a[i];//奇偶性相等
			}
    		cout << ans << '\n';
		}
	}
	return 0;
}

P2058 [NOIP 2016 普及组] 海港

啥阴,我的糖豆味大脑还以为是个 set 题。

Description

计算 nn 条信息。对于输出的第 ii 条信息,需要统计满足 ti−86400<tp≤tit_i-86400<t_p \le t_i 的船只 pp,在所有的 xp,jx_{p,j} 中,总共有多少个不同的数。

Solution

使用队列存储每一个船上的每一个人,当一条新的船到达时,把之前超时的人弹出队列,然后输出答案。

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 100000005;
int n,k,cur,ans,cty[N];
struct node {
	int c,t;
}person;
queue<node>persons;
signed main() {
	int n;
	cin >> n;
	for(int i = 1;i <= n;i ++) {
		cin >> cur >> k;
		while(!persons.empty()) {
			person = persons.front();
			if(person.t + 86400 <= cur) {
				cty[person.c] --;
				if(!cty[person.c]) ans --;
				persons.pop();
				continue;
			}
			break;
		}
		for(int j = 1;j <= k;j ++) {
			int x;
			cin >> x;
			node tmp = {x,cur};
			persons.push(tmp);
			cty[x] ++;
			if(cty[x] == 1) ans ++;
		}
		cout << ans << '\n';
	}
	return 0;
}

T750439 中位数

Description

给定一个长度为 NN 的非负整数序列 AA,对于前奇数项求中位数。

Solution

在盘古开天地的时候,有一位叫做【数据删除】的人发明了一个同时维护一个大根堆和一个小根队的东西,他叫做对顶堆。

首先记录一个变量,记录答案。建一个大根堆和一个小根堆,大根堆存 ≤\leq 中位数的数,小根堆存 >> 中位的数。

向堆中加入元素时,进行比较,选择加入哪一个堆。

当我们要输出答案是,若两边数量相等,则输出小根堆顶即可,若不等,则移一次输出大根堆顶即可。

Code

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n,mid,a[N];
priority_queue<int,vector<int>,less<int> >q1;
priority_queue<int,vector<int>,greater<int> >q2;
signed main() {
	cin >> n;
	cin >> a[1];
	mid = a[1];
	cout << mid << endl;
	for(int i = 2;i <= n;i ++) {
		cin >> a[i];
		if(a[i] > mid) q2.push(a[i]);//比较然后确定入哪个堆
		else q1.push(a[i]);
		if(i % 2 == 1) {//要输出了
			while(q1.size() != q2.size()) {//移
				if(q1.size() > q2.size()) {
					q2.push(mid);
					mid = q1.top();
					q1.pop();
				} else {
                    q1.push(mid);
                    mid = q2.top();
                    q2.pop();
                }
			}
			cout << mid << endl;
		}
	}
	return 0;
}
原文