QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#376764#8133. When Anton Saw This Task He Reacted With 😩pandapythonerTL 3135ms21860kbC++177.0kb2024-04-04 16:25:132024-04-04 16:25:13

Judging History

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

  • [2024-04-04 16:25:13]
  • 评测
  • 测评结果:TL
  • 用时:3135ms
  • 内存:21860kb
  • [2024-04-04 16:25:13]
  • 提交

answer

#include <bits/stdc++.h>


using namespace std;


#define ll long long
#define flt double
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()


const ll inf = 1e18;
mt19937 rnd(234);
const ll mod = 998244353;


template<int x, int y>
using mat = array<array<int, y>, x>;

template<int x, int y, int z>
mat<x, z> mul(const mat<x, y>& a, const mat <y, z>& b) {
    mat<x, z> c;
    for (int i = 0; i < x; i += 1) {
        for (int k = 0; k < z; k += 1) {
            c[i][k] = 0;
            for (int j = 0; j < y; j += 1) {
                c[i][k] = (c[i][k] + ((ll)a[i][j]) * b[j][k]) % mod;
            }
        }
    }
    return c;
}


mat<3, 3> flex() {
    return { array<int, 3>{1, 0, 0},
             array<int, 3>{0, 1, 0},
             array<int, 3>{0, 0, 1} };
}


inline int neg(int a) {
    if (a == 0) {
        return 0;
    }
    return mod - a;
}


mat<3, 3> get_matrix(const mat<1, 3>& a, bool chipi_chipi_chapa_chapa = false) {
    int x = a[0][0], y = a[0][1], z = a[0][2];
    if (!chipi_chipi_chapa_chapa) {
        return { array<int, 3>{0, neg(z), y},
                 array<int, 3>{z, 0, neg(x)},
                 array<int, 3>{neg(y), x, 0} };
    }
    return { array<int, 3>{0, z, neg(y)},
             array<int, 3>{neg(z), 0, x},
             array<int, 3>{y, neg(x), 0} };
}


mat<1, 3> prod(const mat<1, 3>& a, const mat<1, 3>& b) {
    return mul<1, 3, 3>(a, get_matrix(b));
}


struct SGT {
    struct node {
        mat<3, 3> x;
        mat<3, 3> sum;
        int l = -1;
        int r = -1;
        int sz = 0;

        node() {}
        node(const mat<3, 3>& _x) {
            x = _x;
            sz = 1;
        }
    };


    int n;
    vector<node> t;
    int rt;


    void upd(int v) {
        int l = t[v].l;
        int r = t[v].r;
        t[v].sum = flex();
        t[v].sz = 1;
        if (r != -1) {
            t[v].sz += t[r].sz;
            t[v].sum = mul<3, 3, 3>(t[v].sum, t[r].x);
        }
        t[v].sum = mul<3, 3, 3>(t[v].sum, t[v].x);
        if (l != -1) {
            t[v].sz += t[l].sz;
            t[v].sum = mul<3, 3, 3>(t[v].sum, t[l].x);
        }
    }


    int build(int tl, int tr, const vector<mat<1, 3>>& a) {
        if (tl > tr) {
            return -1;
        }
        int tm = (tl + tr) / 2;
        int v = (int)t.size();
        t.push_back(node(get_matrix(a[tm])));
        t[v].l = build(tl, tm - 1, a);
        t[v].r = build(tm + 1, tr, a);
        upd(v);
        return v;
    }


    void build(const vector<mat<1, 3>>& a) {
        n = (int)a.size();
        t.clear();
        rt = build(0, n - 1, a);
    }


    void chng(int v, int i, const mat<1, 3>& x) {
        int l = t[v].l;
        int lsz = l == -1 ? 0 : t[l].sz;
        if (i < lsz) {
            chng(l, i, x);
            upd(v);
            return;
        }
        if (i == lsz) {
            t[v].x = get_matrix(x);
            upd(v);
            return;
        }
        int r = t[v].r;
        chng(r, i - lsz - 1, x);
        upd(v);
    }


    void chng(int i, const mat<1, 3>& x) {
        chng(rt, i, x);
    }


    mat<3, 3> get() {
        if (rt == -1) {
            return flex();
        }
        return t[rt].sum;
    }
};



struct query {
    int v;
    int x, y, z;
};


int n, q;
vector<vector<int>> g;
vector<mat<1, 3>> val;
int c;
vector<query> biba;
vector<int> sz;
bool do_neg;
vector<int> clr;


void build_dfs(int v) {
    sz[v] = 1;
    for (auto to : g[v]) {
        build_dfs(to);
        sz[v] += sz[to];
    }
    if ((int)g[v].size() == 0) {
        return;
    }
    assert((int)g[v].size() == 2);
    int& l = g[v][0];
    int& r = g[v][1];
    if (sz[l] < sz[r]) {
        swap(l, r);
        do_neg = !do_neg;
    }
}


void clr_dfs(int v, int c, int bnd) {
    clr[v] = c;
    for (auto to : g[v]) {
        if (to == bnd) {
            continue;
        }
        clr_dfs(to, c, bnd);
    }
}


vector<mat<1, 3>> hld_dfs(int v, const vector<query>& a) {
    int m = (int)a.size();
    int u = v;
    vector<int> way;
    while ((int)g[u].size() != 0) {
        way.push_back(u);
        u = g[u][0];
    }
    way.push_back(u);
    int k = (int)way.size();
    for (int i = 0; i < k - 1; i += 1) {
        clr_dfs(way[i], i, way[i + 1]);
    }
    clr[u] = k - 1;
    vector<vector<query>> qrs(k);
    for (auto& f : a) {
        int i = clr[f.v];
        qrs[i].push_back(f);
    }
    vector<vector<mat<1, 3>>> vals(k);
    for (int i = 0; i < k; i += 1) {
        if (i < k - 1) {
            vals[i] = hld_dfs(g[way[i]][1], qrs[i]);
        } else {
            vals[i].resize((int)qrs[i].size() + 1);
            vals[i][0] = val[way[i]];
            for (int j = 0; j < (int)qrs[i].size(); j += 1) {
                vals[i][j + 1] = { array<int, 3>{qrs[i][j].x, qrs[i][j].y, qrs[i][j].z} };
            }
        }
    }
    vector<mat<1, 3>> sgt_init(k - 1);
    SGT sgt;
    for (int i = 0; i < k - 1; i += 1) {
        sgt_init[i] = vals[i][0];
    }
    sgt.build(sgt_init);
    for (int i = 0; i < k - 1; i += 1) {
        clr_dfs(way[i], i, way[i + 1]);
    }
    clr[u] = k - 1;
    vector<int> pntr(k);
    vector<mat<1, 3>> rs(m + 1);
    for (int i = 0; i <= m; i += 1) {
        // rs[i] = mul<1, 3, 3>(vals[k - 1][pntr[k - 1]], sgt.get());
        
        auto v = vals[k - 1][pntr[k - 1]];
        for (int j = k - 2; j >= 0; j -= 1) {
            v = mul<1, 3, 3>(v, get_matrix(vals[j][pntr[j]]));
        }
        rs[i] = v;
        
        if (i < m) {
            int v = a[i].v;
            int pos = clr[v];
            pntr[pos] += 1;
            if (pos < k - 1) {
                sgt.chng(pos, vals[pos][pntr[pos]]);
            }
        }
    }
    return rs;
}


int32_t main() {
    if (1) {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    }
    cin >> n >> q;
    g.assign(n, vector<int>());
    val.resize(n);
    for (int i = 0; i < n; i += 1) {
        char c;
        cin >> c;
        if (c == 'x') {
            int l, r;
            cin >> l >> r;
            --l;
            --r;
            g[i] = { l, r };
        } else {
            int x, y, z;
            cin >> x >> y >> z;
            val[i] = { array<int, 3>{x, y, z} };
        }
    }
    c = 0;
    biba.resize(q);
    for (auto& [v, x, y, z] : biba) {
        cin >> v >> x >> y >> z;
        --v;
    }
    do_neg = false;
    sz.resize(n);
    build_dfs(0);
    clr.resize(n);
    auto rs = hld_dfs(0, biba);
    assert((int)rs.size() == q + 1);
    for (int i = 1; i <= q; i += 1) {
        int x = rs[i][0][0], y = rs[i][0][1], z = rs[i][0][2];
        if (do_neg) {
            x = neg(x);
            y = neg(y);
            z = neg(z);
        }
        cout << x << " " << y << " " << z << "\n";
    }
    return 0;
}

/*
5 3
x 2 3
v 1 0 1
x 4 5
v 0 2 1
v 1 1 1
4 1 2 3
5 0 1 1
4 0 2 2


*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3640kb

input:

5 3
x 2 3
v 1 0 1
x 4 5
v 0 2 1
v 1 1 1
4 1 2 3
5 0 1 1
4 0 2 2

output:

998244351 0 2
1 998244351 998244352
0 0 0

result:

ok 9 numbers

Test #2:

score: 0
Accepted
time: 400ms
memory: 21232kb

input:

199999 100000
x 137025 65661
v 572518668 158967010 74946561
x 129836 192657
x 141948 187810
v 574918069 328924434 141474729
x 143312 111002
x 52772 148497
v 922857701 690080961 651915759
v 656198340 28002884 129579416
v 639893144 265359784 646791226
v 796871409 411409966 598676495
v 882562617 224394...

output:

393120558 773766615 387297348
759959566 981774500 128012497
294611811 980011608 533642029
404379574 745296852 53493560
404565501 828869760 78021156
592494858 647751304 881427733
190018467 515243135 518180555
628180500 509984554 257430135
13737245 352087791 917410487
932051309 366591227 479931477
199...

result:

ok 300000 numbers

Test #3:

score: 0
Accepted
time: 438ms
memory: 21180kb

input:

199999 100000
x 154525 80092
v 450407262 725458410 590777520
x 24720 135901
v 719242117 114943104 186796011
v 382530926 89156744 943939337
x 183376 26042
x 109984 157873
x 151637 150600
x 4115 27454
x 163135 92648
x 16764 33212
v 849210403 945083972 689295397
v 471196117 68587428 225597765
x 24643 5...

output:

677067461 996514296 449166851
810403092 258196842 853459733
410756156 253348518 912664471
327134890 519245783 922528759
317367558 536888537 506214109
484753530 879045782 772404948
422920052 152084658 517340457
1207902 348787162 320821077
776293878 699474926 711114530
871858473 468497588 822120121
24...

result:

ok 300000 numbers

Test #4:

score: 0
Accepted
time: 636ms
memory: 20960kb

input:

199999 100000
x 72889 193806
x 35339 33069
v 314802407 406980523 492377265
x 108307 60027
x 144922 140917
v 328481079 117663280 644171354
v 482028404 951751561 166221217
v 936461869 436114879 421819757
x 152919 99965
x 61168 150607
v 56403640 743462679 134896557
v 24809217 462947115 966139991
v 7828...

output:

23709876 380448367 629159667
760678420 539369190 611778104
114926687 653692915 939877414
674199470 304745735 544123803
953800112 186017361 49200537
327282782 871001677 293980713
588783157 502130649 190564297
102680906 993889016 963478755
510012481 105416897 281770975
811082806 367139818 493674194
32...

result:

ok 300000 numbers

Test #5:

score: 0
Accepted
time: 1227ms
memory: 21504kb

input:

199999 100000
x 134204 79203
v 152855933 152545887 271660214
v 393332175 182708769 115884220
v 731792305 965028257 676889584
x 89631 14495
x 142016 85686
v 600051847 172947969 906920949
v 846126215 214556203 657929902
x 125721 133526
x 93179 35713
v 330716449 450417250 611411649
v 114397688 58644961...

output:

139597616 375474977 14619793
889328460 79727434 363703631
397351102 877961602 429046190
588368345 819425899 502148739
520578911 186408072 484373545
997888597 816534316 338411279
334166269 288211584 608252640
509280845 362972392 286415695
363396960 878881251 3658455
711270872 94816531 100279034
48844...

result:

ok 300000 numbers

Test #6:

score: 0
Accepted
time: 3135ms
memory: 21860kb

input:

199999 100000
x 29842 60343
x 22382 27649
v 148997533 411153785 529195552
v 831453378 856711025 439562917
x 183061 152107
v 208562249 845550020 248974502
x 8708 152913
v 901332053 480035531 424365358
v 120556946 620074725 884675784
v 493886564 455460926 851190410
x 63346 64739
x 35246 36255
v 762936...

output:

236797322 190218414 70559261
661765898 266356472 481630021
410967670 613729303 804008156
92638320 37926778 82924205
357869883 232766711 579608532
691702082 124868602 187367212
237610689 608489848 581104410
848616732 907873139 859807891
614624615 454674844 673629667
485784731 743926138 168595096
1826...

result:

ok 300000 numbers

Test #7:

score: -100
Time Limit Exceeded

input:

199999 100000
x 179471 175117
x 189060 178495
x 20142 58065
x 22916 150184
v 704047412 186112247 660817663
v 761554808 199099716 794321264
v 362925435 508140595 251556541
v 65674025 717152823 157775106
v 325965317 977108704 50644678
v 566946741 833186394 771714200
v 996708965 76780827 625429369
v 85...

output:


result: