QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#344691#2865. 天才黑客hos_lyric100 ✓413ms49132kbC++1411.1kb2024-03-04 21:42:562024-03-04 21:42:56

Judging History

This is the latest submission verdict.

  • [2024-03-04 21:42:56]
  • Judged
  • Verdict: 100
  • Time: 413ms
  • Memory: 49132kb
  • [2024-03-04 21:42:56]
  • Submitted

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

struct Hld {
  int n, rt;
  // needs to be tree
  // vertex lists
  // modified in build(rt) (parent removed, heavy child first)
  vector<vector<int>> graph;
  vector<int> sz, par, dep;
  int zeit;
  vector<int> dis, fin, sid;
  // head vertex (minimum depth) in heavy path
  vector<int> head;

  Hld() : n(0), rt(-1), zeit(0) {}
  explicit Hld(int n_) : n(n_), rt(-1), graph(n), zeit(0) {}
  void ae(int u, int v) {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    graph[u].push_back(v);
    graph[v].push_back(u);
  }

  void dfsSz(int u) {
    sz[u] = 1;
    for (const int v : graph[u]) {
      auto it = std::find(graph[v].begin(), graph[v].end(), u);
      if (it != graph[v].end()) graph[v].erase(it);
      par[v] = u;
      dep[v] = dep[u] + 1;
      dfsSz(v);
      sz[u] += sz[v];
    }
  }
  void dfsHld(int u) {
    dis[u] = zeit++;
    const int deg = graph[u].size();
    if (deg > 0) {
      int vm = graph[u][0];
      int jm = 0;
      for (int j = 1; j < deg; ++j) {
        const int v = graph[u][j];
        if (sz[vm] < sz[v]) {
          vm = v;
          jm = j;
        }
      }
      swap(graph[u][0], graph[u][jm]);
      head[vm] = head[u];
      dfsHld(vm);
      for (int j = 1; j < deg; ++j) {
        const int v = graph[u][j];
        head[v] = v;
        dfsHld(v);
      }
    }
    fin[u] = zeit;
  }
  void build(int rt_) {
    assert(0 <= rt_); assert(rt_ < n);
    rt = rt_;
    sz.assign(n, 0);
    par.assign(n, -1);
    dep.assign(n, -1);
    dep[rt] = 0;
    dfsSz(rt);
    zeit = 0;
    dis.assign(n, -1);
    fin.assign(n, -1);
    head.assign(n, -1);
    head[rt] = rt;
    dfsHld(rt);
    assert(zeit == n);
    sid.assign(n, -1);
    for (int u = 0; u < n; ++u) sid[dis[u]] = u;
  }

  friend ostream &operator<<(ostream &os, const Hld &hld) {
    const int maxDep = *max_element(hld.dep.begin(), hld.dep.end());
    vector<string> ss(2 * maxDep + 1);
    int pos = 0, maxPos = 0;
    for (int j = 0; j < hld.n; ++j) {
      const int u = hld.sid[j];
      const int d = hld.dep[u];
      if (hld.head[u] == u) {
        if (j != 0) {
          pos = maxPos + 1;
          ss[2 * d - 1].resize(pos, '-');
          ss[2 * d - 1] += '+';
        }
      } else {
        ss[2 * d - 1].resize(pos, ' ');
        ss[2 * d - 1] += '|';
      }
      ss[2 * d].resize(pos, ' ');
      ss[2 * d] += std::to_string(u);
      if (maxPos < static_cast<int>(ss[2 * d].size())) {
        maxPos = ss[2 * d].size();
      }
    }
    for (int d = 0; d <= 2 * maxDep; ++d) os << ss[d] << '\n';
    return os;
  }

  bool contains(int u, int v) const {
    return (dis[u] <= dis[v] && dis[v] < fin[u]);
  }
  int lca(int u, int v) const {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    for (; head[u] != head[v]; ) (dis[u] > dis[v]) ? (u = par[head[u]]) : (v = par[head[v]]);
    return (dis[u] > dis[v]) ? v : u;
  }
  int jumpUp(int u, int d) const {
    assert(0 <= u); assert(u < n);
    assert(d >= 0);
    if (dep[u] < d) return -1;
    const int tar = dep[u] - d;
    for (u = head[u]; ; u = head[par[u]]) {
      if (dep[u] <= tar) return sid[dis[u] + (tar - dep[u])];
    }
  }
  int jump(int u, int v, int d) const {
    assert(0 <= u); assert(u < n);
    assert(0 <= v); assert(v < n);
    assert(d >= 0);
    const int l = lca(u, v);
    const int du = dep[u] - dep[l], dv = dep[v] - dep[l];
    if (d <= du) {
      return jumpUp(u, d);
    } else if (d <= du + dv) {
      return jumpUp(v, du + dv - d);
    } else {
      return -1;
    }
  }
  // [u, v) or [u, v]
  template <class F> void doPathUp(int u, int v, bool inclusive, F f) const {
    assert(contains(v, u));
    for (; head[u] != head[v]; u = par[head[u]]) f(dis[head[u]], dis[u] + 1);
    if (inclusive) {
      f(dis[v], dis[u] + 1);
    } else {
      if (v != u) f(dis[v] + 1, dis[u] + 1);
    }
  }
  // not path order, include lca(u, v) or not
  template <class F> void doPath(int u, int v, bool inclusive, F f) const {
    const int l = lca(u, v);
    doPathUp(u, l, false, f);
    doPathUp(v, l, inclusive, f);
  }

  // (vs, ps): compressed tree
  // vs: DFS order (sorted by dis)
  // vs[ps[x]]: the parent of vs[x]
  // ids[vs[x]] = x, not set for non-tree vertex
  vector<int> ids;
  pair<vector<int>, vector<int>> compress(vector<int> us) {
    // O(n) first time
    ids.resize(n, -1);
    std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (dis[u] < dis[v]);
    });
    us.erase(std::unique(us.begin(), us.end()), us.end());
    int usLen = us.size();
    assert(usLen >= 1);
    for (int x = 1; x < usLen; ++x) us.push_back(lca(us[x - 1], us[x]));
    std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
      return (dis[u] < dis[v]);
    });
    us.erase(std::unique(us.begin(), us.end()), us.end());
    usLen = us.size();
    for (int x = 0; x < usLen; ++x) ids[us[x]] = x;
    vector<int> ps(usLen, -1);
    for (int x = 1; x < usLen; ++x) ps[x] = ids[lca(us[x - 1], us[x])];
    return make_pair(us, ps);
  }
};

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


/*
  problem
    given directed graph
      edge a b c d:
        a: fr
        b: to
        c: cost
        d: node in trie
    given trie
      edge u v w
        u: fr
        v: to
        w: character
    find shortest path to each vertex
    where cost of path (i[0], i[1], ...): edge seq. is:
    \sum[j] c[j] + \sum[j] |LCP(d[j-1], d[j])|
  
  |LCP|: range min in Euler tour
  edge -> ? -> edge
  edge <- ? <- edge
*/

constexpr Int INF = 1001001001001001001LL;

int N, M, K;
vector<int> A, B, D;
vector<Int> C;
vector<int> U, V;

Hld hld;

struct Vertex {
  vector<int> fr, to;
  void build() {
    vector<int> us;
    for (const int i : fr) us.push_back(D[i]);
    for (const int i : to) us.push_back(D[i]);
    vsps = hld.compress(us);
    const int n = vsps.first.size();
    graph.assign(n, {});
    for (int x = 1; x < n; ++x) {
      const int p = vsps.second[x];
      graph[p].push_back(x);
    }
    seq.clear();
    poss.assign(n, -1);
    dfs(0);
    possFr.clear();
    possTo.clear();
    for (const int i : fr) possFr.push_back(poss[hld.ids[D[i]]]);
    for (const int i : to) possTo.push_back(poss[hld.ids[D[i]]]);
// cerr<<"vsps = "<<vsps<<", seq = "<<seq<<", poss = "<<poss<<endl;
// cerr<<"fr = "<<fr<<": possFr = "<<possFr<<endl;
// cerr<<"to = "<<to<<": possTo = "<<possTo<<endl;
    len = seq.size();
    l = ll = 0;
    r = rr = len - 1;
    iss.assign(len, {});
    for (int k = 0; k < (int)to.size(); ++k) iss[possTo[k]].push_back(to[k]);
  }
  
  pair<vector<int>, vector<int>> vsps;
  vector<vector<int>> graph;
  vector<int> seq;
  vector<int> poss;
  void dfs(int x) {
    const int d = hld.dep[vsps.first[x]];
    poss[x] = seq.size();
    seq.push_back(d);
    for (const int y : graph[x]) {
      dfs(y);
      seq.push_back(d);
    }
  }
  
  vector<int> possFr, possTo;
  int len;
  int l, ll, r, rr;
  vector<vector<int>> iss;
};

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d%d%d", &N, &M, &K);
    A.resize(M);
    B.resize(M);
    C.resize(M);
    D.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d%lld%d", &A[i], &B[i], &C[i], &D[i]);
      --A[i];
      --B[i];
      --D[i];
    }
    U.resize(K - 1);
    V.resize(K - 1);
    for (int i = 0; i < K - 1; ++i) {
      scanf("%d%d%*d", &U[i], &V[i]);
      --U[i];
      --V[i];
    }
    
    hld = Hld(K);
    for (int i = 0; i < K - 1; ++i) {
      hld.ae(U[i], V[i]);
    }
    hld.build(0);
    
    vector<Vertex> ver(N);
    for (int i = 0; i < M; ++i) {
      ver[A[i]].to.push_back(i);
      ver[B[i]].fr.push_back(i);
    }
    for (int u = 0; u < N; ++u) {
      ver[u].build();
    }
    
    vector<int> pt(N + 1);
    pt[0] = M;
    for (int u = 0; u < N; ++u) {
      pt[u + 1] = pt[u] + ver[u].len * 2;
    }
    using Entry = pair<Int, int>;
    priority_queue<Entry, vector<Entry>, greater<Entry>> que;
    vector<Int> dist(pt[N], INF);
    for (const int i : ver[0].to) {
      que.emplace(dist[i] = C[i], i);
    }
    for (; que.size(); ) {
      const Int c = que.top().first;
      const int i = que.top().second;
      que.pop();
      if (dist[i] == c) {
        auto reach = [&](Int cc, int ii) -> void {
          if (chmin(dist[ii], cc)) {
            que.emplace(cc, ii);
          }
        };
        if (i < M) {
          const int u = B[i];
          const int id = lower_bound(ver[u].fr.begin(), ver[u].fr.end(), i) - ver[u].fr.begin();
          // ->
          for (int &k = ver[u].r; k >= ver[u].possFr[id]; --k) {
            reach(c + ver[u].seq[k], pt[u] + k * 2 + 1);
          }
          // <-
          for (int &k = ver[u].l; k <= ver[u].possFr[id]; ++k) {
            reach(c + ver[u].seq[k], pt[u] + k * 2 + 0);
          }
        } else {
          const int u = upper_bound(pt.begin(), pt.end(), i) - pt.begin() - 1;
          const int k = (i - pt[u]) / 2;
          if ((i - pt[u]) % 2) {
            // ->
            for (int &kk = ver[u].rr; kk >= k; --kk) {
              for (const int ii : ver[u].iss[kk]) {
                reach(c + C[ii], ii);
              }
            }
          } else {
            // <-
            for (int &kk = ver[u].ll; kk <= k; ++kk) {
              for (const int ii : ver[u].iss[kk]) {
                reach(c + C[ii], ii);
              }
            }
          }
        }
      }
    }
    
    vector<Int> ans(N, INF);
    for (int i = 0; i < M; ++i) {
      chmin(ans[B[i]], dist[i]);
    }
    for (int u = 1; u < N; ++u) {
      printf("%lld\n", ans[u]);
    }
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

詳細信息

Test #1:

score: 5
Accepted
time: 166ms
memory: 12892kb

input:

10
6 4990 19922
4 3 6 16653
1 5 1 14907
5 3 3 12543
1 6 6 18133
6 4 0 10382
2 4 2 13389
1 6 5 11764
5 6 1 7278
1 6 3 16419
6 5 1 6237
1 4 8 9478
2 2 5 6012
6 4 7 4620
3 4 4 13781
1 3 0 4496
6 4 2 10214
6 3 2 6714
1 2 5 16272
4 3 3 16818
5 3 3 6710
6 5 9 17344
5 4 2 11112
2 3 2 2614
6 5 7 16745
4 3 9...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
2
0
0
0
1
1
0
0
0
0
0
21
5
2
0
3
2
0
0
0
0
2
1
1
2
0
2
0
1
2
0
2
2
2
0
2
5
0
2
0
3
0
0
1
0
2
0
7
3
0
6
0
4
0
2
2
7
4
16
6
8
20
12
7
1
10
7
5
3
1
1
6
7
3
6
3
0
0
9
16
7
10
3
8
9
5
7
0
8
5
7
8
1
23
10
2
1
12
5
1
8
14
4...

result:

ok 4479 lines

Test #2:

score: 5
Accepted
time: 163ms
memory: 12980kb

input:

10
6 4985 19945
3 2 2 1824
6 5 7 2420
3 4 8 16955
6 5 2 5283
3 4 0 530
4 4 5 432
1 6 1 19342
6 6 8 18901
5 4 9 19350
6 5 1 387
6 5 9 1712
6 4 4 19861
2 1 5 19663
2 2 6 19868
3 6 4 2781
4 2 1 1226
1 2 4 14349
2 5 9 10064
1 3 3 14473
3 2 3 4945
2 6 7 13433
5 4 5 12670
2 4 1 7392
4 4 9 10449
6 1 2 7280...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
6
3
1
0
0
0
0
1
7
1
4
1
7
3
0
0
2
0
0
3
2
1
1
4
0
2
0
0
4
3
0
5
15
4
15
1
1
1
2
3
0
2
2
0
0
2
3
99
5
22
6
50
11
9
12
7
16
19
3
3
25
50
1
0
0
7
4
2
5
3
1
1
2
0
11
12
7
4
5
1
21
2
3
23
3
3
2
5
1
2...

result:

ok 4980 lines

Test #3:

score: 5
Accepted
time: 167ms
memory: 12904kb

input:

10
4 4986 19949
3 3 4 10629
2 3 1 3347
4 1 1 7532
4 4 1 2251
3 3 9 14639
2 3 4 682
2 2 1 16614
1 4 4 9015
4 4 9 9520
4 3 5 14918
3 2 5 14349
1 4 9 13119
1 2 0 2659
2 2 9 19574
1 3 3 827
2 4 8 9747
2 4 9 16560
3 4 0 12866
1 4 8 826
4 2 8 8636
2 2 0 11934
4 2 5 2402
2 4 4 9539
4 1 4 17455
4 2 4 17281
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
1
0
1
0
0
2
2
0
0
2
0
0
0
0
2
0
0
1
1
0
3
39
4
3
1
1
2
1
0
5
0
3
2
5
2
0
1
7
0
3
1
0
0
0
3
0
1
4
2
2
6
0
3
0
0
0
1
0
1
0
0
2
2
0
3
2
0
2
4
17
27
2
2
6
3
2
23
24
12
26
0
8
8
6
19
23
0
16
20
17
3
3
23
7
5
19
8
2
4
0
5
16
21
9
1
20
16
4
7
1
5
...

result:

ok 5480 lines

Test #4:

score: 5
Accepted
time: 162ms
memory: 13544kb

input:

10
5 4983 19992
1 3 7 4541
4 2 1 3807
2 2 5 14524
2 5 7 17852
5 5 2 1373
1 5 9 5415
2 4 5 1640
5 4 3 14802
4 4 4 13595
1 3 2 6844
5 3 8 11221
1 5 0 1494
4 5 0 12221
1 2 3 9245
5 1 9 19364
1 5 6 15902
1 5 9 15907
5 3 7 1577
4 4 6 652
2 2 2 5783
4 3 9 6039
1 2 0 12257
2 2 2 4031
1 4 1 9055
2 4 3 15081...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
0
1
4
3
4
2
1
7
0
0
0
0
0
3
3
0
1
0
0
0
6
0
1
0
4
0
7
0
0
1
5
0
3
0
2
5
2
0
1
0
0
0
3
1
2
1
3
119
330
4
2
1
3
87
9
86
83
40
9
0
0
35
8
0
17
1
1
0
1
3
0
8
2
4
40
1
33
2
3
7
6
2
0
17
45
86
0
9
20
1
17
20
17
3
4...

result:

ok 5978 lines

Test #5:

score: 5
Accepted
time: 168ms
memory: 14096kb

input:

10
4 4988 19958
3 3 6 11088
1 3 2 13262
4 1 1 13329
1 2 6 7013
2 2 4 19047
1 4 1 9111
1 1 0 13164
2 4 3 10469
1 1 5 9157
1 4 9 18420
4 3 3 6912
1 1 3 14729
4 2 5 17794
1 3 6 2306
4 4 7 9709
1 1 6 9322
1 1 2 16917
3 1 5 7401
1 1 2 12811
1 3 9 856
2 1 2 968
1 4 1 1764
1 4 1 13252
1 3 2 14558
3 4 6 989...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
0
2
0
2
1
1
0
0
3
0
1
1
7
1
0
3
1
0
2
1
3
0
3
0
0
2
0
1
0
14
1
2
0
0
0
0
1
0
2
1
2
4
1
5
4
4
0
4
35
1
1
3
7
0
6
8
28
1
4
2
6
1
5
20
7
2
4
1
0
2
5
8
9
19
12
4
3
0
11
9
2
11
7
8
1
5
11
1
5
11
2
6
2
5
...

result:

ok 6478 lines

Test #6:

score: 5
Accepted
time: 328ms
memory: 31428kb

input:

10
4 4998 19943
2 3 1 7426
1 4 9 9989
4 2 8 11254
1 2 4 10425
4 4 5 18391
1 3 7 1508
2 3 3 14690
3 2 4 15883
3 2 8 4016
2 3 2 11659
2 1 0 12607
1 4 1 10741
1 4 7 5462
3 2 1 1653
4 2 6 5168
2 3 8 14411
2 1 7 1816
1 4 4 4290
1 1 6 15831
4 2 7 8537
4 2 6 15402
3 4 2 16587
3 1 2 9686
2 3 3 9766
4 2 0 89...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
0
3
6
0
4
0
2
1
0
2
0
1
2
0
1
2
0
1
0
0
1
0
1
0
1
2
0
1
0
2
0
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
4
2
8
8
7
12
1
5
5
6
3
4
7
5
3
0
1
5
4
5
6
5
6
2
8
9
0
8
7
5
1
3
4
4
2
1
8
3
3
4
3
4
0
3
7
4
3
1
2
6
0...

result:

ok 5385 lines

Test #7:

score: 5
Accepted
time: 317ms
memory: 31040kb

input:

10
6 4984 19910
5 5 2 2264
4 4 1 541
2 5 0 19175
6 5 2 18125
1 6 2 16994
1 4 7 6497
5 4 2 10033
2 6 3 17202
4 5 9 7768
6 4 5 19834
4 3 3 16806
3 3 4 14749
2 2 7 831
2 4 2 4307
1 6 9 5587
3 5 0 9514
1 4 0 15445
1 2 1 2489
5 3 1 9696
5 3 2 4174
1 3 9 17514
2 1 4 17554
2 5 0 9930
1 6 3 6804
1 4 1 16090...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
3
1
0
1
1
0
1
1
0
0
0
1
1
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
1
0
10
13
7
3
12
9
12
6
2
8
6
8
7
7
2
1
1
3
6
2
7
4
6
1
7
0
2
5
6
12
3
4
0
5
8
8
9
2
6
6
5
4
5
7
5
7
8
5
4
1
7...

result:

ok 10385 lines

Test #8:

score: 5
Accepted
time: 318ms
memory: 31644kb

input:

10
6 4993 19952
1 3 2 6914
4 5 4 16857
3 6 2 18667
5 2 1 16058
1 2 7 2409
6 5 0 7937
4 3 6 7888
1 4 7 2644
2 2 1 19420
2 6 8 4166
1 4 2 17703
6 5 8 10459
2 2 1 8365
1 3 3 5072
3 1 4 5816
2 1 9 2924
6 6 5 4906
1 4 6 16825
1 6 0 13600
3 6 7 11572
6 6 8 12361
3 2 8 3299
2 5 1 16877
6 6 3 5290
3 4 1 816...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
2
0
0
0
1
0
0
0
1
0
0
1
0
0
0
0
1
0
0
0
0
0
1
1
2
1
0
3
1
0
1
0
0
0
1
1
0
1
0
1
0
0
0
0
1
1
3
0
3
0
1
0
1
0
0
1
0
0
0
0
2
0
2
1
0
1
0
1
3
1
4
3
1
7
9
6
1
9
4
13
11
13
8
7
10
3
11
11
3
10
1
8
4
2
7
8
10
7
0
6
11
8
7
10
1
6
1
14
9
9
2
7
1
0
8
9...

result:

ok 15383 lines

Test #9:

score: 5
Accepted
time: 323ms
memory: 31604kb

input:

10
6 4984 19985
1 5 9 19725
5 3 9 3224
6 4 7 17229
3 6 3 15580
4 3 3 9809
4 2 5 10336
3 4 2 2893
6 6 7 15935
1 4 8 3530
2 3 8 18657
3 5 7 2867
1 1 5 3773
1 3 5 12801
6 2 5 9859
3 6 8 6206
2 6 3 10115
5 3 2 10479
2 4 1 13091
2 1 9 4511
4 3 4 5438
3 5 7 18741
1 1 6 11781
6 4 2 9837
3 5 9 809
1 2 0 697...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
4
5
2
1
0
4
0
5
0
5
0
1
2
0
2
1
0
0
4
2
2
1
1
1
0
1
0
3
2
4
3
0
1
3
3
0
0
1
4
2
1
1
1
1
0
2
113
6
2
5
5
3
5
0
15
20
20
1
2
13
8
8
5
8
1
10
12
4
9
5
10
2
2
8
0
11
5
5
9
1
3
8
5
3
10
8
0
6
0
2
3
7
1...

result:

ok 20386 lines

Test #10:

score: 5
Accepted
time: 313ms
memory: 31780kb

input:

10
5 4981 19908
5 2 5 7847
2 4 0 17200
3 3 0 7650
5 2 2 3879
2 3 2 2556
1 2 0 5079
5 3 8 5551
4 2 0 6754
4 5 4 13987
2 3 5 19759
4 3 9 3494
2 3 1 2343
2 3 2 5505
3 2 7 6477
1 2 4 11240
2 3 7 12541
1 3 4 15664
4 3 1 8026
1 3 0 7141
5 4 6 13607
1 4 4 18417
5 4 1 13175
1 5 4 15409
2 3 6 14894
5 4 9 171...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
1
0
0
3
6
0
4
1
0
0
0
3
0
2
4
1
0
3
2
1
0
0
2
3
2
0
3
3
2
2
0
0
0
0
1
0
2
0
0
3
2
0
3
1
0
5
3
4
0
7
4
4
19
7
5
4
2
6
5
0
5
2
7
6
0
7
5
0
1
0
3
0
2
4
4
4
5
3
6
10
5
0
11
5
5
7
0
7
8
5
7
4
6
7
4
11
6
4
2
3
...

result:

ok 25384 lines

Test #11:

score: 5
Accepted
time: 308ms
memory: 34212kb

input:

10
6 4995 19937
5 5 4 10024
4 3 2 18036
4 5 1 13191
5 4 3 258
1 1 6 1951
6 4 0 7574
2 5 1 8966
2 4 8 17232
3 5 3 16330
4 4 0 9052
3 4 8 18592
4 3 0 10853
6 5 6 260
6 2 1 7979
3 5 3 16859
5 5 7 17000
1 2 6 8282
1 6 3 957
6 4 3 9470
6 5 9 10541
2 6 8 8372
2 6 7 6297
1 2 9 15822
6 2 0 10634
1 1 7 11728...

output:

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

result:

ok 30384 lines

Test #12:

score: 5
Accepted
time: 298ms
memory: 35848kb

input:

10
6 4990 19956
4 1 9 10413
6 4 7 19545
2 5 5 11491
6 5 1 3975
1 5 8 1699
3 6 0 6002
4 3 0 4921
6 6 1 15634
2 4 6 1876
1 3 0 17060
6 3 0 14890
4 2 4 17587
3 3 2 14233
3 2 2 1486
2 4 1 2002
3 2 1 6420
5 4 5 16842
4 6 6 7468
2 4 3 18938
2 2 8 3690
2 3 7 12751
5 5 1 6809
3 3 5 5196
3 2 5 8143
3 4 0 139...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
6
1
0
1
3
2
0
0
1
0
1
1
3
3
3
1
0
2
2
1
2
2
2
1
3
1
1
3
3
0
4
2
0
1
1
2
1
3
2
0
1
0
2
0
29
0
1
0
13
9
18
15
3
2
10
1
9
17
13
11
2
5
1
3
6
5
3
6
11
0
13
0
11
3
1
1
6
5
6
13
9
8
6
8
12
10
13
9
11
5
9
...

result:

ok 35383 lines

Test #13:

score: 5
Accepted
time: 310ms
memory: 39044kb

input:

10
4 4992 19978
3 3 4 18114
3 2 0 9129
4 3 1 2557
1 2 4 17326
4 3 0 15118
3 3 7 14659
2 4 7 5103
2 1 5 4331
2 2 7 16062
4 4 5 16766
1 1 9 633
1 3 0 1709
2 4 8 7795
2 2 9 1723
3 2 4 9500
2 4 8 4041
1 3 6 8318
3 3 2 17002
3 1 1 13373
2 2 7 3406
4 3 9 9423
3 4 8 4516
3 1 1 5267
2 3 7 242
1 4 2 7578
2 4...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0
2
4
2
3
4
0
2
4
1
0
0
0
0
1
1
0
0
1
2
3
0
0
4
0
0
0
0
0
0
0
4
1
2
2
2
1
1
2
2
0
6
0
1
3
0
4
11
11
7
0
1
7
3
6
8
11
1
5
5
5
8
4
0
6
6
3
0
8
4
2
0
3
2
2
5
2
3
3
2
4
1
6
2
5
3
2
3
4
3
8
2
5
5
4
3
4
3
3
2
4...

result:

ok 40383 lines

Test #14:

score: 5
Accepted
time: 319ms
memory: 40716kb

input:

10
5 4985 19935
5 5 4 3085
1 4 2 13575
5 2 5 14600
5 2 6 14953
4 1 8 18145
3 5 4 13619
2 4 8 8245
4 3 7 12415
4 4 3 2072
2 3 2 13368
2 3 8 12771
2 2 8 7915
4 4 1 3325
5 5 4 9067
1 5 8 12922
2 5 8 15516
2 3 4 7827
5 2 8 360
2 4 3 9441
5 4 6 5290
4 2 6 6563
4 4 9 14623
1 5 6 1250
1 3 8 6817
3 1 0 9708...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
1
0
0
0
0
0
0
1
0
0
0
0
1
0
0
0
0
1
0
0
0
0
2
1
0
3
5
1
1
1
0
0
2
0
0
1
0
0
2
0
1
1
0
0
0
0
1
1
0
0
0
2
0
0
1
0
0
1
1
0
1
0
0
2
0
0
0
6
19
8
9
9
17
7
10
12
2
1
19
2
16
7
2
7
7
0
9
10
1
9
2
11
7
0
1
0
9
6
0
3
5
6
7
11
4
12
11
1
9
0
1
7
6
10
7
...

result:

ok 45384 lines

Test #15:

score: 5
Accepted
time: 391ms
memory: 46672kb

input:

10
6 4995 19955
3 4 3 3040
2 3 7 13043
4 3 5 8426
5 1 8 19469
1 2 2 10883
6 4 8 2424
4 3 1 1737
5 2 4 14899
1 3 1 18706
6 3 6 1681
2 3 9 17890
4 4 2 14975
6 1 9 2434
2 4 8 4355
5 4 8 3200
3 5 1 8549
1 6 2 18377
1 2 3 11612
1 2 3 5990
3 6 7 1604
6 4 8 19417
2 5 8 1461
4 3 2 17723
2 3 7 1312
5 4 6 133...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
1
2
0
0
0
0
0
1
2
0
2
0
0
1
0
1
0
2
0
3
0
1
0
0
0
2
1
0
0
1
0
0
1
0
3
2
5
2
0
2
2
0
0
0
5
0
0
2
2
2
2
1
1
6
0
3
1
0
3
0
2
28
3
0
7
12
9
0
7
11
7
12
8
6
26
7
7
6
6
0
9
4
8
6
6
0
8
21
2
3
13
12
13
1
12
8
1
8
13
9
13
9
4
0
7
17...

result:

ok 45380 lines

Test #16:

score: 5
Accepted
time: 413ms
memory: 47368kb

input:

10
6 4992 19968
1 6 6 16842
3 3 7 15861
5 6 0 3497
5 5 5 19954
1 2 6 2069
2 5 5 13875
2 5 5 6179
6 5 0 16086
2 4 0 18793
1 3 4 6990
5 5 1 1486
3 4 8 3505
2 2 5 2834
5 2 9 14770
2 4 7 222
3 4 6 10285
6 1 1 16750
6 6 9 5549
3 4 6 7994
4 4 8 16822
3 3 9 19233
2 3 7 315
4 1 2 3861
5 5 2 8059
4 5 7 14125...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
2
0
1
1
1
2
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
5
1
2
0
0
0
4
5
3
1
0
0
3
0
5
1
0
1
3
2
0
0
0
2
1
0
1
0
1
3
0
0
0
6
0
1
1
0
1
0
1
1
0
0
102
15
3
30
2
7
14
4
8
5
15
3
4
13
8
16
3
9
4
0
12
5
2
0
1
5
1
6
4
9
0
9
8
2
2
6
6
5
1
6
2
5
8
11
6
8
3
...

result:

ok 50382 lines

Test #17:

score: 5
Accepted
time: 365ms
memory: 45784kb

input:

10
6 4996 19904
4 3 6 17307
1 5 4 2473
3 2 7 12144
5 3 7 6672
5 3 6 15910
3 5 6 11688
4 4 8 16190
2 1 2 15197
3 5 4 901
6 4 9 11844
3 6 1 3506
1 6 7 10385
6 3 2 2327
5 5 5 12960
5 5 9 7675
5 4 9 13836
5 2 0 5528
4 5 7 10281
2 5 2 456
1 5 1 15869
6 1 2 16706
1 6 3 14880
6 1 2 10730
4 3 5 2894
6 6 6 9...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1
0
0
1
0
0
0
0
0
1
0
1
0
0
1
11
1
0
0
2
1
5
2
1
5
0
3
1
1
3
0
0
7
1
0
1
3
1
0
1
0
3
5
0
2
2
1
3
0
1
1
0
3
3
5
0
3
0
0
5
1
21
5
5
23
7
3
8
18
1
1
11
2
1
7
5
17
12
8
3
18
20
18
19
6
18
1
26
4
4
7
0
13
1
0
0
7
17
6
6
18
21
5
6
1
0
2...

result:

ok 55380 lines

Test #18:

score: 5
Accepted
time: 375ms
memory: 49132kb

input:

10
6 4995 19947
3 6 2 13262
2 3 1 13329
4 4 6 7013
6 4 4 19047
1 2 1 9111
5 1 0 13164
6 6 3 10469
1 2 5 9157
5 5 9 18420
1 2 3 6912
3 3 3 14729
5 2 5 17794
6 5 6 2306
6 5 7 9709
3 3 6 9322
2 6 2 16917
1 2 5 7401
3 1 2 12811
6 5 9 856
2 6 2 968
6 3 1 1764
2 4 1 13252
5 1 2 14558
4 4 6 989
3 5 0 4787
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
0
1
3
0
0
4
2
1
12
2
0
3
0
0
1
4
2
3
0
2
0
4
0
2
1
0
2
0
3
4
0
1
0
1
6
0
0
1
1
4
1
2
1
6
0
0
0
59
7
3
19
25
0
7
1
7
4
28
4
45
40
7
30
8
4
1
1
37
30
1
27
5
5
19
2
4
6
4
27
22
2
5
9
0
24
3
1
7
26
3
7
...

result:

ok 60383 lines

Test #19:

score: 5
Accepted
time: 303ms
memory: 43712kb

input:

10
4 4982 19982
1 1 1 15234
3 4 6 8936
3 1 3 13315
2 1 9 7802
1 4 3 38
1 4 3 1508
1 1 8 13255
3 3 8 12225
4 3 5 11015
3 2 8 14803
4 2 5 9461
4 3 2 2918
1 4 1 18272
2 1 6 6307
2 4 7 14377
4 2 3 16242
1 4 7 6914
4 2 8 18377
4 3 0 3930
2 1 0 11053
4 3 0 11620
2 4 8 12686
3 4 7 5323
4 3 1 11344
2 1 6 19...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
0
0
0
2
0
0
0
0
0
0
1
0
1
1
6
7
0
4
0
0
4
2
2
0
3
1
0
5
0
1
5
0
1
7
0
0
1
1
0
2
0
2
0
1
2
2
0
0
2
0
1
1
1
0
1
1
0
0
0
1
75
1
1
0
2
62
3
59
0
3
54
7
35
2
58
39
1
0
8
38
29
3
2
50
3
0
43
1
3
9
3
5
8
31
2
8
1
34
9
41
7
3
29...

result:

ok 65309 lines

Test #20:

score: 5
Accepted
time: 327ms
memory: 47808kb

input:

10
5 4995 19956
5 4 1 9042
1 3 5 15683
1 2 5 9
3 4 5 15372
3 4 4 16833
5 4 9 14900
5 5 7 8808
4 4 5 2929
2 2 2 11639
1 4 6 16942
1 2 0 18810
2 4 5 12978
1 4 7 16548
2 1 7 6791
4 4 1 14378
1 2 2 11387
3 3 1 11847
5 3 0 10333
1 4 2 15040
1 3 0 1774
5 5 2 15991
2 1 0 11377
1 4 7 13228
1 3 5 10889
1 2 0...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
0
2
0
0
0
1
0
0
2
2
0
2
0
0
1
0
0
8
5
1
3
0
3
0
2
1
3
1
0
0
4
1
0
4
0
3
1
0
2
1
0
1
2
2
3
2
1
1
0
1
2
0
1
3
1
1
1
0
39
24
5
28
33
29
27
25
14
0
14
46
22
12
6
10
1
1
8
1
8
15
6
7
7
9
0
7
4
7
12
3
1
11
1
0
7
8
13
3
7
6
9
2
1...

result:

ok 70337 lines