QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#865429 | #9731. Fuzzy Ranking | surenjamts# | TL | 294ms | 20928kb | C++20 | 5.4kb | 2025-01-21 18:30:11 | 2025-01-21 18:30:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// We'll keep "int" as 64-bit:
using ll = long long;
#define int long long
#define all(x) (x).begin(), (x).end()
#define put(x) cout << (x) << "\n"
// Kosaraju's algorithm for SCC
void dfs1(int node, const vector<vector<int>>& adj, vector<bool>& visited, stack<int>& st) {
visited[node] = true;
for (int nx : adj[node]) {
if (!visited[nx]) dfs1(nx, adj, visited, st);
}
st.push(node);
}
void dfs2(int node, const vector<vector<int>>& radj, vector<bool>& visited, vector<int>& comp) {
visited[node] = true;
comp.push_back(node);
for (int nx : radj[node]) {
if (!visited[nx]) dfs2(nx, radj, visited, comp);
}
}
vector<int> buildSCC(int n, const vector<vector<int>>& edges) {
// 1) build adjacency
vector<vector<int>> adj(n), radj(n);
for (auto &e : edges) {
adj[e[0]].push_back(e[1]);
radj[e[1]].push_back(e[0]);
}
// 2) DFS for finishing order
stack<int> st;
vector<bool> visited(n,false);
for(int i=0; i<n; i++){
if(!visited[i]) dfs1(i, adj, visited, st);
}
// 3) DFS on reversed in order
fill(visited.begin(), visited.end(), false);
vector<int> compID(n, 0);
int cIndex = 0;
while(!st.empty()){
int node = st.top(); st.pop();
if(!visited[node]) {
cIndex++;
vector<int> comp;
dfs2(node, radj, visited, comp);
for (auto v : comp) {
compID[v] = cIndex;
}
}
}
return compID; // compID[v] is 1-based index of which SCC v belongs to
}
// Utility: sum of pairs in an interval of length L
inline ll pairs_in_interval(ll L) {
if (L <= 1) return 0;
return (L * (L - 1)) / 2;
}
// Get compID-segments in a contiguous slice of the list array
// listArr = your list of nodes (size n), compID[node] = scc
// We'll iterate from start..end (mod n if needed) and sum pairs
ll countPairsInSubarray(const vector<int>& listArr,
const vector<int>& compID,
int n, int start, int end, bool wrap) {
// If not wrap and start <= end, we just do a straightforward pass [start..end].
// If wrap is true, we do [start..n-1], then [0..end].
// We'll split whenever compID changes.
ll ans = 0;
// We'll keep a running length of the current compID block
ll blockLen = 1;
int currentComp = compID[ listArr[start] ];
auto advance = [&](int &i) {
i = (i + 1) % n; // wrap around
};
// The total number of elements in the sub-array
int totalLen;
if (!wrap) {
totalLen = (end - start + 1);
} else {
// e.g. if start=2, end=1, n=5 => sub-array length= (5-2) + (1+1)= 4 => actually 5?
// Actually (end - start + n) % n + 1 is a robust formula
totalLen = ((end - start) % n + n) % n + 1;
}
int idx = start;
for (int step = 1; step < totalLen; step++) {
// Move idx forward
int nxt = (idx + 1) % n;
// If we're in non-wrap mode but idx == end, we should stop.
// But we rely on 'step < totalLen' anyway.
idx = nxt;
int c = compID[ listArr[idx] ];
if (c == currentComp) {
blockLen++;
} else {
ans += pairs_in_interval(blockLen);
blockLen = 1;
currentComp = c;
}
}
// add the final block
ans += pairs_in_interval(blockLen);
return ans;
}
void solve() {
int n, k, q;
cin >> n >> k >> q;
// Build edges from k lists
vector<vector<int>> lists(k, vector<int>(n));
vector<vector<int>> edges;
edges.reserve(k * (n - 1));
// Read permutations
for(int i=0; i<k; i++){
for(int j=0; j<n; j++){
cin >> lists[i][j];
lists[i][j]--; // zero-based
if(j>0){
edges.push_back({lists[i][j-1], lists[i][j]});
}
}
}
// Build compID via Kosaraju
vector<int> compID = buildSCC(n, edges);
// compID[v] is 1-based scc index
ll prev = 0;
while(q--){
ll id, l, r;
cin >> id >> l >> r;
// Decode
id = (id + prev) % k;
l = (l + prev) % n;
r = (r + prev) % n;
// If same comp => (cyclic interpretation) from sample #1
// but that sample only used "full cycle" if l>r. So let's replicate the logic:
bool sameComp = (compID[ lists[id][l] ] == compID[ lists[id][r] ]);
ll ans = 0;
if (sameComp && (r < l)) {
// If the endpoints are in the same comp and r<l, the sample #1 (query2) gave us a full cycle.
// e.g. length = n => pairs = n*(n-1)/2
// Because in test #1, everything was one big SCC, so from l=2..r=1 ended up using all 5 elements.
ans = pairs_in_interval(n);
} else {
// We do a standard "sub-array" approach, possibly wrapping if r<l.
// Count consecutive compID blocks in that sub-array.
bool wrap = (r < l);
ans = countPairsInSubarray(lists[id], compID, n, l, r, wrap);
}
cout << ans << "\n";
prev = ans;
}
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while(T--){
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
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: 0
Accepted
time: 28ms
memory: 3584kb
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:
1 1 0 0 3 2 0 2 2 4 1 0 1 1 1 1 2 4 4 3 1 6 28 0 0 10 10 6 6 15 0 3 10 6 16 6 11 1 2 1 1 6 3 3 0 4 5 3 4 1 1 3 2 4 0 3 4 4 4 4 0 0 1 1 2 0 0 1 2 1 1 0 0 1 4 3 0 4 4 1 3 6 16 16 0 11 16 1 4 15 1 4 2 0 0 2 0 1 2 4 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 3 0 0 1 3 1 0 0 3 3 9 1 9 4 ...
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 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 3 0 1 0 0 0 1 0 0 1 1 1 1 3 0 3 0 0 0 0 0 10 3 1 3 1 2 1 1 1 0 3 3 1 0 1 6 3 6 6 1 0 0 0 0 0 0 2 1 2 0 3 1 1 1 3 1 3 1 3 3 6 3 6 0 1 1 0 6 0 3 1 1 1 1 0 0 0 0 0 0 6 0 0 10 1 0 0 0 1 2 1 1 0 0 0 1 1 1 0 0 1 0 1 1 0 1 3 0 0 0 3 1 0 10 0 4 0 0 2...
result:
ok 40000 lines
Test #5:
score: 0
Accepted
time: 40ms
memory: 3712kb
input:
2000 1 100 100 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 25 0 0 9 0 0 80 0 0 37 0 0 18 0 0 24 0 0 5 0 0 87 0 0 50 0 0 63 0 0 53 0 0 62 0 0 37 ...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 200000 lines
Test #6:
score: 0
Accepted
time: 18ms
memory: 5436kb
input:
10 20 1000 2000 2 5 1 3 12 16 14 15 7 4 19 13 18 10 20 9 11 8 6 17 5 2 12 1 16 3 19 4 7 15 14 18 13 10 9 20 11 8 6 17 2 5 1 16 12 3 7 14 4 19 15 13 18 10 20 9 11 17 6 8 2 5 1 16 3 12 15 7 4 14 19 18 13 10 9 20 11 8 6 17 5 2 3 12 1 16 14 7 4 15 19 18 13 10 20 9 11 6 17 8 2 5 3 1 12 16 19 15 7 4 14 18...
output:
11 0 11 10 1 11 5 10 10 5 13 2 0 18 2 14 2 18 10 13 1 1 0 1 4 7 6 0 15 11 1 4 6 19 3 9 3 1 0 20 2 16 1 0 3 2 3 0 18 1 17 1 1 8 1 5 17 1 3 6 1 2 15 15 2 1 0 3 18 22 0 1 2 15 13 12 20 3 0 9 15 1 4 17 22 16 8 6 13 0 7 11 15 19 15 6 7 10 17 12 9 1 11 1 0 4 6 17 1 17 6 5 1 1 16 14 3 1 12 18 1 0 18 12 17 ...
result:
ok 20000 lines
Test #7:
score: 0
Accepted
time: 22ms
memory: 4096kb
input:
33 3 2000 2000 1 2 3 2 1 3 2 1 3 2 1 3 1 2 3 2 1 3 2 1 3 1 2 3 2 1 3 1 2 3 2 1 3 2 1 3 2 1 3 1 2 3 2 1 3 2 1 3 2 1 3 1 2 3 2 1 3 2 1 3 1 2 3 1 2 3 2 1 3 1 2 3 2 1 3 1 2 3 2 1 3 1 2 3 2 1 3 2 1 3 1 2 3 2 1 3 2 1 3 2 1 3 1 2 3 2 1 3 1 2 3 1 2 3 2 1 3 1 2 3 1 2 3 1 2 3 1 2 3 2 1 3 2 1 3 1 2 3 2 1 3 2 1...
output:
1 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0 1 0 0 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 0 0 1 0 1 1 0 0 0 1 1 0 1 1 0 1 0 1 0 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 0 0 0 0 1 1 0 0 1 1 1 0 1 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 1 0 1 0 1 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 ...
result:
ok 66000 lines
Test #8:
score: 0
Accepted
time: 85ms
memory: 5488kb
input:
10 100 200 20000 33 77 70 12 88 3 29 59 63 41 75 18 42 83 96 44 67 73 79 48 92 54 78 93 21 14 24 72 91 25 71 97 2 89 76 20 37 95 68 87 39 31 5 100 9 23 74 80 7 27 50 69 52 82 81 85 56 84 34 32 17 36 11 16 8 57 49 30 60 86 62 99 13 19 98 43 6 4 46 58 64 45 51 53 10 28 90 26 40 94 35 22 61 15 47 1 55 ...
output:
1 4 2 3 2 4 0 2 1 1 1 0 0 4 0 4 3 2 2 1 4 0 4 0 2 2 4 2 2 0 2 2 1 0 0 1 3 2 2 0 0 0 3 2 0 1 4 1 2 2 3 1 3 0 2 1 1 4 2 1 0 0 3 0 0 2 0 2 4 3 0 4 0 0 3 0 2 2 0 4 3 4 2 3 2 0 2 0 1 1 2 1 2 1 4 0 1 0 1 2 0 2 4 1 0 2 4 2 3 1 4 2 0 2 0 2 0 2 2 0 1 2 1 2 0 3 2 1 2 1 1 4 1 1 1 2 1 2 2 1 0 2 0 1 0 1 1 1 0 2 ...
result:
ok 200000 lines
Test #9:
score: 0
Accepted
time: 175ms
memory: 20928kb
input:
1 316 632 200000 36 93 100 184 246 134 89 157 219 9 18 29 8 109 159 3 255 66 257 27 290 286 283 132 216 175 167 208 238 31 220 296 271 113 269 127 156 300 121 267 99 122 90 288 64 210 28 144 262 60 53 6 96 268 276 279 13 174 287 78 295 72 143 155 146 197 107 35 24 88 187 110 163 204 2 25 226 119 164...
output:
7 87 10 47 74 55 79 64 19 2 104 21 54 1 4 72 81 57 12 74 28 77 82 77 7 22 56 6 81 4 28 24 56 19 25 7 7 4 64 68 51 16 95 23 67 31 33 45 72 69 65 7 24 11 85 25 0 1 0 20 72 43 56 12 16 76 98 91 50 84 23 71 8 14 22 49 64 3 24 2 15 5 90 11 26 40 3 12 4 23 88 65 34 42 25 4 80 3 6 20 41 83 12 10 43 24 3 77...
result:
ok 200000 lines
Test #10:
score: 0
Accepted
time: 294ms
memory: 20920kb
input:
1 632 316 200000 625 142 323 331 85 521 376 51 411 31 418 11 16 66 112 611 459 622 148 247 122 587 249 141 20 88 439 334 233 474 305 381 203 559 228 595 326 535 128 449 138 28 86 56 109 102 428 194 612 256 466 598 189 539 237 59 225 528 200 330 490 560 133 57 590 632 422 213 340 310 49 195 41 217 19...
output:
60 34 45 304 31 71 52 36 123 38 76 28 200 223 55 58 11 275 202 76 285 30 154 113 40 48 355 332 358 33 240 46 29 91 147 75 318 62 162 200 171 124 228 31 285 59 16 206 114 44 163 87 53 65 221 298 23 172 123 91 298 14 57 6 8 71 1 84 3 2 58 20 6 111 244 220 25 137 86 269 134 254 68 64 129 14 65 108 43 3...
result:
ok 200000 lines
Test #11:
score: 0
Accepted
time: 221ms
memory: 19508kb
input:
1 447 447 200000 108 218 332 238 439 329 405 417 75 380 227 395 285 112 24 290 141 390 269 155 364 36 79 403 170 284 348 250 93 331 232 90 419 228 21 424 213 365 353 404 274 144 146 291 88 29 44 12 362 233 312 369 400 95 142 17 327 111 414 86 58 314 163 401 358 197 441 157 160 40 220 180 283 10 63 6...
output:
720 254 101 688 77 209 15 516 644 262 61 102 719 85 49 52 471 94 275 145 3 714 101 385 407 241 154 441 839 247 124 278 100 290 408 290 567 42 72 320 296 528 471 420 293 295 688 516 354 159 812 468 487 671 190 514 396 154 887 64 119 125 604 27 901 669 651 148 432 673 291 183 3 123 946 722 783 382 367...
result:
ok 200000 lines
Test #12:
score: -100
Time Limit Exceeded
input:
2 10000 10 100000 9169 6526 4977 9620 6327 8688 1778 4867 8252 2611 762 6164 1606 5796 6803 7006 5759 9972 8099 6268 5700 9868 896 2146 7128 1326 2696 2311 8764 6495 8013 6340 8057 6116 8447 5480 6736 9968 1048 1357 9013 8647 2334 8332 1514 6336 1486 5441 2963 2814 6910 4862 9616 1340 9839 3105 3703...
output:
43 225 406 1980 718 2299 362 985 1715 111 2449 1855 241 1928 461 1018 2304 1158 2562 446 176 1874 1743 8 165 1024 1025 2514 267 2165 1126 428 1804 14 969 590 765 1839 293 2166 1189 434 83 1862 916 48 2000 126 1170 101 2535 809 1422 589 123 44 1265 794 1025 1568 88 1483 498 2464 904 511 997 305 513 2...