QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#584945 | #7103. Red Black Tree | Tomorrow | RE | 1ms | 3640kb | C++20 | 5.2kb | 2024-09-23 17:49:22 | 2024-09-23 17:49:22 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define C const
#define U unsigned
#define SC static
#define CE constexpr
#define TL template
#define TN typename
#define OP operator
#define pb push_back
#define ft first
#define sd second
#define mi (l + (r - l) / 2)
#define TTT TL<TN T>
#define TTI TL<TN It>
#define BE(v) v.begin(), v.end()
#define VD void
#define BL bool
#define CH char
#define I int
#define LL long long
#define II __int128
#define D double
using PII = pair<I, I>;
TTT using V = vector<T>;
#define FORX(v) for(auto& x : v)
#define FOR(i, b, e) for(auto i = b, _e = (decltype(i))e;i < _e; ++i)
#define FORR(i, b, e) for(auto i = b, _e = (decltype(i))e;i > _e; --i)
#define FORV(v, i) for (I i = 0, _n = v.size(), x = _n ? v[0] : 0;i < _n;x = v[++i])
#define FUNC(T, f, ...) function<T(__VA_ARGS__)> f = [&](__VA_ARGS__)->T
#define DR(T, ...) T __VA_ARGS__; R(__VA_ARGS__);
TTT VD R(T& x) { cin >> x; }
TTT T R() { DR(T, x); return x; }
TL<TN T, TN... A> VD R(T& x, A&... a) { R(x), R(a...); }
CE CH LF = '\n', SP = ' ';
TL<CH s = LF> VD W() { cout << s; }
TL<CH s = LF, TN T> VD W(C T& x) { cout << x << s; }
TL<CH s = LF, TN T, TN... A> VD W(C T& x, C A&... a) { W<SP>(x), W<s>(a...); }
TTT CE T inf() { return numeric_limits<T>::max() / 2; }
TTT VD setmin(T& a, C T& b) { if (b < a)a = b; }
TTT VD setmax(T& a, C T& b) { if (a < b)a = b; }
TTT VD fastpow(T& a, T b, LL i) { for (;i;i >>= 1, b *= b)if (i & 1)a *= b; }
TTT VD fastgss(T& a, C T& b, LL i, U I j = 64)
{
while (!(i >> --j & 1));
for (T c = b;j-- && (a += a * c, c *= c, 1);)if (i >> j & 1)a += c, c *= b;
}
#define OPD(T,S,op,...) \
T& OP op##= (C S& _){{__VA_ARGS__} return *this;}\
friend T OP op (C T& a, C S& b) {T c = a; return c op##= b;}
#define OPB(T,op,...) friend BL OP op (C T& a, C T& b) {return __VA_ARGS__;}
#define OST(T) friend ostream& OP << (ostream & os, C T & a) { return os << a.x; }
struct Mod//WARNING : Mod MUST BE assigned in [0,m) unless using cc
{
SC U I m; SC U LL i;
SC VD set(U I mod) { m = mod, i = (U LL)(-1) / m + 1; }
SC U I mul(U I a, U I b)
{
U LL x = (U LL)a * b, y = ((U II)x * i >> 64) * m;
return x - y + (x < y ? m : 0);
}
U I x; CE Mod() :x(0) {}
TTT CE Mod(T x) : x(x) {}
TTT CE SC Mod cc(T x) { return (x % m + m) % m; }
OPD(Mod, Mod, +, if ((x += _.x) >= m)x -= m;);
OPD(Mod, Mod, -, if ((x += m - _.x) >= m)x -= m;);
OPD(Mod, Mod, *, x = mul(x, _.x););
OPD(Mod, Mod, / , fastpow(*this, _, m - 2););//m must be a prime
OPD(Mod, I, ^, fastpow(*this, *this, _ - 1 + (_ > 0 ? 0 : m - 1)););
OPB(Mod, == , a.x == b.x);
OPB(Mod, != , a.x != b.x);
OST(Mod)
};
U I Mod::m; U LL Mod::i;
TL<TN T, VD(*add)(T& c, C T& a, C T& b)> struct VT//VirtualTree
{
I n, d, rt; V<I> dp, rk; V<V<I>> f; V<V<T>> h;
VT(C V<V<I>>& e, C V<V<T>>& g, I rt = 1) :n(e.size()), d(ilogb(n)), rt(rt),
dp(n), rk(n), f(d + 1, V<I>(n)), h(d + 1, V<T>(n))
{
I num = 0;
FUNC(VD, dfs, I p)
{
rk[p] = num++;
FORV(e[p], i)if (x != f[0][p])
dp[x] = dp[f[0][x] = p] + 1, h[0][x] = g[p][i], dfs(x);
};
dp[rt] = 1; dfs(rt);
FOR(k, 0, d)FOR(i, 1, n)
{
f[k + 1][i] = f[k][f[k][i]];
add(h[k + 1][i], h[k][i], h[k][f[k][i]]);
}
}
I lca(I x, I y)
{
if (dp[x] < dp[y])swap(x, y);
FORR(k, d, -1)if (dp[f[k][x]] >= dp[y])x = f[k][x];
if (x == y)return x;
FORR(k, d, -1)if (f[k][x] != f[k][y])x = f[k][x], y = f[k][y];
return f[0][x];
}
T sum(I p, I x)
{
T res = h[0][x], tmp; x = f[0][x];
FORR(k, d, -1)if (dp[f[k][x]] >= dp[p])
add(res, tmp = res, h[k][x]), x = f[k][x];
return res;
}
VD OP()(V<I> a, V<I>& is, V<V<I>>& e, V<V<T>>& g)
{
FORX(a)is[x] = -1;
sort(BE(a), [&](I x, I y) {return rk[x] < rk[y];});
FOR(i, 0, a.size())a.pb(i ? lca(a[i - 1], a[i]) : rt);
sort(BE(a), [&](I x, I y) {return rk[x] < rk[y];});
a.resize(unique(BE(a)) - a.begin());
FORX(a)is[x] = is[x] == -1;
e[rt].clear(), g[rt].clear();
FOR(i, 1, a.size())
{
I u = lca(a[i - 1], a[i]), v = a[i];
T w = sum(u, v);
e[u].pb(v), e[v].assign({ u });
g[u].pb(w), g[v].assign({ w });
}
}
};
VD add(LL& c, C LL& a, C LL& b) { c = a + b; }
VD test()
{
DR(I, n, m, q); ++n;
V<I> isr(n), is(n);
V<V<I>> te(n), e(n); V<V<LL>> tg(n), g(n);
while (m--)isr[R<I>()] = 1;
FOR(i, 2, n)
{
DR(I, u, v, w);
te[u].pb(v), tg[u].pb(w);
te[v].pb(u), tg[v].pb(w);
}
VT<LL, add> vt(te, tg);
if(n>1000)return;
FUNC(LL, dfs, I p, I fa, LL w, LL lim, I & cnt)
{
LL res = 0;
FORV(e[p], i)
{
if (x == fa)continue;
setmax(res, dfs(x, p, g[p][i], lim, cnt));
}
if (!isr[p] && res + w > lim) { ++cnt;return 0; }
return isr[p] ? 0 : res + w;
};
FUNC(BL, check, LL lim)
{
I cnt = 0;
dfs(1, 0, 0, lim, cnt);
return cnt <= 1;
};
while (q--)
{
V<I> a;DR(I, k);while (k--)a.pb(R<I>());
vt(a, is, e, g);
LL l = -1, r = inf<LL>();
while (l + 1 < r)(!check(mi) ? l : r) = mi;
W(r);
}
}
I main()
{
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if (fopen("test.in", "r"))
{
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
}
I t = 1;
R(t);
while (t--)test();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3640kb
input:
2 12 2 4 1 9 1 2 1 2 3 4 3 4 3 3 5 2 2 6 2 6 7 1 6 8 2 2 9 5 9 10 2 9 11 3 1 12 10 3 3 7 8 4 4 5 7 8 4 7 8 10 11 3 4 5 12 3 2 3 1 2 1 2 1 1 3 1 1 1 2 1 2 3 1 2 3
output:
4 5 3 8 0 0 0
result:
ok 7 lines
Test #2:
score: -100
Runtime Error
input:
522 26 1 3 1 1 4 276455 18 6 49344056 18 25 58172365 19 9 12014251 2 1 15079181 17 1 50011746 8 9 2413085 23 24 23767115 22 2 26151339 26 21 50183935 17 14 16892041 9 26 53389093 1 20 62299200 24 18 56114328 11 2 50160143 6 26 14430542 16 7 32574577 3 16 59227555 3 15 8795685 4 12 5801074 5 20 57457...
output:
148616264 148616264 0 319801028 319801028 255904892 317070839 1265145897 1265145897 1072765445 667742619 455103436 285643094 285643094 285643094 317919339 0 785245841 691421476 605409472 479058444 371688030 345857037 493383271 919185207 910180170 919185207 121535083 181713164 181713164 181713164 181...