QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#312142 | #7998. 矩阵 | unputdownable | 100 ✓ | 6068ms | 603872kb | C++17 | 7.5kb | 2024-01-23 14:12:14 | 2024-01-23 14:12:15 |
Judging History
answer
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#include <bits/stdc++.h>
// #define int long long
#define i64 long long
#define pii pair <int, int>
using namespace std;
inline int read(void) {
int x=0,sgn=1; char ch=getchar();
while(ch<48||57<ch) {if(ch==45)sgn=0;ch=getchar();}
while(47<ch&&ch<58) {x=x*10+ch-48; ch=getchar();}
return sgn? x:-x;
}
void write(int x) {
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
/*
write((Ans%p+p)%p); pls
*/
const int mod=998244353;
const int p=1000000007;
int n,q;
int hed[3003];
int tmp[3003][3003];
struct Node {
int lnk[4];
i64 del[4];
int cur;
i64 val;
inline void locate(int x) {
if(lnk[0]==x) return cur=0,void();
if(lnk[1]==x) return cur=1,void();
if(lnk[2]==x) return cur=2,void();
if(lnk[3]==x) return cur=3,void();
}
inline void update(int v,int x=0) {
x=(x+cur)&3;
del[x]+=v;
}
inline int rotate(int x);
inline void link(int v,int x);
} nod[9100006];
inline int Node::rotate(int x) {
x=(x+cur)&3;
nod[lnk[x]].val=val+del[x];
return lnk[x];
}
inline void Node::link(int v,int x=0) {
x=(x+cur)&3;
lnk[x]=v;
del[x]=nod[v].val-val;
}
inline void link(int a,int ra,int b) {
nod[a].link(b,ra);
nod[b].link(a);
}
int cntnod;
inline void initmat() {
for(int i=1; i<=n; ++i) {
int x=i+1,cur=i+1;
for(int u=1; u<=n; ++u) {
tmp[i][u]=++cntnod;
nod[cntnod].val=cur;
cur=1ll*cur*x%mod;
}
}
for(int i=1; i<=n; ++i) tmp[i][0]=hed[i]=++cntnod,nod[cntnod].link(tmp[i][1]);
for(int i=1; i<=n; ++i) tmp[0][i]=++cntnod,nod[cntnod].link(tmp[1][i]);
for(int i=1; i<=n; ++i) tmp[i][n+1]=++cntnod,nod[cntnod].link(tmp[i][n]);
for(int i=1; i<=n; ++i) tmp[n+1][i]=++cntnod,nod[cntnod].link(tmp[n][i]);
for(int i=1; i<=n; ++i) {
for(int u=1; u<=n; ++u) {
int k=tmp[i][u];
nod[k].link(tmp[i+1][u],0);
nod[k].link(tmp[i][u+1],1);
nod[k].link(tmp[i-1][u],2);
nod[k].link(tmp[i][u-1],3);
}
}
}
inline void debug() {
const int N=n+1;
cerr<<"-------------"<<endl;
for(int i=0; i<=N; ++i) {
for(int u=0; u<=N; ++u) {
// fprintf(stderr,"[%2d|%4d|%d]%c",tmp[i][u],nod[tmp[i][u]].val,nod[tmp[i][u]].cur," \n"[u==N]);
if(tmp[i][u]) fprintf(stderr,"%d%c",nod[tmp[i][u]].cur," \n"[u==N]);
else fprintf(stderr," %c"," \n"[u==N]);
}
}
}
inline void debugAns() {
cerr<<"-------------"<<endl;
for(int i=1; i<=n; ++i) {
for(int u=1; u<=n; ++u) {
fprintf(stderr,"%10d%c",nod[tmp[i][u]].val," \n"[u==n]);
}
}
}
inline void calcval() {
for(int i=1; i<=n; ++i) {
tmp[i][1]=nod[hed[i]].rotate(0);
nod[tmp[i][1]].locate(hed[i]);
for(int u=2; u<=n; ++u) {
tmp[i][u]=nod[tmp[i][u-1]].rotate(2);
nod[tmp[i][u]].locate(tmp[i][u-1]);
}
}
}
inline void locate(int l,int r,int u,int d) {
tmp[u][1]=nod[hed[u]].rotate(0);
nod[tmp[u][1]].locate(hed[u]);
for(int i=1; i<l; ++i) {
tmp[u][i+1]=nod[tmp[u][i]].rotate(2);
nod[tmp[u][i+1]].locate(tmp[u][i]);
}
for(int i=l; i<r; ++i) {
tmp[u-1][i]=nod[tmp[u][i]].rotate(3);
tmp[u][i+1]=nod[tmp[u][i]].rotate(2);
nod[tmp[u-1][i]].locate(tmp[u][i]);
nod[tmp[u][i+1]].locate(tmp[u][i]);
}
tmp[u-1][r]=nod[tmp[u][r]].rotate(3);
nod[tmp[u-1][r]].locate(tmp[u][r]);
(nod[tmp[u][r]].cur+=3)&=3;
for(int i=u; i<d; ++i) {
tmp[i][r+1]=nod[tmp[i][r]].rotate(3);
tmp[i+1][r]=nod[tmp[i][r]].rotate(2);
nod[tmp[i][r+1]].locate(tmp[i][r]);
nod[tmp[i+1][r]].locate(tmp[i][r]);
}
tmp[d][r+1]=nod[tmp[d][r]].rotate(3);
nod[tmp[d][r+1]].locate(tmp[d][r]);
(nod[tmp[d][r]].cur+=3)&=3;
for(int i=r; i>l; --i) {
tmp[d+1][i]=nod[tmp[d][i]].rotate(3);
tmp[d][i-1]=nod[tmp[d][i]].rotate(2);
nod[tmp[d+1][i]].locate(tmp[d][i]);
nod[tmp[d][i-1]].locate(tmp[d][i]);
}
tmp[d+1][l]=nod[tmp[d][l]].rotate(3);
nod[tmp[d+1][l]].locate(tmp[d][l]);
(nod[tmp[d][l]].cur+=3)&=3;
for(int i=d; i>u; --i) {
tmp[i][l-1]=nod[tmp[i][l]].rotate(3);
tmp[i-1][l]=nod[tmp[i][l]].rotate(2);
nod[tmp[i][l-1]].locate(tmp[i][l]);
nod[tmp[i-1][l]].locate(tmp[i][l]);
}
tmp[u][l-1]=nod[tmp[u][l]].rotate(3);
nod[tmp[u][l-1]].locate(tmp[u][l]);
(nod[tmp[u][l]].cur+=3)&=3;
}
signed main() {
// freopen("localinput","r",stdin);
// freopen("localoutput","w",stderr);
n=read(); q=read();
initmat();
// calcval();
// debugAns();
for(int t=1,op,l,r,u,d,v; t<=q; ++t) {
// memset(tmp,0,sizeof tmp);
op=read(); u=read(); l=read(); d=read(); r=read();
locate(l,r,u,d);
if(op==1) {
if(l==r) continue;
for(int i=l; i<r; ++i) link(tmp[u][i],3,tmp[d-(i-l)][l-1]);
link(tmp[u][r],0,tmp[u][l-1]);
for(int i=u; i<d; ++i) link(tmp[i][r],3,tmp[u-1][l+(i-u)]);
link(tmp[d][r],0,tmp[u-1][r]);
for(int i=r; i>l; --i) link(tmp[d][i],3,tmp[u+(r-i)][r+1]);
link(tmp[d][l],0,tmp[d][r+1]);
for(int i=d; i>u; --i) link(tmp[i][l],3,tmp[d+1][r-(d-i)]);
link(tmp[u][l],0,tmp[d+1][l]);
} else {
v=read();
for(int i=l; i<=r; ++i) {
nod[tmp[u-1][i]].update(v);
nod[tmp[d+1][i]].update(v);
}
for(int i=u; i<=d; ++i) {
nod[tmp[i][l-1]].update(v);
nod[tmp[i][r+1]].update(v);
}
if(l!=r&&u!=d) {
for(int i=l; i<r; ++i) nod[tmp[u][i]].update(-v,3);
nod[tmp[u][r]].update(-v);
for(int i=u; i<d; ++i) nod[tmp[i][r]].update(-v,3);
nod[tmp[d][r]].update(-v);
for(int i=r; i>l; --i) nod[tmp[d][i]].update(-v,3);
nod[tmp[d][l]].update(-v);
for(int i=d; i>u; --i) nod[tmp[i][l]].update(-v,3);
nod[tmp[u][l]].update(-v);
} else if(l==r&&u==d) {
nod[tmp[u][l]].update(-v);
nod[tmp[u][l]].update(-v,1);
nod[tmp[u][l]].update(-v,2);
nod[tmp[u][l]].update(-v,3);
} else if(l==r) {
(nod[tmp[u][l]].cur+=3)&=3;
for(int i=u; i<=d; ++i) nod[tmp[i][l]].update(-v,3),nod[tmp[i][l]].update(-v,1);
nod[tmp[u][l]].update(-v);
nod[tmp[d][l]].update(-v);
} else {
for(int i=l; i<=r; ++i) nod[tmp[u][i]].update(-v,3),nod[tmp[u][i]].update(-v,1);
nod[tmp[u][l]].update(-v);
nod[tmp[u][r]].update(-v);
}
}
// calcval();
// debugAns();
}
calcval();
// debugAns();
i64 Ans=0;
const int base=12345;
for(int i=n; i>=1; --i) {
for(int u=n; u>=1; --u) {
Ans=(Ans+nod[tmp[i][u]].val)%p*base%p;
}
}
write(Ans); puts("");
// fprintf(stderr,"%.4lf\n",1.0*clock()/CLOCKS_PER_SEC);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 23ms
memory: 6388kb
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:
ok 1 number(s): "535773693"
Test #2:
score: 10
Accepted
time: 2564ms
memory: 603552kb
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:
343427775
result:
ok 1 number(s): "343427775"
Test #3:
score: 10
Accepted
time: 3738ms
memory: 603516kb
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:
743809971
result:
ok 1 number(s): "743809971"
Test #4:
score: 10
Accepted
time: 3761ms
memory: 603696kb
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:
517390609
result:
ok 1 number(s): "517390609"
Test #5:
score: 10
Accepted
time: 5474ms
memory: 603500kb
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:
632994577
result:
ok 1 number(s): "632994577"
Test #6:
score: 10
Accepted
time: 6068ms
memory: 603872kb
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:
275056325
result:
ok 1 number(s): "275056325"
Test #7:
score: 10
Accepted
time: 3335ms
memory: 602600kb
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:
191562107
result:
ok 1 number(s): "191562107"
Test #8:
score: 10
Accepted
time: 3303ms
memory: 602212kb
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:
787136396
result:
ok 1 number(s): "787136396"
Test #9:
score: 10
Accepted
time: 4698ms
memory: 603312kb
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:
973923858
result:
ok 1 number(s): "973923858"
Test #10:
score: 10
Accepted
time: 4787ms
memory: 602664kb
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 ...
output:
328382756
result:
ok 1 number(s): "328382756"