阔力梯的树(2020 CCPC Wannafly Winter Camp Day2 Div.1&2 )dsu on tree
发布日期:2021-06-28 19:59:46 浏览次数:2 分类:技术文章

本文共 1281 字,大约阅读时间需要 4 分钟。

题解:

dsu on tree
dsu on tree的基本步骤就不说了

看到这题询问结点的子树问题,而且询问时离线的,首先想到的dsu on tree的这个trick。

本题的难题就是如何维护结点所有孩子的这个序列。
1.第一个权值插入时,对于整体是没有影响的,(最少需要2个结点才能对其做出贡献)
2.分类讨论权值插入的位置。
设插入的数为 x x x
(1).如果插入到第一位(假定编号为 y y y)或者最后一位(假定编号为 y y y),那么毋庸置疑增加的贡献为 ( x − y ) ∗ ( x − y ) (x-y)*(x-y) xyxy
(2).如果插入到了中间呢,我们设前面的值为 l l l,后面的值为 r r r,那么贡献则为 ( l − x ) ∗ ( l − x ) + ( r − x ) ∗ ( r − x ) − ( r − l ) ∗ ( r − l ) (l-x) *(l-x)+(r-x)*(r-x)-(r-l)*(r-l) (lx)(lx)+(rx)(rx)(rl)(rl)

对于插入和查询位置的维护,我们可以用一个set来维护即可。

代码:

/*Keep on going Never give up*///#pragma GCC optimize(3,"Ofast","inline")#include
#define int long long#define endl '\n'#define Accepted 0#define AK main()#define I_can signedusing namespace std;const int maxn =2e5+10;const int MaxN = 0x3f3f3f3f;const int MinN = 0xc0c0c00c;typedef long long ll;const int inf=0x3f3f3f3f;const ll mod=1e9+7;vector
edge[maxn];vector
> Q[maxn];bool visited[maxn];int a[maxn],sz[maxn],son[maxn];int ans[maxn];int sum;set
ss;void dfs(int x){
sz[x]=1; for(auto i:edge[x]){
dfs(i); sz[x]+=sz[i]; if(sz[son[x]]
>n; for(int i=2;i<=n;i++){ int x; cin>>x; edge[x].push_back(i); } dfs(1); dsu(1,0); for(int i=1;i<=n;i++){ cout<
<

转载地址:https://blog.csdn.net/xxxxxiao123/article/details/114951902 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:2020 China Collegiate Programming Contest Changchun F - Strange Memory(dsu on tree + 位运算小技巧)
下一篇:Educational Codeforces Round 105 (Rated for Div. 2) C. 1D Sokoban

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月01日 04时03分51秒