51nod1513
题解:
更据题意,在树上深度为没一个数的都放在一起,要用的时候二分出来,看结果
用c++的数据结构
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+,L=;
char s[N];
int n,m,cnt=,max_h=,high[N];
vector<int>E[N],A[N],B[N];
pair<int,int>order[N];
void dfs(int x,int h)
{
max_h=max(h,max_h);
high[x]=h;
order[x].first=++cnt;
A[h].push_back(<<(s[x-]-'a'));
B[h].push_back(cnt);
for (int i=;i<E[x].size();i++)dfs(E[x][i],h+);
order[x].second=cnt;
}
int solve(int x,int h)
{
if (h<=high[x]||!B[h].size())return ;
int l=(int)(lower_bound(B[h].begin(),B[h].end(),
order[x].first)-B[h].begin());
int r=(int)(upper_bound(B[h].begin(),B[h].end(),
order[x].second)-B[h].begin()-);
if (l>r)return ;
int ans=A[h][r]^(l?A[h][l-]:);
if (ans==(ans&(-ans)))return ;
return ;
}
int read()
{
char c=;int x=;
for(;c<''||c>'';c=getchar());
for(;c>=''&&c<='';c=getchar())x=x*+c-'';
return x;
}
int main()
{
n=read();m=read();
for (int i=;i<=n;i++)E[read()].push_back(i);
scanf("%s",&s);
dfs(,);
max_h++;
for (int i=;i<max_h;i++)
for (int j=;j<A[i].size();j++)A[i][j]^=A[i][j-];
while (m--)
{
int v=read(),h=read();
if (solve(v,h-))puts("Yes");
else puts("No");
}
}
51nod1513的更多相关文章
随机推荐
- HDU 1358 Period(KMP+最小循环节)题解
思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...
- spring boot 修改Tomcat端口
package com.tsou.Controller; import org.springframework.boot.*; import org.springframework.boot.auto ...
- Cent OS 常用配置命令
1.ifconfig #查看网络接口状态 2.ifconfig –a #查看主机所有接口的情况 3.ifconfig eth0 192.168.1.106 netmask 255.255.255 ...
- spring实现定时任务的两种方式
本文为博主原创,未经允许不得转载 项目中要经常事项定时功能,在网上学习了下用spring的定时功能,基本有两种方式,在这里进行简单的总结, 以供后续参考,此篇只做简单的应用. 1.在spring-se ...
- 初识C++继承
先是自己凭借自己在课堂上的记忆打了一遍.自然出了错误. //编译错误 #include <iostream> #include <cstdlib> using namespac ...
- PHP生成缩略图的一个方法类(转)
//使用如下类就可以生成图片缩略图 class resizeimage { //图片类型 var $type; //实际宽度 var $width; //实际高度 var $height; //改变后 ...
- js 字符串匹配
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- python 压缩tar 包
import tarfile import os def make_targz(output_filename, source_dir): print("doing!") with ...
- EsayUI + MVC + ADO.NET(仓储基类)
该篇主要讲解工作单元的运用 和DbHlper抽象封装 工作单元仓储基类:BaseRepository (DbHlper抽象封装) 仓储接口(CRUD):IRepository (CRUD接口) ...
- 数据库使用SSIS进行数据清洗教程
OLTP系统的后端关系数据库用于存储不同种类的数据,理论上来讲,数据库中每一列的值都有其所代表的特定含义,数据也应该在存入数据库之前进行规范化处理,比如说“age”列,用于存储人的年龄,设置的数据类型 ...