QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#431757#4387. Static Query on TreeLRAC ✓146ms39844kbC++203.9kb2024-06-06 01:43:452024-06-06 01:43:45

Judging History

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

  • [2024-06-06 01:43:45]
  • 评测
  • 测评结果:AC
  • 用时:146ms
  • 内存:39844kb
  • [2024-06-06 01:43:45]
  • 提交

answer

#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define ii array<int, 2>

using namespace std;
const int maxn = 2e5 + 5;
int n, m, cnt;
int in[maxn], h[maxn], par[18][maxn], out[maxn];
vector<int> adj[maxn];
int seg[4 * maxn];

void dfs(int x = 1, int p = 1)
{
    in[x] = ++cnt;
    h[x] = h[p] + 1;
    par[0][x] = p;
    for (int i = 1; i < 18; i++)
        par[i][x] = par[i - 1][par[i - 1][x]];
    for (int i : adj[x]) dfs(i, x);
    out[x] = cnt;
}

bool cmp(int x, int y)
{
    return in[x] < in[y];
}

int lca(int x, int y)
{
    if (h[x] > h[y]) swap(x, y);
    int dist = h[y] - h[x];
    for (int i = 0; i < 18; i++)
        if (dist >> i & 1)
            y = par[i][y];
    if (x == y) return x;
    for (int i = 17; i >= 0; i--)
        if (par[i][x] != par[i][y])
            x = par[i][x], y = par[i][y];
    return par[0][x];
}

void upd(int pos, int val, int id = 1, int l = 1, int r = n)
{
    if (l == r) return void(seg[id] = val);
    int mid = (l + r) / 2;
    if (pos <= mid) upd(pos, val, id * 2, l, mid);
    else upd(pos, val, id * 2 + 1, mid + 1, r);
    seg[id] = seg[id * 2] + seg[id * 2 + 1];
}

int qry(int lx, int rx, int id = 1, int l = 1, int r = n)
{
    if (lx > r || l > rx) return 0;
    if (lx <= l && r <= rx) return seg[id];
    int mid = (l + r) / 2;
    return qry(lx, rx, id * 2, l, mid) + qry(lx, rx, id * 2 + 1, mid + 1, r);
}

void solve()
{
    cin >> n >> m;
    for (int i = 2, x; i <= n; i++)
        cin >> x, adj[x].emplace_back(i);
    dfs();
    while (m--)
    {
        int A, B, C; cin >> A >> B >> C;
        vector<int> a(A), b(B), c(C);
        for (int &i : a) cin >> i;
        for (int &i : b) cin >> i;
        for (int &i : c) cin >> i;
        if (min({A, B, C}) == 0)
        {
            cout << "0\n";
            continue;
        }
        sort(all(a), cmp);
        sort(all(b), cmp);
        sort(all(c), cmp);
        int pt = 0;
        set<int> from;
        for (int i : a)
        {
            while (pt < b.size() && in[b[pt]] <= in[i])
                pt++;
            int le = 1, ri = 1;
            if (pt > 0)
                le = lca(i, b[pt - 1]);
            if (pt < b.size())
                ri = lca(i, b[pt]);
            if (in[le] > in[ri]) swap(le, ri);
            from.emplace(ri);
        }
        set<int> tmp1, tmp2;
        set<ii> ok;
        for (int i : c)
            tmp1.emplace(i),
            tmp2.emplace(i);
        for (int i : tmp1)
        {
            if (!tmp2.count(i)) continue;
            auto it = tmp2.find(i);
            it = next(it);
            while (it != tmp2.end())
            {
                if (out[*it] > out[i]) break;
                it = next(it);
                tmp2.erase(prev(it));
            }
        }
        for (int i : from) upd(in[i], 1);
        for (int i : from)
            if (qry(in[i], out[i]) == 1)
                ok.insert({in[i], i});
        for (int i : from) upd(in[i], 0);
        int ans = 0;
        for (int i : tmp2)
        {
            ii tmp = {in[i], 0};
            auto it = ok.lower_bound(tmp);
            int pre = -1;
            while (it != ok.end())
            {
                auto [v, u] = *it;
                if (out[u] > out[i]) break;
                ans += (h[u] - h[i] + 1);
                if (pre != -1)
                    ans -= (h[lca(pre, u)] - h[i] + 1);
                pre = u;
                it++;
            }
        }
        cout << ans << "\n";
    }
    for (int i = 1; i <= n; i++)
    {
        adj[i].clear();
        h[i] = in[i] = out[i] = 0;
        for (int j = 0; j < 18; j++)
            par[j][i] = 0;
    }
    cnt = 0;
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
    int tst; cin >> tst; while (tst--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 146ms
memory: 39844kb

input:

1
200000 18309
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 ...

output:

102147
62590
87270
88880
7654
61542
62953
85022
55135
54125
70500
64356
25824
88300
42278
15336
18132
28734
90282
42889
28099
31311
96842
19959
34366
60205
78358
91142
56048
74688
86091
51979
94750
11989
89544
86860
56720
29534
52343
90031
79002
90293
94554
48340
65015
9181
15016
19884
49445
14181
6...

result:

ok 18309 numbers