题面:无

题解:无

代码:

 #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 的树的更多相关文章

  1. 「ZJOI2017」树状数组(二维线段树)

    「ZJOI2017」树状数组(二维线段树) 吉老师的题目真是难想... 代码中求的是 \(\sum_{i=l-1}^{r-1}a_i\),而实际求的是 \(\sum_{i=l}^{r}a_i\),所以 ...

  2. BZOJ2141排队——树状数组套权值线段树(带修改的主席树)

    题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...

  3. luogu3380/bzoj3196 二逼平衡树 (树状数组套权值线段树)

    带修改区间K大值 这题有很多做法,我的做法是树状数组套权值线段树,修改查询的时候都是按着树状数组的规则找出那log(n)个线段树根,然后一起往下做 时空都是$O(nlog^2n)$的(如果离散化了的话 ...

  4. CF1093E Intersection of Permutations 树状数组套权值线段树

    \(\color{#0066ff}{ 题目描述 }\) 给定整数 \(n\) 和两个 \(1,\dots,n\) 的排列 \(a,b\). \(m\) 个操作,操作有两种: \(1\ l_a\ r_a ...

  5. Dynamic Rankings(树状数组套权值线段树)

    Dynamic Rankings(树状数组套权值线段树) 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[ ...

  6. [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)

    [BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...

  7. 【树状数组套权值线段树】bzoj1901 Zju2112 Dynamic Rankings

    谁再管这玩意叫树状数组套主席树我跟谁急 明明就是树状数组的每个结点维护一棵动态开结点的权值线段树而已 好吧,其实只有一个指针,指向该结点的权值线段树的当前结点 每次查询之前,要让指针指向根结点 不同结 ...

  8. 【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)

    [概念] 转载连接:树状数组 讲的挺好. 这两题非常的相似,查询区间的累加和.更新结点.Add(x,d) 与 Query(L,R) 的操作 [题目链接:candy] 唉,也是现在才发现这题用了这个知识 ...

  9. LightOJ 1085(树状数组+离散化+DP,线段树)

    All Possible Increasing Subsequences Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format: ...

随机推荐

  1. webpack学习记录 二

    开发网站 用polyfill(全局污染)  开发框架 用Runtime(局部污染) 在.babelrc文件中

  2. 20155312 张竞予 Exp4 恶意代码分析

    Exp4 恶意代码分析 目录 基础问题回答 (1)如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控. (2)如果 ...

  3. Jmeter学习笔记03-元件作用域及执行顺序

    Jmeter可以通过鼠标拖拽来随意改变元件之间的顺序以及元件的层级关系,随着元件所在域的不同,在执行时候,会有不同效果 层级关系和元件类型决定了在测试计划中各元件的执行顺序. 1)元件的作用域: jm ...

  4. 【原创】大叔问题定位分享(6)Dubbo monitor服务iowait高,负载高

    一 问题 Dubbo monitor所在服务器状态异常,iowait一直很高,load也一直很高,监控如下: iowait如图: load如图: 二 分析 通过iotop命令可以查看当前系统中磁盘io ...

  5. WPF学习:3.Border & Brush

    上一章<WPF学习:2.Layout-Panels-Countainers>主要介绍了布局,容器和面板.这一章主要开始介绍Border(边界)和Brush(画刷). 代码地址:http:/ ...

  6. 使用Java API操作HDFS文件系统

    使用Junit封装HFDS import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org ...

  7. Sharding-JDBC

    1.官网文档:https://shardingsphere.apache.org/document/legacy/3.x/document/cn/manual/sharding-jdbc/usage/ ...

  8. [iOS11] contentInsetAdjustmentBehavior 问题, push back时, 界面会上下移动.

    https://stackoverflow.com/questions/45573829/weird-uitableview-behaviour-in-ios11-cells-scroll-up-wi ...

  9. 005-Spring Boot配置分析-配置文件application、EnvironmentPostProcessor、Profiles

    一.配置文件application 默认配置文件application.propertie或者application.yml,可同时存在 application.propertie增加配置:local ...

  10. PHP游戏概率方法

    <?php function createRandomKey($randArr, $rateKey){ $total = 0; $chooseArr = array(); $pow = 0; / ...