当前位置:网站首页>[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]所创,转载请带上原文链接,感谢
边栏推荐
- 想花钱速学互联网行业,大概花两三个月的时间,出来好找工作吗
- Hystrix 如何解决 ThreadLocal 信息丢失
- Use Python to guess which numbers in the set are added to get the sum of a number
- Version 4.5.7 of swoole was released, and the -- enable swote JSON compilation option was added
- layer.prompt(options, yes) - 输入层
- Design mode (8) -- command mode
- CSDN bug9: to be added
- 世界上最伟大的10个公式,其中一个人尽皆知
- 基于FPGA的MCP4725驱动程序
- SEO industry, what are the 10 pieces of good advice worth collecting?
猜你喜欢
【高级测试工程师】新鲜出炉的三套价值18K的自动化测试面试(网易、字节跳动、美团)
C + + STL container
Q & A and book donation activities of harbor project are in hot progress
中小企业为什么要用CRM系统
高通骁龙875夺安卓处理器桂冠,但外挂5G基带成为它的弊病
B. protocal has 7000eth assets in one week!
Swoole 如何使用 Xdebug 进行单步调试
Key layout of the Central Government: in the next five years, self-reliance and self-improvement of science and technology will be the priority, and these industries will be named
On fedlearner, the latest open source federated machine learning platform of byte
不用懂代码,会打字就可以建站?1111 元礼包帮你一站配齐!
随机推荐
Custom annotation! Absolutely is the sharp weapon that programmer installs force!!
.MD语法入门
He doubled the fluency of the long list of idle fish app
Want to spend money to learn the Internet industry quickly, about two or three months, come out to find a job
一不小心画了 24 张图剖析计网应用层协议!
奸商加价销售mate40,小米可望在高端手机市场夺取更多市场
中小企业为什么要用CRM系统
2020-11-07
iOS14新特性-WidgetKit开发与实践
Q & A and book donation activities of harbor project are in hot progress
How to use Xdebug for single step debugging
设计 API 时通过 POST 获取数据需要注意哪些问题
TCP性能分析与调优策略
CSDN bug7: to be added
Centos7 local source Yum configuration
CSDN bug5: to be added
First acquaintance of file
Exploration and practice of Tencent cloud tbase in the field of distributed HTAP
Call the open source video streaming media platform dawinffc
CSDN bug11: to be added