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. 页面置换算法(最佳置换算法、FIFO置换算法、LRU置换算法、LFU置换算法)

    页面置换产生的原因是:分页请求式存储管理(它是实现虚拟存储管理的方法之一,其中一个特性是多次性-->多次将页面换入或换出内存) 效果最好的页面置换算法:最佳置换算法 比较常用的页面置换算法有:F ...

  2. algorithm@ KMP

    一. KMP算法 KMP算法是一种改进的字符串匹配算法,由D.E.Knuth与V.R.Pratt和J.H.Morris同时发现,简称KMP算法.KMP算法的关键是利用匹配失败后的信息,尽量减少模式串与 ...

  3. poj 1459 Power Network【建立超级源点,超级汇点】

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25514   Accepted: 13287 D ...

  4. PAT 07-图6 旅游规划 (25分)

    有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条路径都是最短的,那么需要输出最便 ...

  5. windows批量创建用户

    一.建立用户的命令行语法: 建立用户:net  user  用户名  密码  /add           (如:net user test 123 /add)  提升权限:net  localgro ...

  6. Java凝视Override、Deprecated、SuppressWarnings具体解释

    一.什么是凝视     说起凝视,得先提一提什么是元数据(metadata).所谓元数据就是数据的数据.也就是说,元数据是描写叙述数据的.就象数据表中的字段一样,每一个字段描写叙述了这个字段下的数据的 ...

  7. [GIF] GIF Loop Coder - Animating with Arrays

    In this lesson, we discuss animating using arrays, and how different data types are interpolated whi ...

  8. ubuntu 远程 ubuntu

    一:被远程端ubuntu配置 參考windows远程ubuntu这篇文章里面的ubuntu配置 二:远程端ubuntu配置 1:打开Remmina Remote Desktop Client软件,例如 ...

  9. oepn sync

    http://blog.csdn.net/cywosp/article/details/8767327 SYNOPSIS #include <sys/types.h> #include & ...

  10. How to add route for IPV6 interface

    Firewall : ifconfig eth1 inet6 add 2000::1/64 ifconfig eth4 inet6 add 5000::1/64 ------------------- ...