QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#441065#5094. 小 H 分糖果JerryTcl15 1035ms89064kbC++145.4kb2024-06-14 12:03:552024-06-14 12:03:56

Judging History

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

  • [2024-06-14 12:03:56]
  • 评测
  • 测评结果:15
  • 用时:1035ms
  • 内存:89064kb
  • [2024-06-14 12:03:55]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, S = N * 64;
vector<int> G[N]; int dft = 0;
int id[N], sz[N], dep[N], fa[N][17];
__int128 sumall[N * 16], sumallnw;
void Dfs(int x) {
    id[x] = ++dft, sz[x] = 1;
    dep[x] = dep[fa[x][0]] + 1;
    for(int i = 0; i < 16; ++i)
        fa[x][i + 1] = fa[fa[x][i]][i];
    for(const int y : G[x]) if(fa[x][0] != y)
        fa[y][0] = x, Dfs(y), sz[x] += sz[y];
};
int Lca(int x, int y) {
    if(dep[x] < dep[y]) swap(x, y);
    for(int i = 16; i >= 0; --i)
        if(dep[fa[x][i]] >= dep[y]) x = fa[x][i];
    if(x == y) return x;
    for(int i = 16; i >= 0; --i)
        if(fa[x][i] != fa[y][i])
            x = fa[x][i], y = fa[y][i];
    return fa[x][0];
}
vector<tuple<int, int, long long>> qry;
struct Node { int c; long long s; };
void operator += (Node &a, const Node &b) { a.c += b.c, a.s += b.s; }
Node operator * (const Node &a, int w) { return {a.c * w, a.s * w}; }
struct Tree {
    int n, h; vector<__int128> s2;
    vector<int> v; vector<Node> t;
    void Build() {
        sort(v.begin(), v.end());
        v.erase(unique(v.begin(), v.end()), v.end());
        v.shrink_to_fit();
        n = v.size(), h = __lg(n);
        t.resize(n + 1), s2.resize(n + 1);
    }
    int Id(int V) {
        return lower_bound(v.begin(), v.end(), V) - v.begin() + 1;
    }
    void Add(int p, Node nd, long long o) {
        for(int x = n - p + 1; x <= n; x += x & -x) t[x] += nd;
        for(int x = n - p + 1; x <= n; x += x & -x) s2[x] += o;
    }
    Node Bound0(int val) {
        int pos = 0; Node ret = {0, 0};
        for(int i = 1 << h; i; i >>= 1)
            if(pos + i <= n && v[n - pos - i] >= val)
                ret += t[pos += i];
        return ret;
    }
    __int128 Bound1(int val) {
        int pos = 0; __int128 ret = 0;
        for(int i = 1 << h; i; i >>= 1)
            if(pos + i <= n && v[n - pos - i] >= val)
                ret += s2[pos += i];
        return ret;
    }
} t[N];
int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n, q; cin >> n >> q;
    for(int i = 1; i < n; ++i) {
        int u, v; cin >> u >> v;
        G[u].push_back(v), G[v].push_back(u);
    }
    vector<int> a(n + 1); Dfs(1);
    for(int i = 1; i <= n; ++i) {
        cin >> a[i], qry.emplace_back(0, id[i], a[i]);
        if(id[i] + sz[i] <= n)
            qry.emplace_back(0, id[i] + sz[i], -a[i]);
        sumallnw += 1ll * a[i] * a[i];
    }
    for(int i = 1; i <= q; ++i) {
        int op, u, v; long long m; cin >> op;
        if(op == 1) {
            cin >> u >> m;
            sumallnw -= 1ll * a[u] * a[u];
            sumallnw += 1ll * m * m;
            qry.emplace_back(0, id[u], -a[u]);
            qry.emplace_back(0, id[u], m);
            if(id[u] + sz[u] <= n)
                qry.emplace_back(0, id[u] + sz[u], a[i]),
                qry.emplace_back(0, id[u] + sz[u], -m);
            a[u] = m;
        } else {
            cin >> u >> v >> m;
            sumall[qry.size()] = sumallnw;
            qry.emplace_back(u, v, m);
        }
    }
    cerr << "push qry" << endl;
    for(const auto &[u, v, w] : qry)
        if(!u) for(int x = v; x <= n; )
            t[x].v.push_back(abs(w)), x += x & -x;
    for(int i = 1; i <= n; ++i) t[i].Build();
    cerr << "build" << endl; int nwpos = -1;
    for(const auto &[u, v, w] : qry) {
        ++nwpos;
        if(!u) for(int x = v; x <= n; x += x & -x) {
            const int p = t[x].Id(abs(w));
            const int g = w > 0 ? 1 : -1;
            t[x].Add(p, {g, w}, 1ll * g * w * w);
        } else {
            const int p = Lca(u, v), q = fa[p][0];
            unordered_map<int, int> M;
            for(int x = id[u]; x; x -= x & -x) ++M[x];
            for(int x = id[v]; x; x -= x & -x) ++M[x];
            for(int x = id[p]; x; x -= x & -x) --M[x];
            for(int x = id[q]; x; x -= x & -x) --M[x];
            vector<pair<int, int>> v;
            for(const auto &[x, y] : M)
                if(y) v.emplace_back(x, y);
            int L = 1, R = 1e9, A = -1;
            // cnt a >= m - (sum a - cnt a * v) >= 0
            Node ret = {0, 0};
            for(; L <= R; ret = {0, 0}) {
                const int M = (L + R) >> 1;
                for(const auto &[x, y] : v)
                    ret += t[x].Bound0(M) * y;
                long long W = w - (ret.s - 1ll * ret.c * M);
                if(W < 0) L = M + 1;
                else if(W > ret.c) R = M - 1;
                else { A = M; break; }
            }
            const long long B = 1e16l;
            if(A == -1) {
                __int128 ans = sumall[nwpos];
                for(const auto &[x, y] : v)
                    ans -= t[x].Bound1(0) * y;
                if(ans >= B)
                    printf("%lld%016lld\n",
                        (long long)(ans / B), (long long)(ans % B));
                else printf("%lld\n", (long long)ans);
                continue;
            }
            long long W = w - (ret.s - 1ll * ret.c * A);
            __int128 ans = (__int128)(ret.c - W) * A * A;
            ans += (__int128)W * (A - 1) * (A - 1);
            for(const auto &[x, y] : v)
                ans -= t[x].Bound1(A) * y;
            ans += sumall[nwpos];
            if(ans >= B)
                printf("%lld%016lld\n",
                    (long long)(ans / B), (long long)(ans % B));
            else printf("%lld\n", (long long)ans);
        }
    }
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 16696kb

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
285080815
284380280
284843737
284628559
284818093
285937088
285124688
284791721
284596741
287272653
287157815
284813695
285347352
284733895
285378471
284840479
286052259
288622497
286899116
287222947
288275264
286877013
287851609
288627346
288242687
288632456
288471723
...

result:

wrong answer 4th lines differ - expected: '285072359', found: '285080815'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 15
Accepted

Test #6:

score: 15
Accepted
time: 1035ms
memory: 88052kb

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: 0
Accepted
time: 965ms
memory: 86864kb

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: 0
Accepted
time: 882ms
memory: 89064kb

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%