QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#820776 | #9731. Fuzzy Ranking | TosakaUCW | WA | 12ms | 3580kb | C++23 | 4.8kb | 2024-12-19 01:37:06 | 2024-12-19 01:37:06 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
#define int i64
#define pb push_back
#define ep emplace
#define eb emplace_back
using std::cerr;
// using namespace std::views;
// using namespace std::ranges;
using std::max, std::min, std::swap, std::array;
using std::cin, std::cout, std::string, std::vector;
using std::ostream;
int read(int x = 0, int f = 0, char ch = getchar()) {
while (ch < 48 or 57 < ch) f = ch == 45, ch = getchar();
while(48 <= ch and ch <= 57) x = x * 10 + ch - 48, ch = getchar();
return f ? -x : x;
}
template <class T1, class T2> ostream &operator<<(ostream &os, const std::pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << '\n'; }
using pii = std::pair<int, int>;
#define fi first
#define se second
struct SCC {
int n;
std::vector<std::vector<int>> adj;
std::vector<int> stk;
std::vector<int> dfn, low, bel;
int cur, cnt;
SCC() {}
SCC(int n) {
init(n);
}
void init(int n) {
this->n = n;
adj.assign(n, {});
dfn.assign(n, -1);
low.resize(n);
bel.assign(n, -1);
stk.clear();
cur = cnt = 0;
}
void addEdge(int u, int v) {
adj[u].push_back(v);
}
void dfs(int x) {
dfn[x] = low[x] = cur++;
stk.push_back(x);
for (auto y : adj[x]) {
if (dfn[y] == -1) {
dfs(y);
low[x] = std::min(low[x], low[y]);
} else if (bel[y] == -1) {
low[x] = std::min(low[x], dfn[y]);
}
}
if (dfn[x] == low[x]) {
int y;
do {
y = stk.back();
bel[y] = cnt;
stk.pop_back();
} while (y != x);
cnt++;
}
}
std::vector<int> work() {
for (int i = 0; i < n; i++) {
if (dfn[i] == -1) {
dfs(i);
}
}
return bel;
}
};
void solve() {
int n = read(), k = read(), q = read();
vector a(k, vector<int>(n));
SCC scc(n);
vector<vector<int>> g(n);
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = read() - 1;
if (j > 0) {
g[a[i][j - 1]].eb(a[i][j]);
scc.addEdge(a[i][j - 1], a[i][j]);
}
}
}
auto bel = scc.work();
vector<int> cnt(n);
for (int i = 0; i < n; i++) {
cnt[bel[i]]++;
}
vector sum(k, vector<int>(n));
for (int i = 0; i < k; i++) {
vector<int> vis(n);
for (int j = 0; j < n; j++) {
if (j > 0) sum[i][j] += sum[i][j - 1];
if (!vis[bel[j]]) {
vis[j] = 1;
sum[i][j] += cnt[bel[j]] * (cnt[bel[j]] - 1) / 2;
}
}
}
// cerr << bel << '\n';
// cerr << cnt << '\n';
for (int las = 0; q--; ) {
int id = (read() + las) % k;
int x = (read() + las) % n;
int y = (read() + las) % n;
// cerr << "query: " << id << ' ' << x << ' ' << y << '\n';
int ans = 0;
int l = x;
for (int L = x, R = y; L <= R; ) {
int mid = L + R >> 1;
if (bel[a[id][mid]] == bel[a[id][x]]) {
l = mid, L = mid + 1;
} else {
R = mid - 1;
}
}
int r = y;
for (int L = x, R = y; L <= R; ) {
int mid = L + R >> 1;
if (bel[a[id][mid]] == bel[a[id][y]]) {
r = mid, R = mid - 1;
} else {
L = mid + 1;
}
}
// [x, l] same
// [l + 1, r - 1] diff
// [r, y] same
if (l > r) {
// int t = min(cnt[bel[a[id][x]]], y - x + 1);
int t = y - x + 1;
ans += t * (t - 1) / 2;
// cout << "t: " << cnt[bel[a[id][x]]] << '\n';
} else {
if (l + 1 <= r - 1) {
ans += sum[id][r - 1];
ans -= sum[id][l];
}
ans += (l - x + 1) * (l - x) / 2;
ans += (y - r + 1) * (y - r) / 2;
}
// cerr << "l, r: " << l << ' ' << r << '\n';
cout << ans << '\n';
las = ans;
}
}
signed main() {
for (int T = read(); T--; solve());
// solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3516kb
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
Wrong Answer
time: 12ms
memory: 3580kb
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:
3 1 0 0 4 10 1 3 2 5 1 0 4 0 3 3 3 3 0 3 1 6 28 0 0 10 10 6 6 15 0 3 10 6 10 21 6 1 2 1 4 21 15 3 3 10 3 10 6 3 1 8 10 1 0 3 4 9 6 8 0 0 1 2 0 0 0 2 0 1 2 0 0 1 7 10 0 7 0 4 3 6 16 16 0 11 16 1 4 15 1 9 6 1 1 3 28 6 15 6 0 0 0 0 0 0 0 0 0 0 1 0 0 6 3 0 3 4 0 0 0 0 0 0 0 0 0 0 0 0 6 1 0 1 6 0 0 0 6 6...
result:
wrong answer 1st lines differ - expected: '1', found: '3'