QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#699178 | #7998. 矩阵 | hos_lyric | 0 | 0ms | 0kb | C++14 | 13.8kb | 2024-11-02 03:03:51 | 2024-11-02 03:03:52 |
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
static constexpr unsigned M = M_;
unsigned x;
constexpr ModInt() : x(0U) {}
constexpr ModInt(unsigned x_) : x(x_ % M) {}
constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
ModInt pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModInt inv() const {
unsigned a = M, b = x; int y = 0, z = 1;
for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
assert(a == 1U); return ModInt(y);
}
ModInt operator+() const { return *this; }
ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModInt &a) const { return (x == a.x); }
bool operator!=(const ModInt &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
using Mint998 = ModInt<998244353>;
using Mint107 = ModInt<1000000007>;
using Mint = Mint107;
// T: monoid representing information of an element and an interval.
// T() should return the identity.
// T::push(T *l, T *r) should push the lazy update.
// T::pull(const Node *l, const Node *r) should pull two intervals.
// The node itself contains an element: This should calculate l * self * r.
// T::operator<(const T &t) is used for "Cmp" functions.
template <class T> struct Splay {
Splay *p, *l, *r;
T t;
Splay() : p(nullptr), l(nullptr), r(nullptr), t() {}
Splay(const T &t_) : p(nullptr), l(nullptr), r(nullptr), t(t_) {}
void push() {
t.push(l ? &l->t : nullptr, r ? &r->t : nullptr);
}
void pull() {
t.pull(l ? &l->t : nullptr, r ? &r->t : nullptr);
}
void pushRec() {
if (p) p->pushRec();
push();
}
void rotate() {
if (p->l == this) { if (r) { r->p = p; } p->l = r; r = p; }
else { if (l) { l->p = p; } p->r = l; l = p; }
Splay *pp = p->p;
if (pp) ((pp->l == p) ? pp->l : pp->r) = this;
p->pull(); p->p = this; p = pp;
}
Splay *splay() {
pushRec();
for (; p; rotate()) if (p->p) ((p->l == this) == (p->p->l == p)) ? p->rotate() : rotate();
pull();
return this;
}
void linkL(Splay *a) {
assert(!l);
l = a; if (l) l->p = this;
pull();
}
void linkR(Splay *a) {
assert(!r);
r = a; if (r) r->p = this;
pull();
}
void linkLR(Splay *a, Splay *b) {
assert(!l); assert(!r);
l = a; if (l) l->p = this;
r = b; if (r) r->p = this;
pull();
}
void cutL() {
if (l) l->p = nullptr;
l = nullptr;
pull();
}
void cutR() {
if (r) r->p = nullptr;
r = nullptr;
pull();
}
void cutLR() {
if (l) l->p = nullptr;
if (r) r->p = nullptr;
l = r = nullptr;
pull();
}
// Splays the leftmost node.
Splay *leftmost() {
Splay *a = this;
for (; a->l; a = a->l) {}
return a->splay();
}
// Splays the rightmost node.
Splay *rightmost() {
Splay *a = this;
for (; a->r; a = a->r) {}
return a->splay();
}
// Iterates the tree, calling f on each node.
template <class F> void dfs(F f) {
push();
if (l) l->dfs(f);
f(this);
if (r) r->dfs(f);
}
// Concatenates two trees.
friend Splay *concat(Splay *a, Splay *b) {
if (!a) return b;
if (!b) return a;
a = a->rightmost();
a->linkR(b);
return a;
}
// Splays the k-th element.
Splay *at(int k) {
assert(0 <= k); assert(k < t.size);
for (Splay *a = this; ; ) {
const int sizeL = a->l ? a->l->t.size : 0;
if (k < sizeL) {
a = a->l;
} else if (k == sizeL) {
return a->splay();
} else {
a = a->r;
k -= (sizeL + 1);
}
}
}
// Splits the tree by size: [0, k), [k, a->size).
friend pair<Splay *, Splay *> splitAt(Splay *a, int k) {
assert(0 <= k); assert(k <= (a ? a->t.size : 0));
if (k == 0) return std::make_pair(nullptr, a);
if (k == a->t.size) return std::make_pair(a, nullptr);
a = a->at(k);
Splay *l = a->l;
a->cutL();
return std::make_pair(l, a);
}
// Splits the tree by operator<: (< target), (>= target).
// Splays the rightmost (< target) and the leftmost (>= target).
friend pair<Splay *, Splay *> splitCmp(Splay *a, const T &target) {
Splay *l = nullptr, *r = nullptr;
for (; a; ) {
a->push();
if (a->t < target) {
l = a;
a = a->r;
} else {
r = a;
a = a->l;
}
}
if (l) {
l->splay();
l->cutR();
}
if (r) r->splay();
return std::make_pair(l, r);
}
// Inserts b into the tree, using T::operator<.
friend void insertCmp(Splay *&a, Splay *b) {
auto s = splitCmp(a, b->t);
b->linkLR(s.first, s.second);
a = b;
}
// Inserts b's nodes into the tree one by one, using T::operator<.
friend void meldRecCmp(Splay *&a, Splay *b) {
if (!b) return;
Splay *l = b->l, *r = b->r;
b->push();
b->cutLR();
meldRecCmp(a, l);
insertCmp(a, b);
meldRecCmp(a, r);
}
// Meld two trees, using T::operator<.
friend Splay *meldCmp(Splay *a, Splay *b) {
if (!a) return b;
if (!b) return a;
if (a->t.size < b->t.size) swap(a, b);
meldRecCmp(a, b);
return a;
}
// Cuts out [kL, kR) of the tree and calls f.
template <class F> friend void range(Splay *&a, int kL, int kR, F f) {
assert(0 <= kL); assert(kL <= kR); assert(kR <= (a ? a->t.size : 0));
if (kL == kR) { f(nullptr); return; }
Splay *b = nullptr;
if (kR < a->t.size) {
b = a->at(kR);
a = b->l;
b->cutL();
}
if (0 < kL) a = a->at(kL - 1)->r;
f(a);
if (b) b->linkL(a->splay());
a->splay();
}
// Applies T::f(args...) to [kL, kR).
template <class F, class... Args>
friend void ch(Splay *&a, int kL, int kR, F f, Args &&... args) {
range(a, kL, kR, [&](Splay *b) -> void { if (b) (b->t.*f)(args...); });
}
// Calculates the product for [kL, kR).
friend T get(Splay *&a, int kL, int kR) {
T t;
range(a, kL, kR, [&](Splay *b) -> void { if (b) t = b->t; });
return t;
}
};
struct Node {
int size;
Mint val, lz;
Node() : size(0), val(0), lz(0) {}
Node(Mint val_) : size(1), val(val_), lz(0) {}
void push(Node *l, Node *r) {
if (lz) {
if (l) l->add(lz);
if (r) r->add(lz);
lz = 0;
}
}
void pull(const Node *l, const Node *r) {
size = (l ? l->size : 0) + 1 + (r ? r->size : 0);
}
void add(Mint d) {
val += d;
lz += d;
}
};
////////////////////////////////////////////////////////////////////////////////
using SN = Splay<Node>;
int N, Q;
SN nodes[3010][3010][4];
SN *yoko[2][3010], *tate[2][3010];
SN *YOKO[4][3010][3], *TATE[4][3010][3];
Mint ans[3010][3010];
void cut(SN *orig, SN *tmp[3], int l, int r) {
auto res = splitAt(orig, r);
tmp[2] = res.second;
res = splitAt(res.first, l);
tmp[1] = res.second;
tmp[0] = res.first;
}
void tuc(SN *&orig, SN *tmp[3], int, int) {
orig = concat(tmp[0], concat(tmp[1], tmp[2]));
}
// #define DEBUG
int main() {
for (; ~scanf("%d%d", &N, &Q); ) {
#ifdef DEBUG
vector<vector<Mint>>a(N,vector<Mint>(N));
#endif
for (int x = 0; x < N; ++x) {
Mint998 v = 1;
for (int y = 0; y < N; ++y) {
v *= (x + 2);
const Mint val = v.x;
#ifdef DEBUG
a[x][y]=val;
#endif
for (int dir = 0; dir < 4; ++dir) {
nodes[x][y][dir] = SN(val);
}
}
}
for (int x = 0; x < N; ++x) {
yoko[0][x] = yoko[1][x] = nullptr;
for (int y = 0; y < N; ++y) yoko[0][x] = concat(yoko[0][x], &nodes[x][y][0]);
for (int y = N; --y >= 0; ) yoko[1][x] = concat(yoko[1][x], &nodes[x][y][1]);
}
for (int y = 0; y < N; ++y) {
tate[0][y] = tate[1][y] = nullptr;
for (int x = 0; x < N; ++x) tate[0][y] = concat(tate[0][y], &nodes[x][y][2]);
for (int x = N; --x >= 0; ) tate[1][y] = concat(tate[1][y], &nodes[x][y][3]);
}
for (int q = 0; q < Q; ++q) {
int O, X0, X1, Y0, Y1;
scanf("%d%d%d%d%d", &O, &X0, &Y0, &X1, &Y1);
--X0;
--Y0;
if (O == 1) {
const int D = X1 - X0;
assert(D == Y1 - Y0);
#ifdef DEBUG
cerr<<COLOR("93")<<"["<<X0<<", "<<X1<<") * ["<<Y0<<", "<<Y1<<")"<<COLOR()<<endl;
vector<vector<Mint>>aa(D,vector<Mint>(D)),bb(D,vector<Mint>(D));
for(int i=0;i<D;++i)for(int j=0;j<D;++j)aa[i][j]=a[X0+i][Y0+j];
for(int i=0;i<D;++i)for(int j=0;j<D;++j)bb[i][j]=aa[j][D-1-i];
for(int i=0;i<D;++i)for(int j=0;j<D;++j)a[X0+i][Y0+j]=bb[i][j];
#endif
for (int i = 0; i < D; ++i) cut(yoko[0][X0 + i], YOKO[0][i], Y0, Y1);
for (int i = 0; i < D; ++i) cut(yoko[1][X0 + i], YOKO[1][i], N - Y1, N - Y0);
for (int i = 0; i < D; ++i) cut(tate[0][Y0 + i], TATE[0][i], X0, X1);
for (int i = 0; i < D; ++i) cut(tate[1][Y0 + i], TATE[1][i], N - X1, N - X0);
for (int i = 0; i < D; ++i) YOKO[2][i][1] = TATE[0][D - 1 - i][1];
for (int i = 0; i < D; ++i) YOKO[3][i][1] = TATE[1][D - 1 - i][1];
for (int i = 0; i < D; ++i) TATE[2][i][1] = YOKO[1][i][1];
for (int i = 0; i < D; ++i) TATE[3][i][1] = YOKO[0][i][1];
for (int i = 0; i < D; ++i) YOKO[0][i][1] = YOKO[2][i][1];
for (int i = 0; i < D; ++i) YOKO[1][i][1] = YOKO[3][i][1];
for (int i = 0; i < D; ++i) TATE[0][i][1] = TATE[2][i][1];
for (int i = 0; i < D; ++i) TATE[1][i][1] = TATE[3][i][1];
for (int i = 0; i < D; ++i) tuc(yoko[0][X0 + i], YOKO[0][i], Y0, Y1);
for (int i = 0; i < D; ++i) tuc(yoko[1][X0 + i], YOKO[1][i], N - Y1, N - Y0);
for (int i = 0; i < D; ++i) tuc(tate[0][Y0 + i], TATE[0][i], X0, X1);
for (int i = 0; i < D; ++i) tuc(tate[1][Y0 + i], TATE[1][i], N - X1, N - X0);
} else if (O == 2) {
Mint D;
scanf("%u", &D.x);
#ifdef DEBUG
cerr<<COLOR("94")<<"["<<X0<<", "<<X1<<") * ["<<Y0<<", "<<Y1<<") "<<D<<COLOR()<<endl;
for(int x=X0;x<X1;++x)for(int y=Y0;y<Y1;++y)a[x][y]+=D;
#endif
for (int x = X0; x < X1; ++x) ch(yoko[0][x], Y0, Y1, &Node::add, D);
for (int x = X0; x < X1; ++x) ch(yoko[1][x], N - Y1, N - Y0, &Node::add, D);
for (int y = Y0; y < Y1; ++y) ch(tate[0][y], X0, X1, &Node::add, D);
for (int y = Y0; y < Y1; ++y) ch(tate[1][y], N - X1, N - X0, &Node::add, D);
} else {
assert(false);
}
}
for (int x = 0; x < N; ++x) {
int y = 0;
yoko[0][x]->dfs([&](SN *u) -> void {
ans[x][y++] = u->t.val;
});
assert(y == N);
}
#ifdef DEBUG
for(int x=0;x<N;++x){cerr<<"a ["<<x<<"] = ";pv(a[x].begin(),a[x].end());}
for(int x=0;x<N;++x){cerr<<"ans["<<x<<"] = ";pv(ans[x],ans[x]+N);}
for(int x=0;x<N;++x)for(int y=0;y<N;++y)assert(a[x][y]==ans[x][y]);
#endif
Mint107 key = 0;
for (int x = N; --x >= 0; ) for (int y = N; --y >= 0; ) {
(key += ans[x][y].x) *= 12345;
}
printf("%u\n", key.x);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Pretests
Final Tests
Test #1:
score: 0
Memory Limit Exceeded
input:
100 3000 1 4 3 100 99 1 3 8 83 88 1 4 7 96 99 1 2 7 90 95 2 6 5 98 98 762240996 1 9 8 98 97 1 3 3 100 100 2 18 7 97 84 230446266 1 2 5 94 97 1 2 18 82 98 1 9 2 100 93 1 3 9 91 97 1 1 2 99 100 1 7 6 98 97 1 2 4 98 100 1 3 2 99 98 1 4 2 100 98 2 9 74 28 83 703323901 1 6 4 100 98 2 2 6 99 93 882702932 ...
output:
535773693
result:
Test #2:
score: 0
Time Limit Exceeded
input:
3000 3000 2 40 215 2496 2298 67590963 2 100 211 2684 2855 670656770 2 417 399 2612 2930 826127245 2 116 779 2407 2596 977282152 2 512 366 2768 2949 737671139 2 191 594 2671 2978 404448620 2 3 11 2720 2633 516813112 2 95 294 2553 2820 112269230 2 284 58 2934 2754 225336228 2 306 53 2815 2984 49218367...
output:
result:
Test #3:
score: 0
Time Limit Exceeded
input:
3000 2000 1 853 610 2674 2431 1 24 415 2490 2881 1 19 150 2842 2973 1 597 79 2977 2459 1 307 408 2397 2498 1 292 316 2907 2931 1 7 42 2953 2988 1 36 30 2953 2947 1 55 438 2609 2992 1 97 204 2664 2771 1 607 254 2803 2450 1 59 159 2808 2908 1 58 12 2991 2945 1 287 400 2506 2619 1 105 143 2949 2987 1 2...
output:
result:
Test #4:
score: 0
Time Limit Exceeded
input:
3000 2000 1 646 55 2815 2224 1 47 78 2964 2995 1 48 7 2882 2841 1 9 8 2999 2998 1 128 36 2984 2892 1 14 5 2990 2981 1 52 81 2934 2963 1 231 317 2612 2698 1 33 7 2960 2934 1 111 165 2732 2786 1 9 2 2994 2987 1 131 224 2866 2959 1 19 49 2917 2947 1 46 44 2997 2995 1 1 14 2969 2982 1 69 49 2975 2955 1 ...
output:
result:
Test #5:
score: 0
Time Limit Exceeded
input:
3000 3000 1 6 5 2997 2996 1 14 362 2624 2972 1 657 108 2405 1856 1 20 27 2983 2990 1 245 236 2903 2894 1 184 100 2957 2873 1 61 47 2954 2940 1 39 89 2935 2985 1 43 49 2956 2962 1 132 138 2763 2769 1 4 4 2998 2998 1 16 59 2752 2795 1 30 24 2997 2991 1 143 11 2959 2827 1 57 44 2887 2874 1 40 17 2991 2...
output:
result:
Test #6:
score: 0
Time Limit Exceeded
input:
3000 3000 1 544 5 2907 2368 1 60 63 2978 2981 1 228 310 2881 2963 1 57 71 2958 2972 1 101 565 2467 2931 1 979 1004 2882 2907 1 37 3 2913 2879 1 418 427 2922 2931 1 30 62 2937 2969 1 83 104 2979 3000 1 4 18 2939 2953 1 67 203 2834 2970 1 357 882 1000 1525 1 34 10 2991 2967 1 8 4 2997 2993 1 2 4 2991 ...
output:
result:
Test #7:
score: 0
Time Limit Exceeded
input:
3000 2000 1 82 42 2883 2843 1 2 48 2939 2985 2 188 176 2376 2976 715286463 1 15 24 2984 2993 1 106 50 2811 2755 2 45 132 2745 2432 339749026 1 5 100 2779 2874 1 343 134 2478 2269 1 106 227 2820 2941 2 10 206 2947 2798 365598855 2 560 106 2582 2903 92795190 1 74 100 2960 2986 1 5 33 2944 2972 2 873 3...
output:
result:
Test #8:
score: 0
Time Limit Exceeded
input:
3000 2000 1 20 5 2990 2975 1 14 10 2999 2995 1 192 89 2950 2847 2 547 646 2654 2940 439095457 1 18 16 3000 2998 1 35 105 2754 2824 1 17 52 2942 2977 2 356 121 2384 2963 959830720 2 76 78 2412 1842 361763869 2 199 46 2624 2931 743439747 2 180 320 2981 2964 772483137 2 173 122 2732 2901 759079379 1 47...
output:
result:
Test #9:
score: 0
Time Limit Exceeded
input:
3000 3000 1 220 131 2785 2696 2 1443 1396 2159 2850 578335729 1 536 1184 1545 2193 1 26 23 2999 2996 1 43 56 2881 2894 1 29 4 2872 2847 2 69 155 2681 2931 88398671 1 66 44 2945 2923 1 53 512 2515 2974 2 402 58 2752 2782 641047796 2 458 36 2859 2756 694998888 2 145 192 2995 2973 483103477 1 117 315 2...
output:
result:
Test #10:
score: 0
Time Limit Exceeded
input:
3000 3000 2 7 47 2887 2544 535526356 2 160 639 2988 2995 343766215 1 122 40 2985 2903 2 62 240 2858 2177 841981272 1 169 18 2682 2531 1 61 286 2729 2954 1 180 27 2994 2841 1 238 330 2881 2973 2 57 188 2880 2914 203515190 1 28 61 2903 2936 2 102 269 2876 2397 681849386 1 3 53 2896 2946 1 101 56 2985 ...