单点修改树中某个节点,查询子树的性质.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. 一步步搭建自己的轻量级MVCphp框架-(四)一个国产轻量级框架Amysql源码分析(3) 总进程对象

    AmysqlProcess类,框架的总进程对象 ./Amysql/Amysql.php 下面还是和以前一样,先上代码~ class AmysqlProcess { public $AmysqlCont ...

  2. Table of Contents - MyBatis

    Getting Started with MyBatis Hello World Integration with Spring Bootstrapping MyBatis Configuring M ...

  3. 增强for循环用法___ArrayList数组实现使用下标最好,LinkedList使用增强型的(转载)

    总结: 1.For-Each循环的缺点:丢掉了索引信息. 当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息. ...

  4. blogs

    http://blogs.msdn.com/b/tess/archive/2008/02/04/net-debugging-demos-information-and-setup-instructio ...

  5. ASP.NET 跨域获取JSON天气数据

    前几天做一个门户网站,在首页需要加载气象数据,采用了中央气象局的接口. 刚开始采用JSONP在前台跨域请求数据,没成功~ 后换成在c#后台请求数据返回... 前端代码: $(function () { ...

  6. 使用runtime给类动态添加方法并调用 - class_addMethod

    上手开发 iOS 一段时间后,我发现并不能只着眼于完成需求,利用闲暇之余多研究其他的开发技巧,才能在有限时间内提升自己水平.当然,“其他开发技巧”这个命题对于任何一个开发领域都感觉不找边际,而对于我来 ...

  7. C语言知识总结(5)

    预处理指令 C语言提供的预处理指令主要有:宏定义.文件包含.条件编译 宏定义 不带参数的宏定义 1>一般形式 #define 宏名 字符串 比如#define A 10 2>作用 它的作用 ...

  8. 升级ionic版本后,创建新项目报Error Initializing app错误解决

    命令行,进入项目路径后,运行 ionic start myApp --v2 命令执行后,报如下错误 Installing npm packages...Error with start undefin ...

  9. 使用notepad++编辑器

    使用notepad++编辑器 在公司时经常要用到文本编辑器去写jsp文件,之前使用的是sublime text 3,但是觉得不太顺手,于是转用notepad++编辑器. 这个编辑器最吸引我的地方是层次 ...

  10. [NOIP2014]解方程

    3732 解方程  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 输入描述 Input Descrip ...