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. Win7 64位 安装E10后 打不开的解决方案 -摘自网络

    但是后来发现点击IE图标没有任何反应,因此从网络上寻求帮助!将经验分享大家!进入注册表(运行regedit),找到 HKEY_CURRENT_USER\Software\Microsoft\Inter ...

  2. 问题-[Delphi]PixelFormat 图像颜色的数据格式

     PixelFormat: (指定图像中每个像素的颜色数据的格式) Delphi                                        微软                  ...

  3. Android UI开发第三十二篇——Creating a Navigation Drawer

    Navigation Drawer是从屏幕的左侧滑出,显示应用导航的视图.官方是这样定义的: The navigation drawer is a panel that displays the ap ...

  4. LLVM在静态分析上的增强 @ WWDC 2013

    在代码还没有真正跑起来的时候,可以利用Clang对代码进行静态分析. 1. 可以应用快捷键Shift+Command+B对项目代码进行分析: 2. 也可以针对某个文件进行分析(现有版本貌似不能针对特定 ...

  5. maven分模块间依赖注意事项

    1.被依赖模块应该先通过 maven -install 命令将该模块打包为jar发布到本地仓库 2.引用的模块通过在pom.xml文件中添加dependence引用 maven -package 将项 ...

  6. SecureCRT 终端仿真程序 v7.0.0.326 中文绿色便携破解版

    http://wd.jb51.net:81/201205/tools/SecureCRT_jb51.rar Secure CRT是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Re ...

  7. [Unity] How to stop camera rendering?

    http://answers.unity3d.com/questions/147988/how-to-pause-the-main-camera-.html I would simply pause ...

  8. POJ 2488-A Knight&#39;s Journey(DFS)

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31702   Accepted: 10 ...

  9. MediaProvider 数据库Audio相关表格相应关系

    Android的MediaProvider提供了一系列的Audio,Video,image等表格,用于APP检索相关的音视频图片信息. 首先来看看Audio相关的表格: ×图中颜色同样的部分表示来自于 ...

  10. 浅谈android4.0开发之GridLayout布局

    作者:李响 本文重点讲述了自android4.0版本号后新增的GridLayout网格布局的一些基本内容,并在此基础上实现了一个简单的计算器布局框架.通过本文,您可以了解到一些android UI开发 ...