参考九野巨巨的博客

查询一个子树内的信息,可以通过DFS序转成线形的,从而用数据结构来维护。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#define MP make_pair
#define FI first
#define SE second
using namespace std; typedef pair<int, int> PII; const int maxn = + ; int n, m; vector<int> G[maxn];
char s[maxn]; struct BIT
{
int C[maxn]; inline int lowbit(int x) { return x & (-x); } void add(int x, int v)
{
while(x <= n) C[x] += v, x += lowbit(x);
} int sum(int x)
{
int ans = ;
while(x) ans += C[x], x -= lowbit(x);
return ans;
} int Query(int l, int r) { return sum(r) - sum(l - ); } }T[]; vector<int> Dep[maxn];
int dfs_clock;
int pre[maxn], post[maxn]; void dfs(int u, int dep)
{
Dep[dep].push_back(u);
pre[u] = ++dfs_clock;
for(int v : G[u]) dfs(v, dep + );
post[u] = dfs_clock;
} vector<PII> queries[maxn]; bool ans[maxn]; int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++)
{
int u; scanf("%d", &u);
G[u].push_back(i);
}
scanf("%s", s + ); int maxh = ;
for(int i = ; i < m; i++)
{
int v, h; scanf("%d%d", &v, &h);
maxh = max(maxh, h);
queries[h].push_back(MP(v, i));
} dfs(, ); for(int i = ; i < m; i++) ans[i] = true; for(int i = ; i <= maxh; i++)
{
if(Dep[i].empty()) break;
if(queries[i].empty()) continue; for(int v : Dep[i]) T[s[v] - 'a'].add(pre[v], ); for(PII x : queries[i])
{
int odd = ;
for(int j = ; j < ; j++)
{
odd += (T[j].Query(pre[x.FI], post[x.FI]) & );
}
if(odd >= ) ans[x.SE] = false;
} for(int v : Dep[i]) T[s[v] - 'a'].add(pre[v], -);
} for(int i = ; i < m; i++) printf("%s\n", ans[i] ? "Yes" : "No"); return ;
}

代码君

CodeForces 570D DFS序 树状数组 Tree Requests的更多相关文章

  1. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  2. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  3. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  4. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  5. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

  6. 模拟赛 T3 DFS序+树状数组+树链的并+点权/边权技巧

    题意:给定一颗树,有 $m$ 次操作. 操作 0 :向集合 $S$ 中加入一条路径 $(p,q)$,权值为 $v$ 操作 1 :给定一个点集 $T$,求 $T$ 的并集与 $S$ 中路径含交集的权和. ...

  7. 计蒜客A1998 Ka Chang (分块+dfs序+树状数组)

    题意 给你一个\(1e5\)的有点权的树,有\(1e5\)个操作: 1.给第\(x\)层的点加上\(y\) 2.求以\(x\)为根的子树的点权和 思路 首先处理出层数为x的所有点 操作2一般都是用df ...

  8. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  9. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

随机推荐

  1. c#基础值类和引用类型

    //值类型:int double char decimal bool enum struct //引用类型:string 数组 自定义类 集合 object 接口 值传递传递的值得本身 引用传递传递的 ...

  2. ubuntu 14.04 安装minidwep-gtk

    一,安装相关的依赖 sudo apt-get install build-essential libssl-dev iw libpcap-dev sqlite3 libsqlite3-dev libp ...

  3. spring项目后台中打印sql语句

    配置方法很简单,只要在配置文件application.yml加入以下语句 logging: level: main.java.iReview.dao: debug 其中包的位置应为dao层的包位置,不 ...

  4. JavaScript笔记5-事件

    一.概述: 事件是可以被JavaScript侦测到的行为.网页中的每个元素都可以产生某些可以触发JavaScript函数的事件.相当于让标签在满足某种条件的时候,调用指定的方法. 二.常用事件 1:o ...

  5. SQL Server数据库log shipping 灾备(Part2 )

    3.配置步骤: 主服务器(A机)设置 (1) 启用Log Shipping Configuration 右键单击需要配置日志传输的数据库->Tasks-> Ship Transaction ...

  6. 【Troubleshooting Case】Exchange Server 组件状态应用排错?

    在Exchange 2013中,引入了“服务器组件状态”的概念.服务器组件状态从运行环境的角度提供对组成Exchange Server的组件的状态的精细控制. 日常排错时,常常会把Exchange 服 ...

  7. ssh连接github连不上

    连接github报端口22连接不上: 输入命令展示出ssh_config内容后: vim /etc/ssh/ssh_config 或者使用open /etc/ssh/ssh_config命令在文本编辑 ...

  8. javaSe-字符型和布尔型

    其实java数据类型一节就可以全部写完了,为什么还需要字符型和布尔型呢,原因是这俩个都很重要: 字符型用char表示,字符分三种: 普通字符:char a = 'a',普通字符表示一个普通的字符,没有 ...

  9. UI高端课程

    目后佐道IT教育正在打架报名中,欢迎高中生.大学生前来学习编程技术和UI设计,招生面向全国. 石破天惊 前100个报名者免费提供高级公寓居住(里面有空调,暖气,热水器,洗衣机). 赠送神秘课程价值29 ...

  10. 重温Javascript(二)-对象

    对象 可以想象成散列表,键值对,值可以是数据或函数 创建对象的方式 1.工厂模式 function createPerson(name, age, job){ var o = new Object() ...