http://codeforces.com/contest/877/problem/E

真的菜的不行,自己敲一个模板,到处都是问题。哎

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 2e5+;
  4. #define lson (q<<1)
  5. #define rson ((q<<1)|1)
  6. struct node
  7. {
  8. int l,r,mid;
  9. int v,lazy;
  10. }tree[maxn*];
  11. int L[maxn],R[maxn],index;
  12. int vis[maxn] = {};
  13. int a[maxn];
  14. vector<int> g[maxn];
  15. void dfs(int x)
  16. {
  17. L[x] = index;
  18. for(int i=;i<g[x].size();i++)
  19. {
  20. int v = g[x][i];
  21. if(!vis[v])
  22. {
  23. vis[v] = ;
  24. index++;
  25. dfs(v);
  26. }
  27. }
  28. R[x] = index;
  29. }
  30. void push_up(int q)
  31. {
  32. tree[q].v = tree[lson].v+tree[rson].v;
  33. }
  34. void build(int l,int r,int q)
  35. {
  36. tree[q].l = l,tree[q].r = r,tree[q].mid = (l+r)/;
  37. tree[q].lazy = ;
  38. if(l==r)
  39. {
  40. tree[q].v = a[l];
  41. //printf("%d %d\n",l,tree[q].v);
  42. return;
  43. }
  44. build(l,tree[q].mid,lson);
  45. build(tree[q].mid+,r,rson);
  46. push_up(q);
  47. }
  48. void push_down(int q)
  49. {
  50. if(tree[q].lazy)
  51. {
  52. tree[lson].v = (tree[lson].r-tree[lson].l+)-tree[lson].v;
  53. tree[rson].v = (tree[rson].r-tree[rson].l+)-tree[rson].v;
  54. tree[q].lazy ^= ;
  55. tree[lson].lazy ^= ;
  56. tree[rson].lazy ^= ;
  57. }
  58. }
  59. void update(int l,int r,int q)
  60. {
  61. if(tree[q].l>=l&&tree[q].r<=r)
  62. {
  63. tree[q].lazy ^= ;
  64. tree[q].v = (tree[q].r-tree[q].l+)-tree[q].v;
  65. return;
  66. }
  67. push_down(q);
  68. if(l<=tree[q].mid) update(l,r,lson);
  69. if(r>=tree[q].mid+) update(l,r,rson);
  70. push_up(q); ///v值由下往上更新
  71. }
  72. int query(int l,int r,int q) ///由上及下
  73. {
  74. if(tree[q].l>=l&&tree[q].r<=r)
  75. {
  76. // printf("%d %d %d %d\n",tree[q].l,tree[q].r,tree[q].v,tree[q].lazy);
  77. return tree[q].v;
  78. }
  79. push_down(q);
  80. int sum = ;
  81. if(l<=tree[q].mid) sum += query(l,r,lson);
  82. if(r>=tree[q].mid+) sum += query(l,r,rson);
  83. return sum;
  84. }
  85. int main()
  86. {
  87. int n;scanf("%d",&n);
  88. for(int i=;i<=n;i++)
  89. {
  90. int x;scanf("%d",&x);
  91. g[x].push_back(i);
  92. g[i].push_back(x);
  93. }
  94. index = ;vis[] = ;
  95. dfs();
  96. for(int i=;i<=n;i++)
  97. {
  98. int cc;
  99. scanf("%d",&cc);
  100. a[L[i]] = cc; ///按dfs序来赋值
  101. }
  102. build(,n,);
  103. int q;scanf("%d",&q);
  104. while(q--)
  105. {
  106. char s[];
  107. int v;
  108. scanf("%s %d",s,&v);
  109. if(s[]=='g')
  110. {
  111. printf("%d\n",query(L[v],R[v],));
  112. }
  113. else
  114. {
  115. update(L[v],R[v],);
  116. }
  117. }
  118. return ;
  119. }
  120. /*
  121. 10
  122. 1 2 3 3 5 5 7 7 8
  123. 0 0 0 0 1 1 1 1 0 0
  124. 10
  125. pow 3
  126. get 3
  127. */

Codeforces Round #442 (Div. 2) Danil and a Part-time Job的更多相关文章

  1. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  2. Codeforces Round #442 Div.2 A B C D E

    A. Alex and broken contest 题意 判断一个字符串内出现五个给定的子串多少次. Code #include <bits/stdc++.h> char s[110]; ...

  3. Codeforces Round #442 (Div. 2)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  4. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  5. 【Codeforces Round #442 (Div. 2) A】Alex and broken contest

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 注意是所有的名字里面,只出现了其中某一个名字一次. [代码] #include <bits/stdc++.h> usin ...

  6. 【Codeforces Round #442 (Div. 2) D】Olya and Energy Drinks

    [链接] 我是链接,点我呀:) [题意] 给一张二维点格图,其中有一些点可以走,一些不可以走,你每次可以走1..k步,问你起点到终点的最短路. [题解] 不能之前访问过那个点就不访问了.->即k ...

  7. 【Codeforces Round #442 (Div. 2) C】Slava and tanks

    [链接] 我是链接,点我呀:) [题意] 有n个位置,每个位置都可能有不定数量的tank; 你每次可以选择一个位置投掷炸弹. 并且,这个位置上的所有tank都会受到你的攻击. 并且失去一点体力. 然后 ...

  8. 【Codeforces Round #442 (Div. 2) B】Nikita and string

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举中间那一段从哪里开始.哪里结束就好 注意为空的话,就全是a. 用前缀和优化一下. [代码] #include <bits/ ...

  9. Codeforces Round #442 (Div. 2) B题【一道模拟题QAQ】

    B. Nikita and string One day Nikita found the string containing letters "a" and "b&qu ...

随机推荐

  1. Django与多个数据库交互

    定义数据库 在Django中使用多个数据库的第一步是告诉Django您将要使用的数据库服务器. 数据库可以有您选择的任何别名.但是,别名 default 有着特殊的意义.Django使用别名为 def ...

  2. Head First Python (二)

    if...else... 1 movies = ["The Holy Grail",1975,"Terry Jones & Terry Gilliam" ...

  3. LeetCode(228) Summary Ranges

    题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...

  4. ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash

    Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encryp ...

  5. Monkey与MonkeyRunner之间的区别

    为了支持黑盒自动化测试的场景,Android SDK提供了monkey和monkeyrunner两个测试工具,这两个测试工具除了名字类似外,还都可以向待测应用发送按键等消息,往往容易产生混淆,以下是他 ...

  6. CodeForces 379F 树的直径 New Year Tree

    题意:每次操作新加两个叶子节点,每次操作完以后询问树的直径. 维护树的直径的两个端点U,V,每次计算一下新加进来的叶子节点到U,V两点的距离,如果有更长的就更新. 因为根据树的直径的求法,若出现新的直 ...

  7. Ubuntu简单指令和热键的学习

    Ubuntu查看本机版本的方法 sudo lsb_release -a即可 注销linux: 输入:exit 注意,离开系统不是关机,基本上,linux本身已经有相当多的工作进行,所以你离开时,这次这 ...

  8. Leetcode 421.数组中两数的最大异或值

    数组中两数的最大异或值 给定一个非空数组,数组中元素为 a0, a1, a2, … , an-1,其中 0 ≤ ai < 231 . 找到 ai 和aj 最大的异或 (XOR) 运算结果,其中0 ...

  9. Game on Tree

    D - Game on Tree Time limit : 2sec / Memory limit : 256MB Score : 1100 points Problem Statement Ther ...

  10. 面向对象编程(四)继承,概念及super关键字,final关键字,Object类常见方法

    继承 概念: ①   继承背后的思想就是基于已存在的类来构建新类; ②   当从已存在类继承时,就重用了它的方法和属性,还可以添加新的方法和属性来定制新类以应对需求; ③   当从其它类导出的类叫作子 ...