P1972 [SDOI2009] HH的项链
题意是给你n个数,再给你m个查询,每次查询一个[l,r],求询问的区间内有多少种不同的数字。
这个题可以使用离线处理+树状数组的方式来处理。
#include <iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<tuple>
using namespace std;
const int N = 1e6 + 5;
int tree[N];
int vis[N];
int arr[N];
tuple<int,int, int> qk[N];
int get_lowbit(int i) {
return i&(-i);
}
void add(int i,int v) {
while (i < N) {
tree[i] += v;
i += get_lowbit(i);
}
}
int ask(int i) {
int ans=0;
if(i == 0)return 0;
while (i)
{
ans+=tree[i];
i-=get_lowbit(i);
}
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>arr[i];
}
int m;
cin>>m;
for (int i = 0; i < m; i++) {
int l,r;
cin>>l>>r;
get<0>(qk[i]) = l, get<1>(qk[i]) = r, get<2>(qk[i]) = i;
}
sort(qk, qk+m,[](const tuple<int, int, int>&p1, const tuple<int, int, int>& p2){
return get<1>(p1) < get<1>(p2);
});
memset(vis, 0,sizeof vis);
int idx = 0;
vector<int>ans(m,0);
for (int i = 1; i <= n; i++) {
if (!vis[arr[i]]) {
vis[arr[i]] = i;
}
else {
add(vis[arr[i]], -1);
vis[arr[i]] = i;
}
add(vis[arr[i]], 1);
while ( idx <m && get<1>(qk[idx]) == i) {
int l = get<0>(qk[idx]), r = get<1>(qk[idx]);
ans[get<2>(qk[idx])] = ask(r) - ask(l-1);
idx++;
}
if(idx == m)break;
}
for (int i = 0; i < m; i++) {
cout<<ans[i]<<endl;
}
}
树上颜色数目
给出一棵 n 个节点以 1 为根的树,节点 u 的颜色为 c_u,现在对于每个结点 u 询问 u 子树里一共出现了多少种不同的颜色。
dfs序+dus,
#include <iostream>
#include<cstring>
using namespace std;
const int N=1e5+5;
int h[N],e[N<<1], nx[N<<1];
int cnt=1;
int color[N];
int colorCnt[N];
int totColor = 0;
void add_edge(int a,int b){
e[cnt] = b, nx[cnt] = h[a], h[a] = cnt++;
}
void add(int i){
if(colorCnt[color[i]]==0)totColor++;
colorCnt[color[i]]++;
}
void del(int i){
colorCnt[color[i]]--;
if(colorCnt[color[i]]==0)totColor--;
}
int big[N];
int son[N];
int ans[N];
int L[N], R[N],time1 = 0, Node[N];
void dfs0(int u,int f){
int v = 0;
L[u] = ++time1;
Node[time1] = u;
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=f){
dfs0(t,u);
if(!big[u] || son[t] > v){
v = son[t];
big[u] = t;
}
}
}
R[u] = time1;
}
void dfs1(int u,int f, bool keep){
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=f && t!=big[u]){
dfs1(t, u, false);
}
}
if(big[u]){
dfs1(big[u], u, true);
}
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=f && t!=big[u]){
for(int j=L[t];j<=R[t];j++){
add(Node[j]);
}
}
}
add(u);
ans[u] = totColor;
if(!keep){
for(int i=L[u];i<=R[u];i++){
del(Node[i]);
}
}
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
add_edge(a,b);
add_edge(b,a);
}
for(int i=1;i<=n;i++)cin>>color[i];
dfs0(1,-1);
dfs1(1, -1, 0);
int m;
cin>>m;
for(int i=0;i<m;i++){
int mi;
cin>>mi;
cout<<ans[mi]<<endl;
}
}
思路差不多,只不过求的是子树最大颜色编号之和,可能存在多个最大值
#include <iostream>
#include<cstring>
#include<unordered_map>
using namespace std;
const int N=1e5+5;
int h[N],e[N<<1], nx[N<<1];
int cnt=1;
int color[N];
void add_edge(int a,int b){
e[cnt] = b, nx[cnt] = h[a], h[a] = cnt++;
}
// unordered_map<int,int>mp;
int colorCnt[N];
long long sum = 0;
int maxc = 0; //最大颜色数量
void add(int i){
colorCnt[color[i]]++;
if(colorCnt[color[i]] > maxc){
maxc = colorCnt[color[i]];
sum = color[i];
}else if(colorCnt[color[i]] == maxc){
sum += color[i];
}
}
void del(int i){
colorCnt[color[i]]--;
}
int big[N];
int son[N];
long long ans[N];
int L[N], R[N],time1 = 0, Node[N];
void dfs0(int u,int f){
L[u] = ++time1;
Node[time1] = u;
son[u] = 1;
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=f){
dfs0(t,u);
son[u] += son[t];
if(!big[u] || son[big[u]] < son[t]){
big[u] = t;
}
}
}
R[u] = time1;
}
void dfs1(int u,int f, bool keep){
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=f && t!=big[u]){
dfs1(t, u, false);
}
}
if(big[u]){
dfs1(big[u], u, true);
}
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=f && t!=big[u]){
for(int j=L[t];j<=R[t];j++){
add(Node[j]);
}
}
}
add(u);
ans[u] = sum;
if(!keep){
for(int i=L[u];i<=R[u];i++){
del(Node[i]);
}
sum = maxc = 0;
}
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)cin>>color[i];
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
add_edge(a,b);
add_edge(b,a);
}
dfs0(1,-1);
dfs1(1, -1, 0);
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
}
}
树上启发式合并
#include <iostream>
#include<vector>
#include<unordered_map>
#include<tuple>
using namespace std;
unordered_map<int,int> mp;//直接用有序列表记录深度为i的最大值
int cnt=1;
const int N=1e5+5;
int h[N], nx[N], e[N];
int arr[N];
void add_edge(int a,int b){
e[cnt] = b, nx[cnt] = h[a], h[a] = cnt++;
}
int big[N],son[N];
vector<int>ans;
vector<pair<int,int>> qk[N];
int dept[N], time1=0, L[N], R[N], Node[N];
void dfs0(int u,int d){
L[u] = ++time1;
Node[time1] = u;
dept[u] = d;
son[u] = 1;
for(int i=h[u];i;i=nx[i]){
int t=e[i];
dfs0(t, d+1);
son[u] += son[t];
if(!big[u] || son[big[u]]< son[t]){
big[u] = t;
}
}
R[u] = time1;
}
void add(int u){
mp[dept[u]] = max(mp[dept[u]], arr[u]);
}
void del(int u){
mp[arr[u]]--;
}
void dfs1(int u,bool keep){
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=big[u]){
dfs1(t, false);
}
}
if(big[u]){
dfs1(big[u], true);
}
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=big[u]){
for(int j=L[t];j<=R[t];j++){
add(Node[j]);
}
}
}
add(u);
// cout<<u<<endl;
for(auto&[k,v]:qk[u]){
ans[v] = mp[dept[u] + k];
}
if(!keep){
mp.clear(); //所有树上信息全部清空即可 这个很重要!!!!!!!!!!!!!!!!!!!!!!!!
}
}
int main()
{
// 请在此输入您的代码
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>arr[i];
}
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
add_edge(a,b);
}
ans.resize(q);
for(int i=0;i<q;i++){
int x,k;
cin>>x>>k;
qk[x].push_back({k, i});
}
dfs0(1,1);
dfs1(1, 0);
for(int i=0;i<q;i++){
cout<<ans[i]<<endl;
}
return 0;
}
和上一题同样思路,上一题是求节点u的第k层最大值,用unordered_map<int,int>表示第k层最大值, 这题是求节点u的第k层不重复字符串,用unordered_map<int,set< string>>存放第k层字符串集合
#include <iostream>
#include<cstring>
#include<vector>
#include <algorithm>
#include<unordered_map>
#include<set>
using namespace std;
const int N = 2e5 + 5;
int h[N], e[N], nx[N];
int cnt = 1;
void add(int a, int b) {
e[cnt] = b, nx[cnt] = h[a], h[a] = cnt++;
}
int time1 = 0;
int c[N];
int L[N],R[N];
int Node[N];
string arr[N];
int deep[N], big[N], son[N];
void dfs(int u, int d) {
deep[u] = d;
L[u] = ++time1;
Node[time1] = u;
son[u] = 1;
for (int i = h[u]; i; i = nx[i]) {
int t = e[i];
dfs(t, d + 1);
son[u] += son[t];
if(!big[u] || son[big[u]] < son[t]){
big[u] = t;
}
}
R[u] = time1;
}
unordered_map<int, set<string>> mp;
vector<pair<int,int>>qk[N];
vector<int>ans;
void dfs1(int u,bool keep){
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=big[u]){
dfs1(t, false);
}
}
if(big[u]){
dfs1(big[u], true);
}
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=big[u]){
for(int j = L[t];j<=R[t];j++){
mp[deep[Node[j]]].insert(arr[Node[j]]);
}
}
}
mp[deep[u]].insert(arr[u]);
for(auto&[k,v]:qk[u]){
ans[k] = mp[deep[u] + v].size();
}
if(!keep){
mp.clear();//直接清空就可以,因为mp存放的是当前子树的信息,以后都用不上了
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
cin >> n;
for (int i = 1; i <= n; i++) {
int f;
cin >> arr[i] >> f;
add(f, i);
}
cin >> q;
dfs(0, 0);
for(int i=0;i<q;i++) {
int v, d;
cin >> v >> d;
qk[v].push_back({i,d});
}
ans.resize(q);
dfs1(0,0);
for(int i=0;i<q;i++){
cout<<ans[i]<<endl;
}
}
题目大意是求子树所有点重新排序是否构成回文, 思路dfs序列+二分 或者dfs+dsu(但超时了)
#include <iostream>
#include<cstring>
#include<vector>
#include <algorithm>
using namespace std;
const int N = 5e5 + 5;
int h[N], e[N], nx[N];
int cnt = 1;
void add(int a, int b) {
e[cnt] = b, nx[cnt] = h[a], h[a] = cnt++;
}
vector<int> dep[N][26];
int time1 = 0;
int c[N];
int in[N], out[N];
void dfs(int u, int d) {
in[u] = ++time1;
dep[d][c[u]].push_back(time1);
for (int i = h[u]; i; i = nx[i]) {
int t = e[i];
dfs(t, d + 1);
}
out[u] = time1;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
cin >> n >> q;
for (int i = 2; i <= n; i++) {
int f;
cin >> f;
add(f, i);
}
string str;
cin >> str;
for (int i = 1; i <= n; i++) {
c[i] = str[i - 1] - 'a';
}
dfs(1, 1);
while (q--) {
int v, d;
cin >> v >> d;
int in_t = in[v], out_t = out[v];
int sum = 0;
for (int i = 0; i < 26; i++) {
if ((upper_bound(dep[d][i].begin(), dep[d][i].end(), out_t) - upper_bound(dep[d][i].begin(), dep[d][i].end(), in_t)) & 1) {
sum++;
}
if (sum > 1)break;
}
if (sum > 1) {
cout << "No\n";
}
else {
cout << "Yes\n";
}
}
}
dsu on tree
#include <iostream>
#include<cstring>
#include<vector>
#include <algorithm>
#include<unordered_map>
#include<set>
using namespace std;
const int N = 5e5 + 5;
int h[N], e[N], nx[N];
int cnt1 = 1;
void add(int a, int b) {
e[cnt1] = b, nx[cnt1] = h[a], h[a] = cnt1++;
}
int time1 = 0;
int c[N];
int L[N],R[N];
int Node[N];
int deep[N], big[N], son[N];
void dfs(int u, int d) {
deep[u] = d;
L[u] = ++time1;
Node[time1] = u;
son[u] = 1;
for (int i = h[u]; i; i = nx[i]) {
int t = e[i];
dfs(t, d + 1);
son[u] += son[t];
if(!big[u] || son[big[u]] < son[t]){
big[u] = t;
}
}
R[u] = time1;
}
int cnt[N][26];
vector<pair<int,int>>qk[N];
vector<int>ans;
void dfs1(int u,bool keep){
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=big[u]){
dfs1(t, false);
}
}
if(big[u]){
dfs1(big[u], true);
}
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=big[u]){
for(int j = L[t];j<=R[t];j++){
cnt[deep[Node[j]]][c[Node[j]]]++;
}
}
}
cnt[deep[u]][c[u]]++;
for(auto&[k,v]:qk[u]){
int sum = 0;
for(int i=0;i<26;i++){
sum += cnt[v][i]&1;
}
ans[k] = sum;
}
if(!keep){
for(int j = L[u];j<=R[u];j++){
cnt[deep[Node[j]]][c[Node[j]]]--;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, q;
cin >> n>>q;
for (int i = 2; i <=n; i++) {
int f;
cin>>f;
add(f, i);
}
string str;
cin>>str;
for(int i=1;i<=n;i++){
c[i] = str[i-1]-'a';
}
dfs(1, 1);
for(int i=0;i<q;i++) {
int v, d;
cin >> v >> d;
qk[v].push_back({i,d});
}
ans.resize(q);
dfs1(1,0);
for(int i=0;i<q;i++){
cout<<(ans[i] <=1 ? "Yes":"No")<<endl;
}
}
dfs序+树状数组
#include <iostream>
#include<cstring>
#include<vector>
using namespace std;
const int N=3e5+5;
int h[N],e[N<<1], nx[N<<1];
int cnt=1;
long long tree[N];
long long ans[N];
int get_lowbit(int i){
return i&(-i);
}
void add(int i,int j){
while(i<N){
tree[i] += j;
i+=get_lowbit(i);
}
}
long long ask(int i){
long long ans=0;
while(i){
ans+=tree[i];
i-=get_lowbit(i);
}
return ans;
}
void add_edge(int a,int b){
e[cnt] = b, nx[cnt] = h[a], h[a] = cnt++;
}
vector<pair<int,int>> qt[N];
void dfs(int u,int f,int d){
for(auto&[dt,x]:qt[u]){
add(d, x);
add(d+dt+1, -x);
}
ans[u] = ask(d);
for(int i=h[u];i;i=nx[i]){
int t=e[i];
if(t!=f){
dfs(t, u, d+1);
}
}
for(auto&[dt,x]:qt[u]){
add(d, -x);
add(d+dt+1, x);
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
cin>>n;
for(int i=0;i<n-1;i++){
int a,b;
cin>>a>>b;
add_edge(a, b);
add_edge(b, a);
}
int q;
cin>>q;
while(q--){
int v,d,x;
cin>>v>>d>>x;
qt[v].push_back({d,x});
}
dfs(1, -1, 1);
for(int i=1;i<=n;i++){
cout<<ans[i]<<" ";
}
}