QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#48504#4387. Static Query on TreelqhsmashAC ✓186ms46536kbC++4.2kb2022-09-14 10:20:532022-09-14 10:20:56

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-09-14 10:20:56]
  • 评测
  • 测评结果:AC
  • 用时:186ms
  • 内存:46536kb
  • [2022-09-14 10:20:53]
  • 提交

answer

#include <iostream>
#include <vector>
#define now nodes[rt]
#define ls nodes[rt << 1]
#define rs nodes[rt << 1 | 1]
using namespace std;
const int maxn = 2e5 + 10;
int n, q;
vector<int> G[maxn];
int fa[maxn], son[maxn], dep[maxn], sz[maxn], dfn[maxn], rk[maxn], top[maxn];
int tot;
void init() {
    tot = 0;
}
void dfs(int u) {
    sz[u] = 1;
    dep[u] = dep[fa[u]] + 1;
    for (auto v : G[u]) {
        dfs(v);
        if (sz[v] > sz[son[u]])
            son[u] = v;
        sz[u] += sz[v];
    }
}
void dfs(int u, int t) {
    top[u] = t;
    dfn[u] = ++tot;
    rk[tot] = u;
    if (!son[u])
        return;
    dfs(son[u], t);
    for (auto v : G[u]) {
        if (v == son[u])
            continue;
        dfs(v, v);
    }
}
struct SegTree {
    struct Node {
        int l, r;
        int a, b, ab;
        int tag_a, tag_b, tag_cls;
    } nodes[maxn << 2];
    void build(int rt, int l, int r) {
        now = {l, r, 0, 0, 0, 0, 0, 0};
        if (l == r)
            return;
        int mid = l + r >> 1;
        build(rt << 1, l, mid);
        build(rt << 1 | 1, mid + 1, r);
    }
    void pushup(int rt) {
        now.a = ls.a + rs.a;
        now.b = ls.b + rs.b;
        now.ab = ls.ab + rs.ab;
    }
    void update_node_a(int rt) {
        now.tag_a = 1;
        now.a = now.r - now.l + 1;
        now.ab = now.b;
    }
    void update_node_b(int rt) {
        now.tag_b = 1;
        now.b = now.r - now.l + 1;
        now.ab = now.a;
    }
    void update_node_cls(int rt) {
        now.tag_cls = 1;
        now.a = now.b = now.ab = now.tag_a = now.tag_b = 0;
    }
    void pushdown(int rt) {
        if (now.tag_cls) {
            update_node_cls(rt << 1);
            update_node_cls(rt << 1 | 1);
            now.tag_cls = 0;
        }
        if (now.tag_a) {
            update_node_a(rt << 1);
            update_node_a(rt << 1 | 1);
            now.tag_a = 0;
        }
        if (now.tag_b) {
            update_node_b(rt << 1);
            update_node_b(rt << 1 | 1);
            now.tag_b = 0;
        }
    }
    void update(int rt, int L, int R, void (SegTree::*op)(int rt)) {
        if (L <= now.l && now.r <= R) {
            (this->*op)(rt);
            return;
        }
        pushdown(rt);
        if (L <= ls.r)
            update(rt << 1, L, R, op);
        if (R >= rs.l)
            update(rt << 1 | 1, L, R, op);
        pushup(rt);
    }
    int query(int rt, int L, int R) {
        if (L <= now.l && now.r <= R) {
            return now.ab;
        }
        pushdown(rt);
        int ans = 0;
        if (L <= ls.r)
            ans += query(rt << 1, L, R);
        if (R >= rs.l)
            ans += query(rt << 1 | 1, L, R);
        return ans;
    }
} seg;
 
void update_chain(int u, void (SegTree::*op)(int rt)) {
    int v = 1;
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]])
            swap(u, v);
        seg.update(1, dfn[top[u]], dfn[u], op);
        u = fa[top[u]];
    }
    if (dep[u] > dep[v])
        swap(u, v);
    seg.update(1, dfn[u], dfn[v], op);
}
 
void update_tree(int u, void (SegTree::*op)(int rt)) {
    seg.update(1, dfn[u], dfn[u] + sz[u] - 1, op);
}
 
int query_tree(int u) {
    return seg.query(1, dfn[u], dfn[u] + sz[u] - 1);
}
 
void solve() {
    cin >> n >> q;
    for (int i = 2; i <= n; i++) {
        cin >> fa[i];
        G[fa[i]].push_back(i);
    }
    dfs(1);
    dfs(1, 1);
    seg.build(1, 1, n);
    while (q--) {
        int a, b, c, t;
        int ans = 0;
        cin >> a >> b >> c;
        for (int i = 0; i < a; i++) {
            cin >> t;
            update_chain(t, &SegTree::update_node_a);
        }
        for (int i = 0; i < b; i++) {
            cin >> t;
            update_chain(t, &SegTree::update_node_b);
        }
        for (int i = 0; i < c; i++) {
            cin >> t;
            ans += query_tree(t);
            update_tree(t, &SegTree::update_node_cls);
        }
        cout << ans << '\n';
        update_tree(1, &SegTree::update_node_cls);
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin >> T;
    while (T--)
        solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 186ms
memory: 46536kb

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