链接:https://nanti.jisuanke.com/t/38229

题意:

给一棵树,多次查询,每次查询两点之间权值<=k的边个数

题解:

离线询问,树链剖分后bit维护有贡献的位置即可

#include <bits/stdc++.h>
#define rep(ii,a,b) for(int ii=a;ii<=b;++ii)
using namespace std;
const int maxn=2e5+10,maxm=2e6+10;
int n,m;
#define tpyeinput int
inline bool read(tpyeinput &num){int flag=1,ch=getchar();if(ch==EOF) return false;num=0;while(ch<'0'||ch>'9'){if(ch=='-') flag=-1;ch=getchar();}while(ch>='0'&&ch<='9'){num=num*10+ch-'0',ch=getchar();}num*=flag;return true;}
inline bool read(tpyeinput &num1,tpyeinput &num2){return read(num1)&&read(num2);}
inline bool read(tpyeinput &num1,tpyeinput &num2,tpyeinput &num3){return read(num1)&&read(num2)&&read(num3);}
inline bool read(tpyeinput &num1,tpyeinput &num2,tpyeinput &num3,tpyeinput &num4){return read(num1)&&read(num2)&&read(num3)&&read(num4);}
struct node{int from,to,cost,next;}e[maxn<<1],e2[maxn];
int head[maxn],nume,cnt2;
inline void add(int a,int b,int c){e[++nume]={a,b,c,head[a]};head[a]=nume;}
int fa[maxn],sz[maxn],top[maxn],remp[maxn],ans[maxn];
int son[maxn],in[maxn],cnt,deep[maxn];
void dfs1(int now,int pre,int d){
deep[now]=d;sz[now]=1;fa[now]=pre;
for(int i=head[now];i;i=e[i].next){
if(e[i].to==pre) continue;
dfs1(e[i].to,now,d+1);
sz[now]+=sz[e[i].to];
if(sz[son[now]]<sz[e[i].to]) son[now]=e[i].to;
}
}
void dfs2(int now,int pre,int st){
top[now]=st;in[now]=++cnt;remp[cnt]=now;
if(son[now]) dfs2(son[now],now,st);
for(int i=head[now];i;i=e[i].next)
if(e[i].to!=pre&&e[i].to!=son[now]) dfs2(e[i].to,now,e[i].to);
}
struct data{int l,r,val,id;}ask[maxn];
int cmp(node a,node b){return a.cost<b.cost;}
int cmp2(data a,data b){return a.val<b.val;}
struct bit{
int node[maxn];
inline int lb(int x) {return x&(-x);}
void update(int pos){for(int i=pos;i<=n;i+=lb(i))node[i]++;}
int ask(int pos){int sum=0; for(int i=pos;i;i-=lb(i))sum+=node[i];return sum;}
inline int query(int l,int r){return ask(r)-ask(l-1);}
}tree;
int query(int a,int b){
int sum=0;
while(top[a]!=top[b]){
if(deep[top[a]]<deep[top[b]]) swap(a,b);
sum+=tree.query(in[top[a]],in[a]);
a=fa[top[a]];
}
if(a==b)return sum;
if(deep[a]>deep[b]) swap(a,b);
sum+=tree.query(in[son[a]],in[b]);
return sum;
}
int main() {
read(n,m);
rep(i,2,n) {int a,b,c;read(a,b,c);add(a,b,c);e2[++cnt2]=e[nume];add(b,a,c);}
sort(e2+1,e2+1+cnt2,cmp);
rep(i,1,m) {ask[i].id=i;read(ask[i].l,ask[i].r,ask[i].val);}
sort(ask+1,ask+1+m,cmp2);
dfs1(1,1,0);dfs2(1,1,1);
int pos=1;
rep(i,1,m){
while(pos<=cnt2&&e2[pos].cost<=ask[i].val) {
int x=e2[pos].to;
if(deep[e2[pos].from]>deep[e2[pos].to]) x=e2[pos].from;
tree.update(in[x]); pos++;
}
ans[ask[i].id]=query(ask[i].l,ask[i].r);
}
rep(i,1,m) printf("%d\n",ans[i]);
}

2019 icpc南昌全国邀请赛-网络选拔赛J题 树链剖分+离线询问的更多相关文章

  1. 焦作网络赛E-JiuYuanWantstoEat【树链剖分】【线段树】

    You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, she wil ...

  2. 2018.9 ECNU ICPC/CCPC Trial Round #2 Query On Tree (树链剖分+线段树维护)

    传送门:https://acm.ecnu.edu.cn/contest/105/problem/Q/ 一棵树,支持两种操作:给一条路径上的节点加上一个等差数列;求两点路径上节点和. 很明显,熟练剖分. ...

  3. 2019南昌邀请赛网络赛:J distance on the tree

    1000ms 262144K   DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...

  4. 2019 ICPC南昌邀请赛网络赛比赛过程及题解

    解题过程 中午吃饭比较晚,到机房lfw开始发各队的账号密码,byf开始读D题,shl电脑卡的要死,启动中...然后听到谁说A题过了好多,然后shl让blf读A题,A题blf一下就A了.然后lfw读完M ...

  5. 2019 ICPC 南昌网络赛

    2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...

  6. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  7. 树链剖分的一种妙用与一类树链修改单点查询问题的时间复杂度优化——2018ACM陕西邀请赛J题

    题目描述 有一棵树,每个结点有一个灯(初始均是关着的).每个灯能对该位置和相邻结点贡献1的亮度.现有两种操作: (1)将一条链上的灯状态翻转,开变关.关变开: (2)查询一个结点的亮度. 数据规模:\ ...

  8. jzoj5987. 【WC2019模拟2019.1.4】仙人掌毒题 (树链剖分+概率期望+容斥)

    题面 题解 又一道全场切的题目我连题目都没看懂--细节真多-- 先考虑怎么维护仙人掌.在线可以用LCT,或者像我代码里先离线,并按时间求出一棵最小生成树(或者一个森林),然后树链剖分.如果一条边不是生 ...

  9. acm 2015北京网络赛 F Couple Trees 树链剖分+主席树

    Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...

随机推荐

  1. (light oj 1102) Problem Makes Problem (组合数 + 乘法逆元)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1102 As I am fond of making easier problems, ...

  2. python总结 + 部署简单项目 到生产

    -> filter过滤:list(filter(lambda x: x[0].find('tmp') == -1, table_temp_r)) -> 自定义map:def map_for ...

  3. [转帖]御界预警:3700余台SQL服务器被入侵挖矿 或导致严重信息泄露事件

    御界预警:3700余台SQL服务器被入侵挖矿 或导致严重信息泄露事件 https://zhuanlan.kanxue.com/article-8292.htm sqlserver的弱密码破解和提权攻击 ...

  4. 【zabbix教程系列】五、邮件报警设置(脚本方式)

    本方式是使用外部邮箱账号发送报警邮件到指定邮箱. 好处是:此邮箱账号既能发送邮件,也能接收邮件,而且避免被当做垃圾邮件. 一.zabbix-server端安装mailx服务 [root@ltt01 ~ ...

  5. qsort.c源码

    /* 版权所有(C) 1991-2019 自由软件资金会. 该文件属于是GUN C语言函数库,由Douglas C. Schmidt(schmidt@ics.uci.edu)所写. GUN C语言函数 ...

  6. git总结二、关于分支上——好好认识下分支是怎么回事

    同样需要先来明确两件事: HEAD指针指向的是当前分支 分支(master, dev)指向的是最新的提交 一开始,git 中只有一个master分支,严格来讲,HEAD不是指向提交而是指向master ...

  7. Transformer

    参考资料: [ERT大火却不懂Transformer?读这一篇就够了] https://zhuanlan.zhihu.com/p/54356280 (中文版) http://jalammar.gith ...

  8. synchronized和lock有什么区别?

    一.原始构成 synchronized是关键字属于JVM层面,monitorenter(底层是通过monitor对象来完成,其实wait/notify等方法也依赖monitor对象只有在同步代码块和同 ...

  9. Ubuntu 16.04 安装opencv3.4.5/cuda/caffe并使用jni笔记

    因操作失误,误卸开发机NVIDIA显卡驱动,先更新操作日志如下: 1>NVIDIA驱动重装 1.卸载系统里的Nvidia残余 sudo apt-get purge nvidia* 2.把显卡驱动 ...

  10. 最小生成树——Prim算法和Kruskal算法

    洛谷P3366 最小生成树板子题 这篇博客介绍两个算法:Prim算法和Kruskal算法,两个算法各有优劣 一般来说当图比较稀疏的时候,Kruskal算法比较快 而当图很密集,Prim算法就大显身手了 ...