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

  1. 3 2
  2. 1 1
  3. o 2
  4. q 1

Sample Output

  1. 1

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5.  
  6. #define lson l,m,rt<<1
  7. #define rson m+1,r,rt<<1|1
  8.  
  9. using namespace std;
  10.  
  11. vector<int> g[];
  12. int n,m,id;
  13.  
  14. const int maxn=;
  15.  
  16. struct Interval
  17. {
  18. int from,to;
  19. }I[];
  20.  
  21. void dfs(int node)
  22. {
  23. I[node].from=id;
  24. id++;
  25. int t=g[node].size();
  26. for(int i=;i<t;i++)
  27. {
  28. dfs(g[node][i]);
  29. }
  30. I[node].to=id-;
  31. }
  32.  
  33. int m0[maxn<<],m1[maxn<<],xxo[maxn<<];
  34.  
  35. void push_up(int rt)
  36. {
  37. m0[rt]=m0[rt<<]+m0[rt<<|];
  38. m1[rt]=m1[rt<<]+m1[rt<<|];
  39. }
  40.  
  41. void push_down(int rt)
  42. {
  43. if(xxo[rt])
  44. {
  45. xxo[rt<<]^=; xxo[rt<<|]^=;
  46. swap(m0[rt<<|],m1[rt<<|]);swap(m0[rt<<],m1[rt<<]);
  47. xxo[rt]=;
  48. }
  49. }
  50.  
  51. void build(int l,int r,int rt)
  52. {
  53. xxo[rt]=;m0[rt]=;m1[rt]=;
  54. if(l==r)
  55. {
  56. m0[rt]=; m1[rt]=;
  57. return ;
  58. }
  59. int m=(l+r)>>;
  60. push_down(rt);
  61. build(lson); build(rson);
  62. push_up(rt);
  63. }
  64.  
  65. void update(int L,int R,int l,int r,int rt)
  66. {
  67. if(L<=l&&r<=R)
  68. {
  69. xxo[rt]^=;
  70. swap(m0[rt],m1[rt]);
  71. return ;
  72. }
  73. int m=(l+r)>>;
  74. push_down(rt);
  75. if(L<=m) update(L,R,lson);
  76. if(R>m) update(L,R,rson);
  77. push_up(rt);
  78. }
  79.  
  80. int query(int L,int R,int l,int r,int rt)
  81. {
  82. if(L<=l&&r<=R)
  83. {
  84. push_down(rt);
  85. return m1[rt];
  86. }
  87. int m=(l+r)>>,ret=;
  88. push_down(rt);
  89. if(L<=m) ret+=query(L,R,lson);
  90. if(R>m) ret+=query(L,R,rson);
  91. push_up(rt);
  92. return ret;
  93. }
  94.  
  95. int main()
  96. {
  97. while(scanf("%d%d",&n,&m)!=EOF)
  98. {
  99. for(int i=;i<=n+;i++) g[i].clear();
  100. memset(m0,,sizeof(m0));
  101. memset(m1,,sizeof(m1));
  102. memset(xxo,,sizeof(xxo));
  103. for(int i=;i<=n;i++)
  104. {
  105. int a;
  106. scanf("%d",&a);
  107. g[a].push_back(i);
  108. }
  109. id=;
  110. dfs();
  111. build(,n,);
  112. while(m--)
  113. {
  114. char cmd[]; int a;
  115. scanf("%s%d",cmd,&a);
  116. if(cmd[]=='o') update(I[a].from,I[a].to,,n,);
  117. else if(cmd[]=='q') printf("%d\n",query(I[a].from,I[a].to,,n,));
  118. }
  119. putchar();
  120. }
  121. return ;
  122. }

ZOJ 3686 A Simple Tree Problem的更多相关文章

  1. 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 ...

  2. zoj 3686 A Simple Tree Problem (线段树)

    Solution: 根据树的遍历道的时间给树的节点编号,记录下进入节点和退出节点的时间.这个时间区间覆盖了这个节点的所有子树,可以当做连续的区间利用线段树进行操作. /* 线段树 */ #pragma ...

  3. BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】

    A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. O ...

  4. xtu数据结构 I. A Simple Tree Problem

    I. A Simple Tree Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld     ...

  5. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  6. ZOJ-3686 A Simple Tree Problem 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 题意:给定一颗有根树,每个节点有0和1两种值.有两种操作: ...

  7. BZOJ 3489: A simple rmq problem

    3489: A simple rmq problem Time Limit: 40 Sec  Memory Limit: 600 MBSubmit: 1594  Solved: 520[Submit] ...

  8. hdu4976 A simple greedy problem. (贪心+DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4976 2014 Multi-University Training Contest 10 1006 A simp ...

  9. hdu 1757 A Simple Math Problem (乘法矩阵)

    A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. hibernate学习-HibernateDemo

    上篇文章我们讲述了eclipse安装hibernate插件的过程,这篇文章我们来做第一个HibernateDemo. 1).hibernate的jar开发包的下载,官网下载地址:http://hibe ...

  2. tst、cmp、bne、beq指令

    1.tst:逻辑处理指令,用于把一个寄存器的内容和另一个寄存器的内容或立即数进行按位的与运算,并根据运算结果更新CPSR中条件标志位的值.当前运算结果为1,则Z=0:当前运算结果为0,则Z=1 cmp ...

  3. ubuntu14.04 yuv文件的播放及视频信息的查看

    1.安装ffmpeg sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install ...

  4. Windows 上如何安装Sqlite

    对SQLite文明已久,却是从来没使用过,今天就来安装试用下. 一.安装 下载地址:http://www.sqlite.org/download.html 将Precompiled Binaries ...

  5. re

    Python3正则表达式应用: 目的:获取匹配的字符串 输出:直接输出或是.group() / .group(0) 常用函数: re.compile 可以把正则表达式编译成一个正则表达式对象,这样可以 ...

  6. tomcat启动报错: org.apache.catalina.deploy.WebXml addFilter

    解决方法为:在Tomacat7的context.xml文件里的<Context>中加上<Loader delegate="true" />

  7. bootstrap-监听滚动实现头部跟随滚动

    实现案例 <body data-spy="scroll" data-target="#bs-example-navbar-collapse-1"> ...

  8. Jquery 循环map的用法

    $.each(map,function(key,values){console.log(key);$(values).each(function(){console.log("\t" ...

  9. BZOJ-2127-happiness(最小割)

    2127: happiness(题解) Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1806  Solved: 875 Description 高一 ...

  10. struts-hibernate-ajax完成区县和街道级联下拉框功能

    前言:这次dao用的是hibernate,控制层和显示层用的是struts,页面用的是ajax... 啰嗦:我做这个用了很久,用了2周,难点没破解的地方,hibernate的多对一关系生成实体类中属性 ...