[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 ...
随机推荐
- koa通过get请求获取参数
1.通过get方式请求获取参数的方式有两种 通过上下文获取 通过request获取 获得的格式有两种:query与querystring 注意:querystring为小写,驼峰格式会导致无法获取 2 ...
- php webshell常见函数
0x1 直接在字符串变量后面加括号, 会调用这个函数: <?php $s = 'system'; $e = 'assert'; $s('whoami'); $e('phpinfo();'); 0 ...
- for 、forEach 、 forof、 forin遍历对比
一.遍历内容的异同 1.for 和 for...in 是针对数组下标的遍历 2.forEach 及 for...of 遍历的是数组中的元素 二.对非数字下标的处理 由于array在js中也是对象中的一 ...
- 32.Longest Valid Parentheses---dp
题目链接:https://leetcode.com/problems/longest-valid-parentheses/description/ 题目大意:找出最长的括号匹配的子串长度.例子:&qu ...
- 关于linux环境下crontab命令环境变量的问题
这几天在弄数据库备份的事情,其中涉及到使用crontab命令自动执行shell脚本的问题,发现将写好的数据库导出脚本export.sh ################################ ...
- 苹果笔记本MacBookPro 的新手使用技巧
Mac 系统的桌面 Mac 的桌面是一个很炫的3D, 背景是一张“星空”图 Dock: 在桌面的下方,有一排图标, 这个叫Dock, 用来快速启动程序, 进入文件夹, 它同时还可以停靠正在运行的程序 ...
- sendEmail实现邮件报警
安装 wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz 或者点击下载 tar -xf sen ...
- 在 ASP.NET Core 具体使用文档
https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x
- Java门派的风险
Java门派的风险 正在看周思博(www.joelonsoftware.com)的新文章.这次是疯狂攻击Java.主要论点是:Java不够难,作为工业语言不错,但作为学校的教学语言,就忒差了.学校应该 ...
- [Linux][Ubuntu18.04.1] nginx+php+MySQL环境搭建
说在前面 今天在腾讯云的CVM服务器搭建了一下环境[主机:标准型S2,Unbuntu18.04的LST版本] 采用了nginx服务器(Nginx 静态处理性能比 Apache高3倍以上,不过apach ...