C. Propagating tree

2024-08-24 20:36
文章标签 tree propagating

本文主要是介绍C. Propagating tree,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

https://codeforces.com/contest/383/problem/C

因为是单点查询,操作一是+-+-

可以分奇偶性考虑

奇数 +-+-, -+-+

偶数  +-+,   -+-

我们可以发现奇数和偶数的标记是相反的

因此我们维护奇数点的标记,如果点是奇数就+val,否者就-val

查询的时候奇数点直接+a[u],偶数点相反再+a[u]

code

// Problem: C. Propagating tree
// Contest: Codeforces - Codeforces Round 225 (Div. 1)
// URL: https://codeforces.com/problemset/problem/383/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include<iostream>
#include<algorithm>
#include<vector>
#define INF (1ll<<60)
using namespace std;
typedef long long ll;
const int N=2e5+9;
int a[N],fa[N],sz[N],wc[N],dep[N],dfn[N],rdfn[N],top[N];
int n,m,r,p,vistime;
vector<int> e[N<<1];
void add(int u,int v){e[u].push_back(v);e[v].push_back(u);
}
void dfs1(int u,int f){fa[u]=f;sz[u]=1;dep[u]=dep[f]+1;for(auto & i : e[u]){if(i!=f){dfs1(i,u);sz[u]+=sz[i];if(sz[i]>sz[wc[u]]){wc[u]=i;}}}
}
void dfs2(int u,int Top){dfn[u]=++vistime;rdfn[vistime]=u;top[u]=Top;if(wc[u]){dfs2(wc[u],Top);for(auto & i : e[u]){if(i!=wc[u] && i!=fa[u]){dfs2(i,i);}   }}
}
struct node{ll val,tag;
}seg[N<<2];
ll tl(ll x){return x<<1;
}
ll tr(ll x){return x<<1|1;
}
void pushup(int id){seg[id].val=(seg[tl(id)].val+seg[tr(id)].val);
}
bool inrange(int L,int R,int l,int r){return L>=l && R<=r;
}
bool outofrange(int L,int R,int l,int r){return  L>r || l>R;
}
void maketag(int id,int l,int r,ll v){(seg[id].val+=(r-l+1)*v);(seg[id].tag+=v);
}
void pushdown(int id,int l,int r){int mid=(l+r)>>1;maketag(tl(id),l,mid,seg[id].tag);  maketag(tr(id),mid+1,r,seg[id].tag);seg[id].tag=0;  
}
ll query(int id,int L,int R,int l,int r){if(inrange(L,R,l,r)){return seg[id].val;}else if(!outofrange(L,R,l,r)){int mid=(L+R)>>1;pushdown(id,L,R);return (query(tl(id),L,mid,l,r)+query(tr(id),mid+1,R,l,r));}else{return 0;}
}
void modify(int id,int L,int R,int l,int r,ll v){if(inrange(L,R,l,r)){maketag(id,L,R,v);}else if(!outofrange(L,R,l,r)){int mid=(L+R)>>1;pushdown(id,L,R);modify(tl(id),L,mid,l,r,v);modify(tr(id),mid+1,R,l,r,v);pushup(id);}
}
ll ask(int id,int l,int r,int pos){if(l==r){return seg[id].val;}int mid=(l+r)>>1;pushdown(id,l,r);if(mid>=pos){return ask(tl(id),l,mid,pos);}else{return ask(tr(id),mid+1,r,pos);}
}
int main(){ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);int n,m;cin>>n>>m;for(int i=1;i<=n;i++){cin>>a[i];}for(int i=1;i<=n-1;i++){int u,v;cin>>u>>v;add(u,v);}//root=1dfs1(1,0);dfs2(1,0);for(int i=1;i<=m;i++){int op;cin>>op;if(op==1){int x,val;cin>>x>>val;val*=(dep[x]&1?1:-1);modify(1,1,n,dfn[x],dfn[x]+sz[x]-1,val);}else{int x;cin>>x;ll ans=ask(1,1,n,dfn[x]);ans*=(dep[x]&1?1:-1);ans+=a[x];cout<<ans<<'\n';}}return 0;
}

这篇关于C. Propagating tree的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1103533

相关文章

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

Sorry!Hbase的LSM Tree就是可以为所欲为!

我们先抛出一个问题: LSM树是HBase里使用的非常有创意的一种数据结构。在有代表性的关系型数据库如MySQL、SQL Server、Oracle中,数据存储与索引的基本结构就是我们耳熟能详的B树和B+树。而在一些主流的NoSQL数据库如HBase、Cassandra、LevelDB、RocksDB中,则是使用日志结构合并树(Log-structured Merge Tree,LSM Tr

【spring】does not have member field ‘com.sun.tools.javac.tree.JCTree qualid

spring-in-action-6-samples 的JDK版本 最小是11,我使用 了22: jdk21 jdk22 都与lombok 不兼容,必须使用兼容版本, 否则报错: thingsboard 的大神解释了: java: java.lang.NoSuchFieldError: Class com

[LeetCode] 863. All Nodes Distance K in Binary Tree

题:https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/ 题目大意 求给树中,距给定 结点 指定长度的 所有结点的val 思路 tree -> graph 、 bfs 先遍历树,并用map记录每个结点的父结点 ,将树变为图,然后 bfs。 /*** Definition for a binary tree

js实现树级递归,通过js生成tree树形菜单(递归算法)

1、效果图 需求:首先这是一个数据集—js的类型,我们需要把生成一个tree形式的对象 : var data = [{ id: 1, name: "办公管理", pid: 0 },{ id: 2, name: "请假申请", pid: 1 },{ id: 3, name: "出差申请", pid: 1 },{ id: 4, name: "请假记录", pid: 2 },{ id:

【unity实战】利用Root Motion+Blend Tree+Input System+Cinemachine制作一个简单的角色控制器

文章目录 前言动画设置Blend Tree配置角色添加刚体和碰撞体代码控制人物移动那么我们接下来调整一下相机的视角效果参考完结 前言 Input System知识参考: 【推荐100个unity插件之18】Unity 新版输入系统Input System的使用,看这篇就够了 Cinemachine虚拟相机知识参考: 【推荐100个unity插件之10】Unity最全的最详细的C

树链剖分 + 后缀数组 - E. Misha and LCP on Tree

E. Misha and LCP on Tree  Problem's Link   Mean:  给出一棵树,每个结点上有一个字母。每个询问给出两个路径,问这两个路径的串的最长公共前缀。 analyse: 做法:树链剖分+后缀数组. 记录每条链的串,正反都需要标记,组成一个长串. 然后记录每条链对应的串在大串中的位置,对大串求后缀数组,最后询问就是在一些链

数据结构 - Codeforces Round #353 (Div. 2) D. Tree Construction

Tree Construction  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定n个数,按照构造Binary Search Tree的方式来构造BST树,按顺序输出每一个非root结点的父节点的值。 analyse

【HDU】4871 Shortest-path tree 最短路+点分治

传送门:【HDU】4871 Shortest-path tree 题目分析: 学了点分治后来看这道题,简直就是水题。。。 但是我竟然花了将近一个晚上才写出来。。。就因为一个地方写漏了T U T。。 首先根据题意求一棵树,最短路一下,然后最小字典序就按照编号顺序遍历邻接表给节点标记。 剩下的就是树分治的事了。 在以重心X为根的子树中,按照X的子节点v的子树中最长路径包含节点数升序遍