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 ...
随机推荐
- 我如何解决Centos下cannot find a valid baseurl for repo的问题的
刚刚安装完centos,进入命令行模式后,发现所有的命令都不能使用,最后一行显示:Error:Cannot find a valid baseurl for repo:base,如何解决? 在cent ...
- VMware安装Ubuntu配置NAT模式下静态IP,解决访问外网问题
安装好VMware后,打开网络连接可以看到有VMware Network Adapter VMnet1和VMware Network Adapter VMnet8两个网络适配器,VMnet1是针对桥接 ...
- python入门:while 循环的基本用法
#!/usr/bin/env python # -*- coding:utf-8 -*- #while 循环的作用 import time while True: ") time.sleep ...
- 关于PHP连接池扩展php-cp遇到的那些坑
php-cp是国内大神写的php第三方扩展,具体就不用多说了,细读https://github.com/swoole/php-cp,下面来说说今天安装方法. 环境:CentOS7.2.1511 由于本 ...
- ccf 201712-3 Crontab(Python实现)
一.原题 问题描述 试题编号: 201712-3 试题名称: Crontab 时间限制: 10.0s 内存限制: 256.0MB 问题描述: 样例输入 3 201711170032 201711222 ...
- Python基础(二)——常用内置函数
1. 常用内置函数 (1)isinstance(object, classinfo) 用于判断一个对象是否为某一类型. object 是实例对象 classinfo 是基本类型如 int, floa ...
- 【LeetCode】Linked List Cycle II(环形链表 II)
这是LeetCode里的第142道题. 题目要求: 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? ...
- python字符串、列表和字典的说明
python字符串.列表和字典的说明 字符串.列表.字典 字符串的作用存储一段数据信息.例如 info = '我爱北京天安门' ,在调取的时候可以直接调取,灵活方便,print(info) 就可以把刚 ...
- 【15】ES6 for Humans: The Latest Standard of JavaScript: ES2015 and Beyond
[15]ES6 for Humans 共148页: 目前看到:已经全部阅读. 亚马逊地址: 魔芋:总结: 我先看的是阮一峰的在线书籍.这本书的内容很多都与之重复的. 居然卖¥463.也是没谁了. ...
- alidoing --使用JS实现多语言框架、喜欢的请进、、瓦特平台!
大家好! 多语言实现的案例:http://alidoing.com/或者http://www.alidoing.com/ 图:切换语言界面 JS代码实现: 1.首先新建一个对象langobj,当然对象 ...