Submission #1013716

# Submission time Handle Problem Language Result Execution time Memory
1013716 2024-07-04T03:14:21 Z 12345678 Tourism (JOI23_tourism) C++17
0 / 100
138 ms 14180 KB
#include <bits/stdc++.h>

using namespace std;

const int nx=1e5+5, kx=17;

int n, m, q, u, v, in[nx], out[nx], pa[nx][kx], t, c[nx], l=1, r, a[nx], b[nx], ans, lvl[nx];
vector<int> d[nx];

void dfs(int u, int p)
{
    pa[u][0]=p;
    lvl[u]=lvl[p]+1;
    for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
    in[u]=++t;
    for (auto v:d[u]) if (v!=p) dfs(v, u);
    out[u]=t;
}

struct segtree
{
    int d[4*nx];
    void update(int l, int r, int i, int idx, int vl)
    {
        if (idx<l||r<idx) return;
        if (l==r) return d[i]+=vl, void();
        int md=(l+r)/2;
        update(l, md, 2*i, idx, vl);
        update(md+1, r, 2*i+1, idx, vl);
        d[i]=d[2*i]+d[2*i+1];
    }
    int query(int l, int r, int i, int ql, int qr)
    {
        if (qr<l||r<ql) return 0;
        if (ql<=l&&r<=qr) return d[i];
        int md=(l+r)/2;
        return query(l, md, 2*i, ql, qr)+query(md+1, r, 2*i+1, ql, qr);
    }
} s;

int getlca(int u, int cur)
{
    if (s.query(1, n, 1, in[u], out[u])==cur) return u;
    for (int i=kx-1; i>=0; i--) if (s.query(1, n, 1, in[u], out[u])!=cur) u=pa[u][i];
    return pa[u][0];
}

void add(int u)
{
    if (l==r) return ans=1, s.update(1, n, 1, in[u], 1), void();
    if (s.query(1, n, 1, in[u], out[u])>0) return s.update(1, n, 1, in[u], 1), void();
    int v=u, lca=getlca(c[l], r-l);
    for (int i=kx-1; i>=0; i--) if (s.query(1, n, 1, in[pa[v][i]], out[pa[v][i]])==0) v=pa[v][i];
    if (lvl[lca]<lvl[v]) ans+=lvl[u]-lvl[v]+1, s.update(1, n, 1, in[u], 1);
    else ans+=lvl[u]-lvl[v]+1+lvl[lca]-lvl[v]+1, s.update(1, n, 1, in[u], 1);
}

void rem(int u)
{
    s.update(1, n, 1, in[u], -1);
    if (s.query(1, n, 1, in[u], out[u])>0) return;
    int v=u, lca=getlca(c[l], r-l+1);
    for (int i=kx-1; i>=0; i--) if (s.query(1, n, 1, in[pa[v][i]], out[pa[v][i]])==0) v=pa[v][i];
    if (lvl[lca]<lvl[v]) ans-=lvl[u]-lvl[v]+1;
    else ans-=(lvl[u]-lvl[v]+1)+lvl[lca]-lvl[v]+1;

}

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n>>m>>q;
    for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
    dfs(1, 1);
    for (int i=1; i<=m; i++) cin>>c[i];
    for (int i=1; i<=q; i++)
    {
        cin>>a[i]>>b[i];
        while (r<b[i]) add(c[++r]);
        while (l<a[i]) rem(c[l++]);
        cout<<ans<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4184 KB Output is correct
2 Incorrect 1 ms 4188 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4184 KB Output is correct
2 Incorrect 1 ms 4188 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 4188 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4188 KB Output is correct
2 Incorrect 120 ms 11624 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4188 KB Output is correct
2 Correct 1 ms 4188 KB Output is correct
3 Correct 2 ms 4184 KB Output is correct
4 Incorrect 138 ms 14180 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4184 KB Output is correct
2 Incorrect 1 ms 4188 KB Output isn't correct
3 Halted 0 ms 0 KB -