QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#295728#6622. Matrix OperationsNOI_AK_MEAC ✓2691ms51968kbC++239.3kb2023-12-31 20:07:362023-12-31 20:07:36

Judging History

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

  • [2023-12-31 20:07:36]
  • 评测
  • 测评结果:AC
  • 用时:2691ms
  • 内存:51968kb
  • [2023-12-31 20:07:36]
  • 提交

answer

#pragma once

#include <assert.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>

#define F(i, l, r) for (int i = l; i ^ r; ++ i)
#define Fe(i, l, r) for (int i = l; i <= r; ++ i)
#define SC(a, b) static_cast <a> (b)

using std::max;

constexpr unsigned MAXBUF = 33554432;

char buf[MAXBUF], buf_t[22], * pout = buf;
const char *pin = buf;

inline unsigned read() {
    for (; *pin < '0'; ++pin)
        ;

    unsigned ans = *pin ^ '0';

    for (; * (++pin) >= '0'; ans = ans * 10 + *pin - '0')
        ;

    return ans;
}

inline void WriteUE(unsigned long long v) {
    char l = 0;

    for (; v > 9; buf_t[l++] = v % 10, v /= 10)
        ;

    for (*pout = SC(char, v) ^ '0'; l; * (++pout) = buf_t[--l] ^ '0')
        ;

    *(pout + 1) = '\n', pout += 2;
    return;
}

constexpr unsigned maxm = 5e5, M = maxm + 10, maxv = 1e9, MEM = 1 << 26;
using i64 = long long;

char pool[MEM], *pool_p = pool;

template<class T>
inline void alloc(T *&p, int sz) {
    p = reinterpret_cast<T *>(pool_p);
    pool_p += sz * sizeof(T);
    assert(pool_p < pool + MEM);
    F(i, 0, sz)new (p + i)T();
}

template<class T>
struct Undo {
    T &x;
    T x0;
    Undo(T &x): x(x), x0(x) {}
    ~Undo() {
        x = x0;
    }
};

#define alloc_scope Undo<char*> _(pool_p)

template<class T>
struct Inf {};

template<>
struct Inf<i64> {
    static constexpr i64 value = 1LL << 40;
};

template<class T>
struct Max {
    T x;
    Max(T x = -Inf<T>::value): x(x) {}
    Max <T> operator + (const Max <T> &w) const { return max(x, w.x); }
    inline void operator += (const Max <T> &w) { x = max(x, w.x); }
};

template<class T>
struct AddOp {
    T x;
    AddOp(T x = T()): x(x) {}
    inline void operator *= (const AddOp <T> &w) { x += w.x; }
    friend inline void operator *= (Max <T> &x, const AddOp <T> &y) { x.x += y.x; }
};

template<class D, class M>
struct MSegTree {
    struct Node {
        D d; M m;
        constexpr inline void app(const M &t) { d *= t, m *= t; }
        constexpr inline void up(const Node &a, const Node &b) { d = a.d + b.d, d *= m; }
    } *tr;
    int mx;
    constexpr inline void alloc(int n) {
        for (mx = 1; mx < n + 2; mx <<= 1);
        ::alloc(tr, mx * 2);
    }
    constexpr inline Node &at(int x) { return tr[mx + x]; }
    D &operator[] (int x) { return tr[mx + x].d; }
    inline void range_update(int l, int r) {
        l += mx, r += mx;
        for (l >>= 1, r >>= 1; l < r; l >>= 1, r >>= 1) for (int x = l; x <= r; ++ x) up(x);
        for (; l; l >>= 1) up(l);
    }
    constexpr inline void up(int x) { tr[x].up(tr[x * 2], tr[x * 2 + 1]); }
    constexpr inline void update(const M &t) { tr[1].app(t); }
    constexpr inline void update_prefix(int x, const M &t) { for (x += mx + 1; x > 1; x >>= 1, up(x)) if (x & 1) tr[x - 1].app(t); }
    constexpr inline D query() { return tr[1].d; }
    constexpr inline void dn(int x) { tr[x * 2].app(tr[x].m), tr[x * 2 + 1].app(tr[x].m), tr[x].m = M(); }
    constexpr inline void dna(int x) {
        x += mx;
        for (unsigned c = __builtin_ctz(mx); c; --c) dn(x >> c);
    }
    void dna(int l, int r) {
        l += mx, r += mx;

        for (int c = __builtin_ctz(mx); c > 0; --c) {
            int L = l >> c, R = r >> c;
            Fe(i, L, R)dn(i);
        }
    }
};

int m;
struct Op {
    int x, y, id, a[2][2];
    void R(int i) {
        x = read(), y = read(), id = i;
        F(i, 0, 2)F(j, 0, 2)a[i][j] = read();
    }
} qs[M], qs2[M];

template<class T>
struct Array {
    T *v;
    int sz;
    Array() {}
    Array(int n) { alloc(n); }
    inline void alloc(int n) { sz = n, ::alloc(v, n); }
    T &operator[](int x) {
        return v[x];
    }
};

template<class T>
struct Grid {
    T *v;
    int xsz, ysz;
    Grid() {}
    Grid(int xn, int yn) {
        alloc(xn, yn);
    }
    void alloc(int xn, int yn) {
        xsz = xn;
        ysz = yn;
        ::alloc(v, xn * yn);
    }
    T &operator()(int x, int y) {
        return v[x * ysz + y];
    }
};

struct Des {
    int sz, x[M], id[M];
    void clr() {
        sz = 0;
    }
    void ins(int a) {
        x[sz++] = a;
    }
    void build() {
        ins(1), ins(m + 1);
        std::sort(x, x + sz);
        sz = std::unique(x, x + sz) - x;
        F(i, 1, sz)F(j, x[i - 1], x[i])id[j] = i;
    }
} xs, ys;

struct NoD {
    char _[0];
    template<class T>
    void operator*=(const T &x) {}
    friend NoD operator+(NoD a, NoD b) {
        return NoD();
    }
};

template<class T>
struct HAddOp {
    T h, a, a1;
    bool commit;
    HAddOp(T x = 0): a1(x), commit(0) {}
    void operator*=(const HAddOp<T> &w) {
        if (w.commit) {
            if (commit) {
                h = max(h, a + a1 + w.h);
                a += a1 + w.a;
                a1 = w.a1;
            } else {
                h = a1 + w.h;
                a = a1 + w.a;
                a1 = w.a1;
                commit = 1;
            }
        } else {
            a1 += w.a1;
        }
    }
};

MSegTree<Max<i64>, AddOp<i64>> tr[M];
MSegTree<NoD, HAddOp<i64>> tr0;

struct DC {
    Array<int> x, y;
    Grid<MSegTree<Max<i64>, AddOp<i64>>::Node> g;
    void set_sz(int sz) {
        x.alloc(sz);
        y.alloc(sz);
    }
    void run(int m1) {
        int sz = x.sz;

        if (sz == 1) {
            F(i, 0, 2)F(j, 0, 2) {
                WriteUE(g(i, j).d.x);
                g(i, j).app(qs[m1].a[i][j]);
            }
            return;
        }

        int o = 0;
        F(I, 0, 2) {
            alloc_scope;
            int sz1 = (I ? sz - sz / 2 : sz / 2);
            DC dc;
            dc.set_sz(sz1);
            Array<int> xe(g.xsz), ye(g.ysz);
            F(i, 0, sz1) {
                xe[x[o + i]] = 1;
                ye[y[o + i]] = 1;
            }
            xe[0] = ye[0] = 0;
            F(i, 1, xe.sz)xe[i] += xe[i - 1];
            F(i, 1, ye.sz)ye[i] += ye[i - 1];
            F(i, 0, sz1) {
                dc.x[i] = xe[x[o + i]];
                dc.y[i] = ye[y[o + i]];
            }
            dc.g.alloc(xe[xe.sz - 1] + 1, ye[ye.sz - 1] + 1);
            F(i, 0, g.xsz)F(j, 0, g.ysz) {
                dc.g(xe[i], ye[j]).d += g(i, j).d;
            }
            dc.run(m1 + o);

            if (!I)
                F(i, 0, g.xsz)F(j, 0, g.ysz) {
                auto v = dc.g(xe[i], ye[j]).m;
                g(i, j).d *= v;
                g(i, j).m = v;
            }

            if (I)
                F(i, 0, g.xsz)F(j, 0, g.ysz) {
                g(i, j).m *= dc.g(xe[i], ye[j]).m;
            }

            o += sz1;
        }
    }
};

i64 v0[M], v1[M];
void cal(int m1, int m2) {
    alloc_scope;

    xs.clr(), ys.clr();
    F(i, m1, m2) {
        Op &q = qs[i];
        xs.ins(q.x);
        ys.ins(q.y);
    }
    xs.build();
    ys.build();

    Grid<i64> g(xs.sz, ys.sz);
    F(i, 1, ys.sz) {
        int L = ys.x[i - 1], R = ys.x[i];
        tr[i].alloc(R - L);
        F(j, L, R)tr[i][j - L + 1] = v0[j];
        tr[i].range_update(1, R - L);
        g(0, i) = tr[i].query().x;
    }
    tr0.alloc(ys.sz);
    F(i, 1, ys.sz)tr0.at(i).m = 0;

    int p = 0;
    F(i, 1, xs.sz) {
        int L = xs.x[i - 1], R = xs.x[i];
        F(x, L, R) {
            for (; p < m && qs2[p].x == x; ++p) {
                Op &q = qs2[p];

                if (q.id >= m1)
                    continue;

                int a1 = q.a[1][0] - q.a[0][0];
                int a2 = q.a[1][1] - q.a[0][1];
                a1 -= a2;
                int w = ys.id[q.y], u = q.y + 1 - ys.x[w - 1];

                i64 a = tr[w].query().x;
                tr[w].update_prefix(u - 1, a1);
                a = tr[w].query().x - a;

                tr0.dna(w);
                auto &w0 = tr0.at(w);
                w0.app(a);

                tr0.update_prefix(w - 1, a1);
                tr0.update(a2);
            }

            HAddOp<i64> m;
            m.h = m.a = m.a1 = 0;
            m.commit = 1;
            tr0.update(m);
        }
        tr0.dna(1, ys.sz);
        F(w, 1, ys.sz) {
            auto &w0 = tr0.at(w).m;
            g(i, w) = g(0, w) + w0.h;
            g(0, w) += w0.a;
            w0 = 0;
        }
    }

    int m0 = m2 - m1;
    DC dc;
    dc.set_sz(m0);
    dc.g.alloc(xs.sz - 1, ys.sz - 1);
    F(i, 1, xs.sz)F(j, 1, ys.sz)dc.g(i - 1, j - 1).d = g(i, j);
    F(i, 0, m0) {
        Op &q = qs[m1 + i];
        dc.x[i] = xs.id[q.x] - 1;
        dc.y[i] = ys.id[q.y] - 1;
    }

    dc.run(m1);

    Fe(i, 1, m)v1[i] = 0;
    F(i, m1, m2) {
        Op &q = qs[i];
        v1[1] += q.a[0][0];
        v1[q.y] += q.a[0][1] - q.a[0][0];
    }
    v0[1] += v1[1];
    Fe(i, 2, m)
        v1[i] += v1[i - 1], v0[i] += v1[i];
}

int main() {
    std::ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr), std::cin.read(buf, MAXBUF), m = read();
    F(i, 0, m)qs[i].R(i);
    F(i, 0, m)qs2[i] = qs[i];
    std::sort(qs2, qs2 + m, [](const Op & a, const Op & b) {
        return a.x < b.x;
    });
    int B = 2 * __builtin_sqrt(m);

    for (int l = 0, r; l < m; l = r)
        r = std::min(l + B, m), cal(l, r);

    std::cout.write(buf, pout - buf);
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
3 3 1 2 3 4
2 3 1 2 3 4
3 2 1 2 3 4

output:

0
0
0
0
1
2
3
4
4
6
6
8

result:

ok 12 numbers

Test #2:

score: 0
Accepted
time: 0ms
memory: 22208kb

input:

2
2 2 1 2 3 4
2 2 1 2 3 4

output:

0
0
0
0
1
2
3
4

result:

ok 8 numbers

Test #3:

score: 0
Accepted
time: 2688ms
memory: 48324kb

input:

100000
67093 67218 893385104 594359125 794096502 23520956
53562 24013 976872478 541348082 931311381 141786527
29629 88684 553269646 897332584 336945056 89731521
88573 10698 190998101 506204664 975770856 122636072
15715 14172 671616417 475691325 38676110 345737808
64855 6417 14521996 940010000 630209...

output:

0
0
0
0
893385104
893385104
893385104
893385104
1870257582
1135707207
1870257582
1135707207
2423527228
2423527228
2062352939
2062352939
2929731892
2929731892
3038123795
2929731892
3286141746
3601348309
3076799905
3076799905
4541358309
3954945780
3999155110
3017391200
4917226953
4330814424
4591348344...

result:

ok 400000 numbers

Test #4:

score: 0
Accepted
time: 2674ms
memory: 46140kb

input:

100000
94251 11095 3 10 7 10
19129 59966 10 10 2 9
4815 14453 4 10 7 10
53788 13834 3 6 10 6
5067 4245 3 3 3 1
83891 60080 10 3 10 7
33623 25386 9 4 2 7
77196 76835 2 9 4 4
79916 60892 2 1 1 10
26265 62655 5 3 9 10
32544 28473 2 3 6 6
6483 16091 2 5 10 1
41896 17639 8 10 5 9
18799 30980 3 3 9 5
4304...

output:

0
0
0
0
10
10
10
10
20
20
20
20
27
30
19
29
23
36
26
36
39
39
36
36
49
49
40
46
58
46
53
50
60
55
57
54
62
56
64
64
67
63
61
74
69
69
67
80
77
74
73
81
85
80
85
90
94
88
95
95
97
85
99
101
91
106
92
106
105
93
112
114
110
99
110
122
113
103
109
130
117
122
114
135
118
113
126
141
148
148
148
148
138...

result:

ok 400000 numbers

Test #5:

score: 0
Accepted
time: 2691ms
memory: 51968kb

input:

100000
60755 50586 100 5 89 85
35792 26506 86 19 62 20
90875 97416 3 60 82 32
80171 65918 27 63 1 81
76860 50498 22 94 8 33
89259 5644 88 2 11 94
18318 6989 77 41 39 4
34934 89397 82 79 98 61
94658 27721 92 69 81 85
47558 66902 62 55 50 4
86719 5058 80 82 8 22
7817 18755 42 81 60 82
83907 5676 26 61...

output:

0
0
0
0
100
100
100
100
186
105
151
105
189
165
233
187
216
228
234
268
238
322
242
301
326
245
336
395
403
284
399
399
485
497
473
497
577
432
582
582
639
639
532
632
721
599
721
654
781
695
697
736
807
724
754
793
854
791
854
889
948
897
826
989
990
900
884
1031
992
930
1103
1057
1081
1031
931
112...

result:

ok 400000 numbers

Test #6:

score: 0
Accepted
time: 2678ms
memory: 51772kb

input:

100000
3086 78800 776 408 563 495
63545 88235 343 745 650 179
27627 36628 389 881 474 194
43979 8840 795 103 143 288
77140 31283 197 123 923 355
79504 15059 412 231 600 641
30733 75949 668 877 535 908
94229 72291 578 827 841 486
45740 80842 761 731 356 433
88486 81800 526 710 298 313
72484 94899 961...

output:

0
0
0
0
776
495
563
495
1119
1240
1213
1240
1508
2121
1687
1687
2303
2224
1975
1975
2898
2898
2898
2898
2912
2578
3539
2691
4074
3599
4074
3599
4161
4282
4915
4358
5008
5013
5271
4791
5723
5723
5569
5030
6406
6702
6278
6702
7211
7632
5788
6509
7830
8008
6677
7001
8760
8150
7034
7356
9051
8895
8252
8...

result:

ok 400000 numbers

Test #7:

score: 0
Accepted
time: 2680ms
memory: 51856kb

input:

100000
62040 83261 2958 8712 7230 6625
30084 70182 9360 2727 5423 5170
31084 35169 2419 9194 1946 9942
96063 16206 692 2218 5879 9116
43887 22960 710 8703 759 3019
25068 97425 105 6123 8494 6356
22583 55519 4478 2646 7457 2437
6792 36282 4585 8874 2930 9024
2323 41391 4309 6496 8138 3514
1951 76274 ...

output:

0
0
0
0
2958
8712
7230
8712
12318
13882
12653
13882
14737
23824
14599
22595
16955
26042
23715
31711
32433
31554
34745
34745
32538
37677
43224
43239
37016
40323
50681
50681
45890
49197
59705
59705
52386
55693
67843
58214
70592
64420
72889
64420
77946
73912
79950
73619
85812
83230
84158
82962
90882
90...

result:

ok 400000 numbers

Test #8:

score: 0
Accepted
time: 2687ms
memory: 48056kb

input:

100000
84470 85085 147362 980341 958154 156999
73761 83182 360469 498984 981282 374212
67806 16961 598545 844749 37055 304069
26887 73428 739243 841288 750814 524579
95855 49502 552019 704354 506941 149796
7633 91069 309127 580507 212360 880388
59838 2239 144887 468272 72198 218373
63922 66934 12178...

output:

0
0
0
0
147362
980341
958154
980341
507831
1479325
1939436
1939436
1352580
2324074
2243505
2324074
2994319
3165362
2994319
2994319
3869716
3869716
3869716
3869716
2706765
4750104
3491684
4433395
3573576
5218376
4129406
4651768
4795513
5366774
4958007
5480369
5110778
5922926
5110778
4372134
4280590
6...

result:

ok 400000 numbers

Test #9:

score: 0
Accepted
time: 2685ms
memory: 50244kb

input:

100000
45722 79381 980235896 498158919 928430171 216003120
73319 84501 239257297 897053668 294741177 38297442
20309 25513 609468974 854148233 314532768 738191552
3097 96949 920471827 380037059 356271729 749175328
91655 11282 953758996 896570759 521930645 241373645
86115 91272 978228853 97510952 1737...

output:

0
0
0
0
980235896
498158919
928430171
216003120
1219493193
1395212587
1223171348
1395212587
2249360820
2249360820
2249360820
2249360820
2749433994
3169832647
1893975845
2317634629
4066403406
4066403406
3214205388
2638238201
4681421843
5044632259
2589678225
2732780009
4820757272
5545112923
3965299123...

result:

ok 400000 numbers