QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#719631#9605. 新生舞会hos_lyric#40 94ms98020kbC++144.9kb2024-11-07 05:31:352024-11-07 05:31:35

Judging History

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

  • [2024-11-07 05:31:35]
  • 评测
  • 测评结果:40
  • 用时:94ms
  • 内存:98020kb
  • [2024-11-07 05:31:35]
  • 提交

answer

// https://yukicoder.me/problems/no/2800

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


// !!!Watch out for stack overflow!!!
// Meldable
// 0 for null, ts[0] = T()
// Manages the number of elements (as a set).
template <class T> struct Seg {
  static constexpr int NUM_NODES = 1 << 23;
  int h;
  int nodesLen;
  int nxt[NUM_NODES][2];
  T ts[NUM_NODES], lz[NUM_NODES];
  void init(int h_) {
    h = h_;
    nodesLen = 1;
    nxt[0][0] = nxt[0][1] = 0;
    ts[0] = T();
    lz[0] = T();
  }
  int newNode() {
    assert(nodesLen < NUM_NODES);
    const int u = nodesLen++;
    nxt[u][0] = nxt[u][1] = 0;
    ts[u] = T(0);
    lz[u] = T(0);
    return u;
  }

  // u: height e
  void ch(int e, int u, T t) {
    if (u) {
      if (e && (t >> (e - 1) & 1)) swap(nxt[u][0], nxt[u][1]);
      lz[u] ^= t;
    }
  }
  void ch(int u, T t) {
    ch(h, u, t);
  }
  // u: height (e+1)
  void push(int e, int u) {
    if (lz[u]) {
      ch(e, nxt[u][0], lz[u]);
      ch(e, nxt[u][1], lz[u]);
      lz[u] = 0;
    }
  }
  void pull(int u) {
    ts[u] = ts[nxt[u][0]] + ts[nxt[u][1]];
  }

  int build(T a) {
    assert(0 <= a); assert(a < T(1) << h);
    const int u = nodesLen;
    for (int e = 0; e <= h; ++e) ts[newNode()] = T(1);
    for (int e = 0; e < h; ++e) nxt[u + (e + 1)][a >> e & 1] = u + e;
    return u + h;
  }

  T mex(int u) {
    if (ts[u] == T(1) << h) return T(1) << h;
    T a = 0;
    for (int e = h; --e >= 0; ) {
      push(e, u);
      if (ts[nxt[u][0]] == T(1) << e) {
        a |= T(1) << e;
        u = nxt[u][1];
      } else {
        u = nxt[u][0];
      }
    }
    return a;
  }

  // Frees v.
  int meld(int e, int u, int v) {
    if (!u) return v;
    if (!v) return u;
    if (!e) {
      ts[u] |= ts[v];
      return u;
    }
    --e;
    push(e, u);
    push(e, v);
    nxt[u][0] = meld(e, nxt[u][0], nxt[v][0]);
    nxt[u][1] = meld(e, nxt[u][1], nxt[v][1]);
    pull(u);
    return u;
  }
  int meld(int u, int v) {
    return meld(h, u, v);
  }

  // Does not push.
  void print(int e, T a, int u) {
    if (!u) return;
    cerr << string(2 * (h - e), ' ') << u << " [" << a << ", " << (a | T(1) << e) << ") " << ts[u] << " " << lz[u] << endl;
    if (!e) return;
    --e;
    print(e, a, nxt[u][0]);
    print(e, a | T(1) << e, nxt[u][1]);
  }
  void print(int u) {
    if (!u) cerr << "[Seg::print] null" << endl;
    print(h, T(0), u);
  }

  // Pushes
  string toString(int e, T a, int u) {
    if (!u) return "";
    if (!e) return to_string(a) + ",";
    --e;
    push(e, u);
    return toString(e, a, nxt[u][0]) + toString(e, a | T(1) << e, nxt[u][1]);
  }
  string toString(int u) {
    return "{" + toString(h, T(0), u) + "}";
  }
};


int N;
vector<int> P;

vector<vector<int>> graph;

vector<int> dp;
Seg<int> seg;
int dfs(int u) {
  int sum = 0;
  int ret = seg.build(0);
  for (const int v : graph[u]) {
    const int res = dfs(v);
    sum ^= dp[v];
    seg.ch(res, dp[v]);
    ret = seg.meld(ret, res);
  }
  seg.ch(ret, sum);
  dp[u] = seg.mex(ret);
// cerr<<"[dfs] "<<u<<": "<<dp[u]<<" "<<seg.toString(ret)<<endl;
  return ret;
}

int main() {
  int T;
  for (; ~scanf("%d", &T); ) {
    int ans = 0;
    for (int t = 0; t < T; ++t) {
      scanf("%d", &N);
      P.assign(N, -1);
      for (int u = 1; u < N; ++u) {
        scanf("%d", &P[u]);
        --P[u];
      }
      
      graph.assign(N, {});
      for (int u = 1; u < N; ++u) {
        graph[P[u]].push_back(u);
      }
      
      int E;
      for (E = 0; !(N < 1 << E); ++E) {}
      seg.init(E);
      dp.assign(N, -1);
      dfs(0);
// cerr<<"dp = "<<dp<<endl;
      ans ^= dp[0];
    }
    printf("%d\n", ans);
  }
  return 0;
}

詳細信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 3ms
memory: 8528kb

input:

12
13
1 2 1 4 2 5 3 3 2 9 6 4
392
1 2 2 4 4 3 3 6 9 10 9 11 13 14 15 15 17 18 19 20 6 21 23 12 24 26 27 28 29 30 31 32 33 31 34 33 36 9 38 40 41 25 42 44 45 46 47 48 49 50 51 52 53 21 32 52 54 58 59 60 53 61 63 64 65 66 67 68 69 70 4 1 71 74 75 76 77 78 79 80 81 52 82 55 84 86 84 87 89 90 91 92 93 1...

output:

1875

result:

ok 1 number(s): "1875"

Test #2:

score: 10
Accepted
time: 3ms
memory: 9196kb

input:

1
5000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 5 21 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 79 85 87 88 89 90 91 92 93 94 95 96 97 98 99 100 10...

output:

4950

result:

ok 1 number(s): "4950"

Test #3:

score: 10
Accepted
time: 4ms
memory: 14604kb

input:

1
5000
1 1 2 1 2 2 3 4 9 10 9 12 13 10 15 12 14 14 18 20 19 22 19 20 21 24 26 27 25 30 30 32 33 33 33 32 34 35 38 40 39 38 40 43 43 44 43 46 48 50 50 48 50 53 53 52 56 56 57 56 57 60 61 61 61 62 66 65 69 67 71 70 71 73 75 73 73 75 78 80 78 80 82 80 82 84 86 85 89 86 87 92 92 92 95 94 95 95 95 100 10...

output:

3204

result:

ok 1 number(s): "3204"

Subtask #2:

score: 0
Memory Limit Exceeded

Test #4:

score: 0
Memory Limit Exceeded

input:

15
1
5
1 1 3 3
10603
1 2 2 1 5 3 6 3 2 7 6 6 10 5 4 16 4 16 18 4 11 22 8 14 2 26 22 9 10 20 28 26 1 27 8 35 3 4 6 25 14 9 33 44 15 26 28 34 24 18 48 5 33 11 37 32 19 14 41 6 46 60 8 25 62 32 59 27 54 50 62 44 51 18 53 46 42 31 26 11 78 71 40 60 2 13 35 84 34 76 51 88 33 13 88 85 80 70 21 74 54 79 75...

output:


result:


Subtask #3:

score: 30
Accepted

Dependency #1:

100%
Accepted

Test #6:

score: 30
Accepted
time: 73ms
memory: 34932kb

input:

16
4
1 1 3
9
1 2 3 4 5 6 7 8
58
1 2 2 4 5 6 7 8 9 2 10 12 13 5 8 14 17 4 18 20 21 22 8 10 4 23 27 5 26 21 20 28 26 33 35 36 37 38 22 39 41 42 43 44 34 45 47 3 31 48 51 52 53 54 44 44 21
30
1 2 1 4 5 2 6 8 8 2 11 5 9 13 10 16 17 18 12 10 11 19 12 23 23 3 16 26 29
36
1 1 1 2 2 1 2 3 1 10 6 3 12 3 8 9 ...

output:

20841

result:

ok 1 number(s): "20841"

Test #7:

score: 30
Accepted
time: 91ms
memory: 98020kb

input:

1
200000
1 2 3 4 5 6 7 7 9 10 11 12 13 14 15 13 16 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 99 100...

output:

195053

result:

ok 1 number(s): "195053"

Test #8:

score: 30
Accepted
time: 86ms
memory: 86948kb

input:

1
200000
1 1 2 2 2 4 4 4 6 10 9 10 11 12 13 15 15 18 19 19 19 20 20 22 23 23 23 24 26 27 28 28 29 34 31 35 33 37 35 40 37 39 43 43 43 46 46 48 48 50 48 52 49 50 53 54 54 56 57 56 61 60 59 60 61 65 64 64 65 69 70 72 73 74 72 73 75 78 76 77 80 79 80 81 81 82 86 84 87 90 88 91 93 93 94 94 94 97 95 100 ...

output:

127754

result:

ok 1 number(s): "127754"

Test #9:

score: 30
Accepted
time: 52ms
memory: 78628kb

input:

1
200000
1 2 3 4 1 3 4 4 4 1 3 2 3 3 3 5 2 1 4 5 4 3 5 3 3 2 2 1 1 2 4 2 4 3 3 1 1 2 4 5 5 5 5 2 1 5 3 4 1 5 2 3 4 5 2 3 3 3 1 2 4 2 2 1 1 2 4 1 3 4 1 4 3 4 4 2 1 4 4 5 1 2 1 1 3 2 4 3 3 3 1 2 3 4 3 1 5 5 2 3 2 2 4 2 2 5 2 2 3 5 1 2 2 1 5 5 3 4 2 1 2 5 3 4 5 5 4 5 1 5 3 3 4 3 5 5 4 2 5 5 2 2 3 3 4 2...

output:

10

result:

ok 1 number(s): "10"

Test #10:

score: 30
Accepted
time: 64ms
memory: 80772kb

input:

1
200000
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 11 12 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 20 20 21 21 22 22 23 23 24 24 25 25 26 26 27 27 28 28 29 29 30 30 31 31 32 32 33 33 34 34 35 35 36 36 37 37 38 38 39 39 40 40 41 41 42 42 43 43 44 44 45 45 46 46 47 47 48 48 49 49 50 50 51 51 52 ...

output:

66

result:

ok 1 number(s): "66"

Test #11:

score: 30
Accepted
time: 94ms
memory: 80688kb

input:

1
200000
1 2 2 1 5 3 2 7 4 1 5 10 2 3 15 16 13 1 12 13 19 13 19 16 23 9 10 1 23 26 30 7 25 16 25 36 16 27 10 39 40 20 6 5 30 3 46 41 35 30 24 28 28 8 38 25 32 38 29 21 56 17 24 38 5 7 62 58 13 8 11 45 47 55 55 12 64 8 55 7 5 31 73 20 85 83 44 74 58 65 57 66 27 47 66 56 90 39 96 15 37 14 30 61 96 73 ...

output:

8192

result:

ok 1 number(s): "8192"

Test #12:

score: 30
Accepted
time: 94ms
memory: 95232kb

input:

1
200000
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 25 62 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 99 100...

output:

199948

result:

ok 1 number(s): "199948"

Subtask #4:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%