ZOJ 3686 A Simple Tree Problem
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 ;
}
ZOJ 3686 A Simple Tree Problem的更多相关文章
- 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 (线段树)
Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...
- BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...
- xtu数据结构 I. A Simple Tree Problem
I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- ZOJ-3686 A Simple Tree Problem 线段树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...
- BZOJ 3489: A simple rmq problem
3489: A simple rmq problem Time Limit: 40 Sec Memory Limit: 600 MBSubmit: 1594 Solved: 520[Submit] ...
- hdu4976 A simple greedy problem. (贪心+DP)
http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- 【Pyrosim案例】02:简单燃烧
1 案例说明 本案例介绍一个简单的燃烧模拟. 本案例通过指定热释放率(Heat Release Rate,HRR)来定义一个500kW的燃烧火焰.利用热释放率来定义燃烧火焰在火灾安全工程中描述火焰的一 ...
- java设计模式之建造者模式
学习了设计模式,一直感觉有进步又没有进步,与同学.同事探讨了一下.变化不可能一会就可以的,需要努力坚持.不管进步大小,也不管是否进步,做到勿忘初心,做自己喜欢的事情就好.还有几个设计模式一直没有写,原 ...
- iframe的优缺点
HTML框架简述 一个浏览器窗体可以通过几个页面的组合来显示.我们可以使用框架来完成(frames)这项工作.(框架可以把HTML文档分为多个页面) 框架页使用了表格的方式组合,可以分为数行与 ...
- 解决 node-gyp rebuild 卡住 的问题
node-gyp在编译前会首先尝试下载node的headers文件,像这样: gyp http GET https://nodejs.org/download/release/v6.8.1/node- ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 单点登录改进版-使用ajax分发cookie避免重定向轮询
前言 继上一篇博文:可跨域的单点登录(SSO)实现方案[附.net代码]虽然实现了单点登录,也存在很多不合理的地方.很多热心的朋友也给出了很多指导性的意见,特别是 LoveCoder提出的意见. 在很 ...
- python基础-生成随机字符串方法
python解释器示例 >>> import uuid >>> uuid.uuid1() UUID('ae6822e6-c976-11e6-82e0-0090f5f ...
- beanstalkd 消息队列
概况:Beanstalkd,一个高性能.轻量级的分布式内存队列系统,最初设计的目的是想通过后台异步执行耗时的任务来降低高容量Web应用系统的页面访问延迟,支持过有9.5 million用户的Faceb ...
- 精选30道Java笔试题解答
转自:http://www.cnblogs.com/lanxuezaipiao/p/3371224.html 都 是一些非常非常基础的题,是我最近参加各大IT公司笔试后靠记忆记下来的,经过整理献给与我 ...
- 如何查看mysql数据库的端口
启动,并进入mysql后,输入命令:show global variables like 'port'; 如下图所示.