传送门

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的更多相关文章

  1. 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 ...

  2. CF 570 D. Tree Requests

    D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...

  3. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  4. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  5. codeforces 570 D. Tree Requests (dfs)

    题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...

  6. Codeforces 570D TREE REQUESTS dfs序+树状数组

    链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...

  7. 爬虫入门系列(三):用 requests 构建知乎 API

    爬虫入门系列目录: 爬虫入门系列(一):快速理解HTTP协议 爬虫入门系列(二):优雅的HTTP库requests 爬虫入门系列(三):用 requests 构建知乎 API 在爬虫系列文章 优雅的H ...

  8. centos7下报错: import requests ImportError: No module named requests

    在网上扒了一个python脚本,在centos7上执行的时候报错: import requestsImportError: No module named requests 原因是:requests是 ...

  9. 转: python requests的安装与简单运用

    requests是Python的一个HTTP客户端库,跟urllib,urllib2类似,那为什么要用requests而不用urllib2呢? 官方文档中是这样说明的: python的标准库urlli ...

随机推荐

  1. 使用Jekyll在Github上搭建博客

    最近在玩github,突然发现很多说明网站或者一些介绍页面全部在一个域名是*****.github.io上. 好奇!!!真的好奇!!!怎么弄的?我也要一个~~~ 于是去网站上查询了一下,找到了http ...

  2. [BZOJ1924][Sdoi2010]所托门王的宝藏(缩点+DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1924 分析: 首先把传送门作为点建图 这个数据很弱的,没有那种卡你的. 把每行的情况存 ...

  3. Matlab学习笔记 figure函数

    Matlab学习笔记 figure函数 matlab中的 figure 命令,能够创建一个用来显示图形输出的一个窗口对象.每一个这样的窗口都有一些属性,例如窗口的尺寸.位置,等等.下面一一介绍它们. ...

  4. CSS Bug

    父子标签间用margin的问题,表现在有时除IE(6/7)外的浏览器子标签margin转移到了父标签上,IE6&7下没有转移.测试代码: <body> <style type ...

  5. oracle判断字段是否存在语句

    declare v_cnt number; begin select count(*) into v_cnt from dba_tab_columns where table_name='T_IDC_ ...

  6. [转]Mybatis极其(最)简(好)单(用)的一个分页插件

    原文地址:http://blog.csdn.net/isea533/article/details/23831273 分页插件示例:http://blog.csdn.net/isea533/artic ...

  7. iPad开发--UIPopoverController简单使用iOS7之前和iOS7之后的使用方法

    一.iOS7之前的Popover的使用 对Popover进行懒加载处理 内容控制器中设置Popover弹出后的尺寸 设置显示的位置,两种情况.1 -- 给BarButtonItem设置Popover的 ...

  8. 【ASP.NET Identity系列教程(一)】ASP.NET Identity入门

    注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...

  9. javaweb写的在线聊天应用

    写这个玩意儿就是想练练手, 用户需要登陆才能在线聊天,不要依赖数据库, 不需要数据库的操作, 所有的数据都是保存在内存中, 如果服务器一旦重启,数据就没有了: 登录界面: 聊天界面: 左侧是在线的用户 ...

  10. 自定义View完全解析

    自定义View主要包括以下3种方式: 一.组合控件,利用已有控件的组合,来满足自己的需求. 例子:顶部导航栏 二.继承已有View,比如继承TextView.ImageView等,根据需要重写相应的方 ...