QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#430770 | #6452. Ski Resort | cmk666 | WA | 9ms | 18236kb | C++23 | 5.6kb | 2024-06-04 14:13:56 | 2024-06-04 14:13:57 |
Judging History
answer
#pragma GCC optimize("Ofast", "unroll-loops")
#include<bits/stdc++.h>
using namespace std; using ll = long long;
#define For(i, j, k) for ( int i = (j) ; i <= (k) ; i++ )
#define Fol(i, j, k) for ( int i = (j) ; i >= (k) ; i-- )
using ull = unsigned long long;
int n, m, q, k, u, v, o[200009], l, oo[100009], ool, tot; ull ko[100009], ban; ll dp[100009][5], dq[5];
bool key[100009]; vector < int > g[100009], t[100009], vt[100009]; vector < pair < int, int > > mdf[59];
namespace TOPO
{
int deg[100009], d[100009], fa[19][100009], f[100009], o[100009]; queue < int > q; vector < int > gr[100009];
inline int lca(int u, int v)
{
if ( d[u] > d[v] ) swap(u, v);
Fol(i, 18, 0) if ( d[fa[i][v]] >= d[u] ) v = fa[i][v];
if ( u == v ) return u;
Fol(i, 18, 0) if ( fa[i][u] != fa[i][v] ) u = fa[i][u], v = fa[i][v];
return fa[0][u];
}
inline int kth(int u, int k) { Fol(i, 18, 0) if ( k & ( 1 << i ) ) u = fa[i][u]; return u; }
inline int calc(int u, int v) { v = kth(u, d[u] - d[v] - 1); return d[v] > d[o[u]] ? v : o[u]; }
inline void work()
{
For(i, 1, n) for ( int j : g[i] ) deg[j]++, gr[j].push_back(i);
for ( o[1] = 1, q.push(1) ; q.size() ; )
{
u = q.front(), q.pop(), d[u] = d[fa[0][u]] + 1;
For(i, 1, 18) fa[i][u] = fa[i - 1][fa[i - 1][u]];
if ( (int)gr[u].size() > 1 )
{
o[u] = u, v = gr[u][0];
For(i, 1, (int)gr[u].size() - 1)
{
v = lca(v, gr[u][i]);
if ( v != gr[u][i] ) mdf[tot].emplace_back(calc(gr[u][i], v), gr[u][i]);
}
if ( v != gr[u][0] ) mdf[tot].emplace_back(calc(gr[u][0], v), gr[u][0]);
ko[u] |= 1ull << tot++, t[f[u] = v].push_back(u);
}
else if ( gr[u].size() ) o[u] = o[gr[u][0]], t[f[u] = gr[u][0]].push_back(u);
for ( int i : g[u] ) { ko[i] |= ko[u]; if ( !--deg[i] ) fa[0][i] = u, q.push(i); }
}
}
}
namespace HLD
{
int fa[100009], h[100009], sz[100009], hs[100009], tp[100009], dfn[100009], seq[100009], cnt;
inline void dfs(int u, int fa = 0)
{
HLD::fa[u] = fa, h[u] = h[fa] + 1, sz[u] = 1;
for ( int i : t[u] )
{
dfs(i, u), sz[u] += sz[i];
if ( sz[i] > sz[hs[u]] ) hs[u] = i;
}
}
inline void dfs2(int u, int tp)
{
HLD::tp[u] = tp, seq[dfn[u] = ++cnt] = u;
if ( hs[u] ) dfs2(hs[u], tp);
for ( int i : t[u] ) if ( i != hs[u] ) dfs2(i, i);
}
inline int lca(int x, int y)
{
while ( tp[x] != tp[y] ) h[tp[x]] > h[tp[y]] ? ( x = fa[tp[x]] ) : ( y = fa[tp[y]] );
return h[x] < h[y] ? x : y;
}
inline int kth(int x, int k)
{
while ( h[x] - h[fa[tp[x]]] <= k ) k -= h[x] - h[fa[tp[x]]], x = fa[tp[x]];
return seq[dfn[x] - k];
}
}
inline bool dfncmp(int u, int v) { return HLD::dfn[u] < HLD::dfn[v]; }
namespace ST
{
struct node
{
int v, len, lz;
inline void apply(int x) { v = x ? len : 0, lz = x; }
} t[100009 << 2];
inline int lc(int p) { return p << 1; }
inline int rc(int p) { return p << 1 | 1; }
inline int md(int l, int r) { return ( l + r ) >> 1; }
inline void pu(int p) { t[p].v = t[lc(p)].v + t[rc(p)].v; }
inline void pd(int p) { if ( ~t[p].lz ) t[lc(p)].apply(t[p].lz), t[rc(p)].apply(t[p].lz), t[p].lz = -1; }
inline void build(int p, int l, int r)
{
t[p].v = t[p].len = r - l + 1, t[p].lz = -1;
if ( l != r ) build(lc(p), l, md(l, r)), build(rc(p), md(l, r) + 1, r);
}
inline void mdf(int p, int l, int r, int lp, int rp, int v)
{
if ( l > rp || r < lp ) return;
if ( lp <= l && r <= rp ) { t[p].apply(v); return; }
pd(p), mdf(lc(p), l, md(l, r), lp, rp, v), mdf(rc(p), md(l, r) + 1, r, lp, rp, v), pu(p);
}
inline int qry(int p, int l, int r, int lp, int rp)
{
if ( l > rp || r < lp ) return 0;
if ( lp <= l && r <= rp ) return t[p].v;
return pd(p), qry(lc(p), l, md(l, r), lp, rp) + qry(rc(p), md(l, r) + 1, r, lp, rp);
}
}
inline void dfs(int uu, int fa = 0)
{
if ( !key[uu] )
{
dp[uu][0] = 1;
for ( int i : vt[uu] )
{
dfs(i, uu), copy(dp[uu], dp[uu] + k + 1, dq), fill(dp[uu], dp[uu] + k + 1, 0);
For(ii, 0, k) For(jj, 0, k - ii) dp[uu][ii + jj] += dq[ii] * dp[i][jj];
}
}
u = uu, v = HLD::kth(u, HLD::h[u] - HLD::h[fa] - 1);
while ( HLD::tp[u] != HLD::tp[v] )
dp[uu][1] += ST::qry(1, 1, n, HLD::dfn[HLD::tp[u]], HLD::dfn[u]), u = HLD::fa[HLD::tp[u]];
dp[uu][1] += ST::qry(1, 1, n, HLD::dfn[v], HLD::dfn[u]);
}
int main()
{
// freopen("lodge.in", "r", stdin), freopen("lodge.out", "w", stdout);
cin.tie(nullptr) -> sync_with_stdio(false);
cin >> n >> m >> q;
For(i, 1, m) cin >> u >> v, g[u].push_back(v);
TOPO::work(), HLD::dfs(1), HLD::dfs2(1, 1), ST::build(1, 1, n);
For(i, 0, tot - 1)
{
auto mdfs = mdf[i]; mdf[i].clear();
for ( auto _ : mdfs )
{
v = _.first, u = _.second;
while ( HLD::tp[u] != HLD::tp[v] )
mdf[i].emplace_back(HLD::dfn[HLD::tp[u]], HLD::dfn[u]), u = HLD::fa[HLD::tp[u]];
mdf[i].emplace_back(HLD::dfn[v], HLD::dfn[u]);
}
}
For(_, 1, q)
{
cin >> k >> l, ban = 0;
For(i, 1, l) cin >> o[i], key[o[i]] = true, ban |= ko[o[i]];
For(i, 0, tot - 1) if ( ban & ( 1ull << i ) )
for ( auto _ : mdf[i] ) ST::mdf(1, 1, n, _.first, _.second, 0);
copy(o + 1, o + l + 1, oo + 1), ool = l, o[++l] = 1, sort(o + 1, o + l + 1, dfncmp);
For(i, 1, l - 1) o[l + i] = HLD::lca(o[i], o[i + 1]);
sort(o + 1, o + l + l, dfncmp), l = unique(o + 1, o + l + l) - o - 1;
For(i, 1, l) vt[o[i]].clear(), fill(dp[o[i]], dp[o[i]] + k + 1, 0);
For(i, 2, l) vt[HLD::lca(o[i - 1], o[i])].push_back(o[i]);
dfs(1), cout << dp[1][k] << '\n';
For(i, 1, ool) key[oo[i]] = false;
For(i, 0, tot - 1) if ( ban & ( 1ull << i ) )
for ( auto _ : mdf[i] ) ST::mdf(1, 1, n, _.first, _.second, 1);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16024kb
input:
4 4 4 1 2 1 3 2 4 3 4 1 1 4 2 1 4 1 1 3 2 2 3 2
output:
2 0 2 1
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 16000kb
input:
8 10 4 1 2 2 3 1 3 3 6 6 8 2 4 2 5 4 7 5 7 7 8 2 3 4 5 6 2 2 6 8 1 1 6 1 1 8
output:
0 0 3 2
result:
ok 4 lines
Test #3:
score: 0
Accepted
time: 0ms
memory: 15964kb
input:
8 9 4 1 2 1 3 3 6 6 8 2 4 2 5 4 7 5 7 7 8 2 3 4 5 6 2 2 6 8 1 1 6 1 1 8
output:
2 0 3 2
result:
ok 4 lines
Test #4:
score: 0
Accepted
time: 0ms
memory: 16020kb
input:
8 8 2 1 2 1 3 2 4 4 5 5 8 3 6 6 7 7 8 2 2 5 7 1 1 8
output:
9 2
result:
ok 2 lines
Test #5:
score: 0
Accepted
time: 3ms
memory: 14004kb
input:
8 8 1020 1 2 1 3 2 4 4 5 5 8 3 6 6 7 7 8 4 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0 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 1 0 0 ...
result:
ok 1020 lines
Test #10:
score: 0
Accepted
time: 3ms
memory: 15968kb
input:
8 9 2 1 2 1 3 2 4 4 5 5 8 3 6 6 7 7 8 4 6 2 2 5 7 1 1 8
output:
2 2
result:
ok 2 lines
Test #11:
score: 0
Accepted
time: 0ms
memory: 15968kb
input:
8 9 1020 1 2 1 3 2 4 4 5 5 8 3 6 6 7 7 8 4 6 1 3 2 4 7 4 1 8 4 3 4 7 8 1 5 1 2 6 7 8 1 3 1 5 8 3 7 1 2 3 4 5 6 7 2 6 2 3 4 5 7 8 4 1 4 3 4 3 5 6 8 1 3 2 7 8 1 3 3 5 8 1 3 2 5 8 1 3 1 4 5 2 3 1 3 6 4 4 5 6 7 8 2 3 3 6 7 4 5 2 3 4 5 7 2 5 2 3 4 5 7 4 3 3 6 7 3 4 1 4 5 7 1 5 3 4 5 6 7 3 6 3 4 5 6 7 8 3...
output:
1 0 0 1 1 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 3 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 0 1 1 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 1 0 1 1 0 0 0 1 0 0 0 0 1 1 0 ...
result:
ok 1020 lines
Test #12:
score: 0
Accepted
time: 0ms
memory: 15924kb
input:
30 32 70 5 6 2 3 16 17 1 9 1 23 27 28 9 10 10 11 21 22 6 7 28 29 15 30 18 19 19 20 1 16 4 5 13 14 7 8 25 26 29 30 8 30 26 27 22 30 23 24 20 21 12 13 17 18 3 4 11 12 24 25 14 15 1 2 4 4 19 20 21 30 4 4 8 13 22 29 4 4 3 10 19 21 4 4 6 18 19 23 4 4 10 13 18 28 4 4 14 25 26 28 4 4 11 17 22 27 4 4 3 5 14...
output:
0 1715 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 84 140 0 0 0 0 0 0 0 0 0 0 420 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 108 0 0 24 105 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 70 lines
Test #13:
score: 0
Accepted
time: 2ms
memory: 18092kb
input:
30 32 70 5 6 2 3 16 17 1 9 1 23 27 28 9 10 10 11 21 22 6 7 28 29 15 30 18 19 19 20 1 16 4 5 13 14 7 8 25 26 29 30 8 30 26 27 22 30 23 24 20 21 12 13 17 18 3 4 11 12 24 25 14 15 1 2 3 3 2 11 12 3 3 2 13 26 3 3 10 26 28 3 3 5 7 8 3 3 1 4 12 3 3 9 16 17 3 3 4 19 27 3 3 3 6 9 3 3 11 17 21 3 3 15 25 26 3...
output:
0 20 0 0 0 0 60 0 0 0 0 0 120 0 0 20 0 112 96 0 0 0 18 0 6 0 196 0 28 0 0 14 49 0 0 36 0 24 10 0 0 0 0 75 0 63 0 16 0 0 0 0 90 0 64 0 0 0 84 0 0 0 0 0 0 0 28 0 0 0
result:
ok 70 lines
Test #14:
score: 0
Accepted
time: 3ms
memory: 15964kb
input:
30 32 70 5 6 2 3 16 17 1 9 1 23 27 28 9 10 10 11 21 22 6 7 28 29 15 30 18 19 19 20 1 16 4 5 13 14 7 8 25 26 29 30 8 30 26 27 22 30 23 24 20 21 12 13 17 18 3 4 11 12 24 25 14 15 1 2 2 2 3 25 2 2 7 20 2 2 14 25 2 2 9 20 2 2 9 12 2 2 18 20 2 2 10 13 2 2 7 9 2 2 2 22 2 2 16 26 2 2 5 24 2 2 5 10 2 2 12 1...
output:
6 30 18 5 0 0 0 6 7 4 8 8 0 12 35 0 24 5 6 0 0 0 30 0 0 8 6 3 30 0 2 0 6 0 2 0 5 0 0 0 21 24 1 2 0 0 0 18 49 8 1 15 35 35 12 14 30 20 9 0 0 4 28 0 18 4 14 15 20 0
result:
ok 70 lines
Test #15:
score: 0
Accepted
time: 0ms
memory: 18224kb
input:
30 32 30 5 6 2 3 16 17 1 9 1 23 27 28 9 10 10 11 21 22 6 7 28 29 15 30 18 19 19 20 1 16 4 5 13 14 7 8 25 26 29 30 8 30 26 27 22 30 23 24 20 21 12 13 17 18 3 4 11 12 24 25 14 15 1 2 1 1 20 1 1 15 1 1 28 1 1 29 1 1 25 1 1 2 1 1 19 1 1 13 1 1 22 1 1 9 1 1 17 1 1 18 1 1 24 1 1 10 1 1 3 1 1 12 1 1 1 1 1 ...
output:
6 8 7 8 4 2 5 6 8 2 3 4 3 3 3 5 1 6 5 2 7 4 7 2 8 2 6 7 4 5
result:
ok 30 lines
Test #16:
score: 0
Accepted
time: 9ms
memory: 14032kb
input:
30 32 19405 5 6 2 3 16 17 1 9 1 23 27 28 9 10 10 11 21 22 6 7 28 29 15 30 18 19 19 20 1 16 4 5 13 14 7 8 25 26 29 30 8 30 26 27 22 30 23 24 20 21 12 13 17 18 3 4 11 12 24 25 14 15 1 2 4 4 9 12 13 17 4 4 6 7 11 21 4 4 3 11 17 19 4 4 8 19 25 30 4 4 4 16 23 29 4 4 10 17 19 27 4 4 2 6 25 26 4 4 1 6 15 2...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 432 672 144 72 0 0 0 48 0 0 0 0 0 0 0 0 1764 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 64 0 0 0 0 0 0 0 0 0 840 0 0 0 0 0 200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 840 0 96 0 0 0 0 0 0 0 336 0 0 0 0 0 36 0 0 0 0 252 0 0 0 0 0 0 0 0 300 0 0 0 0 0 0 0 0 0 0 840 0 0 0 0 0 0 0 0 84 0 0...
result:
ok 19405 lines
Test #17:
score: 0
Accepted
time: 0ms
memory: 18236kb
input:
30 38 70 1 2 2 3 3 4 4 5 5 6 6 7 7 8 1 9 9 10 10 11 11 12 12 13 13 14 14 15 1 16 16 17 17 18 18 19 19 20 20 21 21 22 1 23 23 24 24 25 25 26 26 27 27 28 28 29 16 23 18 23 27 2 28 2 2 9 3 9 15 30 29 30 22 30 8 30 4 4 20 23 29 30 4 4 1 11 26 30 4 4 3 16 18 23 4 4 5 17 19 24 4 4 7 16 18 19 4 4 5 10 18 3...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok 70 lines
Test #18:
score: -100
Wrong Answer
time: 3ms
memory: 15972kb
input:
20 23 6195 20 19 7 20 6 9 1 15 17 5 17 4 8 11 8 7 11 6 2 10 8 14 19 12 4 18 14 13 12 14 18 16 5 2 15 3 13 17 16 5 10 9 10 11 3 8 4 4 6 7 15 16 4 4 4 8 10 16 4 4 8 10 15 20 4 4 1 5 9 18 4 4 3 7 14 17 4 4 3 5 16 17 4 4 6 8 10 18 4 4 5 6 11 15 4 4 8 11 15 18 4 4 4 11 18 19 4 4 4 8 11 17 4 4 3 5 6 12 4 ...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 91st lines differ - expected: '0', found: '3'