QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#244015 | #1268. Diamond Rush | PhantomThreshold# | TL | 4ms | 19908kb | C++20 | 2.4kb | 2023-11-08 20:25:10 | 2023-11-08 20:25:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1'000'000'007;
vector<ll> merge(const vector<ll> &A,const vector<ll> &B){
int n=A.size();
int m=B.size();
vector<ll> C;
int x=0,y=0;
for (;x<n && y<m;){
if (A[x]>=B[y]){
C.push_back(A[x]);
x++;
}
else{
C.push_back(B[y]);
y++;
}
}
for (;x<n;x++) C.push_back(A[x]);
for (;y<m;y++) C.push_back(B[y]);
return C;
}
ll n,Q;
ll pw[400*400+50];
ll a[405][405];
vector<ll> pre[405][405],suf[405][405],f[405][405],g[405][405];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int Tcase=1;
cin >> Tcase;
for (;Tcase--;){
cin >> n >> Q;
pw[0]=1;
for (int i=1;i<=n*n;i++) pw[i]=pw[i-1]*n%mod*n%mod;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) cin >> a[i][j];
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if (i==1 && j==1){
pre[i][j]={a[i][j]};
}
else if (i==1){
pre[i][j]=merge(pre[i][j-1],{a[i][j]});
}
else if (j==1){
pre[i][j]=merge(pre[i-1][j],{a[i][j]});
}
else{
pre[i][j]=merge(max(pre[i-1][j],pre[i][j-1]),{a[i][j]});
}
}
}
for (int i=n;i>=1;i--){
for (int j=n;j>=1;j--){
if (i==n && j==n){
suf[i][j]={a[i][j]};
}
else if (i==n){
suf[i][j]=merge(suf[i][j+1],{a[i][j]});
}
else if (j==n){
suf[i][j]=merge(suf[i+1][j],{a[i][j]});
}
else{
suf[i][j]=merge(max(suf[i+1][j],suf[i][j+1]),{a[i][j]});
}
}
}
for (int i=2;i<=n;i++){
for (int j=n-1;j>=1;j--){
f[i][j]=merge(pre[i-1][j+1],suf[i][j+1]);
if (i!=2){
f[i][j]=max(f[i][j],f[i-1][j]);
}
if (j!=n-1){
f[i][j]=max(f[i][j],f[i][j+1]);
}
}
}
for (int i=n-1;i>=1;i--){
for (int j=2;j<=n;j++){
g[i][j]=merge(pre[i+1][j-1],suf[i+1][j]);
if (j!=2){
g[i][j]=max(g[i][j],g[i][j-1]);
}
if (i!=n-1){
g[i][j]=max(g[i][j],g[i+1][j]);
}
}
}
for (;Q--;){
int r1,r2,c1,c2;
cin >> r1 >> r2 >> c1 >> c2;
vector<ll> route;
bool flagf=(r1>=2 && c2<=n-1);
bool flagg=(r2<=n-1 && c1>=2);
if (flagf && flagg) route=max(f[r1][c2],g[r2][c1]);
else if (flagf) route=f[r1][c2];
else route=g[r2][c1];
ll ans=0;
for (auto x:route){
ans=(ans+pw[x])%mod;
}
cout << ans << "\n";
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 19908kb
input:
1 2 2 2 3 1 4 1 1 2 2 2 2 1 1
output:
276 336
result:
ok 2 lines
Test #2:
score: -100
Time Limit Exceeded
input:
5 399 200000 1 5 3 2 3 5 5 4 3 5 2 5 1 2 4 1 3 1 1 5 5 5 5 2 2 2 3 3 5 3 5 3 1 2 3 2 3 3 4 3 5 3 1 3 4 5 2 1 4 4 5 4 5 3 3 2 4 2 3 5 1 2 4 4 3 2 3 5 4 4 1 2 3 5 5 2 1 5 5 1 4 1 2 5 3 4 5 3 5 5 5 3 2 3 1 2 1 1 2 5 1 4 1 3 4 1 1 3 5 3 2 2 3 1 3 1 3 1 5 1 4 1 1 2 5 1 4 3 1 3 2 5 4 2 3 5 5 2 5 3 1 5 3 1...
output:
941207053 72597563 125162256 674945829 362141056 46633728 833089835 282730934 340464097 953149538 282730934 736432213 513486467 333152891 355535008 797175106 144845122 87755843 408404885 635578224 670481364 176200794 282730934 733794083 302174217 658772773 282730934 556675047 149516187 282730934 362...