QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#441104#5094. 小 H 分糖果Untitled015 4092ms170304kbC++146.8kb2024-06-14 12:47:562024-06-14 12:47:56

Judging History

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

  • [2024-06-14 12:47:56]
  • 评测
  • 测评结果:15
  • 用时:4092ms
  • 内存:170304kb
  • [2024-06-14 12:47:56]
  • 提交

answer

#include<bits/stdc++.h>
#define endl '\n'
#define F first
#define S second
#define int ll
#define rep(i, s, e) for(int i = (s), i##E = (e); i <= i##E; ++i)
#define per(i, s, e) for(int i = (s), i##E = (e); i >= i##E; --i)
#define gmin(x, y) (x = min(x, y))
#define gmax(x, y) (x = max(x, y))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double f128;
typedef pair<int, int> pii;
#ifdef LOCAL_RUN
#define debug(fmt, ...) fprintf(stderr, "[%d] " fmt "\n", __LINE__, ##__VA_ARGS__)
#else
#define debug(...) 0
#endif

class fast_iostream {
private:
    const int MAXBF = 1 << 20; FILE *inf, *ouf;
    char *inbuf, *inst, *ined, *oubuf, *oust, *oued;
    inline void _flush() { fwrite(oubuf, 1, oued - oust, ouf); }
    inline char _getchar() {
        if(inst == ined) inst = inbuf, ined = inbuf + fread(inbuf, 1, MAXBF, inf);
        return inst == ined ? EOF : *inst++;
    }
    inline void _putchar(char c) {
        if(oued == oust + MAXBF) _flush(), oued = oubuf;
        *oued++ = c;
    }
public:
    fast_iostream(FILE *_inf = stdin, FILE *_ouf = stdout)
    :inbuf(new char[MAXBF]), inf(_inf), inst(inbuf), ined(inbuf),
     oubuf(new char[MAXBF]), ouf(_ouf), oust(oubuf), oued(oubuf) {}
    ~fast_iostream() { _flush(); delete[] inbuf; delete[] oubuf; }
    template<typename Int>
    fast_iostream& operator>>(Int& n) {
        static char c;
        Int flg = 1;
        while (!isdigit(c = _getchar()) && c != '-');
        if(c == '-') flg = -1, n = 0;
        else n = c - '0';
        while (isdigit(c = _getchar())) n = n * 10 + c - '0';
        n *= flg;
        return *this;
    }
    fast_iostream& operator>>(char *s) {
        static int c;
        while((c = _getchar()) == ' ' || c == '\n');
        *s++ = c;
        while((c = _getchar()) != ' ' && c != '\n' && c != EOF) *s++ = c;
        *s = 0;
        return *this;
    }
    template <typename Int>
    fast_iostream& operator<<(Int n) {
        if (n < 0) _putchar('-'), n = -n;
        static char S[40]; int t = 0;
        do { S[t++] = '0' + n % 10, n /= 10; } while(n);
        for (int i = 0; i < t; ++i) _putchar(S[t - i - 1]);
        return *this;
    }
    fast_iostream& operator<<(char c) { _putchar(c); return *this; }
    fast_iostream& operator<<(const char *s) {
        for(; *s; ++s) _putchar(*s);
        return *this;
    }
} fio;
#define cin fio
#define cout fio

constexpr int N = 1e5 + 5, B = 700;
int n, q, a[N];
vector<int> to[N];
int fa[N];

namespace LCA {
    int dfn[N], st[18][N];
    void dfs(int u, int f) {
        static int ind;
        st[0][dfn[u] = ++ind] = fa[u] = f;
        for(int v : to[u]) if(v != f) dfs(v, u);
    }
    int get(int x, int y) {
        return dfn[x] < dfn[y] ? x : y;
    }
    void build() {
        rep(j, 1, __lg(n)) rep(i, 1, n - (1 << j) + 1)
            st[j][i] = get(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]);
    }
    int lca(int u, int v) {
        if(u == v) return u;
        if((u = dfn[u]) > (v = dfn[v])) swap(u, v);
        int len = __lg(v - u++);
        return get(st[len][u], st[len][v - (1 << len) + 1]);
    }
    bool check(int u, int v, int x) {
        int uv = lca(u, v);
        if(x == uv) return 1;
        int ux = lca(u, x), vx = lca(v, x);
        return (ux == x && vx == uv) || (ux == uv && vx == x);
    }
}

struct node {
    ll sum;
    int cnt, ls, rs;
    __int128 ss;
} nd[N * 35];
__int128 ss;
inline int& ls(int p) { return nd[p].ls; }
inline int& rs(int p) { return nd[p].rs; }
int tot, rt[N];
void add(int &p, int cp, int l, int r, int pos) {
    // debug("add(%d, %d, %d, %d, %d)", p, cp, l, r, pos);
    nd[p = ++tot] = nd[cp];
    nd[p].sum += pos, nd[p].cnt += 1;
    nd[p].ss += 1ll * pos * pos;
    if(l == r) return;
    int mid = (l + r) >> 1;
    if(pos <= mid) add(ls(p), ls(cp), l, mid, pos);
    else add(rs(p), rs(cp), mid + 1, r, pos);
    // debug("nd[%d] = {sum = %lld, cnt = %d, ls = %d, rs = %d}", p, nd[p].sum, nd[p].cnt, nd[p].ls, nd[p].rs);
}

void rebuild(int u, int f) {
    // debug("rebuild(%d, %d)", u, f);
    add(rt[u], rt[f], 0, 1e9, a[u]);
    ss += 1ll * a[u] * a[u];
    for(int v : to[u]) if(v != f) rebuild(v, u);
}

tuple<ll, ll, __int128> query(int p1, int p2, int p3, int p4, int l, int r, ll m, ll c, vector<int> &vec) {
    if(l == r) return {l, m, nd[p1].ss + nd[p2].ss - nd[p3].ss - nd[p4].ss + (__int128)c * l * l};
    int mid = (l + r) >> 1;
    ll sum = nd[rs(p1)].sum + nd[rs(p2)].sum - nd[rs(p3)].sum - nd[rs(p4)].sum;
    ll cnt = nd[rs(p1)].cnt + nd[rs(p2)].cnt - nd[rs(p3)].cnt - nd[rs(p4)].cnt;
    vector<int> lv, rv;
    for(int v : vec) 
        if(abs(v) <= mid) lv.emplace_back(v);
        else rv.emplace_back(v), sum += v, cnt += v > 0 ? 1 : -1;
    ll tmp = sum - cnt * mid + c * (r - mid);
    if(tmp <= m) {
        return query(ls(p1), ls(p2), ls(p3), ls(p4), l, mid, m - tmp, c + cnt, lv);
    }
    else {
        auto o = query(rs(p1), rs(p2), rs(p3), rs(p4), mid + 1, r, m, c, rv);
        get<2>(o) += nd[ls(p1)].ss + nd[ls(p2)].ss - nd[ls(p3)].ss - nd[ls(p4)].ss;
        for(ll v : lv) 
            if(v > 0) get<2>(o) += v * v;
            else get<2>(o) -= v * v;
        return o;
    }
}

void mian() {
    cin >> n >> q;
    rep(i, 2, n) {
        int u, v; 
        cin >> u >> v;
        to[u].emplace_back(v);
        to[v].emplace_back(u);
    }
    rep(i, 1, n) cin >> a[i];
    LCA::dfs(1, 0);
    LCA::build();
    rebuild(1, 0);
    int cnt = 0;
    vector<pii> vec;
    rep(_, 1, q) {
        int op; cin >> op;
        if(op == 1) {
            int x, v; 
            cin >> x >> v;
            if(a[x]) vec.emplace_back(x, -a[x]), ss -= 1ll * a[x] * a[x];
            if(v) vec.emplace_back(x, a[x] = v), ss += 1ll * v * v;
            ++cnt;
        }
        else {
            int u, v; ll m;
            cin >> u >> v >> m;
            if(cnt >= B) {
                tot = 0;
                ss = 0;
                rebuild(1, 0);
                cnt = 0;
                vec.clear();
                cerr << _ << endl;
            }
            vector<int> tmp;
            for(auto [x, y] : vec) 
                if(LCA::check(u, v, x))
                    tmp.emplace_back(y);
            int lca = LCA::lca(u, v);
            auto [x, y, z] = query(rt[u], rt[v], rt[lca], rt[fa[lca]], 0, 1e9, m, 0, tmp);
            __int128 res = ss - (nd[rt[u]].ss + nd[rt[v]].ss - nd[rt[lca]].ss - nd[rt[fa[lca]]].ss);
            for(ll v : tmp) 
                if(v < 0) res += v * v;
                else res -= v * v;
            res += z;
            if(x) res -= (__int128)y * (x * x - (x - 1) * (x - 1));
            cout << res << endl;
        }
    }
}

signed main() {
    int T = 1; 
    while(T--) mian();
    return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 6ms
memory: 7892kb

input:

866 841
1 864
4 153
9 559
10 410
11 336
12 417
14 666
18 241
21 184
22 849
23 40
25 783
26 189
28 329
29 216
31 864
34 581
40 131
42 625
45 744
47 723
50 633
51 447
52 454
53 88
55 619
60 259
62 680
67 126
72 371
73 742
80 196
81 536
82 647
85 254
87 172
88 489
93 708
94 227
95 340
96 7
7 91
97 594
...

output:

285125508
285374449
285871392
285072359
284419704
284843737
284692039
284936099
285944374
285174668
285019779
284651455
287282253
287175619
284878507
285369672
284880507
285404741
284913527
286053317
288622563
286960150
287194443
288326074
286937403
287883097
288535226
288195055
288643208
288632989
...

result:

wrong answer 64th lines differ - expected: '288202498', found: '289059974'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 15
Accepted

Test #6:

score: 15
Accepted
time: 4092ms
memory: 154024kb

input:

87080 98363
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

27217464773998101198216
27222683135365131711066
27215685950441383375941
27221607244120669838311
27219047117137492446677
27222635053035794978138
27218848172360084265818
27217641965048032442370
27217075857038185043354
27219505943263517662069
27219987830714690994915
27216425553487126261338
272156845500...

result:

ok 49026 lines

Test #7:

score: 15
Accepted
time: 3864ms
memory: 150680kb

input:

84870 94829
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

26588824183614252611956
26590972957572618361173
26588072360121209836363
26591857847070561616545
26589232564845670209408
26592122980539987339833
26587754880131177015120
26589297513918827289821
26587809270957143483620
26589079923893149835091
26592765548568891098479
26589309295038049830001
265880100172...

result:

ok 47183 lines

Test #8:

score: 15
Accepted
time: 3721ms
memory: 170304kb

input:

96634 80387
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
20 21
21 22
22 23
23 24
24 25
25 26
26 27
27 28
28 29
29 30
30 31
31 32
32 33
33 34
34 35
35 36
36 37
37 38
38 39
39 40
40 41
41 42
42 43
43 44
44 45
45 46
46 47
47 48
48 49
49 50
50 51
51 52...

output:

30077825779968347451702
30074759816356090849186
30079514925011418879668
30076799133737016237726
30078990001739930674611
30080891961908067423095
30078486689897323991187
30075141829412423915885
30080001764736455079465
30073677391611280633894
30079391586644084223862
30080833751860317298636
300797099430...

result:

ok 40329 lines

Subtask #4:

score: 0
Skipped

Dependency #1:

0%