Solution:

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

/*
线段树
*/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#define lson x<<1
#define rson x<< 1 | 1
using namespace std; const int MAX = ;
int w[MAX], to[MAX];
int tl[MAX << ], tr[MAX << ], flag[MAX << ], sum[MAX << ][];
int l, r;
struct Edge {
int v, ne;
} edge[MAX << ]; int head[MAX], cnt, num; void Dfs ( int u )
{
w[u] = ++num;
for ( int i = head[u]; i != ; i = edge[i].ne ) {
Dfs ( edge[i].v );
}
to[u] = num;
} void addE ( int u, int v )
{
edge[++cnt].v = v;
edge[cnt].ne = head[u], head[u] = cnt;
} void build ( int x, int l, int r )
{
flag[x] = sum[x][] = sum[x][] = ;
tl[x] = l, tr[x] = r;
if ( l == r ) {
sum[x][] = ;
return ;
}
int mid = ( l + r ) >> ;
build ( lson, l, mid ), build ( rson, mid + , r );
sum[x][] += sum[lson][] + sum[rson][];
} void push ( int x )
{
if ( flag[x] == ) return;
flag[x] = ;
flag[lson] ^= ;
swap ( sum[lson][], sum[lson][] );
flag[rson] ^= ;
swap ( sum[rson][], sum[rson][] );
}
void updata ( int x )
{
sum[x][] = sum[lson][] + sum[rson][];
sum[x][] = sum[lson][] + sum[rson][];
}
void modify ( int x )
{
if ( tl[x] >= l && tr[x] <= r ) {
flag[x] ^= ;
swap ( sum[x][], sum[x][] );
return ;
}
push ( x );
int mid = ( tl[x] + tr[x] ) >> ;
if ( l <= mid ) modify ( lson );
if ( r > mid ) modify ( rson );
updata ( x );
} int query ( int x )
{
if ( tl[x] >= l && tr[x] <= r ) {
return sum[x][];
}
push ( x );
int mid = ( tl[x] + tr[x] ) >> , ans = ;
if ( l <= mid ) ans += query ( lson );
if ( r > mid ) ans += query ( rson );
updata ( x );
return ans;
} int n, m; void init()
{
num = cnt = ;
memset ( head, , sizeof head );
}
int main()
{
while ( scanf ( "%d %d", &n, &m ) != EOF ) {
init();
for ( int i = , v; i <= n; i++ ) {
scanf ( "%d", &v );
addE ( v, i );
}
Dfs ( );
scanf ( "%d", &m );
build ( , , n );
char cmd[];
for ( int i = , u; i <= m; ++i ) {
scanf ( "%s %d", cmd, &u );
l = w[u], r = to[u];
if ( cmd[] == 'o' ) {
modify ( );
} else {
printf ( "%d\n", query ( ) );
}
}
putchar();
}
}

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(线段树)

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

  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. HDOJ-ACM1010(JAVA) 奇偶剪枝法 迷宫搜索

    转载声明:原文转自:http://www.cnblogs.com/xiezie/p/5568822.html 第一次遇到迷宫搜索,给我的感觉是十分惊喜的:搞懂这个的话,感觉自己又掌握了一项技能~ 个人 ...

  2. Java同步块(synchronized block)使用详解

    Java 同步块(synchronized block)用来标记方法或者代码块是同步的.Java同步块用来避免竞争.本文介绍以下内容: Java同步关键字(synchronzied) 实例方法同步 静 ...

  3. MySQL开发中常用的查询语句总结

    1.查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!>,!<,=>, ...

  4. 数据绑定之DataFormatString

    设定BoundField的DataFormatString,通常有以下几种 DataFormatString= "{0:C}" 货币,货币的格式取决于当前Thread中Cultur ...

  5. grub名词理解

    http://baike.baidu.com/link?url=HDv2WL37x1EBS51pCHqbGQIFx7aAJ91h-0afrjOy1UH6MjhKUFPnvjNkOU32OHdVoTS7 ...

  6. android游戏物理引擎开发——粒子系统(三)

    生病了,医院躺了几天,动了个小手术,动手术之后的几天在医院看了几本<大众软件>,又想到自己必须得买台台式机了,这破笔记本实在用不下去了,然后开始喜欢看些硬件的东西,等我熟悉了以后,写几个硬 ...

  7. C#中如何操作2个list

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:C#中如何操作2个list.

  8. android开源项目学习

    FBReaderJ FBReaderJ用于Android平台的电子书阅读器,它支持多种电子书籍格式包括:oeb.ePub和fb2.此外还支持直接读取zip.tar和gzip等压缩文档. 项目地址:ht ...

  9. UVA 185(暴力DFS)

      Roman Numerals  The original system of writing numbers used by the early Romans was simple but cum ...

  10. linux编译安装LAMP

    Linux安装Apache+MySQL+PHP 安装部分依赖 安装apr(可选) # tar -xf apr-1.5.0.tar.bz2 # cd apr-1.5.0 #./configure --p ...