QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#747382 | #9731. Fuzzy Ranking | ucup-team4479# | RE | 0ms | 16952kb | C++23 | 3.7kb | 2024-11-14 17:03:19 | 2024-11-14 17:03:24 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 2e5 + 5;
int dfn[N], low[N], tim = 0, stk[N], top = 0, cnt = 0, scc[N], now;
vector <ll> sum[N];
vector<int> id[N], l[N], r[N], rnk[N];
vector <int> E[N], a[N], rk[N];
map <int, bool> mp[N];
bool vis[N];
void tarjan(int u) {
dfn[u] = low[u] = ++tim;
stk[++top] = u;
vis[u] = 1;
for (int v : E[u]) {
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (vis[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
++cnt;
while (stk[top] != u) {
scc[stk[top]] = cnt;
vis[stk[top]] = 0;
top--;
}
scc[stk[top]] = cnt;
vis[stk[top]] = 0;
--top;
}
}
bool cmp(int a, int b) {
return l[now][a] < l[now][b];
}
void solve() {
int n, k, q;
cin >> n >> k >> q;
for (int i = 1; i <= n; ++i) {
rk[i].resize(k);
vis[i] = dfn[i] = low[i] = scc[i] = 0;
E[i].clear();
mp[i].clear();
}
for (int i = 0; i < k; ++i) {
a[i].resize(n);
for (int j = 0; j < n; ++j)
cin >> a[i][j], rk[a[i][j]][i] = j;
for (int j = 1; j < n; ++j) {
if (mp[a[i][j - 1]].count(a[i][j])) continue;
E[a[i][j - 1]].push_back(a[i][j]);
mp[a[i][j - 1]][a[i][j]] = 1;
}
}
tim = top = cnt = 0;
tarjan(1);
// cout << cnt << endl;
for (int t = 0; t < k; ++t) {
l[t].resize(cnt + 1);
r[t].resize(cnt + 1);
id[t].resize(cnt + 1);
sum[t].resize(cnt + 1);
rnk[t].resize(cnt + 1);
// cout << t << endl;
now = t;
for (int i = 1; i <= cnt; ++i)
l[t][i] = n, r[t][i] = 0, id[t][i] = i;
for (int i = 1; i <= n; ++i) {
int sc = scc[i];
l[t][sc] = min(l[t][sc], rk[i][t]);
r[t][sc] = max(r[t][sc], rk[i][t]);
// cout << i << " " << sc << " " << rk[i][t] << endl;
}
sort(id[t].begin() + 1, id[t].end(), cmp);
for (int i = 1; i <= cnt; ++i)
rnk[t][id[t][i]] = i;
sum[t][0] = 0;
for (int i = 1; i <= cnt; ++i) {
int L = l[t][id[t][i]], R = r[t][id[t][i]];
sum[t][i] = sum[t][i - 1] + 1ll * (R - L + 1) * (R - L) / 2;
// cout << "scc " << i << " " << L << " " << R << endl;
}
}
ll lst = 0;
while (q--) {
int ql, qr, i;
cin >> i >> ql >> qr;
i = (i + lst) % k;
ql = (ql + lst) % n;
qr = (qr + lst) % n;
// cout << i << " " << ql << " " << qr << endl;
int s = rnk[i][scc[a[i][ql]]], t = rnk[i][scc[a[i][qr]]];
int L, R;
L = max(l[i][scc[a[i][ql]]], ql), R = min(r[i][scc[a[i][ql]]], qr);
// cout << "qus " << i << " " << s << " " << t << endl;
ll ans = 0;
ans += 1ll * (R - L + 1) * (R - L) / 2;
if (s == t) {
cout << ans << endl;
lst = ans;
continue;
}
L = max(l[i][scc[a[i][qr]]], ql), R = min(r[i][scc[a[i][qr]]], qr);
// cout << L << " " << R << endl;
ans += 1ll * (R - L + 1) * (R - L) / 2;
ans += sum[i][t - 1] - sum[i][s];
cout << ans << endl;
lst = ans;
}
}
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
cout.tie(0);
int T;
cin >> T;
while (T--) solve();
return 0;
}
/*
3
5 2 2
1 2 3 4 5
5 4 3 2 1
1 0 2
1 2 1
5 3 3
1 2 3 4 5
1 3 2 4 5
1 2 3 5 4
0 0 2
0 2 3
1 0 3
3 2 1
1 2 3
1 3 2
0 1 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16952kb
input:
2 5 2 2 1 2 3 4 5 5 4 3 2 1 1 0 2 1 2 1 5 3 3 1 2 3 4 5 1 3 2 4 5 1 2 3 5 4 0 0 2 0 2 3 1 0 3
output:
3 10 1 1 2
result:
ok 5 lines
Test #2:
score: -100
Runtime Error
input:
2000 10 10 10 4 5 3 6 8 9 2 1 7 10 4 5 6 3 8 9 1 2 10 7 5 4 3 6 8 9 1 2 7 10 4 5 6 3 8 9 1 2 7 10 4 5 3 6 8 9 2 1 10 7 4 5 6 3 8 9 1 2 10 7 5 4 6 3 8 9 1 2 7 10 5 4 6 3 8 9 1 2 10 7 4 5 6 3 8 9 2 1 7 10 5 4 3 6 8 9 2 1 10 7 3 1 6 5 7 8 0 2 3 7 9 9 2 1 9 6 1 6 7 2 3 0 0 4 1 8 1 1 8 7 10 10 10 9 10 5 ...
output:
10 0 1 0 12 4 1 10 73 0 1 1 7 1 1 3 3 3 0 6 1 6 28 0 0 10 10 6 6 15 0 3 16 80 1 92 6 1 7 1 3 101 89 115 3 10 65 10 71 3 1 3 16 -1 0 13 18 -3 77 71 0 0 1 1 2 0 0 1 2 1 1 0 0 3 0 17 0 18 0 11 3 6 16 16 0 11 16 1 4 15 1 15 4 184 0 2 0 6 158 140 10 15 3 3 3 6 1 1 10 28 1 0 0 6 3 0 3 4 0 0 1 171 0 0 3 17...