单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护.

   1:  /*
   2:  DFS序 +线段树
   3:  */
   4:   
   5:  #include <cstdio>
   6:  #include <cstring>
   7:  #include <cctype>
   8:  #include <algorithm>
   9:  #include <vector>
  10:  #include <iostream>
  11:  using namespace std;
  12:   
  13:  #define LL(a)   a<<1
  14:  #define RR(a)   a<<1|1
  15:   
  16:  const int MaxL = 200002;
  17:   
  18:  int pre[MaxL];  // first travel
  19:  int nxt[MaxL];  // nxt travel
  20:  bool vis[MaxL];
  21:  int N;
  22:  vector<vector<int> > E(MaxL);
  23:   
  24:  struct Seg_tree
  25:  {
  26:      int left, right;
  27:      int sum;
  28:  } tt[MaxL<<2];
  29:   
  30:   
  31:  void PushUp(int idx)
  32:  {
  33:      tt[idx].sum = tt[LL(idx)].sum + tt[RR(idx)].sum;
  34:  }
  35:   
  36:  void build(int l,int r,int idx)
  37:  {
  38:      tt[idx].left = l, tt[idx].right = r;
  39:      tt[idx].sum = r-l+1;
  40:      if (l == r) return ;
  41:      int m = (l + r) >> 1;
  42:      build(l,m, LL(idx));
  43:      build(m+1, r, RR(idx));
  44:  }
  45:   
  46:  void update(int p, int idx = 1)
  47:  {
  48:      if(p == tt[idx].left && p == tt[idx].right)
  49:      {
  50:          tt[idx].sum ^=1;
  51:          return ;
  52:      }
  53:      int mid = (tt[idx].left + tt[idx].right)>>1;
  54:      if(p <= mid) update(p, LL(idx));
  55:      else update(p, RR(idx));
  56:      PushUp(idx);
  57:  }
  58:   
  59:  int query(int l, int r, int idx = 1)
  60:  {
  61:      if(l == tt[idx].left && r ==tt[idx].right)
  62:          return tt[idx].sum;
  63:      int mid = (tt[idx].left + tt[idx].right)>>1;
  64:      if(r <= mid) return query(l,r, LL(idx));
  65:      else if(l> mid) return query(l,r, RR(idx));
  66:      else
  67:          return query(l, mid, LL(idx))+ query(mid+1, r, RR(idx));
  68:  }
  69:   
  70:  int mark = 1;
  71:  void dfs( int a)
  72:  {
  73:      vis[a] = 1;
  74:      pre[a] = mark++;
  75:      for(int i=0; i<E[a].size(); i++)
  76:      {
  77:          if(!vis[E[a][i]])
  78:              dfs(E[a][i]);
  79:      }
  80:      nxt[a] = mark++;
  81:  }
  82:  int main()
  83:  {
  84:  //    freopen("1.txt","r",stdin);
  85:      memset(pre, 0, sizeof(pre));
  86:      memset(nxt, 0 ,sizeof(nxt));
  87:      memset(vis, 0 ,sizeof(vis));
  88:   
  89:      scanf("%d", &N);
  90:      for(int i=1; i<N; i++)
  91:      {
  92:          int a,b;
  93:          scanf("%d%d", &a,&b);
  94:          E[a].push_back(b);
  95:          E[b].push_back(a);
  96:      }
  97:      dfs(1);
  98:   
  99:      N*=2;
 100:      build(1, N, 1);
 101:      int M;
 102:      scanf("%d", &M);
 103:      for(int i=1; i<=M; i++)
 104:      {
 105:          char c;
 106:          int a;
 107:          scanf("%s", &c);
 108:          scanf("%d",&a);
 109:          if(c =='Q')
 110:          {
 111:              cout<<query(pre[a],nxt[a], 1) /2<<endl;
 112:          }
 113:          else
 114:          {
 115:              update(pre[a],1);
 116:              update(nxt[a],1);
 117:          }
 118:      }
 119:      return 0;
 120:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

POJ 3321 DFS序+线段树的更多相关文章

  1. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  2. dfs序线段树

    dfs序+线段树,啥?如果在一棵树上,需要你修改一些节点和查询一些节点,如果直接dfs搜的话肯定超时,那用线段树?树结构不是区间啊,怎么用?用dfs序将树结构转化为一个区间,就能用线段树进行维护了. ...

  3. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  4. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  5. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  6. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

  7. 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树

    题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...

  8. F - Change FZU - 2277 (DFS序+线段树)

    题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...

  9. BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树

    题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...

随机推荐

  1. 转: Android基于HLS和RTMP协议的第三方SDK选择

    转自: http://isunxu.xyz/android/between-rtmp-and-hls-third-party-choice/ 协议的详解网上资料都太多了,我就不赘述了.Android上 ...

  2. java中的@Override是否需要

    java中的重载注解 @Override 是否需要?今天被人问到这个问题,回答的不太好,下来看了一下源码 /** * Annotation type used to mark methods that ...

  3. (转)Android之自定义适配器

    ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果. 有这样一个Demo ...

  4. iOS 父子关系

    1.面向对象特征,类的继承 成员变量(实例变量) 子类继承父类所有功能,只能直接(访问)调用父类中的.h中的protect和public成员变量(实例变量)及方法, .h中的私有的成员变量,子类不能直 ...

  5. iOS - 移动设备防丢失App

    一.原理 二.数据获取 三.报警

  6. Android开发之切换activity动画overridePendingTransition

    原文地址:http://blog.sina.com.cn/s/blog_706c449f01011s3v.html overridePendingTransition 在startActivity() ...

  7. ionic项目相关的操作命令

     更新npmD:\Program Files\npm-3.9.0\npmnode cli.js install npm -gf vs安装 更新node.js  windows版直接从官网下载安装包 n ...

  8. 8款效果惊艳的HTML5 3D动画

    1.HTML5 WebGL水面水波荡漾特效 之前已经向各位分享过一款很逼真的HTML5水波荡漾特效,效果还算不错.今天再向大家分享一款更加给力的HTML5水波动画,画面上是一个大水池,水池底部是一颗大 ...

  9. berkerly db 中简单的读写操作(有一些C的 还有一些C++的)

    最近在倒腾BDB,才发现自己确实在C++这一块能力很弱,看了一天的api文档,总算是把BDB的一些api之间的关系理清了,希望初学者要理清数据库基本知识中的环境,句柄,游标的基本概念,这样有助于你更好 ...

  10. 14.Apache配置

    环境:                                                      ↗  atl.example.com  (192.168.1.101) ↗ www.e ...