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. 修改某一条边的权值为 ...
随机推荐
- jsonString转NSDictionary
NSData *webData = [ \": {\"name\": \"Jerry\",\"age\": \"12\& ...
- CSS选择器笔记
一.元素选择符 序号 选择器 含义 1. * 通用元素选择器,匹配任何元素 2. E 标签选择器,匹配所有使用E标签的元素 3. .info class选择器,匹配所有class属性中包含info的元 ...
- 移动开发Html 5前端性能优化指南
详细内容请点击 PC优化手段在Mobile侧同样适用在Mobile侧我们提出三秒种渲染完成首屏指标基于第二点,首屏加载3秒完成或使用Loading基于联通3G网络平均338KB/s(2.71Mb/s) ...
- list笔记总结
1.list是一个复合的复制函数,可以将一个数组一次赋给多个变量.我们常用以下语句遍历一个数组. $arr = array('东','男','西','北'); while(list($k,$v)=ea ...
- How Do I Declare A Block in Objective-C?
As a local variable: returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...}; As a p ...
- WCF之契约
消息交换的双方,为了进行消息交换,而定义的一些数据交换规则,称之为契约. 契约只约束规则,不管实现. 契约对客户端和服务器的要求. 服务器:定义和实现契约.构建ServiceHost实例,然后暴露En ...
- Install GDAL in OpenSUSE 12.3 Linux
Runtime Enviroment:Open SUSE Linux *i385 Notice:if any command disavliable ,you can copy the paramet ...
- ResourceBundle和Properties(转载)
转载: 一般来说,ResourceBundle类通常是用于针对不同的语言来使用的属性文件. 而如果你的应用程序中的属性文件只是一些配置,并不是针对多国语言的目的.那么使用Properties类就可以了 ...
- 济南学习 Day 3 T2 pm
LYK 快跑!(run)Time Limit:5000ms Memory Limit:64MB题目描述LYK 陷进了一个迷宫! 这个迷宫是网格图形状的. LYK 一开始在(1,1)位置, 出口在(n, ...
- 理解 pkg-config 工具
引用了别人的文章:http://www.chenjunlu.com/2011/03/understanding-pkg-config-tool/ 你在 Unix 或 Linux 下开发过软件吗?写完一 ...