[CF226E]Noble Knight's Path
[CF226E]Noble Knight's Path
题目大意:
一棵\(n(n\le10^5)\)个结点的树,初始时所有结点都是白色。\(m(m\le10^5)\)次操作,操作包含以下两种:
- 将点\(u\)涂黑。
- 询问从\(u\)到\(v\)的路径上,只考虑\(y\)以后的操作,第\(k\)个白色的结点(不包含\(u\)和\(v\))。
思路:
树链剖分+主席树。
源代码:
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<forward_list>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
constexpr int N=1e5+1,logN=17;
std::forward_list<int> e[N];
int dep[N],par[N],size[N],son[N],top[N],dfn[N],id[N];
void dfs1(const int &x) {
size[x]=1;
dep[x]=dep[par[x]]+1;
for(auto &y:e[x]) {
dfs1(y);
size[x]+=size[y];
if(size[y]>size[son[x]]) {
son[x]=y;
}
}
}
void dfs2(const int &x) {
id[dfn[x]=++dfn[0]]=x;
top[x]=x==son[par[x]]?top[par[x]]:x;
if(son[x]) dfs2(son[x]);
for(auto &y:e[x]) {
if(y!=son[x]) dfs2(y);
}
}
class FotileTree {
#define mid ((b+e)>>1)
private:
static constexpr int SIZE=N*logN*2;
struct Node {
int val,left,right;
};
Node node[SIZE];
int sz,new_node(const int &p) {
node[++sz]=node[p];
return sz;
}
int length(const int &b,const int &e) const {
return e-b+1;
}
public:
int root[N];
void insert(int &p,const int &b,const int &e,const int &x) {
p=new_node(p);
node[p].val++;
if(b==e) return;
if(x<=mid) insert(node[p].left,b,mid,x);
if(x>mid) insert(node[p].right,mid+1,e,x);
}
int query(const int &p,const int &q,const int &b,const int &e,const int &x) const {
if(node[p].val-node[q].val==0) return 1;
if(node[p].val-node[q].val==length(b,e)) return 0;
if(x<=mid) return query(node[p].left,node[q].left,b,mid,x);
return query(node[p].right,node[q].right,mid+1,e,x);
}
int query(const int &p,const int &q,const int &b,const int &e,const int &l,const int &r) const {
//printf("````%d %d %d %d %d\n",b,e,l,r,node[p].val-node[q].val);
if(node[p].val-node[q].val==0) return length(l,r);
if(node[p].val-node[q].val==length(b,e)) return 0;
if(b==l&&e==r) return length(b,e)-(node[p].val-node[q].val);
int ret=0;
if(l<=mid) ret+=query(node[p].left,node[q].left,b,mid,l,std::min(mid,r));
if(r>mid) ret+=query(node[p].right,node[q].right,mid+1,e,std::max(mid+1,l),r);
return ret;
}
int query(const int &p,const int &q,const int &b,const int &e,const int &l,const int &r,const int &k) const {
if(b==e) return id[b];
if(r<=mid) return query(node[p].left,node[q].left,b,mid,l,r,k);
if(l>mid) return query(node[p].right,node[q].right,mid+1,e,l,r,k);
const int tmp=query(node[p].left,node[q].left,b,mid,l,mid);
if(tmp>=k) return query(node[p].left,node[q].left,b,mid,l,mid,k);
return query(node[p].right,node[q].right,mid+1,e,mid+1,r,k-tmp);
}
#undef mid
};
FotileTree t;
int get_lca(int x,int y) {
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) std::swap(x,y);
x=par[top[x]];
}
if(dep[x]<dep[y]) std::swap(x,y);
return y;
}
int query(int x,int y,const int &p,const int &q) {
int ret=0;
while(top[x]!=top[y]) {
if(dep[top[x]]<dep[top[y]]) std::swap(x,y);
ret+=t.query(t.root[p],t.root[q],1,dfn[0],dfn[top[x]],dfn[x]);
x=par[top[x]];
}
if(dep[x]<dep[y]) std::swap(x,y);
ret+=t.query(t.root[p],t.root[q],1,dfn[0],dfn[y],dfn[x]);
return ret;
}
int query(int x,int y,int k,const int &p,const int &q) {
const int z=get_lca(x,y);
const int tmp1=t.query(t.root[p],t.root[q],1,dfn[0],dfn[x]);
const int sum1=query(x,z,p,q);
k+=tmp1;
if(sum1>=k) {
while(top[x]!=top[z]) {
const int tmp=t.query(t.root[p],t.root[q],1,dfn[0],dfn[top[x]],dfn[x]);
if(tmp<k) {
k-=tmp;
x=par[top[x]];
continue;
}
return t.query(t.root[p],t.root[q],1,dfn[0],dfn[top[x]],dfn[x],tmp-k+1);
x=par[top[x]];
}
const int sum=t.query(t.root[p],t.root[q],1,dfn[0],dfn[z],dfn[x]);
return t.query(t.root[p],t.root[q],1,dfn[0],dfn[z],dfn[x],sum-k+1);
} else {
k=query(x,y,p,q)-k+1;
while(top[y]!=top[z]) {
const int tmp=t.query(t.root[p],t.root[q],1,dfn[0],dfn[top[y]],dfn[y]);
if(tmp<k) {
k-=tmp;
y=par[top[y]];
continue;
}
return t.query(t.root[p],t.root[q],1,dfn[0],dfn[top[y]],dfn[y],tmp-k+1);
y=par[top[y]];
}
const int sum=t.query(t.root[p],t.root[q],1,dfn[0],dfn[z],dfn[y]);
return t.query(t.root[p],t.root[q],1,dfn[0],dfn[z],dfn[y],sum-k+1);
}
}
int main() {
//freopen("travel.in","r",stdin);
//freopen("travel.out","w",stdout);
const int n=getint();
for(register int i=1;i<=n;i++) {
e[par[i]=getint()].emplace_front(i);
}
const int &root=*e[0].begin();
dfs1(root);
dfs2(root);
const int m=getint();
for(register int i=1;i<=m;i++) {
t.root[i]=t.root[i-1];
const int opt=getint();
if(opt==1) {
t.insert(t.root[i],1,n,dfn[getint()]);
}
if(opt==2) {
const int u=getint(),v=getint(),k=getint(),y=getint();
const int tmp=t.query(t.root[i],t.root[y],1,n,dfn[u])+t.query(t.root[i],t.root[y],1,n,dfn[v]);
//printf("````%d %d %d\n",query(u,v,i,y),tmp,k);
if(query(u,v,i,y)-tmp<k) {
puts("-1");
continue;
}
printf("%d\n",query(u,v,k,i,y));
}
}
return 0;
}
[CF226E]Noble Knight's Path的更多相关文章
- CF226E Noble Knight's Path/bzoj4704 旅行
题目描述: bz luogu 题解: 主席树维护大力树剖. 一条路径上不允许过的点的个数是当前袭击数-$y$时袭击数, 所以允许经过的点的个数是总数-当前袭击数+$y$时袭击数. 用主席树去维护每个时 ...
- [Codeforces 226E]Noble Knight's Path
题目大意:有一棵n个节点的树,m年.初始每个节点都有.每天有如下操作:1. 给定c,让c没有(c只可能没有一次).2. 给定s,t,k,y,求从第y+1年到现在(即忽略y+1年之前的操作1),s到t的 ...
- Lintcode: Knight Shortest Path
Given a knight in a chessboard (a binary matrix with 0 as empty and 1 as barrier) with a source posi ...
- The Sorrows of Young Werther
The Sorrows of Young Werther J.W. von Goethe Thomas Carlyle and R.D. Boylan Edited by Nathen Haskell ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- 宽度优先搜索(BFS)— 20180909 - 20180917
BFS几类题: 1.图的遍历:a.层级遍历 b.由点及面 c.拓扑排序 2.简单图最短路径: 简单图:1.无向图 2.边权重一致 图的时间复杂度: N个点,M条边,M最大是N^2,时间复杂度O(N+M ...
- BFS算法的优化 双向宽度优先搜索
双向宽度优先搜索 (Bidirectional BFS) 算法适用于如下的场景: 无向图 所有边的长度都为 1 或者长度都一样 同时给出了起点和终点 以上 3 个条件都满足的时候,可以使用双向宽度优先 ...
- A Child's History of England.22
CHAPTER 8 ENGLAND UNDER WILLIAM THE FIRST, THE NORMAN CONQUEROR Upon the ground where the brave Haro ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
随机推荐
- react 修改state某一属性值
1.state // 筛选框相关数据 searchSelect: { term: { value: '学期', key: '', options: [] }, type_of_personnel: { ...
- 一款已上市MMO手游地图同步方案总结
1. 客户端地图格子的相关知识 在2.5D的MMO游戏里,角色是通过3D的方式渲染,2D的地图是通过2D的方式显示,所以在客户端一般会有三个坐标系: a) 3D坐标系:所有需要3D渲染的角色和光效,都 ...
- tomcat和weblogic的区别
Tomcat是Apache基金会提供的Servlet容器,它支持JSP, Servlet和JDBC等J2EE关键技术,所以用户可以用Tomcat开发基于数据库,Servlet和JSP页面的Web应用, ...
- Ubuntu10.04 下安装RabbitVCS
安装RabbitVCS的方法步骤如下: 1.sudo add-apt-repository ppa:rabbitvcs/ppa #将rabbitvcs的添加到源里面.(次操作会提示是否要添 ...
- python不可以打印.doc文件
[背景] 需求: 打印word文件 模块: python-docx [问题] 传递xxx.doc文件给python脚本,执行后,控制台没有内容输出 经查询后了解到,大致理由: doc是早一代的word ...
- 在Ubuntu上使用pip安装错误 read timed out 处理方法
在终端输入 pip --default-timeout=1000 install -U pip 也就是修改超时时间.
- 你需要知道的Nginx配置二三事
做服务端开发的,工作中难免会遇到处理Nginx配置相关问题.在配置Nginx时,我一直本着“照葫芦画瓢”的原则,复制已有的配置代码,自己修修改改然后完成配置需求,当有人问起Nginx相关问题时,其实仍 ...
- CRM 业务
1. 创建CRM项目 引入插件 创建数据库 from django.db import models from django.db import models class Department(mod ...
- PHP完整的AES加解密算法使用及例子(256位)
依赖PHP自身的mcrypt扩展 <?php class aes { // CRYPTO_CIPHER_BLOCK_SIZE 32 private $_secret_key = 'default ...
- Linux文件系统的详解
这里以 EXT2 文件系统为例 在Linux下,一个磁盘的最前面是MBR,大小为512Byte 在每一个分区下,第一部分是boot sector,接下来是super block,再接下来是inode, ...