poj 3321(dfs序&&树状数组)
来源:互联网 发布:ubuntu 16.10安装lamp 编辑:程序博客网 时间:2024/05/17 10:05
第一次知道dfs序这个东西,可以维护树上的任意一个节点,以这个节点为根的子树上的所有节点的标号。是连续的一段区间。
一棵子树的所有节点在dfs序里是连续一段,主要就是利用这个性质来解题
然后就是树状数组维护区间内苹果的数量
另外这个题很卡时间,我用了输入输出挂才过
#include<cstdio>#include<vector>using namespace std;const int maxn = 100000+10;vector<int>G[maxn];int l[maxn],r[maxn];int tree[maxn];char op[5];int x,key,n;inline int in(){ char ch; int a = 0; while((ch = getchar()) == ' ' || ch == '\n'); a += ch - '0'; while((ch = getchar()) != ' ' && ch != '\n') { a *= 10; a += ch - '0'; } return a;}void dfs(int u){ l[u] = key; int len = G[u].size(); for(int i=0;i<len;i++) { int v = G[u][i]; key++; dfs(v); } r[u] = key;}inline int lowbit(int i){return i&(-i);}inline void add(int i,int x){ while(i<=n) { tree[i] += x; i += lowbit(i); }}inline int getsum(int i){ int sum = 0; while(i) { sum += tree[i]; i -= lowbit(i); } return sum;}int q[maxn];inline void Out(int a){ // 输出外挂 if (a < 0) { putchar('-'); a = -a; } if (a >= 10) { Out(a / 10); } putchar(a % 10 + '0');}int main(){ int u,v; n = in(); for(int i=1;i<n;i++) { u = in(); v = in(); G[u].push_back(v); } key = 1; dfs(1); for(int i=1;i<=n;i++) q[i] = 1,add(i,1); int m; scanf("%d",&m); while(m--) { scanf("%s%d",op,&x); if(op[0]=='Q') { Out(getsum(r[x])-getsum(l[x]-1)); puts(""); } else { if(q[x]==1) add(l[x],-1),q[x]=0; else add(l[x],1),q[x] = 1; } } return 0;}
阅读全文
0 0
- poj 3321(dfs序&&树状数组)
- POJ 3321(dfs序+树状数组)
- dfs 序 + 树状数组 poj 3321
- POJ 3321 dfs序+树状数组
- POJ 3321 Apple Tree(dfs序 + 树状数组)
- POJ 3321 Ultra-QuickSort(树状数组+DFS序)
- POJ 3321 Apple Tree(dfs序 + 树状数组)
- POJ 3321-Apple Tree(DFS序+树状数组)
- poj 3321 Apple Tree(dfs序+树状数组)
- poj 3321 dfs + 树状数组
- poj 3321 dfs+树状数组
- POJ 3321 树状数组+DFS
- POJ 3321 Apple Tree(dfs序+树状数组)
- 【poj 3321】 Apple Tree 树状数组+dfs序
- POJ 3321Apple Tree(dfs序 树状数组)
- poj 3321 Apple Tree(dfs序+树状数组求和模型)
- 【poj】 3321 Apple Tree dfs序+树状数组
- poj 3321 Apple Tree 树状数组 dfs序
- [LeetCode] Convert Sorted Array to Binary Search Tree
- HDU6197 array array array 最长上升子序列(模板题)
- [LeetCode] Convert Sorted List to Binary Search Tree
- JS中script标签位置对页面的影响
- [LeetCode] Next Permutation
- poj 3321(dfs序&&树状数组)
- [LeetCode] Word Ladder
- [WikiOI] 1.1.1 最小数和最大数
- 【BZOJ4872】【SHOI2017】分手是祝愿 期望DP
- [WikiOI] 1.1.2 求和
- hadoop学习3
- 存根类
- [WikiOI] 1.2.1 判断浮点数是否相等
- MFC DLL 的三种类型