2026.7 KDY 补题

· · 个人记录

DAY1

## T1 我们对于每个数构造状态:若有数 $pos$,那么十位二进制数的第 $pos$ 位为 $1$。 比如 1789,二进制为 $(1110000010)_2$。 我们易得状态只有 $2^{10}$ 量级,所以我们直接用桶,对值暴力。 若状态 $i,j$,满足 $i \land j \not= 0$,说明 $i,j$ 有共同数字,那么带来 $cnt_i \times cnt_j$ 的贡献。 注意自己和自己组合还会有 $\dfrac{cnt_i\times (cnt_i-1)}{2}$ 的贡献。 时间 $O(n\log_{10} n)$,在于拆数上。 ```cpp /* _/ _/ _/_/_/_/_/_/ _/ _/ _/_/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/ _/_/_/_/_/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/ _/ _/ _/ _/ */ #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. 对于事件 $1$,我们用一个 $map$ 去做加 $1$ 即可。 如果没有联通,那么我们对其进行合并,按 $siz$ 合并。 启发式合并只体现在优化时间上。 2. 对于事件 $2$,我们考虑在代表节点 $u$ 上建一个 $tag$,记录 $u$ 及其子树参加的聚会数。 那么对于点 $x$,它参加的聚会数就是 $x$ 到代表节点的 $\sum tag_u$ 吗? 有问题,对于合并时两个代表节点 $x,y$,假设 $f_x=y$,那么此时 $x$ 参加的聚会数会是 $tag_x+tag_y$,然而实际上 $x$ 只参加了 $tag_x$ 次聚会! 但这并不意味着 $tag_y$ 就不算进答案里,我们要做的是算出 $tag_y$ 里 $x$ 没参加的聚会数,然而在刚刚合并完时这个没参加的聚会数就是 $tag_y$! 这告诉我们在合并两个代表节点 $(x,y)$ 时要记录一个 $h_x = tag_y$,统计答案时需要减掉 $h_x$。 3. 对于统计 $(x,y)$ 的答案,有两部分: - $mp_{x,y}$,这个不说。 - $x,y$ 共同参加的聚会数。很显然,从 $lca$ 开始 $x,y$ 才共同开始参加聚会。 那 $\sum\limits_{u=lca} tag_u-h_u$ 就是共同参加的聚会数吗? 错! 在 $tag_{lca}$ 中,有可以分为两种聚会: 1. $x,y$ 都参加的聚会 2. $x,y$ 至少一人没参加的聚会。 我们要做的就是把 2 的情况给去掉。显然 2 的情况放在了一个 $h$ 里。 因为只有 $x,y$ 两人都参加才能算进贡献,所以我们要看的是 $x,y$ 哪一个合并的晚,哪一个的 $h$ 就是答案;很显然,时间越晚,$h$ 会更大,取 $\max$ 即可,比如记为 $tp$。 那么答案为 $mp_{x,y} - tp + \sum\limits_{u=lca} (tag_u-h_u)$。 因为树高不会超过 $\log n$,所以每次操作时间也是 $O(\log n)$ 的。 总时间就是 $O(m\log n)$。 ```cpp /* _/ _/ _/_/_/_/_/_/ _/ _/ _/_/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/ _/_/_/_/_/_/ _/ _/ _/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/ _/ _/ _/ _/ */ #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 ; } ```