参考九野巨巨的博客

查询一个子树内的信息,可以通过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. mysql explain 的extra中using index ,using where,using index condition,using index & using where理解

    using index :查找使用了索引,查询结果覆盖了索引 using where:查找使用了索引,不需要回表去查询所需的数据,查询结果是索引的一部分 using index condition:查 ...

  2. watir 的api在线文档

    http://rubydoc.info/gems/watir-webdriver/frames http://rdoc.info/gems/watir-webdriver/frames http:// ...

  3. linq动态分页排序

    if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(dir))//判断排序的字段名称和排序的类型是否为空 { if (d ...

  4. 织梦DeDeCMS友情链接文字显示不全

    文件:/include/taglib/flink.lib.php 把下面代码中的24改为合适的值 $attlist=”type|textall,row|24,titlelen|24,linktype| ...

  5. android动画ppt整理

    案例

  6. Java 文件操作-File

    1.File文件操作 java.io.File用于表示文件(目录),也就是说程序员可以通过File类在程序中操作硬盘上的文件和目录.File类只用于表示文件(目录)的信息(名称.大小等),不能对文件的 ...

  7. (转)SQL注入攻击简介

    如果你是做Javaweb应用开发的,那么必须熟悉那声名狼藉的SQL注入式攻击.去年Sony就遭受了SQL注入攻击,被盗用了一些Sony play station(PS机)用户的数据.在SQL注入攻击里 ...

  8. self & this 上下文

    对象:指向对象的首地址: 函数:代表了函数运行的主要上下文: 内部:在类的内部使用. self Within the body of a class method, self refers to th ...

  9. 为管理复杂组件状态困扰?试试 vue 简单状态管理 Store 模式【转】

    https://juejin.im/post/5cd50849f265da03a54c3877 在 vue 中,通信有几种形式: 父子组件 emit/on vuex 中共享 state 跨组件 Eve ...

  10. Problem O: 国家排序

    Problem O: 国家排序 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 405  Solved: 253[Submit][Status][Web ...