当前位置:网站首页>[CCPC] 2020ccpc Changchun F - band memory | tree heuristic merge (DSU on a tree), chairman tree
[CCPC] 2020ccpc Changchun F - band memory | tree heuristic merge (DSU on a tree), chairman tree
2020-11-10 10:46:00 【osc_l7zl78wt】
Everyone will dsu The competition area of .. I had known that I would have opened the array a little bit ..
Bad 20 It's silver in minutes ..
The last one ccpc I'm sorry ...
The main idea of the topic :
Give a tree
Let's find out :

Topic ideas :
notice lca, That would be to enumerate each point as lca The contribution of
So enumerate the current node as lca when , So what can contribute is , The contribution of any two of his subtrees
So directly enumerate all the points of the current subtree , And then match it with the previous weights
Here we need to split it bit by bit :
a^(b+c) != a^b + a^c
But after dividing the number into bits , For the present lca Weight is ai = c, The current point is ak = a, For all previous weights in the subtree a^c The point of , If k Of the x Is it 1, Then take a look at a^c In the point of How many are in the first k Is it 0, vice versa
In this way, the contribution can be calculated
At this point, an operation is needed :
The weight is calculated as c Of the k position yes 1 The number of
This place seems to work unorder_map perhaps multiset Get rid of it
But it's too safe to play ... Added chairman tree ..( It may not be safe Different results )
As for the heuristic merging here, it is nothing more than the principle of Huffman tree :
Let the subtree with the largest number of nodes visit only once , But here's a point , If the given tree is a chain , It's still going to get stuck n^2/2, But you need to pay attention to one detail : There can be no ai = ai^aj The situation of , because aj Must be greater than 0
So at this point, you can directly exclude the chain of the case, the complexity of Overall control to O(nlogn)
After adding a chairman tree, the overall complexity is :O(nlog^n)
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+6;
const int mod = 1e9+7;
const ll base = 1e9;
ll n,m,p;
ll a[maxn];
int L[maxn],R[maxn];
int cnt = 0;
vector<int>v[maxn];
vector< pair<int,int> >g[maxn];
struct node{
int v[22],w;/// The first k Number of bits
int l,r;
}t[maxn*21];
int root[maxn];
int sz[maxn];
int cot = 0;
void Insert(int &x,int y,int l,int r,int pos,ll w){
x = ++cnt;
t[x] = t[y];
for(int i=0;i<=20;i++)
if(w>>i&1) t[x].v[i]++;
t[x].w ++;
if(l == r) return ;
int mid = (l+r)/2;
if(pos<=mid) Insert(t[x].l,t[y].l,l,mid,pos,w);
else Insert(t[x].r,t[y].r,mid+1,r,pos,w);
}
ll Query(int x,int y,int l,int r,int pos,ll w){
if(l == r){
ll ans = 0;
for(int i=0;i<=20;i++){
if(w>>i&1)
ans += ( (t[y].w - t[x].w) - (t[y].v[i]-t[x].v[i]) )*(1<<i);
else
ans += (t[y].v[i] - t[x].v[i])*(1ll<<i);
}
return ans;
}
int mid = (l+r)/2;
if(pos <= mid) return Query(t[x].l,t[y].l,l,mid,pos,w);
return Query(t[x].r,t[y].r,mid+1,r,pos,w);
}
void dfs(int u,int fa){
sz[u] = 1;
for(int e:v[u]){
if(e == fa) continue;
dfs(e,u);
g[u].push_back({sz[e],e});
sz[u] += sz[e];
}
sort(g[u].begin(),g[u].end());
}
void dfs1(int u){
int sz = g[u].size();
L[u] = ++cot;
Insert(root[cot],root[cot-1],0,1e6,a[u],u);
for(int i=sz-1;i>=0;i--){
int e = g[u][i].second;
dfs1(e);
}
R[u] = cot;
}
ll res = 0;
ll work(int u,int R,int L,int x){
ll temp = 0;
if(a[u]^x||a[u]^x<=1e6)
temp += Query(root[L-1],root[R],0,1e6,a[u]^x,u);
for(auto tempx:g[u]) temp += work(tempx.second,R,L,x);
return temp;
}
void dfs2(int u){
int sz = g[u].size();
int pre = L[u],last = R[u];
for(int i=sz-2;i>=0;i--){
last = R[g[u][i+1].second];
res += work(g[u][i].second,last,pre,a[u]);
}
for(int i=sz-1;i>=0;i--) dfs2(g[u][i].second);
}
int main(){
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
for(int i=1;i<=n-1;i++){
int x,y;scanf("%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
dfs(1,1);
dfs1(1);
dfs2(1);
printf("%lld\n",res);
return 0;
}
/**
6
4 2 1 6 6 5
1 2
2 3
1 4
4 5
4 6
**/
版权声明
本文为[osc_l7zl78wt]所创,转载请带上原文链接,感谢
边栏推荐
- GNU assembly basic mathematical equations multiplication
- Do not understand the code, can type can build a station? 1111 yuan gift bag to help you with one stop!
- First acquaintance of file
- getIServiceManager() 源码分析
- CSDN bug9: to be added
- 注册滴滴加不上车怎么办?要怎么处理?
- Centos7 rsync+crontab 定时备份
- Introduction to. MD grammar
- python math类
- SEO界,值得收藏的10条金玉良言有哪些?
猜你喜欢

csdn bug1:待加

csdn bug4:待加

LeetCode:二叉树(四)

Express learning notes (MOOC)

想花钱速学互联网行业,大概花两三个月的时间,出来好找工作吗
![[paper reading notes] a multilayered informational random walk for attributed social network embedding](/img/3d/657a60600219ce6cfc514ad1b1bb49.jpg)
[paper reading notes] a multilayered informational random walk for attributed social network embedding

《Python Cookbook 3rd》笔记(2.4):字符串匹配和搜索

A professional tour -- a quick tour of GitHub hot spots Vol.45

【CCPC】2020CCPC长春 F - Strange Memory | 树上启发式合并(dsu on a tree)、主席树

CCR coin robot: novel coronavirus pneumonia has accelerated the interest of regulators in CBDC.
随机推荐
MFC界面开发帮助文档——BCG如何在工具栏上放置控件
Api: tiktok: Video Review List
TCP性能分析与调优策略
STATISTICS STATS 380
【goang】sync.WaitGroup详解
《Python Cookbook 3rd》笔记(2.4):字符串匹配和搜索
世界上最伟大的10个公式,其中一个人尽皆知
[technical course] peerconnection in webrtc self built by visual studio 2017_ The client program reported an external symbol error that LNK2019 could not resolve
【goang】 sync.WaitGroup Detailed explanation
ASP.NET Core框架揭秘[博文汇总-持续更新]
安卓快速关机APP
layer.prompt(options, yes) - 输入层
What does the mremote variable in servicemanagerproxy refer to?
CSDN bug3: to be added
Leetcode 1-sum of two numbers
寻找性能更优秀的不可变小字典
【iOS】苹果登录Sign in with Apple
File初相识
python pip命令的使用
Android quick shutdown app