[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 ...
随机推荐
- let块级作用域
let是es6中新加的作用域,即块级作用域. var申明的变量要么全局,要么函数级,而let允许把变量的作用域限制在块级域中,这里的块级可以是()内,或{}内. 示例: code_1: "u ...
- Python os.path.dirname(__file__) os.path.join(str,str)
Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__f ...
- Opencv 配置VS2012
开始接触图像处理有一段时间了,经过前期的调研,和相关入门知识的学习,开始接触一些图像处理应用的工具.Opencv是一个图像处理的开源库,由于其开放的协议架构,国内外很多科研机构和团队都在基于openc ...
- C基础 如何让代码只执行一次
1.0 最简单, 最高效的方式 C 代码运行起点 main 就是个大单例函数. 如果把函数注册在其里面, 那么一定很可以 :) // 某个库需要初始化的函数 void log_init(void) { ...
- C#子线程中更新ui
本文实例总结了C#子线程更新UI控件的方法,对于桌面应用程序设计的UI界面控制来说非常有实用价值.分享给大家供大家参考之用.具体分析如下: 一般在winform C/S程序中经常会在子线程中更新控件的 ...
- POJ 2348 Euclid's Game(辗转相除博弈+自由度分析)
题目链接:http://poj.org/problem?id=2348 题目大意:给你两个数a,b,Stan和Ollie轮流操作,每次可以将较大的数减去较小的数的整数倍,相减后结果不能小于0,谁先将其 ...
- 号外,号外 -几乎所有的binary search和mergesort都有错
号外,号外 -几乎所有的binary search和mergesort都有错 这是Joshua Bloch(Effective Java的作者)在google blog上发的帖子.在说这个帖子之前,不 ...
- EF 剥坑
1.简单 count 会生成不必要的嵌套 var xs = (from x in dbContext.db_API_Operationallog where x.id<1 select 1 ). ...
- vs2013设置语言
设置语言格式 [工具]-[选项]-[国际化]
- poj3414 Pots(BFS)
题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...