树状数组 || JZOI 1024. @szefany 的树
题面:无
题解:无
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=(1e5)+;
int N,T,u,v,num_edge,edge_head[maxn],lsh_cnt,belong[maxn],tr[maxn],Ans[maxn];
//记住树状数组里不可以塞0
//tr中的数会在回溯时删掉,所以不用清空tr
struct Edge{int to,nx;}edge[maxn<<];
inline void Add_edge(int from,int to){
edge[++num_edge].nx=edge_head[from];
edge[num_edge].to=to;
edge_head[from]=num_edge;
return;
}
struct Nd{int yw,w,id;}nd[maxn];
inline bool cmp(const Nd&a,const Nd&b){return a.yw<b.yw;}
inline void Add(int s,int x){
for(int i=s;i>;i-=i&(-i))tr[i]+=x;
return;
}
inline int Find(int s){
int ans=;
for(int i=s;i<=lsh_cnt;i+=i&(-i))ans+=tr[i];
return ans;
}
inline void Dfs(int x,int f){
for(int i=edge_head[x];i;i=edge[i].nx){
int y=edge[i].to;
if(y!=f){
Add(nd[belong[x]].w,);
Dfs(y,x);
Add(nd[belong[x]].w,-);
}
}
Ans[x]=Find(nd[belong[x]].w+);
return;
}
int main(){
scanf("%d",&T);
while(T--){
num_edge=lsh_cnt=;
memset(edge_head,,sizeof(edge_head));
scanf("%d",&N);
for(int i=;i<N;i++){
scanf("%d%d",&u,&v);
Add_edge(u,v);
Add_edge(v,u);
}
for(int i=;i<=N;i++){
scanf("%d",&nd[i].yw);
nd[i].id=i;
}
sort(nd+,nd+N+,cmp);
nd[].w=++lsh_cnt;
belong[nd[].id]=;
for(int i=;i<=N;i++){
if(nd[i].yw!=nd[i-].yw)lsh_cnt++;
nd[i].w=lsh_cnt;
belong[nd[i].id]=i;
}
Dfs(,);
for(int i=;i<=N;i++)printf("%d\n",Ans[i]);
}
return ;
}
By:AlenaNuna
树状数组 || JZOI 1024. @szefany 的树的更多相关文章
- 「ZJOI2017」树状数组(二维线段树)
「ZJOI2017」树状数组(二维线段树) 吉老师的题目真是难想... 代码中求的是 \(\sum_{i=l-1}^{r-1}a_i\),而实际求的是 \(\sum_{i=l}^{r}a_i\),所以 ...
- BZOJ2141排队——树状数组套权值线段树(带修改的主席树)
题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...
- luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)
带修改区间K大值 这题有很多做法,我的做法是树状数组套权值线段树,修改查询的时候都是按着树状数组的规则找出那log(n)个线段树根,然后一起往下做 时空都是$O(nlog^2n)$的(如果离散化了的话 ...
- CF1093E Intersection of Permutations 树状数组套权值线段树
\(\color{#0066ff}{ 题目描述 }\) 给定整数 \(n\) 和两个 \(1,\dots,n\) 的排列 \(a,b\). \(m\) 个操作,操作有两种: \(1\ l_a\ r_a ...
- Dynamic Rankings(树状数组套权值线段树)
Dynamic Rankings(树状数组套权值线段树) 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[ ...
- [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)
[BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...
- 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings
谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...
- 【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)
[概念] 转载连接:树状数组 讲的挺好. 这两题非常的相似,查询区间的累加和.更新结点.Add(x,d) 与 Query(L,R) 的操作 [题目链接:candy] 唉,也是现在才发现这题用了这个知识 ...
- LightOJ 1085(树状数组+离散化+DP,线段树)
All Possible Increasing Subsequences Time Limit:3000MS Memory Limit:65536KB 64bit IO Format: ...
随机推荐
- centos7环境下apache2.2.34的编译安装
.获取apache2..34的源码包 http://archive.apache.org/dist/httpd/httpd-2.2.34.tar.gz .获取apache的编译参数 apache的编译 ...
- 小程序 模态对话框自定义组件(modal)
1. 概述 1.1 说明 小程序中使用wx.showModal(Object object)打开一个模态对话框,但是目前小程序所提供的modal中的内容显示比较死板,不能够完全满足工作中所遇到的功能信 ...
- 洛谷P3398 仓鼠找suger
传送门啦 思路: 那么从 $ A $ 到 $ B $ 可以分解成 先从 $ A $ 到 $ X $ 再从 $ X $ 到 $ B $ ... 另一个同理 假设能相遇 那么 要么在 $ A $ 到 $ ...
- 烽火R2600交换机配置脚本
烽火交换机端口映射配置 ip nat inside source static udp iP 端口号 公网iP 端口号 ip nat inside source interface Vlan-intf ...
- Git 头像修改 原
Git头像分两种: 第一种是直接在你当前托管的git服务网站中自定义上传头像 第二种通过第三方网站修改,基本上所有git服务网站都遵循这点,下面说的就是该模式 其实很简单!!! https://zh- ...
- SQL Server 中执行Shell脚本计算本地文件的内容大小
SQL Server 数据库中除了能执行基本的SQL语句外,也可以执行Shell脚本.默认安装后,SQL中的Shell脚本的功能是关闭的,需要手动打开, 执行以下脚本即可打开该功能. -- 允许配置高 ...
- react-native项目中集成react-native-camera插件
1. 安装 yarn add react-native-camera 2. 手动关联 (1)在AndroidManifest.xml中添加权限配置 <uses-permission androi ...
- IntelliJ IDEA 中 Web项目 目录结构
--src 应用程序源代码和测试程序代码的根目录 --main --java 应用程序源代码目录 --resources 应用程序用到的资源文件(一般都是配置文件) --webapp web项 ...
- HTML,js的基础知识
HTML 元素语法 HTML 元素以开始标签起始 HTML 元素以结束标签终止 元素的内容是开始标签与结束标签之间的内容 某些 HTML 元素具有空内容(empty content) 空元素在开始标签 ...
- impala
impala 1.impala是什么: impala是基于hive的大数据实时分析查询引擎,直接使用Hive的元数据库Metadata,意味着impala元数据都存储在Hive的metastore中. ...