跳到正文

个人记录

2026.7 KDY 补题

_Sorasaki_Hina_约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年7月15日
收录
2026年7月15日
更新
2026年9月6日
篇幅
约 12 分钟
获赞
1

版本

  1. v2当前
  2. v1

DAY1

100+20+0+0=120100+20+0+0=120。

T1

我们对于每个数构造状态:若有数 pospos,那么十位二进制数的第 pospos 位为 11。

比如 1789,二进制为 (1110000010)2(1110000010)_2。

我们易得状态只有 2102^{10} 量级,所以我们直接用桶,对值暴力。

若状态 i,ji,j,满足 i∧j≠0i \land j \not= 0,说明 i,ji,j 有共同数字,那么带来 cnti×cntjcnt_i \times cnt_j 的贡献。

注意自己和自己组合还会有 cnti×(cnti−1)2\dfrac{cnt_i\times (cnt_i-1)}{2} 的贡献。

时间 O(nlog⁡10n)O(n\log_{10} n),在于拆数上。

C++
/*
_/        _/   _/_/_/_/_/_/   _/        _/         _/_/
_/        _/        _/        _/_/      _/       _/   _/
_/        _/        _/        _/  _/    _/      _/     _/
_/_/_/_/_/_/        _/        _/    _/  _/     _/       _/
_/        _/        _/        _/      _/_/     _/_/_/_/_/_/
_/        _/        _/        _/        _/    _/          _/
_/        _/   _/_/_/_/_/_/   _/        _/   _/            _/
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<unordered_map>
#define re register
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define db double
#define fi const int&
#define fl const ll&
#define ful const ull&
#define fc const char&
#define fs const string&
#define debug() puts("Running Successfully")
using namespace std ;
namespace IO {
	template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}
	template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}
	template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}
	template<class T>inline void write(T x,fc c){write(x),putchar(c);}
}using namespace IO ;
const int N = 1e6 + 2 ;
int n ;
ll a[N],cnt[1302] ;
inline int solve (ll x) {
	re int res = 0 ;
	while (x) {
		res |= (1 << (x % 10)) ;
		x /= 10 ;
	}
	return res ;
}
ll ans ;
int main () {
	read(n) ;
	for (re int i = 1 ; i <= n ; ++i) {
		read(a[i]) ;
		++cnt[solve(a[i])] ;
	}
	for (re int i = 0 ; i <= 1024 ; ++i) {
		if (!cnt[i]) continue ;
		ans += cnt[i] * (cnt[i] - 1) / 2 ;
		for (re int j = i + 1 ; j <= 1024 ; ++j) {
			if (!cnt[j]) continue ;
			if ((i & j) != 0) ans += cnt[i] * cnt[j] ;
		}
	}
	write(ans) ;
	return 0 ;
}

T2

启发式合并并查集。

分析每个事件:

  1. 对于事件 11,我们用一个 mapmap 去做加 11 即可。

    如果没有联通,那么我们对其进行合并,按 sizsiz 合并。
    启发式合并只体现在优化时间上。

  2. 对于事件 22,我们考虑在代表节点 uu 上建一个 tagtag,记录 uu 及其子树参加的聚会数。

    那么对于点 xx,它参加的聚会数就是 xx 到代表节点的 ∑tagu\sum tag_u 吗?

    有问题,对于合并时两个代表节点 x,yx,y,假设 fx=yf_x=y,那么此时 xx 参加的聚会数会是 tagx+tagytag_x+tag_y,然而实际上 xx 只参加了 tagxtag_x 次聚会!

    但这并不意味着 tagytag_y 就不算进答案里,我们要做的是算出 tagytag_y 里 xx 没参加的聚会数,然而在刚刚合并完时这个没参加的聚会数就是 tagytag_y!

    这告诉我们在合并两个代表节点 (x,y)(x,y) 时要记录一个 hx=tagyh_x = tag_y,统计答案时需要减掉 hxh_x。

  3. 对于统计 (x,y)(x,y) 的答案,有两部分:

    • mpx,ymp_{x,y},这个不说。
    • x,yx,y 共同参加的聚会数。很显然,从 lcalca 开始 x,yx,y 才共同开始参加聚会。

    那 ∑u=lcatagu−hu\sum\limits_{u=lca} tag_u-h_u 就是共同参加的聚会数吗?

    错!

    在 taglcatag_{lca} 中,有可以分为两种聚会:

    1. x,yx,y 都参加的聚会
    2. x,yx,y 至少一人没参加的聚会。

    我们要做的就是把 2 的情况给去掉。显然 2 的情况放在了一个 hh 里。

    因为只有 x,yx,y 两人都参加才能算进贡献,所以我们要看的是 x,yx,y 哪一个合并的晚,哪一个的 hh 就是答案;很显然,时间越晚,hh 会更大,取 max⁡\max 即可,比如记为 tptp。

    那么答案为 mpx,y−tp+∑u=lca(tagu−hu)mp_{x,y} - tp + \sum\limits_{u=lca} (tag_u-h_u)。

因为树高不会超过 log⁡n\log n,所以每次操作时间也是 O(log⁡n)O(\log n) 的。
总时间就是 O(mlog⁡n)O(m\log n)。

C++
/*
_/        _/   _/_/_/_/_/_/   _/        _/         _/_/
_/        _/        _/        _/_/      _/       _/   _/
_/        _/        _/        _/  _/    _/      _/     _/
_/_/_/_/_/_/        _/        _/    _/  _/     _/       _/
_/        _/        _/        _/      _/_/     _/_/_/_/_/_/
_/        _/        _/        _/        _/    _/          _/
_/        _/   _/_/_/_/_/_/   _/        _/   _/            _/
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<unordered_map>
#define re register
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define db double
#define fi const int&
#define fl const ll&
#define ful const ull&
#define fc const char&
#define fs const string&
#define debug() puts("Running Successfully")
using namespace std ;
namespace IO {
	template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}
	template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}
	template<class T>inline void write(T x){if(x<0){putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}
	template<class T>inline void write(T x,fc c){write(x),putchar(c);}
}using namespace IO ;
const int N = 3e5 + 2 ;
int n,m ;
int op,x,y ;
int f[N],siz[N],tag[N],h[N] ;
unordered_map<int,int> mp[N] ;
inline int find (int x) {
	while (f[x] != x) x = f[x] ;
	return f[x] ;
}
inline void merge (int x,int y) {
	x = find(x),y = find(y) ;
	if (siz[x] > siz[y]) swap(x,y) ;
	f[x] = y,siz[y] += siz[x] ;
	h[x] = tag[y] ;
}
inline int get (int u) {
	re int sum = tag[u],pos = u ;
	while (f[pos] != pos) sum += tag[f[pos]] - h[pos],pos = f[pos] ;
	return sum ;
}
inline int solve (int x,int y) {
	re int res = mp[x].count(y) ? mp[x][y] : 0 ;
	re int tx = x,ty = y ;
	if (find(x) != find(y)) return res ;
	vector<int> lx,ly ;
	while (f[x] != x) lx.push_back(x),x = f[x] ;
	lx.push_back(x) ;
	while (f[y] != y) ly.push_back(y),y = f[y] ;
	ly.push_back(y) ;
	reverse(lx.begin(),lx.end()) ;
	reverse(ly.begin(),ly.end()) ;
	re int pos = 0 ;
	while (pos < lx.size() && pos < ly.size() && lx[pos] == ly[pos]) ++pos ;
	--pos ;
	re int idx = pos,tg = 0 ;
	pos = lx[pos] ;
	x = tx,y = ty ;
	if (x != pos && y != pos) tg = max(h[lx[idx + 1]],h[ly[idx + 1]]) ;
	else if (x != pos) tg = h[lx[idx + 1]] ;
	else if (y != pos) tg = h[ly[idx + 1]] ;
	re int sum = get(pos) - tg ;
	return res + sum ;
}
int main () {
	read(n,m) ;
	for (re int i = 1 ; i <= n ; ++i) f[i] = i,siz[i] = 1 ;
	for (re int i = 1 ; i <= m ; ++i) {
		read(op) ;
		if (op == 1) {
			read(x,y) ;
			if (x > y) swap(x,y) ;
			++mp[x][y] ;
			if (find(x) != find(y)) merge(x,y) ;
		}
		if (op == 2) {
			read(x) ; 
			++tag[find(x)] ;
		}
		if (op == 3) {
			read(x,y) ;
			if (x > y) swap(x,y) ;
			write(solve(x,y),endl) ;
		}
	}
	return 0 ;
}
原文