HDU3887 DFS序+ 线段树
查询树上某个节点的子节点的标号小于其标号的数目. 一个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序+ 线段树的更多相关文章
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
- POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include < ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
- F - Change FZU - 2277 (DFS序+线段树)
题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...
- BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树
题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...
- BZOJ1103 [POI2007]大都市meg dfs序 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...
随机推荐
- PAT (Basic Level) Practise (中文)1027. 打印沙漏(20)
1027. 打印沙漏(20) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 本题要求你写个程序把给定的符号打印成 ...
- shell获取本地ip的三种方法
第一种方法:ifconfig|grep inet |awk '{print $2}'|sed '2d'|awk -F : '{print $2}'第二种方法:ifconfig|grep inet|se ...
- python使用正则表达式文本替换
2D客户端编程从某种意义上来讲就是素材组织,所以,图片素材组织经常需要批量处理,python一定是最佳选择,不管是win/linux/mac都有一个简单的运行环境 举两个应用场景: 如果不是在某个文件 ...
- 简单的MySQL数据库主从同步配置
Master配置: 代码如下 复制代码 log-bin = mysql-binserver-id = 1binlog-do-db = powerdns #要同步的数据库 Master新增slave账号 ...
- Sql日期时间格式转换;取年 月 日,函数:DateName()、DATEPART()
一.sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007 ...
- rac安装oem
[oracle@node1 ~]$ emca -config dbcontrol db -repos recreate -cluster STARTED EMCA at May 31, 2016 3: ...
- 【学习笔记】【C语言】自增自减
1. 简单使用 ++ 自增运算符.如a++,++a,都等价于a = a+1 --自减运算符.如a--,--a,都等价于a = a-1 5++是错误的,因为5是常量 2. ++a和a++的区别 int ...
- Access时间日期比较查询的方法总结
Access日期时间比较查询语句困扰过很多网友,种豆网整理了一下Access日期比较查询的几种方法,假定数据表明为TblName,日期/时间字段名为FDate(这里不能讲FDate设置为字符串,否则比 ...
- 20141109--SQL 练习题-1
create database xinxiku go use xinxiku go create table Student ( Sno ) primary key, Sname ) not null ...
- MYSQL序言
我写MYSQL的文章主要的原因:我发现网上关于MYSQL的文章比较少,而且很多都是参差不齐,几乎找不到可以比较全面的介绍:例如一篇文章介绍如何增删改查,却没有介绍批量新增:于是下次工作中用到的时候又要 ...