贴贴大佬的计蒜客题解:

The Preliminary Contest for ICPC China Nanchang National Invitational and International Silk-Road Programming Contest

离线处理,
运用树链剖分让LCA跑快点

关键是把n-1条边,和m条询问边存起来

然后按边权值W进行升序;

这样在计数询问的时候我们从小到大计数;

每条边只会被记一次且从小到大,这样就不用担心当前计数会受上一计数更新时的影响;

每次把小于等于当前查询的边加到链上;

查询就是查询链上有多少条边被加入过;

#include<bits/stdc++.h>
using namespace std;
const int M=1e5+10;
inline int read(){
int sum=0,x=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
x=0;
ch=getchar();
}
while(ch>='0'&&ch<='9')
sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar();
return x?sum:-sum;
}
int f[M],sz[M],deep[M],son[M],dfn[M],top[M],ans[M],t[M<<2],n,cnt;
vector<int >graph[M];
struct node{
int u,v,w,index;
bool operator<(const node &b)const{
return w<b.w;
}
}q[M],e[M];
void dfs1(int u,int from){
f[u]=from;
sz[u]=1;
deep[u]=deep[from]+1;
for(int i=0;i<graph[u].size();i++){
int v=graph[u][i];
if(v!=from){
dfs1(v,u);
sz[u]+=sz[v]; if(sz[v]>sz[son[u]])
son[u]=v;
}
}
}
void dfs2(int u,int t){
top[u]=t;
dfn[u]=++cnt;
if(!son[u])
return ;
dfs2(son[u],t);
for(int i=0;i<graph[u].size();i++){
int v=graph[u][i];
if(v!=son[u]&&v!=f[u]){
dfs2(v,v);
}
}
}
void update(int sign,int c,int root,int l,int r){
if(l==r){
t[root]+=c;
return ;
}
int midd=l+r>>1;
if(sign<=midd)
update(sign,c,root<<1,l,midd);
else
update(sign,c,root<<1|1,midd+1,r);
t[root]=t[root<<1]+t[root<<1|1];
}
int find(int L,int R,int root,int l,int r){
if(L<=l&&r<=R)
return t[root];
int midd=l+r>>1;
int c=0;
if(L<=midd)
c+=find(L,R,root<<1,l,midd);
if(R>midd)
c+=find(L,R,root<<1|1,midd+1,r);
return c;
}
int solve(int u,int v){
int c=0;
int fu=top[u],fv=top[v];
while(fu!=fv){
if(deep[fu]>=deep[fv]){
c+=find(dfn[fu],dfn[u],1,1,n);
u=f[fu],fu=top[u];
}
else{
c+=find(dfn[fv],dfn[v],1,1,n);
v=f[fv],fv=top[v];
} }
if(dfn[u]<dfn[v])
c+=find(dfn[u]+1,dfn[v],1,1,n);
else if(dfn[u]>dfn[v])
c+=find(dfn[v]+1,dfn[u],1,1,n);
return c; }
int main(){
n=read();
int m=read();
for(int i=1;i<n;i++){
int x=read(),y=read(),w=read();
e[i].u=x,e[i].v=y,e[i].w=w;
graph[x].push_back(y);
graph[y].push_back(x);
}
dfs1(1,1);
dfs2(1,1);
/*cout<<"~~~~~~~~~";
for(int i=1;i<=n;i++)
cout<<dfn[i]<<" ";
cout<<endl;*/
for(int i=1;i<=m;i++){
int x=read(),y=read(),w=read();
q[i].u=x,q[i].v=y,q[i].w=w;
q[i].index=i;
}
sort(e+1,e+n);
sort(q+1,q+1+m);
int cur=1;
for(int i=1;i<=m;i++){
while(cur<n&&e[cur].w<=q[i].w){
int u=e[cur].u,v=e[cur].v;
if(deep[u]<deep[v])
swap(u,v);
update(dfn[u],1,1,1,n);
cur++;
}
ans[q[i].index]+=solve(q[i].u,q[i].v);
}
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
return 0;
}

  

J. Distance on the tree(树链剖分+线段树)的更多相关文章

  1. Aizu 2450 Do use segment tree 树链剖分+线段树

    Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...

  2. 【POJ3237】Tree(树链剖分+线段树)

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

  3. POJ3237 Tree 树链剖分 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ3237 题意概括 Description 给你由N个结点组成的树.树的节点被编号为1到N,边被编号为1 ...

  4. 【CF725G】Messages on a Tree 树链剖分+线段树

    [CF725G]Messages on a Tree 题意:给你一棵n+1个节点的树,0号节点是树根,在编号为1到n的节点上各有一只跳蚤,0号节点是跳蚤国王.现在一些跳蚤要给跳蚤国王发信息.具体的信息 ...

  5. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

  6. Water Tree CodeForces 343D 树链剖分+线段树

    Water Tree CodeForces 343D 树链剖分+线段树 题意 给定一棵n个n-1条边的树,起初所有节点权值为0. 然后m个操作, 1 x:把x为根的子树的点的权值修改为1: 2 x:把 ...

  7. [HDU3710] Battle Over Cities [树链剖分+线段树+并查集+kruskal+思维]

    题面 一句话题意: 给定一张 N 个点, M 条边的无向连通图, 每条边上有边权 w . 求删去任意一个点后的最小生成树的边权之和. 思路 首先肯定要$kruskal$一下 考虑$MST$里面去掉一个 ...

  8. 【Codeforces827D/CF827D】Best Edge Weight(最小生成树性质+倍增/树链剖分+线段树)

    题目 Codeforces827D 分析 倍增神题--(感谢T*C神犇给我讲qwq) 这道题需要考虑最小生成树的性质.首先随便求出一棵最小生成树,把树边和非树边分开处理. 首先,对于非树边\((u,v ...

  9. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  10. POJ3237 (树链剖分+线段树)

    Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...

随机推荐

  1. 【LeetCode】电话号码的字母组合

    [问题]给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:" 输出:["ad ...

  2. C++编程学习(四)声明/枚举

    一.typedef 声明 typedef 为一个已有的类型取一个新的名字 typedef int num;//feet是int的另一个名字num a;//a是int类型 二.枚举类型 enum col ...

  3. select、poll和epoll机制

    一.参考网址 1.select函数及fd_set介绍 2.linux select 函数和 fd_set 用法 2.select.poll和epoll的区别 3.利用select实现IO多路复用TCP ...

  4. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring AOP(面向切面编程)

    面向切面编程(AOP)和面向对象编程(OOP)类似,也是一种编程模式.Spring AOP 是基于 AOP 编程模式的一个框架,它的使用有效减少了系统间的重复代码,达到了模块间的松耦合目的. AOP ...

  5. Python笔记_第五篇_Python数据分析基础教程_文件的读写

    1. 读写文件(基本) savetxt.loadtxt i2 = np.eye(2) print(i2) np.savetxt(r"C:\Users\Thomas\Desktop\eye.t ...

  6. python中的单元测试模块unittest

    unittest的属性: 该文以思维导图的形式描述unittest的重要属性. 其中前四个是unittest最核心的三个属性. testcase:测试用例: testsuite:测试套件,多个测试用例 ...

  7. 68.ORM查询条件:date,time,year,week_day等

    1. date: 首先查看数据库中article表的信息,由表中的create_time字段可以看出时间为2020.2.5 打印出查询的结果: <QuerySet []>:但是查询的结果为 ...

  8. 69.ORM查询条件:isnull和regex的使用

    首先查看数据库中的article表的数据: 定义模型的文件models.py中的示例代码如下: from django.db import models class Category(models.M ...

  9. c++ 排序 冒泡 插入 选择 快速

    //冒泡 #include <iostream> using namespace std; void bubbleSort(int* list,int index) { ;i--) //i ...

  10. Java时间格式化年-月-日-时间

    Date d = new Date(); System.out.println(d); //Sat Mar 16 20:58:56 CST 2019 System.out.println(d.toLo ...