查询树上某个节点的子节点的标号小于其标号的数目. 一个trick是建立线段树之后,从标号小的向标号大的来做更新.

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

.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; }

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

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

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

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

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

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

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

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

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

  5. POJ 3321 DFS序+线段树

    单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include < ...

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

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

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

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

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

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

  9. BZOJ1103 [POI2007]大都市meg dfs序 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...

随机推荐

  1. Storm累计求和中使用各种分组Grouping

    Shuffle Grouping: 随机分组, 随机派发stream里面的tuple, 保证bolt中的每个任务接收到的tuple数目相同.(它能实现较好的负载均衡) Fields Grouping: ...

  2. uva 12544 无向图最小环

    思路:这题的N有500,直接floyd肯定超时. 我的做法是每次枚举一个点,求出包含这个点的最小环. 对所有最小环取最小值.求包含某个点的最小环我用的是启发式搜索,先以该点求一次spfa,然后dfs解 ...

  3. C++之环境搭建

    1.安装g++ 下载https://sourceforge.net/projects/mingw/files/Installer/mingw-get/mingw-get-0.6.2-beta-2013 ...

  4. 揪出ie和Edge的js代码

    var userAgent = navigator.userAgent; var isIE = userAgent.indexOf("compatible") > -1 &a ...

  5. 【.NET基础】--委托、事件、线程(1)

    1,委托 是存放方法的指针的清单,也就是装方法的容器 A, 新建winform项目[01委托],项目中添加dg_SayHi.cs 委托类 用于存储方法 namespace _01委托 { //定义委托 ...

  6. MongoDB - The mongo Shell, Access the mongo Shell Help

    In addition to the documentation in the MongoDB Manual, the mongo shell provides some additional inf ...

  7. 会话—session

    这篇随笔是上一篇的姊妹篇@_@! session       在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下) ...

  8. JavaScript之放大镜效果

    在网上也浏览过许多关于JavaScript放大镜效果的文章,有的代码解释得些隐晦难懂,看的我头有点晕晕的╮(╯﹏╰)╭,我的心情是这样的: 吐槽完了,我们动动小鼠标,当鼠标经过下面这张美女图片时就实现 ...

  9. php验证码无法显示的原因

    前段时间在调试程序的时候出现验证码无法打开的情况, 首先就要确定你已经开启了GD库!只要你不是低版本的PHP基本上这个是默认的!直接加上下面的代码先测试就可以! 如果不通过再查别的原因! 测试最后发现 ...

  10. 一个简单的获取参数的jqure

    今天做项目的时候需要用到上一页面传递过来的参数(只要一个参数),其解决办法就是下面: char latter=location.search.split('=')[1] 以上直接获取到第一个参数的值为 ...