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 ...
随机推荐
- hibernate学习-HibernateDemo
上篇文章我们讲述了eclipse安装hibernate插件的过程,这篇文章我们来做第一个HibernateDemo. 1).hibernate的jar开发包的下载,官网下载地址:http://hibe ...
- tst、cmp、bne、beq指令
1.tst:逻辑处理指令,用于把一个寄存器的内容和另一个寄存器的内容或立即数进行按位的与运算,并根据运算结果更新CPSR中条件标志位的值.当前运算结果为1,则Z=0:当前运算结果为0,则Z=1 cmp ...
- ubuntu14.04 yuv文件的播放及视频信息的查看
1.安装ffmpeg sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install ...
- Windows 上如何安装Sqlite
对SQLite文明已久,却是从来没使用过,今天就来安装试用下. 一.安装 下载地址:http://www.sqlite.org/download.html 将Precompiled Binaries ...
- re
Python3正则表达式应用: 目的:获取匹配的字符串 输出:直接输出或是.group() / .group(0) 常用函数: re.compile 可以把正则表达式编译成一个正则表达式对象,这样可以 ...
- tomcat启动报错: org.apache.catalina.deploy.WebXml addFilter
解决方法为:在Tomacat7的context.xml文件里的<Context>中加上<Loader delegate="true" />
- bootstrap-监听滚动实现头部跟随滚动
实现案例 <body data-spy="scroll" data-target="#bs-example-navbar-collapse-1"> ...
- Jquery 循环map的用法
$.each(map,function(key,values){console.log(key);$(values).each(function(){console.log("\t" ...
- BZOJ-2127-happiness(最小割)
2127: happiness(题解) Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 1806 Solved: 875 Description 高一 ...
- struts-hibernate-ajax完成区县和街道级联下拉框功能
前言:这次dao用的是hibernate,控制层和显示层用的是struts,页面用的是ajax... 啰嗦:我做这个用了很久,用了2周,难点没破解的地方,hibernate的多对一关系生成实体类中属性 ...