跳到正文

个人记录

Week4练习赛总结

OIerLCY约 9 分钟
原文
文章信息与版本

文章信息

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

版本

  1. v1当前
题目编号T1T2T3T4总结
pts00000
耗时20min10min30min0min2h

上机发现已经过去了10min发誓以后要提前俩小时到机房

T1

在莽暴力和推正解之间选择了推了很久正解然后写了个莫名其妙的像正解的0pts暴力。。。

正解是统计前缀’a’,因为替换字符串可以看做是把”bc”放到’a’的前面,也就是一个”bc”可以匹配前面所有相邻的’a’

code

C++
#include<bits/stdc++.h>
using namespace std;
string s;
long long ans;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	cin >> s;
	s = ' ' + s;
	long long len = s.size() - 1, cntA = 0;
	for(int i = 1; i <= len; i ++){
		if(s[i] == 'A') cntA ++;
		else if(s[i] == 'B'){
			if(i + 1 <= len && s[i + 1] == 'C'){
				ans += cntA, i ++;
			} else{
				cntA = 0;
			}
		} else{
			cntA = 0;
		}
	}
	cout << ans;
	return 0;
}

T2

记忆化搜索,赛时DP预期50pts最后因不可抗力因素爆零了

从nn开始记搜,如果可以被对应乘操作逆向整除,那就加上花费继续搜,否则就用最小花费凑成倍数再加上原花费搜

code

C++
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
const int N = 1e18 + 10;
int T, n, a, b, c, d;
map<int, int> mp;
int dfs(int x){
	if(mp.count(x)) return mp[x];
	int ans = x * d;
	if(x % 2){
		ans = min(ans, min(dfs((x - x % 2) / 2), dfs((x + 2 - x % 2) / 2)) + a + d);
	} else{
		ans = min(ans, dfs(x / 2) + a);
	}
	if(x % 3){
		ans = min(ans, min(dfs((x - x % 3) / 3) + (x % 3) * d, dfs((x + 3 - x % 3) / 3) + (3 - x % 3) * d) + b);
	} else{
		ans = min(ans, dfs(x / 3) + b);
	}
	if(x % 5){
		ans = min(ans, min(dfs((x - x % 5) / 5) + (x % 5) * d, dfs((x + 5 - x % 5) / 5) + (5 - x % 5) * d) + c);
	} else{
		ans = min(ans, dfs(x / 5) + c);
	}
	mp[x] = ans;
	return ans;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	cin >> T;
	while(T --){
		cin >> n >> a >> b >> c >> d;
		mp.clear();
		mp[0] = 0, mp[1] = d;
		cout << dfs(n) << '\n';
	}
	return 0;
}

T3

赛时认为every树都是二叉树结果炸了

并查集板子,对于每一个被两个黑节点围住的红节点块求数量,最后遍历所有黑节点乘法原理+乘法分配律统计所有答案即可。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e6 + 10;
string s;
vector<int> e[N];
int fa[N], num[N], n, ans;
void add_edge(int u, int v){
	e[u].push_back(v), e[v].push_back(u);
	return ;
}
int find(int x){
	if(x != fa[x]) return fa[x] = find(fa[x]);
	return x;
}
void Get(int u){
	vector<int> G;
	int sum = 0;
	for(int i = 0, v; i < e[u].size(); i ++){
		v = e[u][i];
		if(s[v] == 'R'){
			int fv = find(v);
			G.push_back(num[fv]);
			sum += num[fv], ans += num[fv];
		}
	}
	int cnt = 0;
	for(int i = 0; i < G.size(); i ++){
		cnt += G[i] * (sum - G[i]);
	}
	ans += cnt / 2;
	return ;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	cin >> n >> s;
	s = ' ' + s;
	for(int i = 1; i <= n; i ++){
		fa[i] = i, num[i] = 1;
	}
	for(int i = 1, u, v; i < n; i ++){
		cin >> u >> v;
		if(s[u] == 'R' && s[v] == 'R'){
			int fu = find(u), fv = find(v);
			if(fu != fv){
				fa[fu] = fv;
				num[fv] += num[fu];
			}
		}
		add_edge(u, v);
	}
	for(int i = 1; i <= n; i ++){
		if(s[i] == 'B') Get(i);
	}
	cout << ans;
	return 0;
}

T4

树状数组的区间修改以及区间求逻辑与

C++
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, mod = 1e9 + 7;
int n, q, m, a[N], pw[N];
struct Bit_Tree {
	int C[N];
	int lowbit(int x) {
		return x & (-x);
	}
	void modify(int x, int value) {
		while(x <= n) {
			C[x] += value, x += lowbit(x);
		}
		return ;
	}
	int query(int x) {
		int ans = 0;
		while(x > 0) {
			ans += C[x], x-= lowbit(x);
		}
		return ans;
	}
	int Q(int l, int r) {
		return query(r) - query(l - 1);
	}
} T[35];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	pw[0] = 1;
	for(int i = 1; i <= 1000000; i ++) {
		pw[i] = (pw[i - 1] * 2) % mod;
	}
	cin >> n;
	for(int i = 1; i <= n; i ++) {
		cin >> a[i];
	}
	for(int i = 1; i <= n; i ++) {
		for(int j = 0; j < 30; j ++) {
			if((a[i] >> j) & 1) {
				T[j].modify(i, 1);
			}
		}
	}
	cin >> q;
	while(q --) {
		int op;
		cin >> op;
		if(op == 1) {
			int pos, x;
			cin >> pos >> x;
			for(int j = 0; j < 30; j ++) {
				if((a[pos] >> j) & 1) {
					T[j].modify(pos, -1);
				}
			}
			a[pos] = x;
			for(int j = 0; j < 30; j ++) {
				if((a[pos] >> j) & 1) {
					T[j].modify(pos, 1);
				}
			}
		} else {
			int l, r, ans = 0;
			cin >> l >> r;
			for(int j = 0; j < 30; j ++) {
				ans += 1ll * (pw[T[j].Q(l, r)] - 1) * pw[j] % mod;
				ans %= mod;
			}
			cout << ans << '\n';
		}
	}
	return 0;
}
原文