QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#719918#9533. Classical Counting Problempeaneval_kala100 ✓647ms53040kbC++2312.4kb2024-11-07 09:50:262024-11-07 09:50:27

Judging History

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

  • [2024-11-07 09:50:27]
  • 评测
  • 测评结果:100
  • 用时:647ms
  • 内存:53040kb
  • [2024-11-07 09:50:26]
  • 提交

answer

#pragma GCC optimize(3, "unroll-loops", "no-stack-protector")
#define atsum(l, r) accumulate(l, r, 0)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 0x3f3f3f3f;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
inline void chkmin(T &x, T y) { x = min(x, y); }
template <typename T>
inline void chkmax(T &x, T y) { x = max(x, y); }
namespace FastIO
{
// ------------------------------
#define IN_HAS_NEG
#define OUT_HAS_NEG
#define CHK_EOF
#define DISABLE_MMAP
// ------------------------------
#if __cplusplus < 201400
#error Please use C++14 or higher.
#endif
#if __cplusplus > 201700
#define INLINE_V inline
#else
#define INLINE_V
#endif
#if ( defined(LOCAL) || defined (_WIN32) ) && !defined(DISABLE_MMAP)
#define DISABLE_MMAP
#endif
#ifndef DISABLE_MMAP
#include<sys/mman.h>
#endif
#ifdef LOCAL
    inline char gc() { return getchar(); }
    inline void pc(char c) { putchar(c); }
#else
#ifdef DISABLE_MMAP
    INLINE_V constexpr int _READ_SIZE = 1 << 18;
    INLINE_V static char _read_buffer[_READ_SIZE], *_read_ptr = nullptr, *_read_ptr_end = nullptr;
    inline char gc()
    {
        if ( __builtin_expect(_read_ptr == _read_ptr_end, false) )
        {
            _read_ptr = _read_buffer;
            _read_ptr_end = _read_buffer + fread(_read_buffer, 1, _READ_SIZE, stdin);
#ifdef CHK_EOF
            if ( __builtin_expect(_read_ptr == _read_ptr_end, false) ) return EOF;
#endif
        }
        return *_read_ptr++;
    }
#else
    INLINE_V static const char *_read_ptr = (const char *)mmap(nullptr, INT_MAX, 1, 2, 0, 0);
    inline char gc() { return *_read_ptr++; }
#endif
    INLINE_V constexpr int _WRITE_SIZE = 1 << 18;
    INLINE_V static char _write_buffer[_WRITE_SIZE], *_write_ptr = _write_buffer;
    inline void pc(char c)
    {
        *_write_ptr++ = c;
        if ( __builtin_expect(_write_buffer + _WRITE_SIZE == _write_ptr, false) )
        {
            fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout);
            _write_ptr = _write_buffer;
        }
    }
    INLINE_V struct _auto_flush
    {
        ~_auto_flush() { fwrite(_write_buffer, 1, _write_ptr - _write_buffer, stdout); }
    }	_auto_flush;
#endif
#ifdef CHK_EOF
    inline bool _isdigit(char c) { return ( c & 16 ) && c != EOF; }
    inline bool _isgraph(char c) { return c > 32 && c != EOF; }
#else
    inline bool _isdigit(char c) { return c & 16; }
    inline bool _isgraph(char c) { return c > 32; }
#endif
    template < class T >
    INLINE_V constexpr bool _is_integer = numeric_limits < T >::is_integer;
    template < class T >
    INLINE_V constexpr bool _is_signed = numeric_limits < T >::is_signed;
    template < class T >
    INLINE_V constexpr bool _is_unsigned = _is_integer < T > && !_is_signed < T >;
    template <> INLINE_V constexpr bool _is_integer < __int128 > = true;
    template <> INLINE_V constexpr bool _is_integer < __uint128_t > = true;
    template <> INLINE_V constexpr bool _is_signed < __int128 > = true;
    template <> INLINE_V constexpr bool _is_unsigned < __uint128_t > = true;
#undef INLINE_V
    inline void read(char &c) { do c = gc(); while ( !_isgraph(c) ); }
    inline void read_cstr(char *s)
    {
        char c = gc(); while ( !_isgraph(c) ) c = gc();
        while ( _isgraph(c) ) *s++ = c, c = gc();
        *s = 0;
    }
    inline void read(string &s)
    {
        char c = gc(); s.clear(); while ( !_isgraph(c) ) c = gc();
        while ( _isgraph(c) ) s.push_back(c), c = gc();
    }
#ifdef IN_HAS_NEG
    template < class T, enable_if_t < _is_signed < T >, int > = 0 >
    inline void read(T &x)
    {
        char c = gc(); bool f = true; x = 0;
        while ( !_isdigit(c) ) { if ( c == 45 ) f = false; c = gc(); }
        if ( f ) while ( _isdigit(c) ) x = x * 10 + ( c & 15 ), c = gc();
        else     while ( _isdigit(c) ) x = x * 10 - ( c & 15 ), c = gc();
    }
    template < class T, enable_if_t < _is_unsigned < T >, int > = 0 >
#else
    template < class T, enable_if_t < _is_integer < T >, int > = 0 >
#endif
    inline void read(T &x)
    {
        char c = gc(); while ( !_isdigit(c) ) c = gc();
        x = 0; while ( _isdigit(c) ) x = x * 10 + ( c & 15 ), c = gc();
    }
    inline void write(char c) { pc(c); }
    inline void write_cstr(const char *s) { while ( *s ) pc(*s++); }
    inline void write(const string &s) { for ( char c : s ) pc(c); }
#ifdef OUT_HAS_NEG
    template < class T, enable_if_t < _is_signed < T >, int > = 0 >
    inline void write(T x)
    {
        char buffer[numeric_limits < T >::digits10 + 1]; int digits = 0;
        if ( x >= 0 )  do buffer[digits++] =  ( x % 10 ) | 48, x /= 10; while ( x );
        else { pc(45); do buffer[digits++] = -( x % 10 ) | 48, x /= 10; while ( x ); }
        while ( digits ) pc(buffer[--digits]);
    }
    template < class T, enable_if_t < _is_unsigned < T >, int > = 0 >
#else
    template < class T, enable_if_t < _is_integer < T >, int > = 0 >
#endif
    inline void write(T x)
    {
        char buffer[numeric_limits < T >::digits10 + 1]; int digits = 0;
        do buffer[digits++] = ( x % 10 ) | 48, x /= 10; while ( x );
        while ( digits ) pc(buffer[--digits]);
    }
    template < int N > struct _tuple_io_helper
    {
        template < class ...T >
        static inline void _read(tuple < T... > &x)
        { _tuple_io_helper < N - 1 >::_read(x), read(get < N - 1 > (x)); }
        template < class ...T >
        static inline void _write(const tuple < T... > &x)
        { _tuple_io_helper < N - 1 >::_write(x), pc(32), write(get < N - 1 > (x)); }
    };
    template <> struct _tuple_io_helper < 1 >
    {
        template < class ...T >
        static inline void _read(tuple < T... > &x) { read(get < 0 > (x)); }
        template < class ...T >
        static inline void _write(const tuple < T... > &x) { write(get < 0 > (x)); }
    };
    template < class ...T >
    inline void read(tuple < T... > &x) { _tuple_io_helper < sizeof...(T) >::_read(x); }
    template < class ...T >
    inline void write(const tuple < T... > &x) { _tuple_io_helper < sizeof...(T) >::_write(x); }
    template < class T1, class T2 >
    inline void read(pair < T1, T2 > &x) { read(x.first), read(x.second); }
    template < class T1, class T2 >
    inline void write(const pair < T1, T2 > &x) { write(x.first), pc(32), write(x.second); }
    template < class T1, class ...T2 >
    inline void read(T1 &x, T2 &...y) { read(x), read(y...); }
    template < class ...T >
    inline void read_cstr(char *x, T *...y) { read_cstr(x), read_cstr(y...); }
    template < class T1, class ...T2 >
    inline void write(const T1 &x, const T2 &...y) { write(x), write(y...); }
    template < class ...T >
    inline void write_cstr(const char *x, const T *...y) { write_cstr(x), write_cstr(y...); }
    template < class T >
    inline void print(const T &x) { write(x); }
    inline void print_cstr(const char *x) { write_cstr(x); }
    template < class T1, class ...T2 >
    inline void print(const T1 &x, const T2 &...y) { print(x), pc(32), print(y...); }
    template < class ...T >
    inline void print_cstr(const char *x, const T *...y) { print_cstr(x), pc(32), print_cstr(y...); }
    inline void println() { pc(10); }
    inline void println_cstr() { pc(10); }
    template < class ...T >
    inline void println(const T &...x) { print(x...), pc(10); }
    template < class ...T >
    inline void println_cstr(const T *...x) { print_cstr(x...), pc(10); }
}
using namespace FastIO;
template <typename T>
inline void clear(T &x) {
    T y;
    swap(x, y);
}
const int N = 2e5 + 10;
struct Seg {
    unsigned w[N << 2], len[N << 2], tag[N << 2];
    bitset<N << 2> used;
    int st[N << 2], top;
    inline void maketag(int u, unsigned v) {
        if (!used.test(u)) used.set(u), st[++top] = u;
        w[u] += len[u] * v, tag[u] += v;
    }
    inline void pushdown(int u) {
        if (tag[u]) maketag(u << 1, tag[u]), maketag(u << 1 | 1, tag[u]), tag[u] = 0;
    } 
    inline void update(int u, int L, int R, int l, int r, unsigned v) {
        if (!used.test(u)) used.set(u), st[++top] = u;
        if (L >= l && R <= r) return maketag(u, v);
        if (L > r || R < l) return;
        int M = L + R >> 1;
        pushdown(u);
        update(u << 1, L, M, l, r, v), update(u << 1 | 1, M + 1, R, l, r, v);
        w[u] = w[u << 1] + w[u << 1 | 1];
        len[u] = len[u << 1] + len[u << 1 | 1];
    }
    inline void ins(int u, int L, int R, int x, unsigned v) {
        if (!used.test(u)) used.set(u), st[++top] = u;
        if (L == R) return len[u] += v, void(w[u] += tag[u] * v);
        pushdown(u);
        int M = L + R >> 1;
        if (x <= M )ins(u << 1, L, M, x, v);
        else  ins(u << 1 | 1, M + 1, R, x, v);
        w[u] = w[u << 1] + w[u << 1 | 1];
        len[u] = len[u << 1] + len[u << 1 | 1];
    }
    inline unsigned query(int u, int L, int R, int l, int r) {
        if (!used.test(u)) used.set(u), st[++top] = u;
        if (L >= l && R <= r) return w[u];
        if (L > r || R < l) return 0;
        pushdown(u);
        int M = L + R >> 1;
        return query(u << 1, L, M, l, r) + query(u << 1 | 1, M + 1, R, l, r);
    }
    inline void clr() {
        while (top) w[st[top]] = tag[st[top]] = len[st[top]] = 0, used.reset(st[top--]);
    }
} seg;
struct Tree{
    basic_string<int> vec[N];
    int top[N], son[N], siz[N], dfn[N], dfncnt, fa[N], dep[N];
    void dfs(int u) {
        siz[u] = 1;
        for (int v : vec[u])
            if (v != fa[u]) {
                fa[v] = u, dep[v] = dep[u] + 1, dfs(v), siz[u] += siz[v];
                if (siz[v] > siz[son[u]]) son[u] = v;
            }
    }
    void dfs2(int u) {
        dfn[u] = ++dfncnt;
        if (son[u]) top[son[u]] = top[u], dfs2(son[u]);
        for (int v : vec[u])
            if (v != fa[u] && v != son[u]) dfs2(top[v] = v);
    }
    inline int lca(int u, int v) {
        while (top[u] != top[v]) dep[top[u]] > dep[top[v]] ? u = fa[top[u]] : v = fa[top[v]];
        return dep[u] < dep[v] ? u : v; 
    }
    inline int dist(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; }
    inline void addedge(int u, int v) {
        vec[u] += v;
        vec[v] += u;
    }
} mn, mx;
int fa[N], n;
bitset<N> qwq;
void dfsa(int u) {
    qwq.set(u);
    for (int v : mn.vec[u])
        if (v != mn.fa[u]) dfsa(v);
}
int dfsb(int u) {
    int ans = qwq.test(u);
    for (int v : mx.vec[u])
        if (v != mx.fa[u]) ans += dfsb(v);
    return ans;
}
basic_string<int> G[N];
inline int find(int u) { return fa[u] == u ? u : (fa[u] = find(fa[u])); }
int T;
unsigned ans;
inline void update(int u) {
    seg.ins(1, 1, n, mx.dfn[u], u);
    int c = u;
    while (c) {
        seg.update(1, 1, n, mx.dfn[mx.top[c]], mx.dfn[c], 1);
        c = mx.fa[mx.top[c]];
    }
}
inline void ins(int u) {
    update(u);
    for (int v : mn.vec[u])
        if (v != mn.fa[u]) ins(v);
}
inline unsigned query(int u) {
    unsigned ans = 0;
    while (u) ans += seg.query(1, 1, n, mx.dfn[mx.top[u]], mx.dfn[u]), u = mx.fa[mx.top[u]];
    return ans;
}
inline void dsu(int u) {
    for (int v : mn.vec[u])
        if (v != mn.fa[u] && v != mn.son[u]) dsu(v), seg.clr();
    if (mn.son[u]) dsu(mn.son[u]);
    update(u);
    for (int v : mn.vec[u])
        if (v != mn.fa[u] && v != mn.son[u]) ins(v);
    ans += u * query(u);
}
inline void work() {
    ans = 0;
    for (int i = 1; i <= n; i++) mn.son[i] = mx.son[i] = 0, clear(mx.vec[i]), clear(mn.vec[i]);
    for (int i = 1; i <= n; i++) mn.fa[i] = mx.fa[i] = 0;
    mn.dfncnt = mx.dfncnt = 0;
    read(n);
    for (int i = 1; i <= n; i++) clear(G[i]);
    for (int i = 1; i < n; i++) {
        int u, v;
        read(u, v);
        G[u] += v, G[v] += u;
    }
    for (int i = 1; i <= n; i++) fa[i] = i;
    for (int i = 1; i <= n; i++)
        for (int v : G[i])
            if (v < i) mx.addedge(find(v), i), fa[find(v)] = i;
    for (int i = 1; i <= n; i++) fa[i] = i;
    for (int i = n; i; i--)
        for (int v : G[i])
            if (v > i) mn.addedge(find(v), i), fa[find(v)] = i;
    mn.dfs(1), mx.dfs(n);
    mn.dfs2(mn.top[1] = 1), mx.dfs2(mx.top[n] = n);
    seg.clr();
    dsu(1);
    println(ans);
}
int main() {
    read(T);
    while (T--) work();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 27ms
memory: 38836kb

input:

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

output:

1592
2672
1502
3164
1869
3983
1215
2628
1548
4137
957
2441
1865
2974
1530
1701
2261
2554
1760
2259
2323
3480
2319
1375
1648
4365
2377
1544
1912
1114
1697
2814
2208
2397
1360
1806
1747
2365
1418
1773
2343
2292
2475
1480
1650
1998
981
1378
1785
1984
3003
989
3453
1656
2008
2302
3492
2682
2393
2994
176...

result:

ok 10000 lines

Subtask #2:

score: 10
Accepted

Test #2:

score: 10
Accepted
time: 40ms
memory: 35480kb

input:

5000
20
20 19
7 20
16 19
2 7
15 16
13 2
11 7
6 2
14 2
12 15
5 14
17 2
4 20
3 6
18 20
10 5
1 16
9 14
8 15
20
7 17
15 7
10 17
13 7
18 10
9 17
6 18
16 18
14 16
1 10
19 17
3 18
2 13
8 19
5 1
11 14
20 8
4 18
12 11
20
20 1
17 1
18 20
3 1
9 18
16 18
10 18
12 18
6 9
5 16
19 18
4 5
11 17
14 18
15 17
7 6
13 7...

output:

20721
38034
34273
22829
17722
27544
46022
45137
16644
27269
28662
25035
26312
21148
14106
28176
17802
35960
12683
53453
11349
31418
12177
38063
13437
15209
40896
36164
24851
27149
33448
35621
21295
18051
15404
16388
23302
23641
22600
18168
15109
26323
22612
53786
26857
17201
29605
13181
18756
16472
...

result:

ok 5000 lines

Test #3:

score: 10
Accepted
time: 36ms
memory: 37344kb

input:

5000
20
2 5
12 2
20 2
3 20
1 5
10 12
9 20
4 9
11 20
6 20
16 3
8 3
13 8
17 10
14 2
18 17
7 3
19 13
15 11
20
15 6
12 15
17 6
5 17
3 12
18 15
8 18
11 5
13 15
16 17
14 13
9 11
20 5
7 13
1 8
4 1
19 5
10 9
2 3
20
5 15
10 15
3 5
9 15
20 5
6 10
17 20
16 6
11 10
19 17
7 5
4 16
14 17
13 17
12 14
18 13
2 5
8 3...

output:

12838
26475
28287
25843
18772
22056
29113
38525
15746
14068
27472
30414
24262
17868
21848
45416
16243
11822
29882
14933
54527
29300
18796
15975
26275
65954
32332
32468
25904
26481
15872
14722
12571
16132
12703
29222
14381
14446
58713
50838
32213
20501
24381
18175
24763
29058
16690
18122
20539
32235
...

result:

ok 5000 lines

Subtask #3:

score: 10
Accepted

Test #4:

score: 10
Accepted
time: 53ms
memory: 35592kb

input:

2500
40
29 30
24 29
36 29
7 24
18 36
4 18
21 24
17 21
1 17
14 30
19 24
34 19
12 34
11 30
23 30
16 34
38 14
35 24
27 29
20 34
3 17
22 17
31 21
26 14
5 14
2 20
15 16
39 4
37 38
10 3
25 22
33 29
32 10
13 35
9 18
8 2
6 23
40 34
28 7
40
32 10
33 32
2 32
7 10
22 7
21 10
31 22
40 32
37 32
25 22
16 22
39 10...

output:

810633
404452
117099
243743
296777
474314
650868
172328
309229
117742
243602
365391
470337
224328
611246
135238
282936
391899
241985
241809
365040
159975
153715
361771
436106
282863
365203
134808
384355
137564
271407
537918
241082
212081
412678
461768
430833
460584
236968
256887
457800
439758
244646...

result:

ok 2500 lines

Test #5:

score: 10
Accepted
time: 56ms
memory: 33728kb

input:

1666
60
48 60
14 48
35 48
10 48
28 10
43 60
57 60
12 60
38 12
29 28
22 57
17 22
9 60
16 48
21 43
8 57
52 43
37 8
5 28
27 17
24 5
34 29
49 57
3 27
23 37
53 16
13 12
47 37
6 27
39 43
56 13
36 47
19 9
25 19
2 60
32 9
41 25
7 23
45 23
31 5
44 7
46 8
11 16
42 14
26 13
30 24
18 26
55 8
58 28
54 46
1 26
15...

output:

721013
1066623
1062972
1881033
697159
1529292
773227
791222
1614211
2341895
651702
976430
2375602
741248
1733771
2261741
2252942
4137189
1426473
2388105
670178
896629
2879777
843521
1182424
2832567
4026284
2804238
1501560
508974
1290125
1013982
1845299
1256080
3065089
2015363
2166807
2717402
2216766...

result:

ok 1666 lines

Test #6:

score: 10
Accepted
time: 57ms
memory: 37376kb

input:

1250
80
56 47
60 56
34 56
24 60
19 47
18 34
69 24
64 69
20 64
39 34
42 20
28 60
50 64
33 47
67 24
51 47
62 33
5 34
8 24
31 24
61 5
22 19
79 22
12 5
71 28
77 18
70 67
26 64
75 67
16 60
7 42
49 64
11 42
76 51
73 5
35 49
15 33
65 8
78 47
23 62
44 19
38 22
45 39
37 73
57 12
53 37
36 50
43 51
14 24
21 22...

output:

9825885
3893226
6116875
10086749
5393096
6522315
5920355
7902829
3381069
7569894
11567605
8651236
8257852
8756874
4255356
5362908
2357220
1811999
2132744
7582576
6297893
3149082
6203806
8273964
3567508
3978460
2050230
3292482
4784268
3382210
6900055
4094135
11029041
10556808
5959680
5565869
14793452...

result:

ok 1250 lines

Test #7:

score: 10
Accepted
time: 69ms
memory: 34988kb

input:

1000
100
27 59
62 59
77 62
64 27
29 77
3 64
35 77
58 77
48 29
51 35
31 51
80 29
36 62
17 27
99 59
65 59
78 51
2 36
63 59
45 80
93 77
86 36
30 29
72 36
94 29
40 72
49 65
44 58
1 64
81 51
10 2
43 29
18 43
28 18
12 64
20 12
32 40
56 12
7 17
84 30
24 62
89 51
23 43
11 48
92 27
19 59
41 62
79 64
37 10
97...

output:

31974913
12093412
14105413
3172720
10694179
7923959
6328108
11225351
16726813
7154491
20683709
15448313
6702811
12057927
5646735
11944823
20882833
12781298
6563862
11034477
14478585
15412978
12307953
21275884
6567913
19896453
5099613
19928517
7874152
20318428
33070320
13452965
5982093
6647881
221816...

result:

ok 1000 lines

Subtask #4:

score: 15
Accepted

Test #8:

score: 15
Accepted
time: 75ms
memory: 36052kb

input:

500
200
63 7
145 63
78 145
103 63
163 7
97 163
57 7
186 78
30 145
25 57
56 97
112 25
50 7
162 50
105 112
126 57
32 126
95 105
188 105
124 112
86 186
82 162
143 162
194 95
183 97
101 82
197 82
200 186
96 143
146 124
164 197
54 95
195 57
131 143
130 146
193 112
36 97
16 163
62 193
93 124
121 96
1 96
1...

output:

126443395
98757645
53031961
291855662
249009043
162378675
132960917
162056007
329056810
108103316
299902243
119131433
120999023
298936590
233906403
125093815
164591715
335168622
158851967
154337469
199778607
124138841
154231483
148367087
328821194
199730727
214600421
117839595
69955641
188267743
108...

result:

ok 500 lines

Test #9:

score: 15
Accepted
time: 95ms
memory: 36256kb

input:

200
500
190 329
121 190
31 329
274 121
391 274
50 31
79 121
397 31
27 397
67 391
144 121
23 79
352 31
36 391
304 50
284 391
448 23
104 144
34 352
323 144
17 274
60 27
390 34
313 274
75 27
5 67
223 60
185 79
217 144
68 329
446 185
399 68
156 397
51 68
258 75
473 31
146 75
496 156
181 352
434 79
334 2...

output:

3397794692
2928277062
3103858497
154671595
3801613407
1845716072
3939200980
1428371420
1172721046
1241934548
3733101085
2767233351
511155950
1998255682
1883525235
2008135166
1434460021
159624931
4040758374
2297898704
269887615
3312161125
3867499745
2334161982
4224343114
3903581708
567822306
25585921...

result:

ok 200 lines

Test #10:

score: 15
Accepted
time: 109ms
memory: 38528kb

input:

100
1000
357 35
94 35
80 35
805 80
236 80
165 805
583 357
575 805
743 94
353 357
423 583
773 357
204 94
558 353
788 743
252 805
19 353
868 35
968 743
789 868
214 788
156 19
463 156
427 165
500 788
797 558
721 353
117 789
761 80
169 805
197 80
47 165
119 721
185 500
237 353
247 237
208 805
491 80
432...

output:

2673471398
4292125373
1343868658
3107657120
266520979
1541593572
1446269746
2633422195
1439534990
3141756706
1657394538
1657528872
338270
1137567645
1309145575
4100210508
1176806332
1478905565
3368333919
893489146
838424622
3314572650
444945585
3654020026
2706743444
2411845211
2123694075
3979217896
...

result:

ok 100 lines

Test #11:

score: 15
Accepted
time: 121ms
memory: 34588kb

input:

50
2000
1827 1711
342 1827
1832 1711
1689 342
504 1827
1642 504
1346 1827
1345 1689
52 1827
852 1711
379 1711
1324 1689
862 504
1400 1324
733 862
458 504
1736 1346
1839 504
1634 379
1104 1346
912 458
235 1345
1088 504
87 504
845 1832
559 1827
1482 733
516 1839
998 852
1696 559
345 1696
863 504
248 9...

output:

917126840
2237073293
2385880511
620308988
1386099808
868143037
173569650
2250120512
3150663775
3344857473
213801405
2245716498
755172521
1616607299
3214196809
3850242099
603018244
551472507
168294444
2108552628
39809209
13283767
508284300
2875978449
3871824467
1246833010
2464374958
1749141591
273785...

result:

ok 50 lines

Test #12:

score: 15
Accepted
time: 127ms
memory: 35952kb

input:

50
2000
184 1524
914 184
977 184
94 914
163 977
1678 1524
12 914
1721 163
1215 914
1766 163
214 1215
1415 94
1840 163
1096 12
1504 1678
1230 1415
1604 1721
1241 1504
1318 1504
420 184
1413 1230
1109 1504
1327 914
1014 1096
1831 184
612 1230
295 1109
1482 1241
483 295
93 1096
1149 1318
800 1413
41 14...

output:

4240371587
2491841356
1212590620
136985608
3063417188
3141849558
2531065808
3783088908
596251808
3050326799
3403744286
412062308
4272672179
1547192568
803786680
857943975
4221588258
1202478330
3831605028
855429272
2339826001
3369634154
1313609172
3457718674
4057316238
362819851
818784121
523000430
3...

result:

ok 50 lines

Subtask #5:

score: 15
Accepted

Test #13:

score: 15
Accepted
time: 128ms
memory: 35656kb

input:

33
3000
1322 1797
2442 1797
626 2442
1979 2442
485 1979
1428 1979
2196 1979
1499 1797
1936 2442
2921 1979
751 1499
2182 2921
979 1499
935 1979
1136 485
1880 1797
1084 2921
349 751
482 349
1009 979
1003 349
2056 482
2959 1136
1288 751
496 2442
1693 935
2045 1499
868 1009
1264 1428
2891 868
1045 1288
...

output:

3914500827
327554069
391027586
979652421
2494881767
236140220
2346024452
2163053318
3655145076
2743479090
589039971
2540607281
1715902488
1847495962
4188258866
2741527567
1841317327
3081320723
1708279848
3607543506
4065758426
3996971978
1920118810
1867947137
3320884977
2180552284
235816630
355893588...

result:

ok 33 lines

Test #14:

score: 15
Accepted
time: 159ms
memory: 35680kb

input:

10
10000
5445 6636
3767 5445
275 5445
8797 6636
3028 3767
8805 8797
8727 6636
8514 8805
7072 8805
1717 6636
5720 8727
4373 275
4166 4373
5735 7072
3679 8727
842 6636
6414 1717
2275 4373
9827 8797
1886 6414
4320 8797
8179 5720
9346 1886
8524 8727
3692 3028
416 5445
5955 3692
8500 1886
1910 6414
7217 ...

output:

4274793245
254213159
2247270664
2544643228
1082664730
2905589050
467019468
3117700443
4011931349
3001623237

result:

ok 10 lines

Test #15:

score: 15
Accepted
time: 177ms
memory: 38392kb

input:

3
30000
22314 7310
28209 22314
10736 22314
26950 22314
8494 26950
6511 28209
13135 10736
4239 26950
3690 7310
13592 26950
13573 6511
29782 26950
1390 3690
27910 4239
1600 13592
7959 3690
17707 22314
12396 6511
26367 1390
25184 10736
27747 4239
2155 25184
13259 28209
18865 7959
23083 13135
14593 7959...

output:

3969587518
179731823
2877266544

result:

ok 3 lines

Test #16:

score: 15
Accepted
time: 163ms
memory: 37172kb

input:

3
30000
7419 12032
23227 7419
15838 23227
717 23227
27008 15838
27878 27008
13791 15838
15226 13791
5878 15838
10339 5878
11462 12032
16562 12032
19043 27878
18113 27878
15923 12032
9100 5878
25572 27008
7018 25572
1528 18113
5920 27878
12801 7419
11086 12032
29576 12032
13632 29576
6892 13791
13092...

output:

2232342596
3113008729
3207634870

result:

ok 3 lines

Test #17:

score: 15
Accepted
time: 180ms
memory: 37240kb

input:

3
30000
233 25136
21401 233
18248 21401
2683 18248
25299 25136
7330 25299
23069 18248
19970 25299
25665 2683
4059 25665
15457 21401
25891 15457
2277 25891
29249 233
25388 29249
29805 233
136 4059
15868 21401
12359 2683
17993 136
834 15868
17705 25891
15002 25665
859 4059
397 136
21059 12359
16115 15...

output:

1891687691
4286604632
1194968781

result:

ok 3 lines

Subtask #6:

score: 20
Accepted

Test #18:

score: 20
Accepted
time: 275ms
memory: 48568kb

input:

1
98303
65520 65521
65521 65519
65519 65522
65522 65517
65517 65518
65518 65516
65516 65523
65523 65513
65513 65514
65514 65512
65512 65515
65515 65510
65510 65511
65511 65509
65509 65524
65524 65505
65505 65506
65506 65504
65504 65507
65507 65502
65502 65503
65503 65501
65501 65508
65508 65498
6549...

output:

2387240282

result:

ok single line: '2387240282'

Test #19:

score: 20
Accepted
time: 632ms
memory: 41048kb

input:

1
100000
72651 74015
74015 53999
53999 82883
82883 49285
49285 18491
18491 57017
57017 14822
14822 80585
80585 2393
2393 95415
95415 53193
53193 85537
85537 6136
6136 67847
67847 74149
74149 87362
87362 56875
56875 36575
36575 63221
63221 24881
24881 70084
70084 18858
18858 10916
10916 12540
12540 9...

output:

2050334631

result:

ok single line: '2050334631'

Test #20:

score: 20
Accepted
time: 629ms
memory: 40400kb

input:

1
100000
34724 22839
22839 36196
36196 48281
48281 76153
76153 47939
47939 63440
63440 70687
70687 44040
44040 65361
65361 62112
62112 11797
11797 89597
89597 36911
36911 5069
5069 48575
48575 20966
20966 95642
95642 52437
52437 88678
88678 77335
77335 53313
53313 35231
35231 85082
85082 74199
74199...

output:

746654424

result:

ok single line: '746654424'

Test #21:

score: 20
Accepted
time: 647ms
memory: 37268kb

input:

1
100000
43937 87425
87425 14024
14024 30838
30838 24475
24475 76153
76153 26430
26430 6738
6738 42792
42792 61639
61639 96671
96671 81535
81535 40471
40471 55118
55118 20311
20311 79890
79890 12082
12082 84049
84049 21637
21637 58586
58586 34151
34151 45233
45233 22789
22789 91250
91250 54125
54125...

output:

2623773461

result:

ok single line: '2623773461'

Subtask #7:

score: 25
Accepted

Test #22:

score: 25
Accepted
time: 228ms
memory: 40120kb

input:

1
100000
16150 88283
9425 88283
87988 88283
52935 88283
69816 88283
51311 88283
6910 9425
59991 87988
54743 6910
19614 52935
22926 69816
91163 88283
17233 19614
64177 19614
92097 91163
57414 9425
96321 6910
17859 54743
59810 69816
66565 17859
9948 96321
84506 59810
3928 64177
63031 54743
6214 87988
...

output:

1787575884

result:

ok single line: '1787575884'

Test #23:

score: 25
Accepted
time: 224ms
memory: 43672kb

input:

1
100000
62262 63575
73160 63575
96365 62262
47519 96365
21455 96365
59846 62262
58337 96365
35161 58337
86407 62262
75478 73160
85060 58337
87416 63575
93832 21455
79046 59846
10212 63575
13214 96365
19580 87416
20323 85060
16635 63575
9463 75478
48664 19580
89760 10212
44672 87416
81712 62262
4685...

output:

3201252214

result:

ok single line: '3201252214'

Test #24:

score: 25
Accepted
time: 228ms
memory: 45400kb

input:

1
100000
45919 20230
54450 20230
41113 45919
2407 41113
46209 2407
60230 20230
69678 2407
56794 46209
46860 2407
21259 46860
76025 20230
22875 46209
35360 56794
23919 54450
38616 46209
32589 45919
41382 41113
92866 35360
25194 92866
31051 56794
77445 38616
72712 31051
70220 46860
62936 22875
49049 4...

output:

1408792727

result:

ok single line: '1408792727'

Test #25:

score: 25
Accepted
time: 208ms
memory: 43300kb

input:

1
100000
12337 64284
29089 12337
62292 64284
97288 64284
40021 62292
17782 62292
44533 29089
11114 29089
39182 40021
32105 44533
96395 39182
22375 29089
42005 96395
68061 44533
72549 40021
69336 64284
38466 69336
57201 11114
19998 62292
83177 69336
93468 39182
58369 62292
67850 39182
50910 22375
673...

output:

3351808169

result:

ok single line: '3351808169'

Test #26:

score: 25
Accepted
time: 208ms
memory: 46664kb

input:

1
100000
22466 68227
45347 68227
67554 68227
55553 22466
82416 67554
807 55553
39312 68227
68629 45347
82918 22466
90176 68227
81747 90176
70957 55553
19671 70957
33403 807
52966 67554
82101 33403
48470 22466
40948 45347
53089 90176
1792 82416
93729 68629
50761 68629
17137 52966
27111 52966
61380 53...

output:

2982675783

result:

ok single line: '2982675783'

Test #27:

score: 25
Accepted
time: 79ms
memory: 53040kb

input:

1
100000
4 5
5 7
7 9
9 10
10 12
12 16
16 18
18 20
20 21
21 22
22 24
24 26
26 28
28 32
32 34
34 37
37 38
38 40
40 42
42 44
44 45
45 47
47 49
49 57
57 58
58 61
61 68
68 69
69 71
71 73
73 74
74 76
76 78
78 79
79 80
80 81
81 83
83 85
85 88
88 90
90 91
91 94
94 98
98 99
99 100
100 101
101 103
103 104
104...

output:

1173931940

result:

ok single line: '1173931940'

Test #28:

score: 25
Accepted
time: 116ms
memory: 48904kb

input:

1
100000
2 4
4 6
6 10
10 11
11 13
13 14
14 16
16 18
18 19
19 25
25 29
29 30
30 31
31 32
32 33
33 34
34 36
36 39
39 41
41 44
44 46
46 47
47 48
48 49
49 50
50 51
51 53
53 59
59 61
61 62
62 63
63 64
64 66
66 67
67 72
72 73
73 74
74 76
76 81
81 82
82 83
83 84
84 87
87 90
90 91
91 93
93 94
94 95
95 98
98...

output:

3364223310

result:

ok single line: '3364223310'

Test #29:

score: 25
Accepted
time: 130ms
memory: 47684kb

input:

1
100000
1 3
3 8
8 10
10 13
13 15
15 19
19 20
20 22
22 23
23 26
26 27
27 28
28 30
30 31
31 32
32 36
36 39
39 40
40 41
41 44
44 47
47 54
54 55
55 56
56 58
58 61
61 62
62 63
63 72
72 74
74 76
76 77
77 79
79 81
81 82
82 85
85 87
87 92
92 93
93 95
95 96
96 97
97 102
102 104
104 105
105 107
107 111
111 1...

output:

714456019

result:

ok single line: '714456019'

Test #30:

score: 25
Accepted
time: 145ms
memory: 49936kb

input:

1
100000
3 6
6 7
7 8
8 10
10 11
11 14
14 18
18 21
21 25
25 27
27 28
28 29
29 31
31 33
33 35
35 36
36 37
37 39
39 40
40 41
41 44
44 45
45 46
46 47
47 49
49 50
50 56
56 61
61 64
64 67
67 69
69 71
71 73
73 74
74 79
79 80
80 84
84 85
85 86
86 87
87 91
91 92
92 93
93 94
94 95
95 97
97 98
98 100
100 101
1...

output:

4042991246

result:

ok single line: '4042991246'

Test #31:

score: 25
Accepted
time: 162ms
memory: 43840kb

input:

1
100000
5 6
6 7
7 10
10 12
12 13
13 14
14 16
16 18
18 19
19 20
20 22
22 23
23 31
31 32
32 37
37 39
39 40
40 41
41 45
45 46
46 49
49 50
50 52
52 54
54 56
56 58
58 59
59 62
62 63
63 65
65 67
67 68
68 70
70 76
76 77
77 80
80 82
82 83
83 85
85 87
87 96
96 102
102 105
105 107
107 108
108 109
109 110
110...

output:

3900075091

result:

ok single line: '3900075091'

Test #32:

score: 25
Accepted
time: 208ms
memory: 46304kb

input:

1
100000
4 5
5 6
6 9
9 12
12 13
13 17
17 18
18 20
20 21
21 22
22 24
24 26
26 28
28 30
30 35
35 37
37 38
38 46
46 47
47 48
48 49
49 50
50 55
55 57
57 58
58 62
62 65
65 67
67 68
68 69
69 70
70 76
76 78
78 79
79 80
80 81
81 83
83 84
84 85
85 86
86 87
87 90
90 91
91 93
93 94
94 96
96 99
99 100
100 106
1...

output:

3418237095

result:

ok single line: '3418237095'

Test #33:

score: 25
Accepted
time: 271ms
memory: 44172kb

input:

1
100000
1 2
2 3
3 4
4 6
6 14
14 16
16 18
18 20
20 21
21 23
23 24
24 25
25 31
31 33
33 34
34 36
36 38
38 42
42 43
43 44
44 46
46 47
47 48
48 50
50 51
51 53
53 54
54 55
55 56
56 61
61 62
62 63
63 64
64 66
66 67
67 72
72 74
74 76
76 80
80 81
81 85
85 86
86 87
87 90
90 92
92 95
95 98
98 99
99 102
102 1...

output:

3560442703

result:

ok single line: '3560442703'

Test #34:

score: 25
Accepted
time: 403ms
memory: 38208kb

input:

1
100000
1 4
4 5
5 9
9 10
10 11
11 12
12 16
16 17
17 21
21 27
27 29
29 33
33 34
34 36
36 37
37 40
40 42
42 46
46 47
47 48
48 52
52 54
54 55
55 59
59 61
61 62
62 66
66 67
67 68
68 70
70 72
72 74
74 75
75 76
76 77
77 78
78 79
79 83
83 85
85 87
87 93
93 96
96 83238
83238 99
99 100
100 101
101 103
103 1...

output:

4078156478

result:

ok single line: '4078156478'

Test #35:

score: 25
Accepted
time: 616ms
memory: 43348kb

input:

1
100000
69124 13938
13938 89981
89981 18806
18806 39741
39741 5738
5738 25296
25296 11914
11914 79193
79193 64999
64999 86981
86981 210
210 4953
4953 96054
96054 66076
66076 1974
1974 27290
27290 17367
17367 54724
54724 64515
64515 72049
72049 55651
55651 48
48 58482
58482 96784
96784 22698
22698 3...

output:

574415279

result:

ok single line: '574415279'