orz...hzwer 对着大神的 code 看 , 稍微理解了.

考虑一只牛到达 , 那它所在子树全部 +1 , 可以用BIT维护

-----------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
 
#define rep( i , n ) for( int i = 0 ; i < n ; i++ )
#define clr( x , c ) memset( x , c , sizeof( x ) )
#define Rep( i , n ) for( int i = 1 ; i <= n ; ++i )
 
using namespace std;
const int maxn = 100000 + 5;
 
vector< int > G[ maxn ];
 
void add_edge( int u , int v ) {
G[ u ].push_back( v );
G[ v ].push_back( u );
}
 
int b[ maxn ];
int n;
 
void init() {
rep( i , n )
   G[ i ].clear();
}
 
#define lowbit( x ) ( x & -x )
 
void update( int x , int v ) {
for( ; x <= n ; x += lowbit( x ) )
   b[ x ] += v;
   
}
 
int query( int x ) {
int res = 0;
for( ; x ; x -= lowbit( x ) )
  
   res += b[ x ];
   
return res;
}
 
int ans[ maxn ];
int pos[ maxn ];
 
void dfs( int x , int fa ) {
ans[ pos[ x ] ] = query( pos[ x ] );
update( pos[ x ] , 1 );
rep( i , G[ x ].size() ) {
int to = G[ x ][ i ];
if( to == fa ) continue;
dfs( to , x );
}
update( pos[ x ] , -1 );
}
int main() {
freopen( "test.in" , "r" , stdin );
cin >> n;
rep( i , n - 1 ) {
int u , v;
scanf( "%d%d" , &u , &v );
u-- , v--;
add_edge( u , v );
}
Rep( i , n ) {
int t;
scanf( "%d" , &t );
--t;
pos[ t ] = i;
}
dfs( 0 , -1 );
Rep( i , n )
   printf( "%d\n" , ans[ i ] );
return 0;
}

-----------------------------------------------------------------------

1782: [Usaco2010 Feb]slowdown 慢慢游

Time Limit: 1 Sec  Memory Limit: 64 MB
Submit: 574  Solved: 350
[Submit][Status][Discuss]

Description

每天Farmer John的N头奶牛(1 <= N <= 100000,编号1…N)从粮仓走向他的自己的牧场。牧场构成了一棵树,粮仓在1号牧场。恰好有N-1条道路直接连接着牧场,使得牧场之间都恰好有一条路径相连。第i条路连接着A_i,B_i,(1 <= A_i <= N; 1 <= B_i <= N)。奶牛们每人有一个私人牧场P_i (1 <= P_i <= N)。粮仓的门每次只能让一只奶牛离开。耐心的奶牛们会等到他们的前面的朋友们到达了自己的私人牧场后才离开。首先奶牛1离开,前往P_1;然后是奶牛2,以此类推。当奶牛i走向牧场P_i时候,他可能会经过正在吃草的同伴旁。当路过已经有奶牛的牧场时,奶牛i会放慢自己的速度,防止打扰他的朋友。 考虑如下的牧场结构(括号内的数字代表了牧场的所有者)。 

Input

* 第1行 : 一个正整数N * 第2…N行: 第i+1行包括一对正整数A_i,B_i * 第N+1..N+N行: 第 N+i行 包括一个正整数: P_i

Output

* 第一行到第N行:第i行表示第i只奶牛需要被放慢的次数

Sample Input

5
1 4
5 4
1 3
2 4
4
2
1
5
3

Sample Output

0
1
0
2
1

HINT

Source

BZOJ 1782: [Usaco2010 Feb]slowdown 慢慢游( BIT + dfs )的更多相关文章

  1. bzoj 1782: [Usaco2010 Feb]slowdown 慢慢游【dfs序+线段树】

    考虑每头牛到达之后的影响,u到达之后,从1到其子树内的点需要放慢的都多了一个,p为u子树内点的牛ans会加1 用线段树维护dfs序,每次修改子树区间,答案直接单点查询p即可 #include<i ...

  2. [bzoj 1782] [Usaco2010 Feb]slowdown慢慢游

    [bzoj 1782] [Usaco2010 Feb]slowdown慢慢游 Description 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N)从 ...

  3. 1782: [Usaco2010 Feb]slowdown 慢慢游

    1782: [Usaco2010 Feb]slowdown 慢慢游 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 570  Solved: 346[Sub ...

  4. 【BZOJ】1782: [Usaco2010 Feb]slowdown 慢慢游

    [算法]DFS序+树状数组 [题解]题意相当于统计前i-1个点在第i个点的祖先的个数,显然可以用dfs维护,用树状数组差分维护前缀和. 出栈不新加节点就要注意左闭右开,即in[a[i]]处+1,ou[ ...

  5. BZOJ1782: [Usaco2010 Feb]slowdown 慢慢游

    1782: [Usaco2010 Feb]slowdown 慢慢游 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 541  Solved: 326[Sub ...

  6. 树状数组【bzoj1782】: [Usaco2010 Feb]slowdown 慢慢游

    [bzoj1782]: [Usaco2010 Feb]slowdown 慢慢游 Description 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1-N) ...

  7. BZOJ 1782 洛谷 2982 [Usaco2010 Feb]slowdown 慢慢游

    [题解] 一头牛走到i,相当于把i点的子树的点权都加1,查询减慢的次数就是查询目的地的点权. 预处理dfs序,某个点的子树的dfs序是连续的一段.差分后用树状数组维护,变成点修区查.或者直接线段树区修 ...

  8. 【bzoj1782】[Usaco2010 Feb]slowdown 慢慢游 树链剖分+线段树

    题目描述 每天Farmer John的N头奶牛(1 <= N <= 100000,编号1…N)从粮仓走向他的自己的牧场.牧场构成了一棵树,粮仓在1号牧场.恰好有N-1条道路直接连接着牧场, ...

  9. BZOJ 2015: [Usaco2010 Feb]Chocolate Giving( 最短路 )

    裸最短路.. ------------------------------------------------------------------------------------ #include ...

随机推荐

  1. DateTimePicker——开源的Android日历类库

    Github托管地址:https://github.com/flavienlaurent/datetimepicker

  2. Android studio之更改快捷键及自动导包

    更改AS中的代码提示快捷键,AS做的也挺智能的,在Keymap中可以选择使用eclipse的快捷键设置,但是虽然设置了,对有些快捷键还是不能使用,那么就需要我们手动去修改了. 在代码提示AS默认的快捷 ...

  3. Gson使用初探

    参考地址: http://www.stormzhang.com/android/2014/05/22/android-gson/ 我的示例代码: public void doGsonTest(View ...

  4. Codeforces 353D Queue(构造法)

    [题目链接] http://codeforces.com/contest/353/problem/D [题目大意] 10^6个男女排队,每一秒,如果男生在女生前面,即pos[i]是男生,pos[i+1 ...

  5. 掌握下面常用函数,学php不再难

    一.写入文件 1.打开资源(文件)fopen($filename,$mode) 2.写文件fwrite($handle,$str) 3.关闭文件fclose($handle) 4.一步写入file_p ...

  6. c# 获取全屏 中鼠标焦点的位置坐标

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  7. SVD神秘值分解

    SVD分解 SVD分解是LSA的数学基础,本文是我的LSA学习笔记的一部分,之所以单独拿出来,是由于SVD能够说是LSA的基础,要理解LSA必须了解SVD,因此将LSA笔记的SVD一节单独作为一篇文章 ...

  8. HDU 1002 A + B Problem II(大整数相加)

    A + B Problem II Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u De ...

  9. 2014年辛星Javascript解读第二节

    本小节我们解说一下Javascript的语法,尽管js语言很easy,它的语法也相对好学一些,可是不学总之还是不会的,因此,我们来一探到底把. ********凝视************* 1.我们 ...

  10. if...else if...else

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prog ...