AC日记——Count on a tree bzoj 2588
Description
Input
Output
M行,表示每个询问的答案。最后一个询问不输出换行符
Sample Input
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2
Sample Output
8
9
105
7
HINT
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>#define LL long long #define maxn 100001using namespace std;struct TreeNodeType {LL dis,lc,rc; }; struct TreeNodeType tree[maxn*45];struct EdgeType {LL to,next; }; struct EdgeType edge[maxn<<1];LL if_z,n,m,hash[maxn],hash_[maxn],cnt,head[maxn]; LL size_,tot,root[maxn],deep[maxn],f[maxn],size[maxn]; LL belong[maxn];char Cget;inline void read_int(LL &now) {now=0,if_z=1,Cget=getchar();while(Cget>'9'||Cget<'0'){if(Cget=='-') if_z=-1;Cget=getchar();}while(Cget>='0'&&Cget<='9'){now=now*10+Cget-'0';Cget=getchar();}now*=if_z; }inline void edge_add(LL from,LL to) {cnt++;edge[cnt].to=to;edge[cnt].next=head[from];head[from]=cnt; }void tree_build(LL now,LL l,LL r) {tree[now].dis=0;if(l==r) return ;LL mid=(l+r)>>1;tree[now].lc=++tot;tree_build(tot,l,mid);tree[now].rc=++tot;tree_build(tot,mid+1,r); }void tree_add(LL pre,LL now,LL to,LL l,LL r) {tree[now].dis=tree[pre].dis+1;if(l==r) return ;LL mid=(l+r)>>1;if(to>mid){tree[now].lc=tree[pre].lc;tree[now].rc=++tot;tree_add(tree[pre].rc,tree[now].rc,to,mid+1,r);}else{tree[now].rc=tree[pre].rc;tree[now].lc=++tot;tree_add(tree[pre].lc,tree[now].lc,to,l,mid);} }void search(LL now,LL pre) {LL pos=cnt++;f[now]=pre;deep[now]=deep[pre]+1;hash_[now]=lower_bound(hash+1,hash+size_+1,hash_[now])-hash;root[now]=++tot;tree_add(root[pre],root[now],hash_[now],1,size_);for(LL i=head[now];i;i=edge[i].next){if(edge[i].to==pre) continue;search(edge[i].to,now);}size[now]=cnt-pos; }void search_(LL now,LL chain) {LL pos=0;belong[now]=chain;for(LL i=head[now];i;i=edge[i].next){if(edge[i].to==f[now]) continue;if(size[edge[i].to]>size[pos]) pos=edge[i].to;}if(pos==0) return ;search_(pos,chain);for(LL i=head[now];i;i=edge[i].next){if(pos==edge[i].to||edge[i].to==f[now]) continue;search_(edge[i].to,edge[i].to);} }inline LL tree_lca(LL x,LL y) {while(belong[x]!=belong[y]){if(deep[belong[x]]<deep[belong[y]]) swap(x,y);x=f[belong[x]];}if(deep[x]<deep[y]) return x;else return y; }inline LL tree_query(LL x,LL y,LL lca,LL flca,LL l,LL r,LL k) {LL dis,mid;while(l!=r){dis=tree[tree[x].lc].dis+tree[tree[y].lc].dis-tree[tree[lca].lc].dis-tree[tree[flca].lc].dis;mid=(l+r)>>1;if(k>dis){k-=dis;l=mid+1;lca=tree[lca].rc;flca=tree[flca].rc;x=tree[x].rc,y=tree[y].rc;}else{r=mid;lca=tree[lca].lc;flca=tree[flca].lc;x=tree[x].lc,y=tree[y].lc;}}return l; }int main() {read_int(n),read_int(m);for(LL i=1;i<=n;i++){read_int(hash[i]);hash_[i]=hash[i];}LL from,to;for(LL i=1;i<n;i++){read_int(from),read_int(to);edge_add(from,to);edge_add(to,from);}sort(hash+1,hash+n+1);size_=unique(hash+1,hash+n+1)-hash-1;root[0]=++tot;tree_build(root[0],1,size_);cnt=0,search(1,0);cnt=0,search_(1,1);LL K,last=0;for(LL i=1;i<=m;i++){read_int(from),read_int(to),read_int(K);from=from^last;LL lca=tree_lca(from,to);last=hash[tree_query(root[from],root[to],root[lca],root[f[lca]],1,size_,K)];if(i!=m) printf("%lld\n",last);else printf("%lld",last);}return 0; }
转载于:https://www.cnblogs.com/IUUUUUUUskyyy/p/6382286.html
AC日记——Count on a tree bzoj 2588相关推荐
- AC日记——[HNOI2008]玩具装箱toy bzoj 1010
1010 思路: 斜率优化DP: 跪烂大佬 代码: #include <bits/stdc++.h> using namespace std; #define maxn 50005 #de ...
- BZOJ 2588 Spoj 10628 Count on a tree | 树上主席树
BZOJ 2588 Count on a tree 题面 求树上两点之间路径上第k大的点权. 题解 一开始看到这道题觉得是树剖,然后又听说是主席树,然后以为是主席树+树剖,差点没吓死-- 然后发现,如 ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Submit: 7669 Solved: 1894 [Sub ...
- BZOJ 2588: Spoj 10628. Count on a tree 树上跑主席树
2588: Spoj 10628. Count on a tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/J ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- 主席树 || 可持久化线段树 || LCA || BZOJ 2588: Spoj 10628. Count on a tree || Luogu P2633 Count on a tree...
题面: Count on a tree 题解: 主席树维护每个节点到根节点的权值出现次数,大体和主席树典型做法差不多,对于询问(X,Y),答案要计算ans(X)+ans(Y)-ans(LCA(X,Y) ...
- Count on a tree
bzoj 2588: Spoj 10628. Count on a tree http://www.lydsy.com/JudgeOnline/problem.php?id=2588 Descrip ...
- BZOJ2588 Count on a tree DFS序+LCA+值域主席树
Count on a tree 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个询问的答 ...
- BZOJ2588 Count on a tree 【树上主席树】
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Submit: 7577 Solved: 185 ...
最新文章
- 脉冲神经网络在目标检测的首次尝试,性能堪比CNN | AAAI 2020
- 换64位Win7了,感觉还行
- java byte 判断相等_深入理解Java装箱与拆箱
- AUTOSAR从入门到精通100讲(十)-DoIP协议介绍
- Hyper-V Server联机调整虚拟硬盘大小
- [BZOJ3000] Big Number (Stirling公式)
- Servlet和JSP的异同。
- 知识图谱之社交网络分析(SNA)之python处理
- Python 去除白色背景
- flyaway mysql_keycloak搭配mysql
- OSPF -LSA的类型及特点
- Laravel中pluck的使用——返回指定的字段值信息列表
- html 样式大全,2017最新css样式大全
- 记录清理服务器挖矿木马warmup的命令
- 在线文档查看器:Gleamtech Document Viewer 6.6.1
- FastDB 很难得的一篇分析
- Assignment写作怎么正确有效地阅读?
- Python匿名函数和高阶函数
- 微信抢红包的方案_微信抢红包方法图文详解
- 制作价目表的程序和软件
热门文章
- vue 请求时方法执行顺序问题,异步请求无法同时处理多个接口,使用同步顺序执行:async/await使用
- mysql分窗函数_频谱分析中如何选择合适的窗函数
- WordPress 主题教程 #3:开始 Index.php
- 对 app_offline.htm 的几点错误认识
- jQuery - Bootstrap 3 and Youtube in Modal
- Linux Shell高级技巧(三)
- xml 中插入html代码
- 层次分析法(AHP)——matlab代码实现
- 数据建模-聚类分析-K-Means算法
- Linux——VIM开发C++自动补全(STL、括号)和开发环境配置