标签:return 技术分享 括号 time ges ext using gre inpu
#include <cmath> #include <cstdio> #include <algorithm> using namespace std; char buf[10000000], *ptr = buf - 1; inline int readint(){ int f = 1, n = 0; char ch = *++ptr; while(ch < ‘0‘ || ch > ‘9‘){ if(ch == ‘-‘) f = -1; ch = *++ptr; } while(ch <= ‘9‘ && ch >= ‘0‘){ n = (n << 1) + (n << 3) + ch - ‘0‘; ch = *++ptr; } return f * n; } const int maxn = 100000 + 10; struct Edge{ int to, next; Edge(){} Edge(int _t, int _n): to(_t), next(_n){} }e[maxn * 2]; int fir[maxn] = {0}, cnt = 0; inline void ins(int u, int v){ e[++cnt] = Edge(v, fir[u]); fir[u] = cnt; e[++cnt] = Edge(u, fir[v]); fir[v] = cnt; } int fa[maxn], dep[maxn], siz[maxn], son[maxn]; void dfs1(int u){ son[u] = 0; siz[u] = 1; for(int v, i = fir[u]; i; i = e[i].next){ v = e[i].to; if(v == fa[u]) continue; fa[v] = u; dep[v] = dep[u] + 1; dfs1(v); siz[u] += siz[v]; if(!son[u] || siz[v] > siz[son[u]]) son[u] = v; } } int top[maxn], ref[maxn], tcnt = 0; void dfs2(int u){ ref[u] = ++tcnt; if(!son[u]) return; top[son[u]] = top[u]; dfs2(son[u]); for(int v, i = fir[u]; i; i = e[i].next){ v = e[i].to; if(v == fa[u] || v == son[u]) continue; top[v] = v; dfs2(v); } } int n; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 int sum[maxn << 2] = {0}; void Update(int qw, int l, int r, int rt){ sum[rt]++; if(l == r) return; else{ int mid = l + r >> 1; if(qw <= mid) Update(qw, lson); else Update(qw, rson); } } int Query(int ql, int qr, int l, int r, int rt){ if(ql <= l && r <= qr) return sum[rt]; else{ int mid = l + r >> 1, ret = 0; if(ql <= mid) ret += Query(ql, qr, lson); if(qr > mid) ret += Query(ql, qr, rson); return ret; } } inline void solve(int x){ int ans = 0; while(top[x] != 1){ ans += Query(ref[top[x]], ref[x], 1, n, 1); x = fa[top[x]]; } ans += Query(ref[1], ref[x], 1, n, 1); printf("%d\n", ans); } int main(){ fread(buf, sizeof(char), sizeof(buf), stdin); n = readint(); for(int i = 1; i < n; i++) ins(readint(), readint()); fa[1] = 0; dfs1(1); top[1] = 1; dfs2(1); for(int p, i = 1; i <= n; i++){ p = readint(); solve(p); Update(ref[p], 1, n, 1); } return 0; }
[BZOJ1782][Usaco2010 Feb]slowdown 慢慢游
标签:return 技术分享 括号 time ges ext using gre inpu
原文地址:http://www.cnblogs.com/ruoruoruo/p/7522804.html