QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#302223 | #6622. Matrix Operations | NOI_AK_ME | WA | 3265ms | 38508kb | C++23 | 9.1kb | 2024-01-10 17:44:51 | 2024-01-10 17:44:52 |
Judging History
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, maxm = 5e5, M = maxm + 10, maxv = 1e9, MEM = 1 << 26;
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;
}
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, x0;
constexpr inline Undo(T &x): x(x), x0(x) {}
constexpr inline ~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); }
constexpr inline void dna(int l, int r) {
l += mx, r += mx;
for (unsigned c = __builtin_ctz(mx); c; --c) { unsigned L = l >> c, R = r >> c; Fe(i, L, R) dn(i); }
}
};
int m;
struct Op {
unsigned x, y, id, a[2][2];
inline 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);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22048kb
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: 22060kb
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: -100
Wrong Answer
time: 3265ms
memory: 38508kb
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:
wrong answer 2529th numbers differ - expected: '335149009071', found: '1008307112981'