zoj 3686 A Simple Tree Problem (线段树)
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 (线段树)的更多相关文章
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
- 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 ...
- ZOJ-3686 A Simple Tree Problem 线段树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- 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 ...
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
- 【BZOJ4999】This Problem Is Too Simple!(线段树)
[BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...
- xtu数据结构 I. A Simple Tree Problem
I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
随机推荐
- 5 crucial optimizations for SSD usage in Ubuntu Linux
I bought my first SSD more than 5 years ago (late 2007), for my white MacBook Core2Duo 2.0 Ghz. It m ...
- Java到底是不是一种纯面向对象语言?
本文由码农网 – Dee1024原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! Java——是否确实的 “纯面向对象”?让我们深入到Java的世界,试图来证实它. 在我刚开始学习 J ...
- spring boot配置写法
转自:http://blog.csdn.net/mickjoust/article/details/51646658 每日金句 你要搞清楚自己人生的剧本:不是你父母的续集,不是你子女的前传,更不是你朋 ...
- 标准I/O库之格式化I/O
本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. 一.格式化输出 执行格式化输出处理的是4个pr ...
- MVC - HtmlHelper类
传统的Html元素不能和服务端数据进行绑定 HtmlHelper类提供了一系列的方法来生成Html元素 并可以实现与数据绑定在一起 然后生成Html Html.BeginForm(actionName ...
- 15 Linux Split and Join Command Examples to Manage Large Files--reference
by HIMANSHU ARORA on OCTOBER 16, 2012 http://www.thegeekstuff.com/2012/10/15-linux-split-and-join-co ...
- leecode 每日解题思路 102-Binary Tree Level Order Traversal
題目描述: 题目链接: 102-Binary Tree Level Order Traversal 这个问题要解决的是如何逐层遍历一个二叉树,并把同一层元素放入同一list中, 再将所有元素返回. 其 ...
- OC最实用的runtime总结,面试、工作你看我就足够了!
前言 runtime的资料网上有很多了,部分有些晦涩难懂,我通过自己的学习方法总结一遍,主要讲一些常用的方法功能,以实用为主,我觉得用到印象才是最深刻的,并且最后两个demo也是MJExtension ...
- arcgis10 安装1721错误
arcgis10 安装1721错误,主要是ArcGIS License Manager 服务程序的位置不对,注册表lmgrd.exe中修改lmgrd.ex
- .net System.TypeInitializationException 类型初始值设定项引发异常
错误原因:静态字段连接数据库内容错误(应为Integrated Security=True) 果然解决! 来自:http://blog.csdn.net/zhang1597116/article/de ...