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 ...
随机推荐
- HW4.46
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW4.45
public class Solution { public static void main(String[] args) { int count = 0; for(int i = 1; i < ...
- novnc ignoring socket not reay
- Spark shell里的语句探索
获得垃圾链接数据集的命令如下: wget http://www-stat.stanford.edu/~tibs/ElemStatLearn/datasets/spam.data scala> v ...
- 问题.NETwebservice其他电脑无法使用-测试窗体只能用于来自本地计算机的请求
问题:webservice其他电脑无法使用-测试窗体只能用于来自本地计算机的请求 问题现象: 本地测试没有问题. 其他电脑打开webservice出现 测试窗体只能用于来自本地计算机的请求. 或者 & ...
- Android基于XMPP Smack Openfire下学习开发IM(五)连接断开重连
学习过程中大家都碰到过连接被断开的问题给困扰吧,下面教大家如何做到连接断开后,重新连接 首先要创建连接监听器,用来监听连接状态,这里我写了一个类 继承了ConnectionListener,重写了里面 ...
- hdu 3996 (最大权闭合图)
题意:有n个区域布局,每个区域有一些金矿,挖开金矿需要一定的费用,可以得到一定的利润,要想挖开一个金矿,必须挖开所有在这个金矿上边的金矿,求最大利益,给的数据价值太大,用64位. 分析:如果一个金矿可 ...
- spring事物传播机制 事物隔离级别
Spring事务类型详解: PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. PROPAGATION_SUPPORTS--支持当前事务,如 ...
- String的点点滴滴
一.String 的 equals()到底比较的是什么?equals() 与 == 的区别? 当使用关系运算符==比较两个对象时,是比较两个对象使用的内存地址和内容是否相同,如果两个对象使用的是同一个 ...
- SOAP web service用AFNetWorking实现请求
问: This is my current call to (asmx) SOAP web service: NSString *soapMessage = [NSString stringWithF ...