参考九野巨巨的博客

查询一个子树内的信息,可以通过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. bjut193E 吃饭时的怪癖

    题目: http://bjutacm.openjudge.cn/lianxi/193E/ 思路: n的所有质因数之和等于phi(n) * n / 2, phi(n)为欧拉函数. 实现: #includ ...

  2. [20190620]日常学习记录(三)-初识promise及vuex

    在学习promise之前重温了Ajax的原生js实现, 在原生js中发送一个http请求首先new XMLHttpRequest() 然后定义状态变更事件 浏览器监听请求的状态,触发不同状态下相应的代 ...

  3. Windows下Python多版本共存

    Windows下Python多版本共存 Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas) 0.0 因为公司项目,需要Python两个 ...

  4. UNITY_MATRIX_MVP和UnityObjectToClipPos

    在unity5.6以上版本中,shader中的UNITY_MATRIX_MVP将会被UnityObjectToClipPos替代,以后我们在写顶点函数时就是这样的 v2f vert(appdata v ...

  5. 初识Adapter

    首先得了解Adapter层级关系: 示例,将user对象适配到textview public class User { private String userName; private String ...

  6. BZOJ 2725: [Violet 6]故乡的梦 最短路+线段树

    2725: [Violet 6]故乡的梦 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 678  Solved: 204[Submit][Status ...

  7. Java和ABAP中的几种引用类型的分析和比较

    Java编程语言中几种不同的引用类型是面试时经常容易被问到的问题:强引用,软引用,弱引用,虚引用. 其实除了Java之外,某些 其他编程语言也有类似概念,比如ABAP.今天我们就来比较一下. 根据AB ...

  8. db2疑难解决

    http://www-01.ibm.com/support/knowledgecenter/?lang=zh#!/SSEPGG_9.5.0/com.ibm.db2.luw.messages.sql.d ...

  9. GWT-2.5.1离线安装

    GWT官方离线包下载地址 http://dl.google.com/eclipse/plugin/3.7/zips/gpe-e37-latest-updatesite.zip 以下是GWTDesign ...

  10. cookie存验证码时间,时间没走完不能再次点击

    <script> var balanceSeconds=getcookie('Num'); console.log(balanceSeconds) var timer; var isCli ...