树状数组 || 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: ...
随机推荐
- Java(6)for循环
一.for循环的使用场合 1.1.while循环——先判断,再循环 while(1.条件表达式){ //2.循环操作 //3.更改循环条件表达式 } 1.2.do…while——先 ...
- APPLE-SA-2019-3-25-6 iCloud for Windows 7.11
APPLE-SA-2019-3-25-6 iCloud for Windows 7.11 iCloud for Windows 7.11 is now available and addresses ...
- SQL Server - Partition by 和 Group by对比
参考:https://www.cnblogs.com/hello-yz/p/9962356.html —————————————————— 今天大概弄懂了partition by和group by的区 ...
- hiho 1097 最小生成树一·Prim算法 (最小生成树)
题目: 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 最近,小Hi很喜欢玩的一款游戏模拟城市开放出了新Mod,在这个Mod中,玩家可以拥有不止一个城市了! 但是,问 ...
- vue 报错总结
关闭vue-cli 默认eslint规则: 找到 build -> webpack.base.config.js ,删除箭头指向代码 1.Newline required at end of f ...
- lnmp.org 安装环境的,root权限都没法删除网站文件夹,问题解决-转
rm -rf删除网站目录时出现rm: cannot remove `.user.ini': Operation not permitted rm -rf删除网站目录时出现rm: cannot remo ...
- 2017蓝桥杯第十题(k倍区间)
#include<iostream> #include<stdio.h> using namespace std; ; ],a[N]; int lowbit(int n){ r ...
- 【原创】大叔问题定位分享(23)Ambari安装向导点击下一步卡住
ambari安装第一步是输入集群name,点击next时页面卡住不动,如下图: 注意到其中一个接口请求结果异常,http://ambari.server:8080/api/v1/version_def ...
- mysql数据库基本语句
我们除了可以在mysql数据库或phpmyadmin中登陆数据库我们还可以使用linux中命令进行创建,下面我来给大家介绍一个简单的数据库创建方法吧. 首选用putty连接linux服务器,进行命令行 ...
- 在node中使用MongoDB
1.下载安装包,进行安装: https://www.mongodb.com/download-center/community 参考网址:https://www.cnblogs.com/ymwange ...