CF570D:Tree Requests
DFS重标号+二分
打比赛的时候想了很多方法..DFS序,BFS序,倍增什么的都考虑了一遍,但是几乎要么是可以维护两个区间但是代码复杂度爆炸,要么就是只能维护单一维度的信息。
这道题的具体做法就是先DFS遍历一遍,记一下每个点的出入栈时间。按照每个点的深度排序。这样就可以二分出深度,然后根据入栈的时间可以再在这个深度内二分出合适的区间。范围就是询问节点的出入栈时间。
//CF 570D
//by Cydiater
//2016.10.14
#include <iostream>
#include <cstdlib>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define up(i,j,n) for(int i=j;i<=n;i++)
#define down(i,j,n) for(int i=j;i>=n;i--)
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
inline int read(){
char ch=getchar();int x=0,f=1;
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int LINK[MAXN],len=0,N,M,tim=0,pos[MAXN],Xor[MAXN],lef1,lef2,rig1,rig2;
char s[MAXN];
struct edge{
int y,next;
}e[MAXN];
struct Node{
int in,out,dep,id,v;
}a[MAXN];
namespace solution{
inline void insert(int x,int y){e[++len].next=LINK[x];LINK[x]=len;e[len].y=y;}
inline bool cmp(Node x,Node y){return x.dep==y.dep?x.in<y.in:x.dep<y.dep;}
void dfs(int Node,int deep){
a[Node].in=++tim;a[Node].dep=deep;a[Node].id=Node;
for(int i=LINK[Node];i;i=e[i].next)dfs(e[i].y,deep+1);
a[Node].out=++tim;
}
void init(){
N=read();M=read();
up(i,2,N){
int Node=read();
if(i==Node)continue;
insert(Node,i);
}
scanf("%s",s+1);
up(i,1,N)a[i].v=s[i]-'a';
dfs(1,1);
sort(a+1,a+N+1,cmp);
up(i,1,N){
pos[a[i].id]=i;
Xor[i]=((1<<a[i].v)^Xor[i-1]);
}
}
int get1(int lim){//<=
int leftt=1,rightt=N,mid;
while(leftt+1<rightt){
mid=(leftt+rightt)>>1;
if(a[mid].dep>=lim) rightt=mid;
else leftt=mid;
}
if(a[leftt].dep>=lim) return leftt;
return rightt;
}
int get2(int lim){//>=
int leftt=1,rightt=N,mid;
while(leftt+1<rightt){
mid=(leftt+rightt)>>1;
if(a[mid].dep<=lim) leftt=mid;
else rightt=mid;
}
if(a[rightt].dep<=lim) return rightt;
return leftt;
}
int get3(int lim){//<=
int leftt=lef1,rightt=rig1,mid;
while(leftt+1<rightt){
mid=(leftt+rightt)>>1;
if(a[mid].in>=lim) rightt=mid;
else leftt=mid;
}
if(a[leftt].in>=lim) return leftt;
return rightt;
}
int get4(int lim){//>=
int leftt=lef1,rightt=rig1,mid;
while(leftt+1<rightt){
mid=(leftt+rightt)>>1;
if(a[mid].in<=lim) leftt=mid;
else rightt=mid;
}
if(a[rightt].in<=lim) return rightt;
return leftt;
}
void slove(){
while(M--){
int Node=pos[read()],dep=read();
if(a[Node].dep>=dep){puts("Yes");continue;}
lef1=get1(dep);rig1=get2(dep);
lef2=get3(a[Node].in+1);rig2=get4(a[Node].out-1);
int S=Xor[rig2]^Xor[lef2-1];int flag=0;
if(lef2>rig2){
puts("Yes");
continue;
}
up(i,0,25)if((S&(1<<i))==(1<<i)&&flag){
puts("No");
flag=-1;
break;
}else if((S&(1<<i))==(1<<i)) flag=1;
if(flag!=-1)puts("Yes");
}
}
}
int main(){
//freopen("input.in","r",stdin);
using namespace solution;
init();
slove();
return 0;
}
CF570D:Tree Requests的更多相关文章
- Codeforces Round #316 (Div. 2) D. Tree Requests dfs序
D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- CF 570 D. Tree Requests
D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组 异或
http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...
- codeforces 570 D. Tree Requests 树状数组+dfs搜索序
链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...
- codeforces 570 D. Tree Requests (dfs)
题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...
- Codeforces 570D TREE REQUESTS dfs序+树状数组
链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...
- 爬虫入门系列(三):用 requests 构建知乎 API
爬虫入门系列目录: 爬虫入门系列(一):快速理解HTTP协议 爬虫入门系列(二):优雅的HTTP库requests 爬虫入门系列(三):用 requests 构建知乎 API 在爬虫系列文章 优雅的H ...
- centos7下报错: import requests ImportError: No module named requests
在网上扒了一个python脚本,在centos7上执行的时候报错: import requestsImportError: No module named requests 原因是:requests是 ...
- 转: python requests的安装与简单运用
requests是Python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢? 官方文档中是这样说明的: python的标准库urlli ...
随机推荐
- web安全——应用(java)
简介 由于网络技术日趋成熟,黑客们也将注意力从以往对网络服务器的攻击逐步转移到了对web应用的攻击.据最新调查,信息安全有75%都发生在web应用而非网络层面. 场景 控制访问的权限.只让可以访问的访 ...
- mysql full text全文索引必要条件
show variables like 'ft_m%' 'ft_max_word_len', '84''ft_min_word_len', '4' 对于英文来说, ft_min_word_len=4是 ...
- js队列
用指针和数组模拟基本队列 http://blog.csdn.net/zhuwq585/article/details/53177192 js下的事件队列,或者异步队列 http://www.jb51. ...
- vijos-1447 开关灯泡-大整数开方算法
描述 一个房间里有n盏灯泡,一开始都是熄着的,有1到n个时刻,每个时刻i,我们会将i的倍数的灯泡改变状态(即原本开着的现将它熄灭,原本熄灭的现将它点亮),问最后有多少盏灯泡是亮着的. 提示 范围:40 ...
- 为什么要加入<!doctype html>这个文档声明——IE怪异模式
调试了很久,发现了一个非常细微但又十分重要的问题,又一次我在对于文档声明类型的时候,声明了如下类型 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...
- 无法将 匿名方法 转换为类型“System.Delegate”,因为它不是委托类型:解决方法
http://blog.csdn.net/xiaochongchong1248/archive/2009/11/20/4841193.aspx?1271573283 编程环境要求:VS2008/FX2 ...
- Maven的内置变量
Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...
- Hessian Matrix
函数\(f\)的Hessian矩阵由是由它的二阶偏导数组成的方阵 \[ H = \begin{bmatrix} \dfrac{\partial^2 f}{\partial x_1^2} & \ ...
- 区间DP HDU 4283
t个数据 n个权值 1->n 可以入栈调整顺序 花费 第k个出来 w[i]*(k-1); 求花费最少 #include<stdio.h> #include<string.h&g ...
- SpringMVC 部署项目静态资源文件访问问题
问题:采用SpringMVC 部署项目后程序加载或用浏览器访问时出现类似的警告,2011-01-19 10:52:51,646 WARN [org.springframework.web.servle ...