QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#564703 | #7878. Matrix Distances | Bird# | RE | 7ms | 28672kb | C++14 | 884b | 2024-09-15 13:35:28 | 2024-09-15 13:35:29 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
int c[N][N],n,m;
vector<int> v[N][N];
long long ans;
struct BIT{
long long sum[N];
void add(int x,int val){
for(;x<=n;x+=x&-x) sum[x]+=val;
}
long long qry(int x){
long long res=0;
for(;x;x-=x&-x) res+=sum[x];
return res;
}
}T[2];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) cin>>c[i][j];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
v[c[i][j]][i].push_back(j);
for(int i=1;i<=m;i++){
int tot=0;
long long num=0;
for(int j=1;j<=n;j++){
for(auto &x:v[i][j]){
ans+=x*T[0].qry(x)-T[1].qry(x);
ans+=T[1].qry(n)-T[1].qry(x)-x*(T[0].qry(n)-T[0].qry(x));
ans+=1ll*tot*j-num;
T[0].add(x,1);
T[1].add(x,x);
num+=j;tot++;
}
}
for(int j=1;j<=n;j++) T[0].sum[j]=T[1].sum[j]=0;
}
cout<<ans*2;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 28672kb
input:
2 2 1 1 2 2
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 28216kb
input:
4 4 1 3 2 4 2 1 2 3 1 3 3 2 3 2 1 4
output:
152
result:
ok 1 number(s): "152"
Test #3:
score: -100
Runtime Error
input:
1000 1000 227980299 227980299 227980299 227980299 227980299 776958596 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 227980299 776958596 227980299 227980299 329001637 227980299 227980299 227980299 329001637 227980299 227980299 227980299 227980299 227980299 ...