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 ...
随机推荐
- MHA
MHA 1. MHA简介 1.1 MHA工作原理总结为如下 1.2 MHA工具包介绍 2. 部署MHA 2.1 环境介绍 2.2 一主两从复制搭建 2.3 配置互信 2.4 下载MHA 2.5 安装M ...
- Linux基础学习-使用vsftpd服务传输文件
使用vsftpd服务传输文件 1 安装vsftpd [root@qdlinux ~]# yum install vsftpd Loaded plugins: product-id, search-di ...
- pandas时间数据的集成处理
工作中遇到的一个问题: 统计各地区新能源汽车的充电时长 数据来源是北理新源的单日全球的运行数据. 这里仅统计北上广重庆四个地区的 数据处理的代码就省略了 需要整理好的是4个dataframe(数据已保 ...
- org.apache.catalina.webresources.Cache.backgroundProcess The background cache eviction process was unable to free [10] percent of the cache for Context [/filestore] - consider increasing the maximum s
需要耐心啊,太急于求成,希望直接就得到解决方法了...以至于正确方法都已经出现了,我却没有耐心看下去,所以反而又耽误了不少时间.... 项目加载100+张图片,还有一个小的MP4,所以console警 ...
- viewController备注
1.按结构可以对iOS的所有ViewController分成两类: 1).主要用于展示内容的ViewController,这种ViewController主要用于为用户展示内容,并与用户交互,如UIT ...
- selenium2中TestNG相关解释
testNg官网:http://testng.org/doc/documentation-main.html 新建testNG class的时候,同时也新建了一个TestNG.xml的文件. 此xml ...
- LayaAir环境TypeScript
1.1 Node.js下载 TypeScript开发HTML5需要Node.js环境,如果没有安装过的,请前往官方下载(建议LTS版本),如图1,URL地址为:https://nodejs.org ...
- UVa 1630 区间DP Folding
一个字符串如果能简写,要么是重复多次,按题中的要求简写:要么是左右两个部分分别简写后再拼起来. dp(i, j)表示字串(i, j)所能被简写的最短的字符串. 判断一个字符串是否为周期串以及求出它的周 ...
- POJ - 1321 深度优先搜索入门
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> us ...
- org.hibernate.AnnotationException: No identifier specified for entity: com.example1.demo1.Entity.User错误
最近在公司带人,他们问我的问题在这里也顺便总结下. 此项目为SpringDataJpa项目. 出现的错误如下: Caused by: org.hibernate.AnnotationException ...