2019 icpc南昌全国邀请赛-网络选拔赛J题 树链剖分+离线询问
链接: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题 树链剖分+离线询问的更多相关文章
- 焦作网络赛E-JiuYuanWantstoEat【树链剖分】【线段树】
You ye Jiu yuan is the daughter of the Great GOD Emancipator. And when she becomes an adult, she wil ...
- 2018.9 ECNU ICPC/CCPC Trial Round #2 Query On Tree (树链剖分+线段树维护)
传送门:https://acm.ecnu.edu.cn/contest/105/problem/Q/ 一棵树,支持两种操作:给一条路径上的节点加上一个等差数列;求两点路径上节点和. 很明显,熟练剖分. ...
- 2019南昌邀请赛网络赛:J distance on the tree
1000ms 262144K DSM(Data Structure Master) once learned about tree when he was preparing for NOIP(N ...
- 2019 ICPC南昌邀请赛网络赛比赛过程及题解
解题过程 中午吃饭比较晚,到机房lfw开始发各队的账号密码,byf开始读D题,shl电脑卡的要死,启动中...然后听到谁说A题过了好多,然后shl让blf读A题,A题blf一下就A了.然后lfw读完M ...
- 2019 ICPC 南昌网络赛
2019 ICPC 南昌网络赛 比赛时间:2019.9.8 比赛链接:The 2019 Asia Nanchang First Round Online Programming Contest 总结 ...
- 计蒜客 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 ...
- 树链剖分的一种妙用与一类树链修改单点查询问题的时间复杂度优化——2018ACM陕西邀请赛J题
题目描述 有一棵树,每个结点有一个灯(初始均是关着的).每个灯能对该位置和相邻结点贡献1的亮度.现有两种操作: (1)将一条链上的灯状态翻转,开变关.关变开: (2)查询一个结点的亮度. 数据规模:\ ...
- jzoj5987. 【WC2019模拟2019.1.4】仙人掌毒题 (树链剖分+概率期望+容斥)
题面 题解 又一道全场切的题目我连题目都没看懂--细节真多-- 先考虑怎么维护仙人掌.在线可以用LCT,或者像我代码里先离线,并按时间求出一棵最小生成树(或者一个森林),然后树链剖分.如果一条边不是生 ...
- acm 2015北京网络赛 F Couple Trees 树链剖分+主席树
Couple Trees Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://hihocoder.com/problemset/problem/123 ...
随机推荐
- 不能完整读取txt文件问题
txt文件内容 5 1.3 0.4 3.4 -1.7 16.7 0.89 14.17 4.8 1.34 0.42 3.36 -2 16.2 0.9 14.8 4.9 1.30 0.37 3.51 -1 ...
- Cnario Player 接入视频采集卡采集外部音视频信号测试
测试产品 型号: TC-D56N1-30P采集卡 参数: 1* HDMI 1.4输入, PCIe 接口为PCI-Express x4(Gen2), 最高支持4096x2160@30Hz, 支持1920 ...
- Django后端向前端直接传html语言防止转义的方法(2种)
Django后端向前端直接传html语言防止转义的方法(2种) 目的,为了让前端对后端传输的这种方式不转义 1.使用mark_safe() from django.utils.safestring i ...
- 失去焦点布局在ios12-微信6.7.0版本以上不回滚的解决方案
微信页面input获取焦点,布局上移,失去焦点布局在ios12-微信6.7.0版本以上不回滚的解决方案: setTimeout(function(){ let scrollHeight = docum ...
- LODOP打印用JS获取的当前日期
该文详细一步步解释JS获取当前时间的方法,新手小白也看到懂,最后是实际的获取当前年月份的方法.JS中的Date()对象,包含很多当前系统时间的方法,首先建立一个Date()对象,这里取名为date,然 ...
- python读取导出数据
1,python读取csv的某一列 import pandas as pd data1 = pd.read_csv('cotton.csv', usecols=[0, 1], encoding='ut ...
- 【长期更新】迈向现代化的 .Net 配置指北
1. 欢呼 .NET Standard 时代 我现在已不大提 .Net Core,对于我来说,未来的开发将是基于 .NET Standard,不仅仅是 面向未来 ,也是 面向过去:不只是 .Net C ...
- windows下安装oracle11g测试是否成功与监听器问题和网页控制台登录
测试步骤1:请执行操作系统级的命令:tnsping orcl 测试步骤 2:请执行操作系统级的命令:sqlplus system/password@orcl 安装完oracle后要启动oracle服务 ...
- 主成分分析_PCA解释
粘贴自:http://blog.codinglabs.org/articles/pca-tutorial.html 数据的向量表示及降维问题 向量的表示及基变换 协方差矩阵及优化目标 协方差矩阵对角化 ...
- MFC:编辑区 Edit 的属性及使用
Edit Control 编辑控件是 MFC 中使用较多的控件之一 1. Edit 的属性 Acccept Files -> True 控件接受拖放文件 Multiline -> True ...