bzoj 2588 : Spoj 10628. Count on a tree
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<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define lson l,m,rt<<1
- #define rson m+1,r,rt<<1|1
- #define mid int m = (l + r) >> 1
- const int M = 2e5 + ;
- int p[M][],dep[M],head[M],sum[M*],ls[M*],rs[M*],root[M*];
- int cnt1,n,idx,cnt,f[M];
- struct node {
- int to,next;
- }e[M];
- void add(int u,int v){
- e[++cnt1].to=v;e[cnt1].next=head[u];head[u]=cnt1;
- e[++cnt1].to=u;e[cnt1].next=head[v];head[v]=cnt1;
- }
- int lca(int a,int b){
- if(dep[a] > dep[b]) swap(a,b);
- int h = dep[b] - dep[a];
- for(int i = ;(<<i)<=h;i++){
- if((<<i)&h) b = p[b][i];
- }
- if(a!=b){
- for(int i = ;i >= ;i --){
- if(p[a][i]!=p[b][i]){
- a = p[a][i]; b = p[b][i];
- }
- }
- a = p[a][];
- }
- return a;
- }
- void build(int l,int r,int &rt){
- rt = ++idx;
- sum[rt] = ;
- if(l == r) return;
- mid;
- build(l,m,ls[rt]);
- build(m+,r,rs[rt]);
- }
- void update(int p,int l,int r,int old,int &rt){
- rt = ++idx;
- ls[rt] = ls[old]; rs[rt] = rs[old]; sum[rt] = sum[old] + ;
- if(l == r) return ;
- mid;
- if(p <= m) update(p,l,m,ls[old],ls[rt]);
- else update(p,m+,r,rs[old],rs[rt]);
- }
- int query(int a,int b,int lc,int cl,int l,int r,int k){
- if(l == r) return l;
- mid;
- int cnt = sum[ls[a]] + sum[ls[b]] - sum[ls[lc]] - sum[ls[cl]];
- if(k <= cnt)
- return query(ls[a],ls[b],ls[lc],ls[cl],l,m,k);
- else
- return query(rs[a],rs[b],rs[lc],rs[cl],m+,r,k-cnt);
- }
- int a[M],b[M];
- void dfs(int u,int fa){
- f[u] = fa;
- update(a[u],,cnt,root[fa],root[u]);
- for(int i = head[u];i;i = e[i].next){
- int v = e[i].to;
- if(v == fa) continue;
- p[v][] = u;
- dep[v] = dep[u] + ;
- dfs(v,u);
- }
- }
- void init()
- {
- cnt1 = ; idx = ;
- memset(head,,sizeof(head));
- memset(dep,,sizeof(dep));
- memset(p,,sizeof(p));
- memset(f,,sizeof(f));
- dep[] = ;
- }
- int main()
- {
- int m;
- while(scanf("%d%d",&n,&m)!=EOF){
- init();
- for(int i = ; i <= n;i ++){
- scanf("%d",&a[i]);
- b[i] = a[i];
- }
- int l,r,c;
- sort(b+,b+n+);
- cnt = unique(b+,b++n)-b-;
- for(int i = ;i <= n;i ++)
- a[i] = lower_bound(b+,b+cnt+,a[i]) - b;
- for(int i = ;i <= n-;i ++){
- scanf("%d%d",&l,&r);
- add(l,r);
- }
- build(,cnt,root[]);
- dfs(,);
- for(int j = ;(<<j)<=n;j++)
- for(int i = ;i <= n;i++)
- p[i][j] = p[p[i][j-]][j-];
- int num = ;
- for(int i = ;i <= m;i ++){
- scanf("%d%d%d",&l,&r,&c);
- l = l^num;
- int lc = lca(l,r);
- int id = query(root[l],root[r],root[lc],root[f[lc]],,cnt,c);
- num = b[id];
- printf("%d\n",b[id]);
- }
- }
- return ;
- }
bzoj 2588 : Spoj 10628. Count on a tree的更多相关文章
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- 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
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- BZOJ 2588: Spoj 10628. Count on a tree( LCA + 主席树 )
Orz..跑得还挺快的#10 自从会树链剖分后LCA就没写过倍增了... 这道题用可持久化线段树..点x的线段树表示ROOT到x的这条路径上的权值线段树 ----------------------- ...
- Bzoj 2588 Spoj 10628. Count on a tree(树链剖分LCA+主席树)
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MB Description 给定一棵N个节点的树,每个点 ...
- bzoj 2588 Spoj 10628. Count on a tree (可持久化线段树)
Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 7669 Solved: 1894[Submi ...
- 主席树 || 可持久化线段树 || 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) ...
- BZOJ 2588: Spoj 10628. Count on a tree 主席树+lca
分析:树上第k小,然后我想说的是主席树并不局限于线性表 详细分析请看http://www.cnblogs.com/rausen/p/4006116.html,讲的很好, 然后因为这个熟悉了主席树,真是 ...
- ●BZOJ 2588 Spoj 10628. Count on a tree
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2588 题解: 主席树,在线,(求LCA)感觉主席树真的好厉害...在原树上建主席树.即对于原 ...
- 洛谷 2633 BZOJ 2588 Spoj 10628. Count on a tree
[题解] 蜜汁强制在线... 每个点开一个从它到根的可持久化权值线段树.查询的时候利用差分的思想在树上左右横跳就好了. #include<cstdio> #include<algor ...
随机推荐
- python三:循环语句练习--小白博客
# 打印0-10去掉5 count = - : count += : continue print(count) # 打印0-10的偶数 count = : print(count) count+= ...
- Educational Codeforces Round 52 (Rated for Div. 2) -C
#include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...
- Python Revisited Day 02 (数据类型)
目录 Python 关键字 整数 整数转换函数 整数位逻辑操作符 浮点类型 math模块函数与常量 复数 精确的十进制数字 decimal 字符串 str.format() 格式规约 Python 关 ...
- 【学习总结】GirlsInAI ML-diary day-4:变量/Variable
[学习总结]GirlsInAI ML-diary 总 原博github链接-day4 变量/Variable 变量是计算机编程中一个很基础的概念,在计算机程序中,variables are reser ...
- jQuery学习(监听DOM加载)
jQuery的extend方法 function njQuery() { } /* njQuery.extend = function (obj) { // 此时此刻的this就是njQuery这个类 ...
- laravel log改为时间格式
1 providers新建文件 LogRotateServiceProvider.php <?php namespace App\Providers; use Monolog\Formatter ...
- 5 Http请求中文乱码处理
java 乱码分很多种,这里主要研究解决http请求中出现乱码的情况. http请求出现中文乱码的主要原因:发送方与接收方编码不一致,服务器默认支持的编码与web应用不一致,如:tomcat 是国外程 ...
- Oracle可视化工具PL/SQL Developer的安装与配置
安装程序: 安装目录不能有中文和空格,否则无法进行远程连接. 推荐使用 D:\PLSQLDeveloper 为安装目录 破解PLSQLDeveloper 使用工具 PLSQL Developer10. ...
- mybatis二级缓存详解
1 二级缓存简介 二级缓存是在多个SqlSession在同一个Mapper文件中共享的缓存,它是Mapper级别的,其作用域是Mapper文件中的namespace,默认是不开启的.看如下图: 1. ...
- vue 项目使用 webpack 构建自动获取电脑ip地址
1.开发 H5 时移动端,经常会使用真机进行调试本地环境.webpack 配置服务器好多脚手架写的都是固定的,而在团队开发中需要每人配置自己的本机 ip 进行开发,每次开启开发环境的都需要修改,并且还 ...