QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#719891 | #9533. Classical Counting Problem | peaneval_kala | 15 | 58ms | 35204kb | C++23 | 11.4kb | 2024-11-07 09:38:55 | 2024-11-07 09:38:56 |
Judging History
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];
}
inline void ins(int u, int L, int R, int x) {
if (!used.test(u)) used.set(u), st[++top] = u;
if (L == R) return void(w[u] += tag[u]);
int M = L + R >> 1;
ins(u << 1, L, M, x), ins(u << 1 | 1, M + 1, R, x);
w[u] = w[u << 1] + w[u << 1 | 1];
}
inline void clr() {
while (top) 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;
inline void work() {
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);
assert(n <= 20);
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[1] = 1);
seg.clr();
unsigned ans = 0;
for (int i = 1; i <= n; i++) {
dfsa(i);
for (int c = i; c; c = mx.fa[c])
if (qwq.test(c)) {
unsigned res = dfsb(c);
ans += res * unsigned(c) * unsigned(i);
}
qwq.reset();
}
println(ans);
}
int main() {
read(T);
while (T--) work();
return 0;
}
詳細信息
Subtask #1:
score: 5
Accepted
Test #1:
score: 5
Accepted
time: 46ms
memory: 33732kb
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: 53ms
memory: 35040kb
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: 58ms
memory: 35204kb
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: 0
Runtime Error
Test #4:
score: 0
Runtime Error
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:
result:
Subtask #4:
score: 0
Runtime Error
Test #8:
score: 0
Runtime Error
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:
result:
Subtask #5:
score: 0
Runtime Error
Test #13:
score: 0
Runtime Error
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:
result:
Subtask #6:
score: 0
Runtime Error
Test #18:
score: 0
Runtime Error
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:
result:
Subtask #7:
score: 0
Runtime Error
Test #22:
score: 0
Runtime Error
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 ...