QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#48503#4387. Static Query on TreelqhsmashAC ✓339ms70596kbC++4.7kb2022-09-14 10:20:052022-09-14 10:20:07

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:07]
  • 评测
  • 测评结果:AC
  • 用时:339ms
  • 内存:70596kb
  • [2022-09-14 10:20:05]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 2e5 + 50;

int T, n, q, A, B, C, stk[N << 2], tp, vis[N << 2];
vector<int> g[N];
int top[N], son[N], id[N], siz[N], fa[N], dep[N], idx;

struct node {
    int l, r, dp[8], lazy;
    node () { 
        lazy = 0;
        memset (dp, 0, sizeof (int) * 8);
        dp[0] = 1;
    }
    node (int le, int ri): l(le), r(ri) {
        lazy = 0;
        memset (dp, 0, sizeof (int) * 8);
        dp[0] = r - l + 1;
    }
}t[N << 2];
#define ls (x << 1)
#define rs (x << 1 | 1)

void dfs (int u, int p) {
    siz[u] = 1, fa[u] = p, dep[u] = dep[p] + 1;
    for (int v : g[u]) {
        if (v == p) continue;
        dfs (v, u);
        siz[u] += siz[v];
        if (siz[son[u]] < siz[v]) son[u] = v;
    }
}

void dfs2 (int u, int tt) {
    top[u] = tt, id[u] = ++ idx;
    if (! son[u]) return ;
    dfs2 (son[u], tt);
    for (int v : g[u]) {
        if (v == fa[u] || v == son[u]) continue;
        dfs2 (v, v);
    }
}

void pushdown (int x, int v) {
        if (! vis[x]) stk[++ tp] = x, vis[x] = true;
        for (int i = 7; i >= 0; i --) {
                if ((i | v) != i) {
                        t[x].dp[i | v] += t[x].dp[i];
                        t[x].dp[i] = 0;
                }
        }    
        t[x].lazy |= v;
}

void pushdown (int x) {
    if (t[x].lazy) {
        t[ls].lazy |= t[x].lazy;
        t[rs].lazy |= t[x].lazy;
        if (! vis[ls]) stk[++ tp] = ls, vis[ls] = true;
        if (! vis[rs]) stk[++ tp] = rs, vis[rs] = true;
        for (int i = 7; i >= 0; i --) {
                if ((i | t[x].lazy) != i) {
                        t[ls].dp[i | t[x].lazy] += t[ls].dp[i];
                        t[rs].dp[i | t[x].lazy] += t[rs].dp[i];
                        t[ls].dp[i] = t[rs].dp[i] = 0;
                }
        }
        t[x].lazy = 0;
    }
}

void pushup (int x) {
    if (! vis[x]) stk[++ tp] = x, vis[x] = true;
    for (int i = 0; i < 8; i ++) 
        t[x].dp[i] = t[ls].dp[i] + t[rs].dp[i];
}

void build (int l, int r, int x = 1) { 
    t[x] = node ();
    t[x].l = l, t[x].r = r;
    if (l == r) return ;
    int mid = (l + r) >> 1;
    build (l, mid, ls), build (mid + 1, r, rs);
    pushup (x);
    // cerr << t[x].l << ' ' << t[x].r << ' ' << t[x].dp[0] << endl; 
}

void update (int l, int r, int v, int x = 1) {
    if (l <= t[x].l && t[x].r <= r) {
        pushdown (x, v);
        return ;
    }
    pushdown (x);
    int mid = (t[x].l + t[x].r) >> 1;
    if (l <= mid) update (l, r, v, ls);
    if (r >  mid) update (l, r, v, rs);
    pushup (x);
}

void update_path (int u, int v, int c) {
    while (top[u] != top[v]) {
        if (dep[top[u]] < dep[top[v]]) swap (u, v);
        update (id[top[u]], id[u], c);
        u = fa[top[u]];
    }
    if (dep[u] < dep[v]) swap (u, v);
    update (id[v], id[u], c);
}

void print (int x = 1) {
    if (t[x].l == t[x].r) {
        cerr << t[x].l << endl;
        for (int i = 0; i < 8; i ++) 
            cerr << t[x].dp[i] << ' ';
        cerr << endl;
        return ;
    }
    print (ls), print (rs);
}

int main() {
#ifdef LOCAL
    clock_t c1 = clock();
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    // ===================================================    
    scanf ("%d", &T);
    while (T --) {
        scanf ("%d%d", &n, &q);
        for (int i = 1; i <= n; i ++) g[i].clear ();
        dep[1] = 0, idx = 0, tp = 0;
        memset (son, 0, sizeof (son));
        memset (vis, 0, sizeof (vis));
        for (int i = 2; i <= n; i ++) {
            int x; scanf ("%d", &x);
            g[i].push_back (x);
            g[x].push_back (i);
        }
        dfs (1, 1);
        dfs2 (1, 1);
        build (1, n);
        while (q --) {
            scanf ("%d%d%d", &A, &B, &C);
            for (int i = 1; i <= A; i ++) {
                int x; scanf ("%d", &x);
                update_path (x, 1, 1);
            }
            for (int i = 1; i <= B; i ++) {
                int x; scanf ("%d", &x);
                update_path (x, 1, 2);
            }
            for (int i = 1; i <= C; i ++) {
                int x; scanf ("%d", &x);
                update (id[x], id[x] + siz[x] - 1, 4);
            }
            printf ("%d\n", t[1].dp[7]);
            while (tp) {
                vis[stk[tp]] = false;
                int lef = t[stk[tp]].l, rig = t[stk[tp]].r;
                t[stk[tp]] = node (lef, rig), tp --;
            }
        }
    }
    // ===================================================    
#ifdef LOCAL
    cerr << "Time Used: " << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 339ms
memory: 70596kb

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