Description

Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the labels are 0.

We define this kind of operation: given a subtree, negate all its labels.

And we want to query the numbers of 1's of a subtree.

Input

Multiple test cases.

First line, two integer N and M, denoting the numbers of nodes and numbers of operations and queries.(1<=N<=100000, 1<=M<=10000)

Then a line with N-1 integers, denoting the parent of node 2..N. Root is node 1.

Then M lines, each line are in the format "o node" or "q node", denoting we want to operate or query on the subtree with root of a certain node.

Output

For each query, output an integer in a line.

Output a blank line after each test case.

题目大意:给一棵多叉树,初始值都为0,o x为翻转以x为根的子树,q x为查询以x为根的子树有多少个1

思路:这数据范围,暴力是不行的,怎么暴力都是不行的>_<。这题的要求是:修改一大片、查询一大片,比较容易想到的就是线段树(树状数组也可以,不过要翻转嘛……好像有难度……反正我不会>_<)。问题是这玩意儿怎么转换成线段树呢?要转化成线段树,就要把每个点的子孙们都放到一片连续的空间里。这时,若使用DFS,遍历的顺序刚刚好符合要求,于是我们就DFSo(∩_∩)o 。DFS途中就可以算出每个点的及其子孙覆盖的区域。然后变成线段树之后呢,随便搞搞就行了o(∩_∩)o

 #include <cstdio>
#include <cstring> const int MAX = ; int flip[MAX*], sum[MAX*], cnt[MAX*];//tree
int head[MAX], next[MAX], to[MAX], ecnt;
int beg[MAX], size[MAX], dfs_clock;
int y1, y2; void tle() {while() ;} void init() {
ecnt = ;
dfs_clock = ;
memset(head, , sizeof(head));
memset(flip, , sizeof(flip));
memset(cnt, , sizeof(cnt));
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
} void dfs(int x) {
size[x] = ;
beg[x] = ++dfs_clock;
for(int p = head[x]; p; p = next[p]) {
dfs(to[p]);
size[x] += size[to[p]];
}
} void maintain(int x, int l, int r) {
int lc = x * , rc = x * + ;
if(l < r) {
cnt[x] = cnt[rc] + cnt[lc];
}
} void pushdown(int x) {
int lc = x * , rc = x * + ;
if(flip[x]) {
flip[x] = ;
flip[lc] ^= ;
cnt[lc] = sum[lc] - cnt[lc];
flip[rc] ^= ;
cnt[rc] = sum[rc] - cnt[rc];
}
} void update(int x, int l, int r) {
int lc = x * , rc = x * + ;
if(y1 <= l && r <= y2) {
flip[x] ^= ;
cnt[x] = sum[x] - cnt[x];
}
else {
pushdown(x);
int mid = (l + r) / ;
if(y1 <= mid) update(lc, l, mid);
if(mid < y2) update(rc, mid + , r);
maintain(x, l, r);
}
} int ans; void query(int x, int l, int r) {
int lc = x * , rc = x * + ;
if(y1 <= l && r <= y2) ans += cnt[x];
else {
pushdown(x);
int mid = (l + r) / ;
if(y1 <= mid) query(lc, l, mid);
if(mid < y2) query(rc, mid + , r);
}
} void build(int x, int l, int r) {
int lc = x * , rc = x * + ;
if(l == r) {
sum[x] = ;
}
else {
int mid = (l + r) / ;
build(lc, l, mid);
build(rc, mid + , r);
sum[x] = sum[lc] + sum[rc];
}
} int main() {
int n, m, x;
char c[];
while(scanf("%d%d", &n, &m) != EOF) {
init();
for(int i = ; i <= n; ++i) {
scanf("%d", &x);
add_edge(x, i);
}
dfs();
build(, , n);
while(m--) {
scanf("%s%d", c, &x);
y1 = beg[x]; y2 = beg[x] + size[x] - ;
if(c[] == 'o') {
update(, , n);
}
if(c[] == 'q') {
ans = ;
query(, , n);
printf("%d\n", ans);
}
}
puts("");
}
}

ZOJ 3686 A Simple Tree Problem(线段树)的更多相关文章

  1. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

  2. zoj 3686 A Simple Tree Problem (线段树)

    Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...

  3. ZOJ-3686 A Simple Tree Problem 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...

  4. bzoj 3489 A simple rmq problem - 线段树

    Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...

  5. hdu 4973 A simple simulation problem. (线段树)

    题目链接 题意: 给定n长的序列 m个操作 序列默认为 1, 2, 3···n 操作1:D [l,r] 把[l,r]区间增长 :( 1,2,3,4 进行 D [1,3]变成 1,1,2,2,3,3,4 ...

  6. BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

    A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...

  7. 【BZOJ4999】This Problem Is Too Simple!(线段树)

    [BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...

  8. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  9. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

随机推荐

  1. Spring Boot在反序列化过程中:jackson.databind.exc.InvalidDefinitionException cannot deserialize from Object value

    错误场景 用Spring boot写了一个简单的RESTful API,在测试POST请求的时候,request body是一个符合对应实体类要求的json串,post的时候报错. 先贴一段error ...

  2. Java分布式锁之数据库方式实现

    之前的文章<Java分布式锁实现>中列举了分布式锁的3种实现方式,分别是基于数据库实现,基于缓存实现和基于zookeeper实现.三种实现方式各有可取之处,本篇文章就详细讲解一下Java分 ...

  3. 对象转换成JSON字符串

    定义一个Student类: 1 class Student { 2 public $name; 3 public $age; 4 function __construct($name, $age) { ...

  4. Win7装在其他盘 (非C盘)办法

    Win7装在其他盘 (非C盘)办法 1]将GHO还原到其他盘(非C盘),如H盘 2]用进U盘系统,里的工具,恢复启动H盘 3]将H盘的Boot文件夹,及其他根目录的所有文件复制到C盘根目录,重启即可开 ...

  5. 组播___IGMP

    一.基本概念: 1.协议概述: 是运行在主机和与主机直连的路由器之间,其实现的功能是双向的:一方面,主机通过IGMP通知路由器希望接收某个特定组播组的信息:另一方面,路由器通过IGMP周期性地查询局域 ...

  6. Python写网络后台脚本

    Python写网络后台脚本. 首先安装Python3.6.5,在centos中自带的Python是2.6版本的,现在早就出现了3.6版本了况且2和3 之间的差距还是比较大的,所以我选择更新一下Pyth ...

  7. vim 版本更新

    sudo add-apt-repository ppa:jonathonf/vim sudo apt update sudo apt install vim 如果您想要卸载它, 请使用如下命令 sud ...

  8. war2 洛谷模拟赛day2 t3 状压

    (new )   war2 题解:总体数据而言,我们很容易想到着就是DP啊,我们DP数组,用状态压缩,代表有那些点已经被占领过了,代表上一次我占的是那个.对于每一次状态转移,若当前我们要占领的Port ...

  9. 北京Uber优步司机奖励政策(2月18日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  10. 如何理解 UL94HB , UL94-V0 , UL94-V1 , UL94-V2

    塑料阻燃等级由HB,V-2,V-1向V-0逐级递增: UL94V0,V1,V2是不同的阻燃等级,其等级不同,耐燃的测试方法亦不同,测试判定的标准也不同. V0的测试方法是将测试物倾斜45度,用酒精灯点 ...