QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#536795#5102. Dungeon CrawlerRngBased#WA 4ms5680kbC++204.4kb2024-08-29 16:50:552024-08-29 16:50:58

Judging History

你现在查看的是最新测评结果

  • [2024-08-29 16:50:58]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:5680kb
  • [2024-08-29 16:50:55]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pi3 tuple<int, int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;

static const int P = 12;

struct seg 
{
    static const ll INF = 1e18;
    ll seg[4 * 2005];
    int n;
    void init(int _n)
    {
        n = _n;
        fill(seg, seg + 4 * n, -INF);
    }
    void modify(int pos, ll val, int i = 1, int L = 1, int R = -1)
    {
        if (R == -1) R = n;
        if (L == R)
            seg[i] = max(seg[i], val);
        else 
        {
            int mid = (L + R) / 2;
            if (pos <= mid)
                modify(pos, val, i << 1, L, mid);
            else 
                modify(pos, val, i << 1 | 1, mid + 1, R);
            seg[i] = max(seg[i << 1], seg[i << 1 | 1]);
        }
    }
    ll query(int qL, int qR, int i = 1, int L = 1, int R = -1)
    {
        if (R == -1) R = n;
        if (qL <= L && R <= qR)
            return seg[i];
        else if (R < qL || qR < L)
            return -INF;
        else 
        {
            int mid = (L + R) / 2;
            return max(query(qL, qR, i << 1, L, mid),
                        query(qL, qR, i << 1 | 1, mid + 1, R));
        }
    }
} seg;

int n, q, vis[2005], pre[2005][P], t, ins[2005], k;
ll ans[200005], d[2005], deep[2005], sum;
pii io[2005];
vector<pii> qs[2005][2005];
vector<pll> E[2005];

void dfs(int u)
{
    io[u].F = ++t;
    vis[u] = 1;
    deep[u] = d[u];
    for (auto [v, w] : E[u])
    {    
        if (!vis[v])
        {
            pre[v][0] = u;
            for (int i = 0; i + 1 < P; i++)
                pre[v][i + 1] = pre[pre[v][i]][i];
            d[v] = d[u] + w;
            dfs(v);
            deep[u] = max(deep[v], deep[u]);
        }
    }
    io[u].S = t;
}

bool is_child(int r, int c)
{
    return io[r].F <= io[c].F && io[c].S <= io[r].S;
}

int jump(int u, int v)
{
    for (int i = P - 1; i >= 0; i--)
        if (!is_child(pre[v][i], u))
            v = pre[v][i];
    return v;
}

int lca(int u, int v)
{
    v = jump(u, v);
    if (!is_child(v, u))
        v = pre[v][0];
    return v;
}

vector<ll> pm = {0};
void dfsolve(int u, int r, ll rootd )
{
    ++k;
    ins[u] = k;
    vis[u] = 1;
    multiset<ll, greater<ll>> ms;
    for (auto [v, w] : E[u])
        if (!vis[v])
            ms.insert(deep[v]);

    for (auto [trap, i] : qs[r][u])
    {
        if (is_child(trap, u))
            ans[i] = 1;
        else if (is_child(u, trap))
            ans[i] = -rootd;
        else
        {
            int l = lca(trap, u);
            int ch = jump(trap, u);
            ans[i] = -max({pm[ins[l]],
                           seg.query(ins[ch], n) + 2 * d[l],
                           deep[u] - 2 * (d[u] - d[l])});
        }
    }
    for (auto [v, w] : E[u])
        if (!vis[v])
        {
            ms.erase(ms.find(deep[v]));
            ll mxd = 0;
            if (!ms.empty()) mxd = *ms.begin();
            mxd -= 2 * d[u];
            seg.modify(k, mxd);
            ms.insert(deep[v]);

            mxd = max(mxd, pm.back());
            pm.emplace_back(mxd);
            dfsolve(v, r, rootd);
            pm.pop_back();
        }
    ins[u] = 0;
    --k;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> n >> q;
    for (int i = 1; i < n; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        E[u].emplace_back(v, w);
        E[v].emplace_back(u, w);
        sum += 2 * w;
    }
    for (int i = 1; i <= q; i++)
    {
        int s, k, t;
        cin >> s >> k >> t;
        qs[s][k].emplace_back(t, i);
    }
    io[0] = pii(0, n + 1);
    for (int r = 1; r <= n; r++)
    {
        t = 0, d[r] = 0;
        fill(vis + 1, vis + 1 + n, 0);
        for (int i = 0; i < P; i++)
            pre[r][i] = 0;
        dfs(r);
        seg.init(n);
        fill(vis + 1, vis + 1 + n, 0);
        dfsolve(r, r, *max_element(d + 1, d + 1 + n));
    }
    for (int i = 1; i <= q; i++)
    {
        if (ans[i] > 0)
            cout << "impossible\n";
        else 
            cout << sum + ans[i] << '\n';
    }
}

/*
5 4
1 2 600000000
1 3 200000000
3 4 800000000
3 5 400000000
1 2 4
1 4 2
5 2 1
4 3 1

7 1
1 2 1
2 6 100
2 7 1
1 3 1
3 4 1
3 5 1
1 7 5
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 5680kb

input:

22 5
1 2 7
2 3 5
3 4 8
3 6 6
3 7 3
2 5 6
5 8 2
8 9 11
2 10 16
1 11 12
11 12 4
11 13 9
1 14 25
14 15 4
15 16 5
15 17 6
15 18 1
15 19 8
14 20 7
20 21 9
20 22 17
1 19 9
1 9 19
1 8 9
1 9 8
2 22 11

output:

316
293
293
impossible
323

result:

wrong answer 5th lines differ - expected: '314', found: '323'