QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#695878#9492. 树上简单求和peaneval_kala5 42ms44496kbC++2311.9kb2024-10-31 20:58:412024-10-31 20:58:45

Judging History

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

  • [2024-10-31 20:58:45]
  • 评测
  • 测评结果:5
  • 用时:42ms
  • 内存:44496kb
  • [2024-10-31 20:58:41]
  • 提交

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, C = 35;
int n, m, cc;
ull a[N], ans[N];
struct Tree{
basic_string<int> vec[N];
int top[N], son[N], siz[N], dfn[N], dfncnt, fa[N], dep[N], id[N];
ull qwq[N];
void dfs(int u) {
    qwq[u] += a[u];
    siz[u] = 1;
    for (int v : vec[u])
        if (v != fa[u]) {
            qwq[v] = qwq[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) {
    id[dfn[u] = ++dfncnt] = u;
    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 ull val(int u, int v) { return qwq[u] + qwq[v] - qwq[lca(u, v)] - qwq[fa[lca(u, v)]]; }
} T1, T2;
int B;
int pos[N];
struct Block {
    ull val[N], res[N];
    inline void clr() { memset(val, 0, sizeof(val)), memset(res, 0, sizeof(res)); }
    inline void update(int u, ull v) { val[u] += v, res[pos[u]] += v; }
    inline ull query(int l, int r) {
        if (pos[l] == pos[r]) {
            ull ans = 0;
            for (int c = l; c <= r; c++) ans += val[c];
            return ans;
        }
        ull ans = 0;
        for (int c = l; pos[c] == pos[l]; c++) ans += val[c];
        for (int c = r; pos[c] == pos[r]; c--) ans += val[c];
        for (int c = pos[l] + 1; c < pos[r]; c++) ans += res[c];
        return ans;
    }
} B1;
struct Block2 {
    ull val[N], res[N];
    inline void clr() { memset(val, 0, sizeof(val)), memset(res, 0, sizeof(res)); }
    inline ull query(int c) { return val[c] + res[pos[c] - 1]; }
    inline ull query(int l, int r) { return query(r) - query(l - 1); }
    inline void update(int u, ull v) {
        for (int c = pos[u]; c <= pos[n]; c++) res[c] += v;
        for (int c = u; pos[c] == pos[u]; c++) val[c] += v;
    } 
} B2;
int type[N * C], ql[N * C], qr[N * C];
ull qaq[N * C];
inline void update(int u, int v, ull k) {
    while (T1.top[u] != T1.top[v]) {
        if (T1.dep[T1.top[u]] < T1.dep[T1.top[v]]) swap(u, v);
        ++cc, ql[cc] = T1.dfn[T1.top[u]], qr[cc] = T1.dfn[u], qaq[cc] = k, u = T1.fa[T1.top[u]];
    }
    if (T1.dfn[u] > T1.dfn[v]) swap(u, v);
    ++cc, ql[cc] = T1.dfn[u], qr[cc] = T1.dfn[v], qaq[cc] = k;
}
inline void query(int u, int v, int id) {
    while (T2.top[u] != T2.top[v]) {
        if (T2.dep[T2.top[u]] < T2.dep[T2.top[v]]) swap(u, v);
        ++cc, type[cc] = id, ql[cc] = T2.dfn[T2.top[u]], qr[cc] = T2.dfn[u], u = T2.fa[T2.top[u]];
    }
    if (T2.dfn[u] > T2.dfn[v]) swap(u, v);
    ++cc, ql[cc] = T2.dfn[u], qr[cc] = T2.dfn[v], type[cc] = id;
}
ull tag;
inline void solve(int L, int R) {
    tag = 0;
    B1.clr(), B2.clr();
    for (int i = L; i <= R; i++) B1.update(T2.dfn[T1.id[i]], 1);
    for (int i = 1; i <= cc; i++)
        if (type[i]) {
            ans[type[i]] += B1.query(ql[i], qr[i]) * tag;
            ans[type[i]] += B2.query(ql[i], qr[i]);
        } else {
            if (L > qr[i] || R < ql[i]) continue;
            if (L >= ql[i] && R <= qr[i]) tag += qaq[i];
            else {
                for (int c = max(L, ql[i]); c <= min(R, qr[i]); c++) B2.update(T2.dfn[T1.id[c]], qaq[i]);
            }
        }
}
int main() {
    read(n, m);
    B = sqrt(n);
    for (int i = 1; i <= n; i++) read(a[i]), pos[i] = (i - 1) / B + 1;
    for (int i = 1; i < n; i++) {
        int u, v;
        read(u, v), T1.vec[u].push_back(v), T1.vec[v].push_back(u);
    }
    for (int i = 1; i < n; i++) {
        int u, v;
        read(u, v), T2.vec[u].push_back(v), T2.vec[v].push_back(u);
    }
    T1.dfs(1), T2.dfs(1);
    T1.dfs2(T1.top[1] = 1), T2.dfs2(T2.top[1] = 1);
    for (int i = 1; i <= m; i++) {
        int x, y;
        ull k;
        read(x, y, k);
        ans[i] = T2.val(x, y);
        update(x, y, k), query(x, y, i);
    }
    for (int l = 1, r; l <= n; l = r + 1) {
        r = min(l + B - 1, n);
        solve(l, r);
    }
    for (int i = 1; i <= m; i++) println(ans[i]);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 5
Accepted

Test #1:

score: 5
Accepted
time: 42ms
memory: 43220kb

input:

3000 3000
7236742292501328495 17973811477309806363 16075782662531676171 17971236571771878676 11392080645527132110 3685563455925680459 9773593720088356683 8313828403245053795 7736401634567449043 1634817828009987181 6951124933529719486 12775126714635387213 15460977209223753216 397573676785925632 31372...

output:

12105153858659381124
18367442707572066757
11668241962484097878
11288238120352358472
1742468310074622166
9942835997686093671
3305677510569607477
17741602000425004088
14984128302052618266
1075081718074605786
6509217537832509095
16750513627843273113
8569443169249732820
14475184194298579044
156111071108...

result:

ok 3000 lines

Test #2:

score: 5
Accepted
time: 39ms
memory: 43192kb

input:

3000 3000
1612333876155866602 8538417838700679227 6080765231437578796 17905224638340228394 12270907925903144224 17944105326358594564 17302041033966840611 1006351124625222126 496336153231744288 9393087977687876980 9553975238547373621 9361882717200384390 15051881329169144319 9757999873162420435 882725...

output:

11133131376095771981
7909873024850695144
16250639243139481926
14562550655578101207
8274205996508264973
178549413271904466
2368406276743327913
7464009386554813982
9439464815411774627
1471756740732097060
15201641099137019227
6774030298556871576
18156105511913219667
1553508745644446823
4225137078364117...

result:

ok 3000 lines

Test #3:

score: 5
Accepted
time: 34ms
memory: 43408kb

input:

3000 3000
9709246061666095435 1861649101703072889 10620139893353930613 17635186539135419482 710209455559527146 6075101384669982511 1120305006358459674 9703156967435388252 1397046737759839382 5259056712870179169 8253156305433022999 710199693203327302 15130650033641744675 10720111924616886955 15543351...

output:

7834604406305153073
5037061270969117785
16481572776620825702
15177894197606565804
3120320619896892806
18008650876379132344
7417108723176816402
13515164814425439399
3299769942258542105
15897528270699011770
11642805469843844638
16764682282380318054
4824039114054405772
4859834102876213962
1234210473247...

result:

ok 3000 lines

Test #4:

score: 5
Accepted
time: 32ms
memory: 43412kb

input:

3000 3000
16538965545220923528 18062192327708400751 10422465150728338588 3471522151129113073 1236650672072793692 1942240200040301168 13090729759591037952 15335798523677372669 9912100622761466753 11177948788405690381 3710859061697501523 4984944638666762977 17278589713462878008 6371292801024547050 868...

output:

8182453933067329108
13535217473847106938
17067385337010269798
3806121648880466130
11322569288575153037
11079197311131660121
9670138330007803226
6554062218199796758
965954569567598779
18055887214749050688
6142620503089407421
8690117812667761187
9547139298346295115
8890987597519353054
1755036654049586...

result:

ok 3000 lines

Test #5:

score: 5
Accepted
time: 33ms
memory: 43408kb

input:

3000 3000
17759588706587888497 10550000524636484378 11601004513528075994 7150322911283804521 4459707248078569712 10692395730842402625 8940418793863522991 12967068928670540447 9954278250450015940 13702413838608801301 10598390500439869870 15110245227553613794 490862872212325709 15164980555660957366 94...

output:

9743736929788175512
16812303667256960040
14694223512340829897
550204232580650311
1175342872438242313
17622261358285047637
7413682703975031220
12643066512274062227
1868985217436232595
5471830334855681322
8070132260376389587
3970361922096052085
218281824643752746
991917103472727104
2960248244218479023...

result:

ok 3000 lines

Subtask #2:

score: 0
Time Limit Exceeded

Dependency #1:

100%
Accepted

Test #6:

score: 12
Accepted
time: 4ms
memory: 44496kb

input:

5 7
0 3 2 6 4
1 2
2 4
1 5
5 3
3 4
4 2
2 5
5 1
5 3 0
3 2 5
4 4 4
4 4 3
5 2 0
3 4 3
5 5 6

output:

15
21
10
13
17
26
18

result:

ok 7 lines

Test #7:

score: 0
Time Limit Exceeded

input:

70000 70000
3805295436278888199 9842309351516174725 1566744796319231180 2206519284152256579 2715928675931950447 6346821976624501261 16020972671480798719 14702021753902144915 17127828773798978442 15779168055669690475 4964561323934614661 9395102787554964450 6377076753365184543 15167378195767668817 288...

output:


result:


Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

input:

200000 200000
622783158027686223 2242697872372232537 8481648430436878777 10092474834140799044 15403999682625301609 12614289513474949582 9180944589267018841 7823784919308285798 8257785171198951273 5134508521895120821 8041682272181381093 3835432206618893170 2653803171409877650 5589823419153460372 1007...

output:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #27:

score: 0
Time Limit Exceeded

input:

200000 200000
1958469220619413759 14991498002015735322 6054491201406941902 18206143187746582567 15082377615826460430 2936248617457291604 10073577150351675920 16534472678586906457 2207599132486246393 10301540360769075442 1492580560381080472 551692353431379140 13238280352539145808 8462626987240986565 ...

output:


result:


Subtask #6:

score: 0
Time Limit Exceeded

Test #34:

score: 0
Time Limit Exceeded

input:

200000 200000
6794776813641982926 1561596256197101737 10910039723053043515 7892247858295192798 12233819960547881004 17695389034783066733 9173201689566865598 17626618141377486739 7358781671024283919 6787559733384974662 3884392438269280436 14872846228351316833 9037842441501571648 14299818404271084016 ...

output:


result:


Subtask #7:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%