参考九野巨巨的博客

查询一个子树内的信息,可以通过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. Java函数的传参机制

    一 最近的一个项目,里面各种返回void,参数用引用类型,那叫一个熟和多,但是也把我绕糊涂了. 我就打算好好理一理java的传参机制,整理一番 二 很多人一听Java的传参,那一定会脱口而出,java ...

  2. This file's format is not supported or you don't specify a correct format. 解决办法

    string path = @"c:\请假统计表.xlsx"; Workbook workBook = new Workbook(); workBook.Open(path); A ...

  3. WPF (VisualChildren)可视化子元素详解

    VisualChildrenCount    的 FrameworkElement 实现始终返回 0 或 1.  如果类所要维护的可视化子元素集合的成员数可能超过 1,则这样的类必须重写此属性和 Ge ...

  4. Json 后台转对象的方式或者获取属性值方式。

    类似这类的 json字符串 后台转成 model 或者取其中一个属性值. 需要去掉前后引号 以及将转义字符去掉.空格代替 resoult = resoult.Substring(0, resoult. ...

  5. 利用Vagrant and VirtualBox搭建core os环境

    利用Vagrant and VirtualBox搭建core os环境 系统环境 ubuntu 14.04 x64 vagrant 1.7.4 virtualbox 4.3.10 git 1.9.1 ...

  6. Java的API及Object类、String类、字符串缓冲区

    Java 的API 1.1定义 API: Application(应用) Programming(程序) Interface(接口) Java API就是JDK中提供给开发者使用的类,这些类将底层的代 ...

  7. for循环操作DOM缓存节点长度?

    不管是在网上,还是在翻看书籍的时候,都能看到在使用for循环操作DOM节点时要做数节点长度的缓存,以确保性能最优化! 这二种写法格式大致是下面这样的 /*节点集合*/ var domarr=docum ...

  8. Hello World另类写法

    #include<iostream> #define _ using namespace std; #define __ int main() #define ___ { #define ...

  9. Android端WebRTC点对点互连

    项目准备 信令服务器代码:https://github.com/matthewYang92/WebRtcServer(代码改自ProjectRTC) 安装Node.js 进入项目根目录,命令行:npm ...

  10. win7 dos窗口模拟帧刷新

    前几天是白色情人节,临时脑抽写了个表白神器 高端大气上档次,就是不知道该送给谁,经过两天的反射弧思考决定还是写给博客娘吧.- -~ 这个程序就是打开后,在Dos窗口内模拟写出几行字母.其实主要就是模拟 ...