离线 + 树状数组

如果子树中的一个深度的所有点中有两个以上的字母出现了奇数次,那么这个询问的答案就是$No$,其他的情况吧都是$Yes$。

由于只有$26$个字母,我们可以考虑暴力检验,把树映射到$dfs$序上然后看一看子树区间中每一个字母出现了多少次。

我先写了一个动态开点的线段树,然后$O(26 * (n + q)logn)$完美爆炸了。

然后我们发现一个深度的所有点是可以相互利用的,这样子只要堆所有的询问离线一下按照深度排个序就好了,开$26$个树状数组单点修改+ 区间求和就做完了。

感觉速度飞快。

时间复杂度$O(nlogn + 26 * qlogn)$。

Code:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; const int N = 5e5 + ;
const int M = ; int n, qn, tot = , head[N];
int dfsc = , dep[N], id[N], siz[N];
char let[N];
vector <int> vec[N]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} struct Querys {
int x, h, res, id; friend bool operator < (const Querys &u, const Querys &v) {
return u.h < v.h;
} } q[N]; inline void read(int &X) {
X = ; char ch = ; int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} bool cmp(int x, int y) {
return dep[x] < dep[y];
} inline void chkMax(int &x, int y) {
if(y > x) x = y;
} void dfs(int x, int fat, int depth) {
dep[x] = depth, id[x] = ++dfsc, siz[x] = ;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x, depth + );
siz[x] += siz[y];
}
} namespace Bit {
int s[M][N]; #define lowbit(p) (p & (-p)) inline void modify(int t, int p, int v) {
for(; p <= n; p += lowbit(p))
s[t][p] += v;
} inline int query(int t, int p) {
int res = ;
for(; p > ; p -= lowbit(p))
res += s[t][p];
return res;
} } using namespace Bit; int main() {
// freopen("Sample.txt", "r", stdin);
// freopen("my.out", "w", stdout); read(n), read(qn);
for(int fat, i = ; i <= n; i++) {
read(fat);
add(fat, i), add(i, fat);
}
dfs(, , ); int maxd = ;
for(int i = ; i <= n; i++) {
chkMax(maxd, dep[i]);
vec[dep[i]].push_back(i);
} scanf("%s", let + );
for(int i = ; i <= qn; i++) {
q[i].res = , q[i].id = i;
read(q[i].x), read(q[i].h);
} sort(q + , q + + qn);
for(int i = ; i <= qn; i++) {
int lst = i;
for(; q[i].h == q[lst].h; i++);
i--;
if(q[i].h > maxd) continue; int vecSiz = vec[q[i].h].size();
for(int j = ; j < vecSiz; j++) {
int pos = vec[q[i].h][j];
modify(let[pos] - 'a', id[pos], );
} for(int j = lst; j <= i; j++)
for(int c = ; c < ; c++) {
int tmp = query(c, id[q[j].x] + siz[q[j].x] - ) - query(c, id[q[j].x] - );
if(tmp & ) ++q[q[j].id].res;
} for(int j = ; j < vecSiz; j++) {
int pos = vec[q[i].h][j];
modify(let[pos] - 'a', id[pos], -);
}
} /* for(int i = 1; i <= qn; i++)
printf("%d ", q[i].res);
printf("\n"); */ for(int i = ; i <= qn; i++)
if(q[i].res <= ) puts("Yes");
else puts("No"); return ;
}

CF570D Tree Requests的更多相关文章

  1. 解题:CF570D Tree Requests

    题面 DSU on tree确实很厉害,然后这变成了一道裸题(逃 还是稍微说一下流程吧,虽然我那个模板汇总里写过 DSU on tree可以以$O(n\log n)$的复杂度解决树上子树统计问题,它这 ...

  2. CF570D——Tree Requests

    1.题目大意:给你一棵树,每个点有一个字符,然后我们定义1的深度是1,然后1的儿子深度是2... 然后有一个询问,询问以i为根节点的子树,然后深度是k的那层的所有字符,可否组成一个回文串 2.分析:首 ...

  3. Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  4. CF 570 D. Tree Requests

    D. Tree Requests http://codeforces.com/problemset/problem/570/D 题意: 一个以1为根的树,每个点上有一个字母(a-z),每次询问一个子树 ...

  5. Codeforces 570D TREE REQUESTS dfs序+树状数组 异或

    http://codeforces.com/problemset/problem/570/D Tree Requests time limit per test 2 seconds memory li ...

  6. Codeforces 570D - Tree Requests(树上启发式合并)

    570D - Tree Requests 题意 给出一棵树,每个节点上有字母,查询 u k,问以 u 为根节点的子树下,深度为 k 的所有子节点上的字母经过任意排列是否能构成回文串. 分析 一个数组 ...

  7. codeforces 570 D. Tree Requests 树状数组+dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  8. codeforces 570 D. Tree Requests (dfs)

    题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...

  9. Codeforces 570D TREE REQUESTS dfs序+树状数组

    链接 题解链接:点击打开链接 题意: 给定n个点的树.m个询问 以下n-1个数给出每一个点的父节点,1是root 每一个点有一个字母 以下n个小写字母给出每一个点的字母. 以下m行给出询问: 询问形如 ...

随机推荐

  1. HWOJ-求字符串最后一个单词的长度

    题目:给定一个字符串,求最后一个单词的长度,每个单词中间有空格. 例如:输入:hello world   输出:5 C代码:通过. #include <stdio.h> #define m ...

  2. 【转】C#中的线程 入门

    Keywords:C# 线程 Source:http://www.albahari.com/threading/ Author: Joe Albahari Translator: Swanky Wu ...

  3. matlab中hdl coder 的使用

    今天摸索了一下hdl coder的使用方法,各个步骤主要是照猫画虎,有些地方还是不理解,先总结一下: 1.要想调用quartus或者Xilinx综合布局布线需要先设置,设置的方法有两种,命令窗口输入 ...

  4. zju 校队选拔 被虐记

    选拔已经开始了三天才想起来写游记 QAQ.. 7.12 弱弱的Sky_miner来到了ZJU,过程中被热成狗... 然后见到了无数大二大三的大佬们,过程中被热成狗... 后来听靖哥哥说集训的注意事项, ...

  5. 积累 ---- PHP可能会遇到的面试题

    1.白盒测试和黑盒测试的区别 2.Bootstrap是什么 3.OOP是什么意思 4.git和svn的使用 5.常用的git命令 6.lamp开发环境 7.高内聚,低耦合

  6. maven打war包后无法依赖本地工程的jar包,造成debug时跳到class文件而不是本地java文件

    问题现象:项目结构如下 growup-service | - - - - - -growup-api | - - - - - -growup-core | - - - - - -growup-war ...

  7. POJ2478(欧拉函数)

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15242   Accepted: 6054 D ...

  8. Mvn+Jetty启动项目

    这里要注意,Mvn加jetty启动项目,主要用到的是Maven的jetty插件,和你下的Jetty服务器没什么关系. 我的运行环境是jdk1.7,Eclipse-mars,Maven是Eclipse自 ...

  9. NOIP2008普及组第3题 传球游戏

    NOIP2008普及组第3题 传球游戏 时间限制: 1 Sec  内存限制: 128 MB提交: 29  解决: 16[提交][状态][讨论版][命题人:外部导入] 题目描述 上体育课的时候,小蛮的老 ...

  10. Rest之路 - 第一个Rest程序

    在 Eclipse 里新建一个 Dynamic project 将 Jersey 的 jar 包,拷贝到 WebContent -> WEB-INF -> lib 文件夹 Add jars ...