跳到正文

个人记录

9.10数据结构模拟赛

Millie128_80066B约 15 分钟
原文
文章信息与版本

文章信息

发布
2026年9月10日
收录
2026年9月10日
更新
2026年11月12日
篇幅
约 15 分钟

版本

  1. v1当前

比赛

T2

题意

对于一个下标为 ii 的点

他与其它位置做差求和即为这个点的贡献

有可能删除/添加一个点

算法分析

首先对于没有修改的情况,贡献可以这样计算

C++
void solve() {
	for (int i = 1; i <= n; i++) 
		if (a[i] == 1) {
			ll res1 = sum[n] - sum[i - 1];
			ll res2 = cnt[n] - cnt[i - 1];
			ans = (ans + (res1 - i * res2 % mod + mod) % mod) % mod;
		}
}

也就是把其他坐标和相加在把对应数量个 ii 减去,与分步加是等价的

所以我们维护两个东西

  • sum(tree[p][1])sum(tree[p][1]): 块内 linklink 状态下标和
  • cnt(tree[p][2])cnt(tree[p][2]): 块内 linklink 状态数量

那么对于一次添加

ans=ans+x∗cnt(1,x−1)−sum(1,x−1);ans = ans + x * cnt(1, x - 1) - sum(1, x - 1); ans=ans−x∗cnt(x+1,n)+sum(x+1,n);ans = ans - x * cnt(x + 1, n) + sum(x + 1, n);

删除减掉即可

但注意到赛时只有 25pts

所以要好好取模哦 ans = (ans % mod + mod) % mod;

code
C++
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 1e6 + 10;
const ll mod = 1e9 + 7;

int a[N], n, cnt[N];
ll tree[N][3], sum[N];
ll ans;
// 1 sum
// 2 cnt 

void solve() {
	for (int i = 1; i <= n; i++) 
		if (a[i] == 1) {
			ll res1 = sum[n] - sum[i - 1];
			ll res2 = cnt[n] - cnt[i - 1];
			ans = (ans + (res1 - i * res2 % mod + mod) % mod) % mod;
		}
}

void push_up(int p) {
	tree[p][1] = (tree[p << 1][1] + tree[p << 1 | 1][1] + mod) % mod;
	tree[p][2] = (tree[p << 1][2] + tree[p << 1 | 1][2] + mod) % mod;
//	tree[p][1] = tree[p << 1][1] + tree[p << 1 | 1][1];
//	tree[p][2] = tree[p << 1][2] + tree[p << 1 | 1][2];
}

void build(int p, int pl, int pr) {
	if (pl == pr) {
		tree[p][1] = a[pl] * pl % mod;
		tree[p][2] = a[pl];
		return;
	}
	
	int mid = (pl + pr) >> 1;
	build(p << 1, pl, mid);
	build(p << 1 | 1, mid + 1, pr);
	
	push_up(p);
}

void update(int pos, int p, int pl, int pr, int d) {
	if (pl == pr) {
		tree[p][1] = d;
		tree[p][2] = (d != 0);
		return;
	}
	
	int mid = (pl + pr) >> 1;
	if (pos <= mid) update(pos, p << 1, pl, mid, d);
	else update(pos, p << 1 | 1, mid + 1, pr, d);
	
	push_up(p);
}

ll query(int L, int R, int p, int pl, int pr, int id) {
	if (L > R) return 0;
	
	if (L <= pl && pr <= R) return tree[p][id] % mod;
	
	int mid = (pl + pr) >> 1;
	ll res = 0;
	if (L <= mid) res = (res + query(L, R, p << 1, pl, mid, id) + mod) % mod;
	if (R > mid) res = (res + query(L, R, p << 1 | 1, mid + 1, pr, id) + mod) % mod;
	
	return res;
}

int main() {
//	freopen("as02.in", "r", stdin);
//	freopen("as02.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	for (int i = 1; i <= n; i++) {
		char x;
		cin >> x;
		if (x == '1') a[i] = 1;
		else a[i] = 0;
		cnt[i] = (cnt[i - 1] + a[i]) % mod;
		sum[i] = (sum[i - 1] + a[i] * i) % mod;
	}
	
	build(1, 1, n);
	solve();
//	cout << "ans"<< query(1, n, 1, 1, n, 2) << endl;
	cout << ans << '\n';
	int q;
	cin >> q;
	while (q--) {
		int opt, x;
		cin >> opt >> x;
		if (opt == 1) { // to 1
			ans = (ans + x * query(1, x - 1, 1, 1, n, 2) % mod - query(1, x - 1, 1, 1, n, 1) + mod) % mod;
			ans = (ans - x * query(x + 1, n, 1, 1, n, 2) % mod + query(x + 1, n, 1, 1, n, 1) + mod) % mod;
			ans = (ans % mod + mod) % mod;
			update(x, 1, 1, n, x);
		} else { // to 0
			ans = (ans - x * query(1, x - 1, 1, 1, n, 2) % mod + query(1, x - 1, 1, 1, n, 1) + mod) % mod;
			ans = (ans + x * query(x + 1, n, 1, 1, n, 2) % mod - query(x + 1, n, 1, 1, n, 1) + mod) % mod;
			update(x, 1, 1, n, 0);
		}
		cout << ans << '\n';
	}
	
	return 0;
}
/*
5
10001
0
---
5
00001
7
1 1
2 5
2 1
1 2
1 4
1 3
1 1

1
2
1
0
1
2
3
4
*/

P6753 [BalticOI 2013] Ball Machine

题意

给定一棵树,在根节点放一些球,如果下方有空位,那么它就会往下滚到下面的节点

如果有多个节点选择,它会选择以其为根节点的子树中标号最小值最小的儿子节点

从一个位置上拿走球,上面的球也会滚下来

给定一些操作

算法分析

首先对于操作一放球我们考虑先把放球的顺序转化成数列填数

发现他其实本质就是一个后序遍历(左右根)

C++
bool cmp(int x, int y) {
	return minl[x] < minl[y];
} 

void dfs(int u) {
	// 根据子树最小编号 sort 
	sort(e[u].begin(), e[u].end(), cmp);
	
	for (auto v : e[u]) dfs(v);
	
	// 先遍历 son (后序遍历) 
	dfn[u] = ++tim;
	id[tim] = u; // 需要从时间戳映射到节点编号
	// 按照时间戳小的排序,时间戳越小越优先放球 
	q.push(dfn[u]);
}

题目中说优先选字数内部最小编号所在子树,也就是 code 中的按 minl 排序

用一个小根堆维护时间戳从小到大,这里用一个 id 数组映射到节点编号方便输出

对于操作二,我们发现拿掉一个球其实等价于将最上方的球放到此处,所以我们考虑倍增找,同时跳的节点数就是滚下来的数量

C++
int solve(int u) {
	int res = 0;
	// 从上下往上跳到第一个有球的点
	for (int i = 20; i >= 0; i--) {
		if (!vis[dp[u][i]]) continue; // 如果是空的跳过
		// 开始有球了 
		u = dp[u][i];
		res += (1 << i); // 跳过的数量就是落下的球数 
	}
	vis[u] = 0; // 空了,相当于将第一个放到了空位 
	q.push(dfn[u]); // 放入新空位 
	
	return res;
}
code
C++
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 2e5 + 10;

int dp[N][30], minl[N], rt;
int dfn[N], tim, id[N];
priority_queue<int, vector<int>, greater<int> > q;
vector<int> e[N];
bool vis[N];

void init(int u) {
	// 倍增初始化 
	for (int i = 1; i <= 20; i++) dp[u][i] = dp[dp[u][i - 1]][i - 1];
	
	// minl[u]:以 u为根子树内部最小编号,用于选择路径 
	minl[u] = u; 
	for (auto v : e[u]) {
		init(v);
		minl[u] = min(minl[u], minl[v]);
	}
}

bool cmp(int x, int y) {
	return minl[x] < minl[y];
} 

void dfs(int u) {
	// 根据子树最小编号 sort 
	sort(e[u].begin(), e[u].end(), cmp);
	
	for (auto v : e[u]) dfs(v);
	
	// 先遍历 son (后序遍历) 
	dfn[u] = ++tim;
	id[tim] = u; // 需要从时间戳映射到节点编号
	// 按照时间戳小的排序,时间戳越小越优先放球 
	q.push(dfn[u]);
}

int solve(int u) {
	int res = 0;
	// 从上下往上跳到第一个有球的点
	for (int i = 20; i >= 0; i--) {
		if (!vis[dp[u][i]]) continue; // 如果是空的跳过
		// 开始有球了 
		u = dp[u][i];
		res += (1 << i); // 跳过的数量就是落下的球数 
	}
	vis[u] = 0; // 空了,相当于将第一个放到了空位 
	q.push(dfn[u]); // 放入新空位 
	
	return res;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	
	int n, t;
	cin >> n >> t;
	for (int i = 1; i <= n; i++) {
		cin >> dp[i][0];
		if (dp[i][0]) e[dp[i][0]].push_back(i);
		else rt = i;
	}
	
	init(rt);
	dfs(rt);
	
	while (t--) {
		int opt, x;
		cin >> opt >> x;
		if (opt == 1) {
			for (int i = 1; i <= x; i++) {
				int u = q.top();
				q.pop();
				vis[id[u]] = 1; // 空位放球 
				if (i == x) cout << id[u] << '\n'; // cout 最后一个球落在了哪里 
			}
		} else cout << solve(x) << '\n';
	}
	
	return 0;
}

P7537 [COCI 2016/2017 #4] Rima

题意

A,BA,B 的最长公共后缀的长度为 LCS(A,B)\text{LCS}(A,B)

当 LCS(A,B)≥max⁡(∣A∣,∣B∣)−1\text{LCS}(A,B) \ge \max(|A|,|B|)-1 时,我们认为 A,BA,B 两个字符串押韵

NN 个字符串,从中组合出一个长度最长的字符串序列使得序列中相邻两个字符串押韵

算法分析

注意到后缀并不好处理,所以我们转成前缀就可以用字典树了

对于节点 uu,我们发现合法的押韵序列只可能来自于左侧的子树内部的链/u自身/右侧的子树内部的链

但这显然是伪了(abc abp)

所以其实最长链也是合法的贡献

ans=左侧的子树内部的链+u自身是否存在+u除去左右侧儿子节点+右侧的子树内部的链ans = 左侧的子树内部的链+u自身是否存在+u除去左右侧儿子节点 +右侧的子树内部的链

实际上不一定每个节点都有两个 son

所以 dp[u]=mx1+max(0,son−1)+1dp[u] = mx1 + max(0, son - 1) + 1

code
C++
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int N = 3e6 + 10;

int trie[N][30], n, ans, idx, dp[N];
bool vis[N];

void insert(string s) {
	int p = 0, len = s.size();
	for (int i = len - 1; i >= 0; i--) {
		int c = s[i] - 'a';
		if (!trie[p][c]) trie[p][c] = ++idx;
		p = trie[p][c];
	}
	vis[p] = 1;
}

void dfs(int u) {
	int mx1 = 0, mx2 = 0, son = 0;
	for (int i = 0; i < 26; i++) {
		int ne = trie[u][i];
		if (!ne) continue;
		
		dfs(ne);
		
		if (vis[ne]) son++;
		if (dp[ne] > mx1) mx2 = mx1, mx1 = dp[ne];
		else if (dp[ne] > mx2) mx2 = dp[ne];
	}
	
	if (vis[u]) dp[u] = mx1 + max(0, son - 1) + 1;
	else dp[u] = 0;
	
	ans = max(ans, mx1 + mx2 + vis[u] + max(son - 2, 0));
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	for (int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		insert(s); 
	}
	
	dfs(0);
	
	cout << ans;
	
	return 0;
}
原文