QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#820787 | #9886. Long Sequence Inversion 2 | WTR2007 | WA | 0ms | 3736kb | C++20 | 2.4kb | 2024-12-19 01:54:05 | 2024-12-19 01:54:05 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define MULT_TEST 0
using namespace std;
typedef long double ldb;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const int inv2 = (MOD + 1) / 2;
inline int read() {
int w = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
w = (w << 1) + (w << 3) + ch - 48;
ch = getchar();
}
return w * f;
}
inline void Add(int &x, int y) {
x += y;
if (x >= MOD) x -= MOD;
}
inline void Del(int &x, int y) {
x -= y;
if (x < 0) x += MOD;
}
inline int Pow(int a, int b) {
int ans = 1;
if (a >= MOD) a %= MOD;
for (; b; b >>= 1) {
if (b & 1) ans = ans * a % MOD;
a = a * a % MOD;
}
return ans;
}
template<class T>
struct BIT {
const int n;
vector<T> s;
BIT(int n) : n(n), s(n + 5) {}
inline void Modify(int x, T v) {
if (x <= 0) return ;
for (; x <= n; x += x & (-x)) s[x] += v;
}
inline T Query(int x) {
T ans = 0;
for (; x; x -= x & (-x)) ans += s[x];
return ans;
}
};
inline void Solve() {
int n, m, ans = 0;
n = read(), m = read();
int tot = m * (m - 1) / 2;
vector<int> p(n), q(n), pom(2 * n);
vector<vector<int>> v(n, vector<int>(m, 0));
for (int i = 0; i < n; i++) p[i] = read();
for (int i = 0; i < n; i++) q[p[i]] = i;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
v[i][j] = read();
pom[0] = 1;
for (int i = 1; i < 2 * n; i++) pom[i] = pom[i - 1] * m % MOD;
BIT<int> T(m + 3);
for (int i = n - 1; i >= 0; i--) {
int id = q[i], sum = 0, res = 0;
int t = id - T.Query(id);
BIT<int> A(m + 3);
for (int j = 0; j < m; j++) {
Add(sum, A.Query(m - v[id][j] + 1));
A.Modify(m - v[id][j] + 1, 1);
}
Add(res, tot % MOD * pom[n - 1 + i]);
int _s = ((2 * sum - tot) % MOD + MOD) % MOD;
Add(res, _s * pom[n - 1 + t] % MOD);
Add(ans, res * inv2 % MOD);
T.Modify(id + 1, 1);
}
printf("%lld\n", ans);
}
signed main() {
int _ = 1;
#if MULT_TEST
_ = read();
#endif
while (_--) Solve();
return 0;
}
/*
3 2
2 0 1
1 0
1 0
0 1
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3660kb
input:
3 2 2 0 1 1 0 1 0 0 1
output:
14
result:
ok "14"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3704kb
input:
2 4 1 0 2 0 3 1 1 2 3 0
output:
60
result:
ok "60"
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3736kb
input:
9 10 2 5 7 3 8 1 4 6 0 9 2 4 0 1 6 7 3 5 8 4 1 6 7 8 0 5 9 2 3 1 9 2 4 6 8 5 7 0 3 9 0 8 2 5 1 6 7 3 4 1 6 0 7 3 9 2 4 5 8 4 5 2 9 1 6 7 3 0 8 7 0 5 6 1 9 2 4 3 8 3 2 1 6 7 0 8 9 4 5 9 2 4 3 5 8 0 6 7 1
output:
403645842
result:
wrong answer 1st words differ - expected: '138876070', found: '403645842'