QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#335896#7415. Fast Spanning Treehos_lyricML 57ms211284kbC++147.4kb2024-02-24 08:55:232024-02-24 08:55:25

Judging History

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

  • [2024-02-24 08:55:25]
  • 评测
  • 测评结果:ML
  • 用时:57ms
  • 内存:211284kb
  • [2024-02-24 08:55:23]
  • 提交

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


// Meldable
// 0 for null, ts[0] = T()
// chPoint(u, a): point update
// chRange(u, a, b): range update s.t. T() -> T()
//   T::push(T *l, T *r)
//   T::pull(const T &l, const T &r)
//   T::meld(const T &t)
template <class T> struct Seg {
  static constexpr int NUM_NODES = 1 << 23;
  int l0, r0;
  int nodesLen;
  int ls[NUM_NODES], rs[NUM_NODES];
  T ts[NUM_NODES];
  void init(int l0_, int r0_) {
    l0 = l0_;
    r0 = r0_;
    nodesLen = 1;
    ls[0] = rs[0] = 0;
    ts[0] = T();
  }
  int newNode() {
    assert(nodesLen < NUM_NODES);
    const int u = nodesLen++;
    ls[u] = rs[u] = 0;
    ts[u] = T();
    return u;
  }
  void push(int u) {
    ts[u].push(ls[u] ? &ts[ls[u]] : nullptr, rs[u] ? &ts[rs[u]] : nullptr);
  }
  void pull(int u) {
    ts[u].pull(ts[ls[u]], ts[rs[u]]);
  }

  template <class F, class... Args>
  void chPoint(int &u, int l, int r, int a, F f, Args &&... args) {
    if (!u) u = newNode();
    if (l + 1 == r) {
      (ts[u].*f)(args...);
      return;
    }
    const int mid = l + ((r - l) >> 1);
    push(u);
    (a < mid)
      ? chPoint(ls[u], l, mid, a, f, args...)
      : chPoint(rs[u], mid, r, a, f, args...);
    pull(u);
  }
  template <class F, class... Args>
  void chPoint(int &u, int a, F f, Args &&... args) {
    assert(l0 <= a); assert(a < r0);
    chPoint(u, l0, r0, a, f, args...);
  }

  template <class F, class... Args>
  void chRange(int &u, int l, int r, int a, int b, F f, Args &&... args) {
    if (!u) return;
    if (b <= l || r <= a) return;
    if (a <= l && r <= b) {
      (ts[u].*f)(args...);
      return;
    }
    const int mid = l + ((r - l) >> 1);
    push(u);
    chRange(ls[u], l, mid, a, b, f, args...);
    chRange(rs[u], mid, r, a, b, f, args...);
    pull(u);
  }
  template <class F, class... Args>
  void chRange(int &u, int a, int b, F f, Args &&... args) {
    assert(l0 <= a); assert(a <= b); assert(b <= r0);
    chRange(u, l0, r0, a, b, f, args...);
  }

  T get(int u, int l, int r, int a, int b) {
    if (!u) return T();
    if (b <= l || r <= a) return T();
    if (a <= l && r <= b) return ts[u];
    const int mid = l + ((r - l) >> 1);
    push(u);
    const T tL = get(ls[u], l, mid, a, b);
    const T tR = get(rs[u], mid, r, a, b);
    pull(u);
    T t;
    t.pull(tL, tR);
    return t;
  }
  T get(int u, int a, int b) {
    assert(l0 <= a); assert(a <= b); assert(b <= r0);
    return get(u, l0, r0, a, b);
  }

  // Frees v.
  int meld(int u, int v, int l, int r) {
    if (!u) return v;
    if (!v) return u;
    if (l + 1 == r) {
      ts[u].meld(ts[v]);
      return u;
    }
    const int mid = l + ((r - l) >> 1);
    push(u);
    push(v);
    ls[u] = meld(ls[u], ls[v], l, mid);
    rs[u] = meld(rs[u], rs[v], mid, r);
    pull(u);
    return u;
  }
  int meld(int u, int v) {
    return meld(u, v, l0, r0);
  }

  /*
  void print(int depth, int u, int l, int r) const {
    if (!u) return;
    cerr << string(2 * depth, ' ') << u << " [" << l << ", " << r << ") " << ts[u] << endl;
    if (l + 1 == r) return;
    const int mid = l + ((r - l) >> 1);
    print(depth + 1, ls[u], l, mid);
    print(depth + 1, rs[u], mid, r);
  }
  void print(int u) const {
    if (!u) cerr << "[Seg::print] null" << endl;
    print(0, u, l0, r0);
  }
  //*/
};


constexpr Int INF = 1001001001001001001LL;

struct Node {
  pair<Int, int> mn;
  Int lz;
  Node() : mn(INF, -1), lz(0) {}
  void push(Node *l, Node *r) {
    if (lz) {
      if (l) l->add(lz);
      if (r) r->add(lz);
      lz = 0;
    }
  }
  void pull(const Node &l, const Node &r) {
    mn = min(l.mn, r.mn);
  }
  void meld(const Node &t) {
    chmin(mn, t.mn);
  }
  void add(Int val) {
    mn.first += val;
    lz += val;
  }
  // leaf
  void change(Int val, int id) {
    mn = make_pair(val, id);
  }
};


vector<int> uf;
int root(int u) {
  return (uf[u] < 0) ? u : (uf[u] = root(uf[u]));
}
bool connect(int u, int v) {
  u = root(u);
  v = root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}


Seg<Node> seg;

int N, M;
vector<Int> W;
vector<int> A, B;
vector<Int> S;

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    W.resize(N);
    for (int u = 0; u < N; ++u) {
      scanf("%lld", &W[u]);
    }
    A.resize(M);
    B.resize(M);
    S.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d%lld", &A[i], &B[i], &S[i]);
      --A[i];
      --B[i];
    }
    
    uf.assign(N, -1);
    vector<Int> ws = W;
    
    seg.init(0, M << 1);
    vector<int> fs(N, 0);
    for (int u = 0; u < N; ++u) {
      fs[u] = seg.newNode();
    }
    
    priority_queue<int, vector<int>, greater<int>> que;
    auto update = [&](int i) -> void {
      const int ra = root(A[i]);
      const int rb = root(B[i]);
      const Int hp = S[i] - ws[ra] - ws[rb];
// cerr<<"[update] i = "<<i<<", hp = "<<hp<<endl;
      if (hp <= 0) {
        que.push(i);
        seg.chPoint(fs[ra], i << 1 | 0, &Node::change, INF, -1);
        seg.chPoint(fs[rb], i << 1 | 1, &Node::change, INF, -1);
      } else {
        seg.chPoint(fs[ra], i << 1 | 0, &Node::change, (hp - 1) / 2, i);
        seg.chPoint(fs[rb], i << 1 | 1, &Node::change, (hp    ) / 2, i);
      }
    };
    for (int i = 0; i < M; ++i) {
      update(i);
    }
    vector<int> ans;
    for (; que.size(); ) {
      const int i = que.top();
      que.pop();
      const int ra = root(A[i]);
      const int rb = root(B[i]);
// cerr<<"merge? "<<i<<" "<<ra<<" "<<rb<<endl;
      if (ra != rb) {
        ans.push_back(i);
        seg.chRange(fs[ra], 0, M << 1, &Node::add, -ws[rb]);
        seg.chRange(fs[rb], 0, M << 1, &Node::add, -ws[ra]);
        connect(ra, rb);
        const int r = root(ra);
        ws[r] = ws[ra] + ws[rb];
        fs[r] = seg.meld(fs[ra], fs[rb]);
        for (; seg.ts[fs[r]].mn.first < 0; ) {
// cerr<<"  mn = "<<seg.ts[fs[r]].mn<<endl;
          update(seg.ts[fs[r]].mn.second);
        }
      }
    }
    
    printf("%d\n", (int)ans.size());
    for (int j = 0; j < (int)ans.size(); ++j) {
      if (j) printf(" ");
      printf("%d", ans[j] + 1);
    }
    puts("");
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 16ms
memory: 201092kb

input:

5 5
1 4 3 4 0
4 5 5
3 1 1
2 5 2
4 3 1
4 1 4

output:

4
2 3 1 4

result:

ok 5 number(s): "4 2 3 1 4"

Test #2:

score: 0
Accepted
time: 19ms
memory: 203860kb

input:

3 5
3 2 2
1 2 6
1 2 6
1 2 3
1 2 6
2 3 6

output:

2
3 5

result:

ok 3 number(s): "2 3 5"

Test #3:

score: 0
Accepted
time: 27ms
memory: 200956kb

input:

10 20
4 6 6 1 7 9 1 8 7 9
5 3 2
1 10 10
5 6 7
9 6 9
3 1 1
6 8 1
5 7 0
9 5 4
10 3 4
8 6 8
3 10 6
5 3 8
3 7 9
1 9 10
10 1 0
5 7 6
6 10 1
6 5 9
5 8 2
9 2 4

output:

8
1 2 3 4 5 6 7 20

result:

ok 9 numbers

Test #4:

score: 0
Accepted
time: 23ms
memory: 201128kb

input:

10 20
0 10 4 6 2 0 2 0 2 8
5 10 8
7 1 6
10 5 0
9 8 0
5 8 4
5 1 8
10 3 6
5 4 3
9 2 6
10 3 6
4 3 1
3 10 3
6 1 3
3 2 5
6 9 2
4 2 5
10 6 5
8 6 3
2 1 0
2 3 6

output:

9
1 4 5 6 2 7 8 9 13

result:

ok 10 numbers

Test #5:

score: 0
Accepted
time: 36ms
memory: 201960kb

input:

100 200
49 21 70 93 66 51 36 59 56 62 10 4 46 73 22 48 89 17 72 60 29 64 19 56 32 54 55 0 77 86 34 35 56 17 55 2 98 80 73 71 64 37 61 22 89 96 99 28 0 35 56 45 74 81 30 3 66 96 28 16 29 43 60 61 60 95 83 5 73 1 28 47 73 44 8 4 91 34 100 23 4 93 44 87 72 5 13 88 100 52 56 61 100 80 14 30 59 97 51 43
...

output:

96
1 2 4 5 6 7 8 10 11 12 13 14 15 16 17 18 19 9 20 21 22 23 25 26 3 27 28 29 30 31 32 33 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 52 53 34 54 55 56 58 59 60 61 62 63 64 66 67 68 69 70 71 72 74 76 78 80 81 82 87 89 91 94 95 96 100 43 101 24 84 107 109 110 117 119 129 142 144 169 170 195

result:

ok 97 numbers

Test #6:

score: 0
Accepted
time: 27ms
memory: 202568kb

input:

100 200
24 84 94 70 81 51 70 9 39 61 34 89 37 23 21 52 19 65 37 53 85 70 27 74 46 45 49 76 36 51 38 8 91 84 30 21 82 93 54 4 65 44 66 78 42 42 74 11 12 66 24 88 65 75 78 55 61 45 21 83 29 24 85 37 73 25 25 40 96 100 26 28 18 30 56 41 15 11 88 61 42 74 8 97 14 34 5 39 24 94 84 8 3 54 75 62 73 36 58 4...

output:

98
1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 11 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 63 64 65 66 67 68 70 71 72 73 74 75 77 78 80 81 82 84 85 87 88 89 90 91 92 94 96 98 99 103 104 129 156 163 164 167 185 199

result:

ok 99 numbers

Test #7:

score: 0
Accepted
time: 33ms
memory: 202308kb

input:

1000 2000
919 776 189 249 535 136 885 695 607 561 235 179 233 501 828 472 532 337 721 344 71 885 630 612 867 132 392 624 477 527 529 17 960 726 235 661 407 923 214 882 554 194 68 561 993 393 548 92 269 41 754 647 1 924 510 573 464 911 845 303 624 583 255 861 668 998 648 901 251 766 349 248 504 117 6...

output:

979
2 3 4 5 6 7 8 10 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 36 38 39 42 43 44 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 71 72 73 74 75 76 77 78 79 80 81 82 45 83 84 85 86 87 88 89 90 91 92 93 94 95 97 98 99 100 101 102 103 105 41 106 107 108 1...

result:

ok 980 numbers

Test #8:

score: 0
Accepted
time: 23ms
memory: 202880kb

input:

1000 2000
838 14 274 368 930 991 554 645 860 709 557 447 647 723 384 339 801 423 843 957 649 71 997 106 521 713 275 876 396 541 377 586 934 998 330 221 529 635 515 216 643 742 507 270 902 288 440 622 950 258 295 505 358 540 452 414 888 601 815 386 759 689 742 840 994 786 974 648 379 102 450 754 624 ...

output:

970
1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 2 23 24 25 26 28 29 30 31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 53 27 54 56 52 57 58 59 60 61 62 63 64 65 67 68 69 70 71 72 73 76 77 78 79 80 81 82 83 84 85 86 87 89 90 91 93 95 97 98 99 100 101 102 104 105 106 108 75 109 ...

result:

ok 971 numbers

Test #9:

score: 0
Accepted
time: 57ms
memory: 211284kb

input:

10000 20000
2770 4102 7211 7463 5376 956 7654 6142 7795 4467 5954 3814 401 4217 3088 9 3334 5642 8164 4848 1690 4609 9606 1472 2055 3238 4429 1866 2551 1891 3108 231 8719 5315 3920 9718 2547 9932 2680 15 7872 7302 4641 1096 7732 5134 1348 3489 4436 7903 168 9090 2765 2650 8730 7433 6196 9299 8451 43...

output:

9826
1 3 4 5 6 7 9 10 11 12 15 16 20 21 22 23 24 25 26 29 30 32 33 34 35 36 39 40 41 42 43 45 46 47 48 49 50 51 52 53 55 57 58 59 60 61 62 63 65 66 67 68 69 71 72 73 74 75 76 27 77 79 81 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 106 107 108 109 110 111 113 114 116 117 11...

result:

ok 9827 numbers

Test #10:

score: 0
Accepted
time: 55ms
memory: 206672kb

input:

10000 20000
6023 137 374 4404 7135 4862 3664 1753 2615 5821 3591 1821 1772 8756 6173 3166 8856 8030 994 8260 4267 4477 691 8970 5445 9593 452 7959 3303 6588 3700 4861 1268 9344 2475 2435 7968 1011 9437 5883 4815 2770 9056 4423 2924 1525 4211 4655 9676 8745 429 9269 8489 4940 6051 8532 6732 6831 7866...

output:

9797
1 2 5 6 8 10 11 12 13 14 15 16 17 19 20 21 22 23 24 26 27 29 30 31 32 33 34 35 36 37 38 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 59 60 61 62 63 64 66 67 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 93 94 96 97 99 100 101 103 104 105 106 107 108 109 110 111 112 113...

result:

ok 9798 numbers

Test #11:

score: -100
Memory Limit Exceeded

input:

300000 300000
79589 56209 39109 45927 40583 85355 37235 93800 80252 94753 77793 8508 27123 53965 42678 78268 75590 62814 33653 46238 58818 17465 5587 99163 91597 76787 82053 11436 79434 6262 59411 20917 1182 10838 63928 74239 21886 15024 29599 86110 97887 2230 71925 42987 37897 58688 65784 50117 486...

output:


result: