跳到正文

个人记录

csp考前集训-数据结构

NanNanzi_QvQ约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

P2776 [SDOI2007] 小组队列

用m个队列记录每一个小组内部元素的顺序,再用一个队列ord记录小组的顺序,

每次pop时,找到ord队头的元素i,在q[i]中进行pop,如果q[i]被pop成空的,就在ord中进行一次pop

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, a[maxn], tt, x;
string s;
queue<int> q[305], ord;
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	cin >> tt;
	while (tt--) {
		cin >> s;
		if (s == "push") {
			cin >> x;
			if (q[a[x]].empty())
				ord.push(a[x]);
			q[a[x]].push(x);
		}
		else {
			cout << q[ord.front()].front() << endl;
			q[ord.front()].pop();
			if (q[ord.front()].empty())
				ord.pop();
		}
	}
	return 0;
}

P2201 数列编辑器

用两个栈 s1,s2分别表示光标前面和后面的元素

I:s1.push(x)

D:s1.pop()

L:s2.push(s1.top()), s1.pop()

R:s1.push(s2.top()), s2.pop()

Q:记录两个数组,分别为前缀和数组s和前缀和的前缀最大值数组pre

插入第n+1个元素:

s[n+1]=s[n]+a[n+1],

pre[n+1]=max(s[n+1],pre[n])

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6 + 5;
int tt;
ll x, pre[maxn], s[maxn];
char op;
stack<ll> s1, s2;
int main()
{
	cin >> tt;
	while (tt--) {
		cin >> op;
		if (op == 'I') {
			cin >> x;
			s1.push(x);
			int n = s1.size() - 1;
			s[n] = n == 0 ? x : s[n - 1] + x;
			pre[n] = n == 0 ? s[0] : max(pre[n - 1], s[n]);
		}
		else if (op == 'D')
			s1.pop();
		else if (op == 'L')
			s2.push(s1.top()), s1.pop();
		else if (op == 'R') {
			x = s2.top();
			s1.push(s2.top()), s2.pop();
			int n = s1.size() - 1;
			s[n] = n == 0 ? x : s[n - 1] + x;
			pre[n] = n == 0 ? s[0] : max(pre[n - 1], s[n]);
		}
		else {
			cin >> x;
			cout << pre[x - 1] << endl;
		}
	}
	return 0;
}

P7442 「EZEC-7」维护序列

根据数据范围推测可能需要使用位运算

肯定不能暴力模拟每次操作,应该存在一种算法,在知道进行了几次操作1,几次操作2的基础上,计算出每个数的位置

手玩发现,操作1实际上是将每个下标的二进制最后一位移动到第一位,操作2是将每个下标的最后一位取反后移动到第一位

使用两个数组

pos[i]:原来的第i位变成了第几位

flag[i]:原本下标为i的位是否被反转

因为每次操作对所有数字更新方式一样,因此只需要修改pos和flag数组,就可以利用这两个数组计算出每个下标x最后变成了几

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e6 + 5;
ll n, m, op, x, pos[35], flag[35];
int main()
{
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m;
	for (int i = 0; i <= n - 1; i++)
		pos[i] = i;
	bool f = false;
	while (m--) {
		cin >> op >> x;
		if (op == 1) {
			f = true;
			if (x == 1) {//翻转
				for (int i = 0; i <= n - 1; i++)
					if (pos[i] == 0)
						flag[i]++;
			}
			int tmp = pos[n-1];
			for (int i = n - 1; i > 0; i--)
				pos[i] = pos[i - 1];
			pos[0] = tmp;

		}
		else {
			if (!f) {
				cout << x << endl;
				continue;
			}
			ll ans = 0;
			for (int i = 0; i <= n - 1; i++) {
				int k = (x >> pos[i]) & 1;
				if (flag[i] % 2 == 1) k ^= 1;
				ans += (1ll << i) * k;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

P10288 [GESP样题 八级] 区间

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mx = 1e5 + 5;
int t;
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> t;
	while (t--) {
		vector<vector<int> >pos;
		int n, top, a[mx], b[mx], q, l, r, p;
		cin >> n;
		pos.resize(n + 1);
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			b[i] = a[i];
		}
		sort(b + 1, b + n + 1);
		int m = unique(b + 1, b + n + 1) - b - 1;
		for (int i = 1; i <= n; i++) {
			int x = lower_bound(b + 1, b + m + 1, a[i]) - b;
			pos[x].push_back(i);
		}
		cin >> q;
		while (q--) {
			cin >> l >> r >> p;
			int x = lower_bound(b + 1, b + m + 1, p) - b;
			if (b[x] != p) {
				cout << 0 << endl;
				continue;
			}
			auto L = lower_bound(pos[x].begin(), pos[x].end(), l);
			auto R = upper_bound(pos[x].begin(), pos[x].end(), r);
			cout << R - L << endl;
		}
	}
	return 0;
}

P8359 [SNOI2022] 垃圾回收

统计每一秒1所在的连通块中有哪些点,答案加上这些点的ai的和

删除边不容易记录哪些点和1连接,所以改为反向操作,每遇到一次删除操作,就加上一条边, 在并查集中合并这条边的两个端点。

记录每次GC操作的时间,每遇到一次GC操作,就加上(1所在连通块的内存的和)*(下一次GC操作和本次GC操作的时间差)

要使用带权并查集,在合并的时候还要更新并查集中元素的内存的和

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 4e5 + 5;
int n, m, q, fa[maxn], vis[maxn], cnt;
//vis:哪些边被删掉
ll siz[maxn], sum;
struct edge {
	int x, y;
}e[maxn];
struct op {
	string s;
	int x;
}p[maxn];
int find(int x) {
	return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y) {
	int rx = find(x), ry = find(y);
	if (rx != ry) {
		fa[rx] = ry;
		siz[ry] += siz[rx];
		//siz[i]表示以i为根节点的并查集中内存的和
	}
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m >> q;
	for (int i = 1; i <= m; i++)
		cin >> e[i].x >> e[i].y;
	for (int i = 1; i <= q; i++) {
		cin >> p[i].s;
		if (p[i].s == "DELETE") {
			cin >> p[i].x;
			vis[p[i].x] = 1;
		}
	}
	for (int i = 1; i <= n; i++)
		cin >> siz[i], fa[i] = i, sum += siz[i];
	for (int i = 1; i <= m; i++)
		if (!vis[i]) merge(e[i].x, e[i].y);
	int lst = q + 1;//最后删掉了所有节点
	unsigned long long ans = 0;
	for (int i = q; i >= 1; i--) {
		if (p[i].s == "GC") {
			ans += siz[find(1)] * (lst - i);
			lst = i;
		}
		else merge(e[p[i].x].x, e[p[i].x].y);
	}
	ans += sum * lst;
	cout << ans;
	return 0;
}

[AHOI2024 初中组] 操作

C++
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 4e5 + 5, mod = 10007;
int n, m, k, c, o[maxn], x[maxn], y[maxn];
//d:差分数组
ll t[maxn], d[maxn], a[maxn];
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> n >> m >> k;
	for (int i = 1; i <= k; i++) {
		cin >> c;
		t[c]++;
	}
	for (int i = m; i >= 1; i--)
		d[i] = (t[i] - t[i + 1] + 10 * mod) % mod;
	for (int i = 1; i <= m; i++)
		cin >> o[i] >> x[i] >> y[i];
	for (int i = m; i >= 1; i--) {
		t[i] = (t[i + 1] + d[i]) % mod;
		if (o[i] == 1) continue;
		(d[y[i]] += t[i]) %= mod;
		(d[x[i] - 1] -= t[i] - mod) %= mod;
	}
	for (int i = 1; i <= m; i++) {
		if (o[i] == 1)
			(a[x[i]] += t[i] * y[i]) %= mod;
	}
	for (int i = 1; i <= n; i++)
		cout << a[i] << ' ';
	return 0;
}
原文