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 100001 using namespace std; struct TreeNodeType {
LL dis,lc,rc;
};
struct TreeNodeType tree[maxn*]; struct EdgeType {
LL to,next;
};
struct EdgeType edge[maxn<<]; 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=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
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=;
if(l==r) return ;
LL mid=(l+r)>>;
tree[now].lc=++tot;
tree_build(tot,l,mid);
tree[now].rc=++tot;
tree_build(tot,mid+,r);
} void tree_add(LL pre,LL now,LL to,LL l,LL r)
{
tree[now].dis=tree[pre].dis+;
if(l==r) return ;
LL mid=(l+r)>>;
if(to>mid)
{
tree[now].lc=tree[pre].lc;
tree[now].rc=++tot;
tree_add(tree[pre].rc,tree[now].rc,to,mid+,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]+;
hash_[now]=lower_bound(hash+,hash+size_+,hash_[now])-hash;
root[now]=++tot;
tree_add(root[pre],root[now],hash_[now],,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=;
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==) 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)>>;
if(k>dis)
{
k-=dis;
l=mid+;
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=;i<=n;i++)
{
read_int(hash[i]);
hash_[i]=hash[i];
}
LL from,to;
for(LL i=;i<n;i++)
{
read_int(from),read_int(to);
edge_add(from,to);
edge_add(to,from);
}
sort(hash+,hash+n+);
size_=unique(hash+,hash+n+)-hash-;
root[]=++tot;
tree_build(root[],,size_);
cnt=,search(,);
cnt=,search_(,);
LL K,last=;
for(LL i=;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]],,size_,K)];
if(i!=m) printf("%lld\n",last);
else printf("%lld",last);
}
return ;
}
AC日记——Count on a tree bzoj 2588的更多相关文章
- AC日记——Count on a tree II spoj
Count on a tree II 思路: 树上莫队: 先分块,然后,就好办了: 来,上代码: #include <cmath> #include <cstdio> #inc ...
- AC日记——算术天才⑨与等差数列 bzoj 4373
4373 思路: 判断一个数列是否是等差数列: 1,最大值减去最小值==(区间个数-1)*k: 2,gcd==k: 3,不能有重复(不会这判断这一条,但是数据水就过了): 来,上代码: #includ ...
- AC日记——[HNOI2008]玩具装箱toy bzoj 1010
1010 思路: 斜率优化DP: 跪烂大佬 代码: #include <bits/stdc++.h> using namespace std; #define maxn 50005 #de ...
- AC日记——[Sdoi2008]Cave 洞穴勘测 bzoj 2049
2049 思路: lct模板: 代码: #include <cstdio> #include <cstring> #include <iostream> #incl ...
- AC日记——[HNOI2012]永无乡 bzoj 2733
2733 思路: 启发式合并splay(n*log^2n): 来,上代码: #include <cstdio> #include <cstring> #include < ...
- AC日记——[HNOI2008]水平可见直线 bzoj 1007
1007 思路: 维护一个下凸壳: 用单调栈来维护这玩意儿: 先将斜率排序: 然后判断栈顶元素和当前元素的交点x是否小于栈顶元素和栈顶上一个元素的交点x: 注意: 人神共愤的精度问题和输出空格问题: ...
- AC日记——小M的作物 bzoj 3438
3438 思路: 最小割(完全不懂看的题解): s向每个作物连边,s-x ai,x-t bi: 然后s向作物集合连边,cia: 作物集合拆点向t连边,cib: 作物集合第一个点向作物连边INF: 作物 ...
- 【BZOJ】【2588】COT(Count On a Tree)
可持久化线段树 maya……树么……转化成序列……所以就写了个树链剖分……然后每个点保存的是从它到根的可持久化线段树. 然后就像序列一样查询……注意是多个左端点和多个右端点,处理方法类似BZOJ 19 ...
- 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 ...
随机推荐
- php过滤html标签
<?php function kill_html($str){ //清除HTML标签 $st=-1; //开始 $et=-1; //结束 $stmp=array(); $stmp[]=" ...
- 搜索引擎elasticsearch常用指令演示
目录 交互方式 常用操作示例 添加文档 删除文档 修改文档 查询 简单查询 高级多条件查询 交互方式 操作ES有3种方式: kibana控制台(Dev Tools) Http + json api接口 ...
- 笔记-python-实用-程序运算时间计算
方法1 import datetime starttime = datetime.datetime.now() #long running endtime = datetime.datetime.no ...
- 卸载firefox多余的搜索引擎
火狐默认了几个搜索引擎,百度,bing,yahoo等.搜一些技术方面的东西的时候,google返回的结果比这些要准确有用.所以想卸载掉那些不用的. 具体操作: 点击搜索栏,左侧搜索引擎图票右下角的倒三 ...
- CSS 工程化 小结
CSS 工程化 组成:1.组织 (代码目录)2.优化(那种实现方式更好) 3.构建(代码完成后需要经过哪些处理步骤) 4.维护 常见问题 1.如何解决 CSS 模块化问题 1.Less Sass 等C ...
- 和为s的两个数字 【微软面试100题 第十四题】
题目要求: 输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s.如果有多对数字的和等于s,输出任意一对即可. 例如输入数组{1,2,4,7,11,15}和数字15.由于4+1 ...
- shutil——高级的 文件、文件夹、压缩包 处理模块
高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])复制文件内容(不包含元数据)从类文件对象src到类文件对dst.可选参数leng ...
- Zookeeper在windows环境下安装
1.已安装JDK并配置好了环境变量 2.下载Zookeeper,在清华大学镜像下载,选择合适版本 https://mirrors.tuna.tsinghua.edu.cn/apache/zookee ...
- [python工具][1]sublime安装与配置
http://www.cnblogs.com/wind128/p/4409422.html 1 官网下载版本 http://www.sublimetext.com/3 选择 Windows - al ...
- TOJ2680: 最大矩阵连乘次数
2680: 最大矩阵连乘次数 Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Submit: 144 ...