QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#761607#5222. 大森林hos_lyric100 ✓220ms24692kbC++145.9kb2024-11-19 01:45:212024-11-19 01:45:21

Judging History

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

  • [2024-11-19 01:45:21]
  • 评测
  • 测评结果:100
  • 用时:220ms
  • 内存:24692kb
  • [2024-11-19 01:45:21]
  • 提交

answer

#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")


// Link-Cut Tree
//   solid path as BST; left: root side
//   Modify update() for other data.
struct Tree {
  Tree *l, *r, *p;
  int sz;
  int id;
  Int val, sum;
  explicit Tree(int id_ = -1, Int val_ = 0) : id(id_), val(val_), sum(val_) {
    l = r = p = nullptr;
    sz = 1;
  }
  void update() {
    sz = (l ? l->sz : 0) + 1 + (r ? r->sz : 0);
    sum = (l ? l->sum : 0) + val + (r ? r->sum : 0);
  }
  bool isRoot() const {
    return (!p || (p->l != this && p->r != this));
  }
  void rotate() {
         if (p->l == this) { if (r) { r->p = p; } p->l = r; r = p; }
    else if (p->r == this) { if (l) { l->p = p; } p->r = l; l = p; }
    Tree *pp = p->p;
    if (pp) {
           if (pp->l == p) pp->l = this;
      else if (pp->r == p) pp->r = this;
    }
    p->update(); p->p = this; p = pp;
  }
  void splay() {
    for (; !isRoot(); rotate()) {
      if (!p->isRoot()) ((p->l == this) == (p->p->l == p)) ? p->rotate() : rotate();
    }
    update();
  }

  // Makes the path from v to the root solid
  // Returns the node where it entered the last solid path
  Tree *expose() {
    Tree *u = this, *v = nullptr;
    for (; u; u = u->p) { u->splay(); u->r = v; u->update(); v = u; }
    splay();
    return v;
  }

  // parent of this := u
  void link(Tree *u) {
    expose(); u->expose(); p = u; u->r = this; u->update();
  }

  // parent of this := null
  void cut() {
    expose(); l->p = nullptr; l = nullptr; update();
  }

  // the root of the tree this belongs
  Tree *root() {
    expose();
    for (Tree *u = this; ; u = u->l) if (!u->l) { u->splay(); return u; }
  }

  // LCA of this and u
  //   Assumes this->root == u->root
  Tree *lca(Tree *u) {
    expose(); return u->expose();
  }
};

////////////////////////////////////////////////////////////////////////////////


int N, M;
int C, T, Q;
vector<int> O, L, R, X;
vector<int> I, U, V;

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    C = 1;
    O = L = R = X = I = U = V = {};
    O.push_back(1);
    L.push_back(0);
    R.push_back(N);
    X.push_back(0);
    for (int m = 0; m < M; ++m) {
      int o;
      scanf("%d", &o);
      if (o == 0) {
        int l, r;
        scanf("%d%d", &l, &r);
        --l;
        O.push_back(o);
        L.push_back(l);
        R.push_back(r);
        X.push_back(C++);
      } else if (o == 1) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        --l;
        --x;
        O.push_back(o);
        L.push_back(l);
        R.push_back(r);
        X.push_back(x);
      } else if (o == 2) {
        int i, u, v;
        scanf("%d%d%d", &i, &u, &v);
        --i;
        --u;
        --v;
        I.push_back(i);
        U.push_back(u);
        V.push_back(v);
      } else {
        assert(false);
      }
    }
    T = O.size();
    Q = I.size();
cerr<<"C = "<<C<<", T = "<<T<<", Q = "<<Q<<endl;
    
    vector<int> birth(C, -1);
    for (int t = 0; t < T; ++t) if (O[t] == 0) birth[X[t]] = t;
    birth[0] = T;
    /*
      1<----1<----1<----1
       \ \   \     \ \   
        0 0   0     0 0  
      activate O[t] = 1: link to birth[X[t]]
    */
    vector<int> par(T + 1, -1);
    {
      int last = T;
      for (int t = 0; t < T; ++t) {
        par[t] = last;
        if (O[t] == 1) last = t;
      }
    }
    vector<Tree> nodes(T + 1);
    for (int t = 0; t <= T; ++t) nodes[t] = Tree(t, (t == T || O[t] == 0) ? 1 : 0);
    for (int t = 0; t < T; ++t) nodes[t].link(&nodes[par[t]]);
    
    vector<vector<int>> addss(N), remss(N);
    for (int t = 0; t < T; ++t) if (O[t] == 1) {
      int l = L[t], r = R[t];
      if (X[t]) {
        // ignore if X[t] does not exist in a tree
        chmax(l, L[birth[X[t]]]);
        chmin(r, R[birth[X[t]]]);
      }
      if (l < r) {
        addss[l].push_back(t);
        remss[r - 1].push_back(t);
      }
    }
    vector<vector<int>> qss(N);
    for (int q = 0; q < Q; ++q) {
      qss[I[q]].push_back(q);
    }
    
    vector<int> ans(Q, 0);
    for (int i = 0; i < N; ++i) {
      for (const int t : addss[i]) {
        nodes[t].cut();
        nodes[t].link(&nodes[birth[X[t]]]);
      }
      for (const int q : qss[i]) {
        const int tU = birth[U[q]];
        const int tV = birth[V[q]];
        nodes[tU].expose(); ans[q] += (nodes[tU].sum - 1);
        nodes[tV].expose(); ans[q] += (nodes[tV].sum - 1);
        Tree *l = nodes[tU].lca(&nodes[tV]);
        l->expose();
        ans[q] -= 2 * (l->sum - 1);
      }
      for (const int t : remss[i]) {
        nodes[t].cut();
        nodes[t].link(&nodes[par[t]]);
      }
    }
    
    for (int q = 0; q < Q; ++q) {
      printf("%d\n", ans[q]);
    }
  }
  return 0;
}

详细


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 1ms
memory: 4136kb

input:

100 100
1 10 71 1
0 3 92
2 76 2 1
2 80 1 2
1 27 40 2
1 2 50 1
0 52 66
2 7 1 2
2 75 2 1
2 23 1 1
2 81 2 1
0 50 97
1 7 21 4
2 26 1 2
0 45 47
1 10 95 3
2 20 2 1
0 8 50
0 14 61
1 1 57 4
2 39 7 2
1 1 42 7
0 70 90
0 68 100
1 24 54 5
0 49 80
2 44 6 6
0 12 22
1 11 64 11
0 16 90
1 52 55 12
2 82 4 12
2 10 2 6...

output:

1
1
1
1
0
1
1
1
2
0
2
2
2
2
0
1
2
2
3
0
2
1
2
2
2
2
1
0
3
0
3
2
0
2
3
2
1
2
1
1
1
2
2

result:

ok 43 numbers

Test #2:

score: 10
Accepted
time: 116ms
memory: 22924kb

input:

100000 200000
1 1 100000 1
2 9654 1 1
2 79828 1 1
2 47659 1 1
2 48703 1 1
1 1 100000 1
0 1 100000
0 1 100000
0 1 100000
2 44711 3 4
2 35462 2 1
1 1 100000 2
0 1 100000
2 29862 2 1
2 47087 3 1
0 1 100000
2 80323 4 3
1 1 100000 3
1 1 100000 5
1 1 100000 4
0 1 100000
2 11134 6 2
2 95357 2 1
0 1 100000
...

output:

0
0
0
0
2
1
1
1
2
1
1
4
2
2
3
3
4
4
4
3
9
5
6
5
7
8
4
4
8
7
9
8
3
1
10
7
5
11
5
6
4
6
5
5
7
10
0
5
11
11
9
2
15
3
8
1
12
7
9
15
2
8
12
1
14
2
17
12
3
14
15
7
5
17
6
13
13
12
5
11
11
11
3
14
2
5
20
8
4
3
11
1
6
14
2
2
1
14
3
16
3
10
9
23
4
12
17
5
0
2
6
16
16
6
21
13
3
24
19
16
1
22
1
20
13
5
1
32
25...

result:

ok 66761 numbers

Test #3:

score: 10
Accepted
time: 114ms
memory: 22852kb

input:

100000 200000
0 1 100000
1 1 100000 2
0 1 100000
0 1 100000
0 1 100000
2 16518 1 1
1 1 100000 5
2 19009 4 4
0 1 100000
2 3748 5 4
2 78412 5 3
2 39725 6 2
1 1 100000 5
1 1 100000 6
2 35808 3 1
1 1 100000 5
1 1 100000 6
2 67685 5 5
1 1 100000 5
1 1 100000 4
2 65922 1 4
2 48437 2 4
2 18398 4 4
2 41254 ...

output:

0
0
2
2
2
2
0
2
1
0
2
2
1
4
2
0
3
2
2
2
2
3
3
4
0
2
1
3
2
3
2
2
5
4
4
0
2
2
3
3
3
5
4
5
4
9
9
9
6
3
8
5
7
6
11
12
3
3
2
8
4
3
6
4
7
4
11
13
0
10
9
12
9
6
9
11
19
0
7
12
16
8
12
12
10
6
12
7
9
8
6
8
19
2
3
17
11
5
6
13
14
7
3
3
4
8
8
17
5
21
4
9
9
24
13
16
6
23
24
12
18
17
16
17
4
26
12
4
26
27
32
5
...

result:

ok 66721 numbers

Test #4:

score: 10
Accepted
time: 218ms
memory: 24600kb

input:

100000 200000
0 33106 78190
1 33106 78190 2
2 66397 1 2
2 22399 1 1
2 28777 1 1
2 97812 1 1
0 26922 92888
1 26922 92888 3
2 29224 1 1
0 5724 84985
1 5724 84985 4
2 6338 1 1
0 47653 72885
1 47653 72885 5
0 5576 91318
1 5576 91318 6
0 25864 89692
1 25864 89692 7
0 23749 99855
1 23749 99855 8
0 11672 9...

output:

1
0
0
0
0
0
2
3
1
0
1
2
3
1
6
10
3
2
6
6
2
5
3
11
0
4
10
10
0
2
22
4
3
2
34
7
6
31
3
31
4
13
2
27
7
3
8
25
5
9
10
32
13
10
13
20
7
1
1
1
44
26
0
23
5
13
23
4
1
21
4
27
52
15
1
2
2
24
45
9
2
23
35
13
17
36
30
20
25
16
75
18
42
53
70
40
16
2
7
23
5
57
17
18
4
25
30
4
6
2
86
73
15
103
19
23
73
99
4
53
...

result:

ok 66392 numbers

Test #5:

score: 10
Accepted
time: 220ms
memory: 24264kb

input:

100000 200000
2 72839 1 1
0 30550 41916
1 30550 41916 2
2 62121 1 1
2 80290 1 1
2 76257 1 1
0 943 93201
1 943 93201 3
0 16103 95866
1 16103 95866 4
0 865 97876
1 865 97876 5
0 9931 85356
1 9931 85356 6
2 77421 3 6
0 41810 93396
1 41810 93396 7
2 65175 1 5
0 1076 68529
1 1076 68529 8
0 14720 77975
1 ...

output:

0
0
0
0
3
3
0
1
4
1
0
1
9
1
7
5
3
3
7
13
1
14
16
0
5
11
16
1
3
10
0
9
14
18
9
28
18
10
15
1
19
14
7
28
13
27
6
18
59
0
4
11
3
22
17
45
4
8
1
6
7
6
2
34
21
1
1
43
1
3
9
27
0
12
28
25
13
8
11
9
6
41
22
66
7
1
1
53
1
1
51
30
20
9
33
68
7
26
29
32
34
53
66
2
31
33
33
33
39
6
8
57
64
80
8
25
14
0
43
35
9...

result:

ok 66554 numbers

Test #6:

score: 10
Accepted
time: 202ms
memory: 24492kb

input:

100000 200000
2 46707 1 1
0 14977 77707
0 20302 86058
0 49088 93421
2 95497 1 1
0 8283 96209
2 91976 1 5
0 3929 91482
0 11619 74339
2 82059 3 4
0 6657 70188
1 20586 77514 5
0 1877 99200
2 98704 9 9
2 31624 3 7
1 13906 96087 6
1 22878 94096 9
1 41412 97926 7
1 4250 82188 9
2 95289 5 5
0 26242 57965
2...

output:

0
0
1
2
0
2
0
1
2
2
0
1
0
3
2
4
4
0
2
2
6
2
2
0
3
3
2
0
1
2
7
0
3
1
2
3
1
3
5
2
0
9
4
0
2
2
2
1
0
1
2
0
4
0
4
4
7
0
1
10
9
8
5
3
4
4
5
1
2
6
2
5
13
0
0
13
1
7
5
9
3
8
12
6
11
3
7
10
6
12
7
2
4
3
2
1
15
4
3
12
1
8
1
5
5
3
3
10
1
5
3
5
8
13
2
4
9
9
14
2
3
2
4
10
15
9
3
19
15
5
3
16
10
2
1
27
2
10
6
13...

result:

ok 66011 numbers

Test #7:

score: 10
Accepted
time: 207ms
memory: 24516kb

input:

100000 200000
0 20137 62745
1 24168 88623 2
2 96666 1 1
2 736 1 1
0 28435 92570
1 13229 82956 3
1 5240 52328 3
0 30311 73812
1 32301 98591 4
1 1159 93532 1
2 68995 4 1
2 32120 3 4
0 14419 85232
2 3883 1 1
0 42047 98253
1 7811 82438 5
2 91112 1 1
0 44196 78232
1 6282 63461 5
1 27529 79979 5
0 58358 9...

output:

0
0
2
1
0
0
3
3
3
2
0
2
2
5
3
3
3
7
5
1
4
0
6
2
2
2
7
2
0
2
6
3
5
2
5
11
3
12
1
8
6
6
2
6
1
2
2
3
7
3
3
2
13
6
3
1
11
5
8
7
4
3
1
8
11
2
10
3
7
2
21
3
1
5
10
7
4
3
9
1
15
6
2
3
4
3
6
3
7
13
11
3
2
15
14
8
22
3
2
1
2
9
17
14
2
3
11
4
2
6
30
11
2
0
12
8
29
17
1
3
2
2
2
6
13
13
6
6
2
8
10
23
4
3
14
22
...

result:

ok 66622 numbers

Test #8:

score: 10
Accepted
time: 201ms
memory: 24164kb

input:

100000 200000
1 21762 71206 1
1 16400 90368 1
1 3122 98113 1
1 17150 91373 1
2 38606 1 1
1 8128 81031 1
0 23618 91527
1 24518 65007 1
1 17457 92923 1
2 16030 1 1
2 77768 2 2
2 9707 1 1
1 41391 60870 1
0 21463 85209
1 28931 93209 1
0 2527 96990
2 83948 3 1
2 60956 1 2
1 6424 85535 3
1 3351 97588 4
2 ...

output:

0
0
0
0
1
1
0
2
3
0
4
1
2
0
3
1
2
3
1
0
0
1
3
0
3
4
7
7
4
4
2
0
7
2
9
2
10
3
2
11
4
3
4
3
6
7
4
2
4
0
2
6
3
5
5
2
7
10
2
5
2
4
13
12
5
3
6
3
6
0
5
8
9
12
4
5
2
16
7
5
8
17
3
2
0
6
3
4
8
7
14
3
19
16
13
2
1
10
24
6
7
24
2
3
15
15
9
8
8
0
4
2
9
3
8
6
7
11
8
13
2
6
7
5
5
3
24
5
3
2
0
13
31
8
3
19
12
2
...

result:

ok 66813 numbers

Test #9:

score: 10
Accepted
time: 199ms
memory: 24420kb

input:

100000 200000
2 25021 1 1
0 5966 78753
2 7999 2 1
1 1203 48828 2
2 61501 1 2
0 8075 87603
0 5974 94178
0 11260 60286
1 14966 67820 4
0 21285 29742
1 30572 93129 6
1 4241 83943 5
2 50054 5 5
1 21494 43899 6
2 83822 4 3
0 10862 84471
2 45765 4 4
0 52129 54472
1 31171 74052 8
2 11296 2 5
1 7985 64831 6...

output:

0
1
1
0
2
0
1
0
2
1
0
4
4
4
0
2
0
3
4
1
2
2
1
1
4
5
5
4
2
5
3
3
2
2
5
2
4
5
7
7
2
4
1
2
2
4
6
6
6
11
7
0
2
2
6
1
2
2
2
12
10
5
1
11
5
2
2
9
4
2
5
0
1
5
1
7
4
3
20
10
3
4
5
8
2
7
5
1
5
7
0
10
6
3
6
3
19
18
16
2
9
16
24
8
19
4
25
24
3
9
12
11
2
2
3
5
2
7
4
8
3
31
3
3
9
2
4
6
23
3
6
5
5
9
15
11
10
5
10...

result:

ok 67032 numbers

Test #10:

score: 10
Accepted
time: 207ms
memory: 24692kb

input:

100000 200000
2 64847 1 1
0 14665 91814
1 1420 74156 2
0 44465 74460
1 965 46933 1
2 59368 2 2
2 78904 2 2
1 25821 90685 1
0 10504 89921
1 2691 80783 4
1 36610 58490 2
0 9268 92641
2 26758 5 2
2 65350 2 3
1 15205 95238 2
1 2683 94019 4
1 6398 78237 5
1 14229 75969 2
0 41501 99203
0 29141 98602
2 870...

output:

0
0
0
3
1
2
2
2
2
2
0
1
0
2
4
4
3
4
2
2
3
0
2
2
2
2
0
1
0
2
6
2
1
2
3
2
10
0
2
4
10
6
1
10
1
5
2
7
3
13
5
0
2
12
2
2
7
5
11
1
3
2
3
15
1
3
14
12
2
14
6
4
1
9
6
11
3
4
7
8
2
1
4
15
7
1
17
3
6
4
6
9
11
1
12
11
2
2
1
7
3
4
9
2
11
2
7
1
11
3
2
5
2
1
3
8
25
18
8
4
24
10
2
3
21
9
12
11
15
0
7
3
11
5
4
14
...

result:

ok 66412 numbers

Extra Test:

score: 0
Extra Test Passed