QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#431753#4387. Static Query on TreeLRAC ✓230ms62112kbC++204.4kb2024-06-06 01:37:522024-06-06 01:37:53

Judging History

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

  • [2024-06-06 01:37:53]
  • 评测
  • 测评结果:AC
  • 用时:230ms
  • 内存:62112kb
  • [2024-06-06 01:37:52]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#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];
ii 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)
{
//    assert(min(x, y) >= 1 && max(x, y) <= n);
    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];
}

ii operator + (ii a, ii b)
{
    ii c;
    for (int i = 0; i < 2; i++)
        c[i] = a[i] + b[i];
    return c;
}

ii operator - (ii a, ii b)
{
    ii c;
    for (int i = 0; i < 2; i++)
        c[i] = abs(a[i] - b[i]);
    return c;
}

void upd(int pos, ii 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];
}

ii qry(int lx, int rx, int id = 1, int l = 1, int r = n)
{
    if (lx > r || l > rx) return {0, 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]);//, cerr << pt - 1 << " " << b[pt - 1] << "help\n";
            if (pt < b.size())
                ri = lca(i, b[pt]);//, cerr << pt << " " << b[pt] << "help\n";
//            cerr << le << " " << ri << " fuckkk\n";
            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, 1});
        for (int i : from)
        {
            auto cur = qry(in[i], out[i]);
            if (cur[0] == 1) ok.insert({in[i], i});
        }
        for (int i : from) upd(in[i], {0, 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();
}

详细

Test #1:

score: 100
Accepted
time: 230ms
memory: 62112kb

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