首页 技术 正文
技术 2022年11月10日
0 收藏 916 点赞 5,086 浏览 2178 个字

A Simple Tree Problem


Time Limit: 3 Seconds      Memory Limit: 65536 KB


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

We define this kind of operation: given a subtree, negate all its labels.

And we want to query the numbers of 1’s of a subtree.

Input

Multiple test cases.

First line, two integer N and M, denoting the numbers of nodes and numbers of operations and queries.(1<=N<=100000, 1<=M<=10000)

Then a line with N-1 integers, denoting the parent of node 2..N. Root is node 1.

Then M lines, each line are in the format “o node” or “q node“, denoting we want to operate or query on the subtree with root of a certain node.

Output

For each query, output an integer in a line.

Output a blank line after each test case.

Sample Input

3 2
1 1
o 2
q 1

Sample Output

1

Author: CUI, Tianyi
Contest: ZOJ Monthly, March 2013

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 using namespace std; vector<int> g[];
int n,m,id; const int maxn=; struct Interval
{
int from,to;
}I[]; void dfs(int node)
{
I[node].from=id;
id++;
int t=g[node].size();
for(int i=;i<t;i++)
{
dfs(g[node][i]);
}
I[node].to=id-;
} int m0[maxn<<],m1[maxn<<],xxo[maxn<<]; void push_up(int rt)
{
m0[rt]=m0[rt<<]+m0[rt<<|];
m1[rt]=m1[rt<<]+m1[rt<<|];
} void push_down(int rt)
{
if(xxo[rt])
{
xxo[rt<<]^=; xxo[rt<<|]^=;
swap(m0[rt<<|],m1[rt<<|]);swap(m0[rt<<],m1[rt<<]);
xxo[rt]=;
}
} void build(int l,int r,int rt)
{
xxo[rt]=;m0[rt]=;m1[rt]=;
if(l==r)
{
m0[rt]=; m1[rt]=;
return ;
}
int m=(l+r)>>;
push_down(rt);
build(lson); build(rson);
push_up(rt);
} void update(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
xxo[rt]^=;
swap(m0[rt],m1[rt]);
return ;
}
int m=(l+r)>>;
push_down(rt);
if(L<=m) update(L,R,lson);
if(R>m) update(L,R,rson);
push_up(rt);
} int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
push_down(rt);
return m1[rt];
}
int m=(l+r)>>,ret=;
push_down(rt);
if(L<=m) ret+=query(L,R,lson);
if(R>m) ret+=query(L,R,rson);
push_up(rt);
return ret;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<=n+;i++) g[i].clear();
memset(m0,,sizeof(m0));
memset(m1,,sizeof(m1));
memset(xxo,,sizeof(xxo));
for(int i=;i<=n;i++)
{
int a;
scanf("%d",&a);
g[a].push_back(i);
}
id=;
dfs();
build(,n,);
while(m--)
{
char cmd[]; int a;
scanf("%s%d",cmd,&a);
if(cmd[]=='o') update(I[a].from,I[a].to,,n,);
else if(cmd[]=='q') printf("%d\n",query(I[a].from,I[a].to,,n,));
}
putchar();
}
return ;
}
上一篇: yum 操作复习
下一篇: Python-pandas
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294