signedmain(){ kin >> T; while (T--) { kin >> n; for (int i = 1; i <= n; ++i) deg[i] = 0; for (int i = 1, u, v; i < n; ++i) { kin >> u >> v; ++deg[u], ++deg[v]; } int cnt = 0; for (int i = 1; i <= n; ++i) { if (deg[i] == 1) ++cnt; } kout << (cnt + 1) / 2 << '\n'; } return0; }
namespace segt { structnode { int64 setTag, addTag, sum; } t[N << 2]; voidpush_up(int p){ t[p].sum = t[p << 1].sum + t[p << 1 | 1].sum; } voidpush_down(int p, int l, int r){ if (t[p].setTag) { int mid = (l + r) >> 1, ls = p << 1, rs = p << 1 | 1; t[ls].setTag = t[rs].setTag = t[p].setTag; t[ls].addTag = t[rs].addTag = t[p].addTag = 0; t[ls].sum = (mid - l + 1) * t[p].setTag; t[rs].sum = (r - mid) * t[p].setTag; t[p].setTag = 0; } elseif (t[p].addTag) { int mid = (l + r) >> 1, ls = p << 1, rs = p << 1 | 1; if (t[ls].setTag) t[ls].setTag += t[p].addTag; else t[ls].addTag += t[p].addTag; if (t[rs].setTag) t[rs].setTag += t[p].addTag; else t[rs].addTag += t[p].addTag; t[ls].sum += (mid - l + 1) * t[p].addTag; t[rs].sum += (r - mid) * t[p].addTag; t[p].addTag = 0; } } voidbuild(int p, int l, int r, int *a){ t[p].setTag = t[p].addTag = 0; if (l == r) { t[p].sum = a[l]; return; } int mid = (l + r) >> 1, ls = p << 1, rs = p << 1 | 1; build(ls, l, mid, a); build(rs, mid + 1, r, a); push_up(p); } voidall_add(){ if (t[1].setTag) t[1].setTag += 1; else t[1].addTag += 1; t[1].sum += n; } voidrange_set(int p, int l, int r, int L, int R, int val){ if (L <= l && r <= R) { t[p].setTag = val; t[p].addTag = 0; t[p].sum = (r - l + 1) * val; return; } push_down(p, l, r); int mid = (l + r) >> 1, ls = p << 1, rs = p << 1 | 1; if (L <= mid) range_set(ls, l, mid, L, R, val); if (R > mid) range_set(rs, mid + 1, r, L, R, val); push_up(p); } }
signedmain(){ kin >> T; while (T--) { kin >> n; for (int i = 0; i < n; ++i) { kin >> a[i]; tmp[a[i]] = i; } for (int i = 1; i < n; ++i) tmp[i] = max(tmp[i], tmp[i - 1]); for (int i = 0; i < n; ++i) tmp[i] = n - tmp[i];
segt::build(1, 0, n - 1, tmp); int64 ans = segt::t[1].sum; for (int i = 1; i < n; ++i) { segt::all_add(); segt::range_set(1, 0, n - 1, a[i - 1], n - 1, 1); ans = max(ans, segt::t[1].sum); } kout << ans << '\n'; } return0; }