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 ...
随机推荐
- Divide Candies CodeForces - 1056B (数学)
Arkady and his friends love playing checkers on an n×nn×n field. The rows and the columns of the fie ...
- HTML基础知识个人总结
[学习的网站是主要是W3school,还加上一些其他搜索学习到的内容,仅在博客做个人整理] 一.标签——尖括号围成的关键词,成对出现. ※使用时必须符合标签嵌套规则 1. (1) <!DOCTY ...
- MVC中使用Hangfire按秒执行任务
更新Hangfire版本到1.7.0,才支持使用按秒循环任务执行 RecurringJob.AddOrUpdate("test",()=>writeLog("每20 ...
- cocos creator入门
前面的话 Cocos Creator 是一个完整的游戏开发解决方案,包括了 cocos2d-x 引擎的 JavaScript 实现,以及快速开发游戏所需要的各种图形界面工具.Cocos Creator ...
- C语言博客作业02--循环结构
1.本章学习总结 1.1 思维导图 1.2 本章学习体会及代码量学习体会 1.2.1 学习体会 经过本周学习,对c循环结构有了深入,无论是单层循环结构还是嵌套循环结构的问题,我都学会有一定的解决能力, ...
- git使用kdiff3合并乱码问题
https://blog.csdn.net/u011008029/article/details/72644515 在合并代码过程中发现kdiff打开的文件都是乱码,解决方案如下: 第一步:点击Set ...
- 快速去水印(win10换图3D工具)
之前抠图都用ps啥的,后来发现win10自带的工具画图3D可以直接扣简单的图案,达到去水印的效果 1.将图片放入软件中 2.使用神奇选择工具,调整大小,框出图标 3.点击下一步,将没选上的或选多的进行 ...
- 「FFT」题单(upd 2019.4.28)
持续更新(last upd 2019.4.28) ZJOI2014 力 [题目链接] 解法 对原式进行转换,然后卷积FFT套上去求解就可以了. 推导过程简洁版: \[F_i=\sum_{j<i} ...
- git学习02 - log查看&版本回退
1.查看更新记录 git log / git log --pretty=oneline D:\learn\git_test>git log commit a915e7b12076673d778 ...
- ubuntu apt update failed to fetch
When I do command sudo apt update, always get belowing errors: Err:1 http://archive.ubuntu.com/ubunt ...