翻译

附上代码

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 2e5+3;
int x = 0, f = 1; char c = getchar();
while (c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while (c <= '9' && c >= '0') {x = x*10 + c-'0'; c = getchar();}
return x * f;
}
int n, rt, head[maxn], Index, L[maxn], R[maxn], cnt;
struct edge {
int nxt, to;
}ed[maxn];
inline void addedge(int x, int y) {
}
inline void dfs(int x, int fr) {
L[x] = ++ Index;
if(ed[i].to == fr) continue;
dfs(ed[i].to, x);
}
R[x] = Index;
}
struct TREE {
int l, r, sum;
}tree[maxn << 2];
struct Segment_Tree {
#define Lson (k << 1)
#define Rson ((k << 1) + 1)
inline void build(int k, int ll, int rr) {
tree[k].l = ll, tree[k].r = rr;
tree[k].sum = 0;
if(tree[k].l == tree[k].r) return ;
int mid = (tree[k].l + tree[k].r) >> 1;
build(Lson, ll, mid);
build(Rson, mid+1, rr);
}
inline void update(int k, int pos, int num) {
if(tree[k].l == tree[k].r && tree[k].l == pos) {
tree[k].sum += num;
return ;
}
int mid = (tree[k].l + tree[k].r) >> 1;
if(pos <= mid) update(Lson, pos, num);
else update(Rson, pos, num);
tree[k].sum = tree[Lson].sum + tree[Rson].sum;
}
inline int query(int k, int l, int r) {
int res = 0;
if(l <= tree[k].l && r >= tree[k].r)
return tree[k].sum;
int mid = (tree[k].l + tree[k].r) >> 1;
if(l <= mid) res += query(Lson, l, r);
if(r > mid) res += query(Rson, l, r);
return res;
}
}T;
int main() {
while (scanf("%d%d", &n, &rt) == 2) {
if(n == 0 && rt == 0) return 0;
cnt = 0, Index = 0;
int x, y;
for(int i=1; i<n; i++) {
}
dfs(rt, 0);
T.build(1, 1, n);
for(int i=1; i<=n; i++) {
printf("%d", T.query(1, L[i], R[i]));
T.update(1, L[i], 1);
if(i == n) printf("\n");
else printf(" ");
}
}
}

posted @ 2018-09-28 08:51  Mystical-W  阅读(...)  评论(...编辑  收藏