QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#532031 | #2344. Hobson's Trains | Swarthmore# | AC ✓ | 514ms | 699300kb | C++20 | 4.3kb | 2024-08-24 23:42:38 | 2024-08-24 23:42:38 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0Rd(i, a) for (int i = (a) - 1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define sz(x) (int) (int)(x).size()
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
const char nl = '\n';
int N, K;
const int maxn = 5e5+5;
int fwd[maxn];
vector<int> bck[maxn];
bool incycle[maxn];
int cycleSize[maxn];
int ans[maxn];
int cur[maxn], vis[maxn];
vector<int> st;
vector<vector<int>> cycles;
pair<int, int> cycleIndex[maxn];
void DFS(int i) {
if (vis[i]) return;
vis[i] = 1;
cur[i] = 1;
st.push_back(i);
if (cur[fwd[i]]) {
int cyc = 0;
for (int j = sz(st)-1; j >= 0; j--) {
incycle[st[j]] = true;
cyc++;
if (st[j] == fwd[i]) break;
}
int idx = cyc - 1;
for (int j = sz(st)-1; j >= 0; j--) {
cycleSize[st[j]] = cyc;
cycleIndex[st[j]] = {cycles.size(), idx--};
if (st[j] == fwd[i]) break;
}
cycles.push_back(vector<int>(cyc, 0));
}
else {
DFS(fwd[i]);
}
st.pop_back();
cur[i] = 0;
}
struct DS {
int sum = 0;
deque<int> v;
int size() {
return v.size();
}
void add() {
sum++;
v.push_front(1);
}
void pop() {
assert(!v.empty());
sum -= v.back();
v.pop_back();
}
};
void merge(DS& a, DS& b) {
if (a.size() < b.size()) swap(a, b);
for (int i = 0; i < b.size(); i++) {
a.v[i] += b.v[i];
a.sum += b.v[i];
}
}
DS* dfs(int i) {
DS* vi = new DS();
for (int j: bck[i]) {
if (!incycle[j]) {
DS* vj = dfs(j);
merge(*vi, *vj);
}
}
vi->add();
if (vi->size() > K + 1) vi->pop();
// cout << i << ": " << vi->sum << endl;
// for (int j: vi->v) {
// cout << j << ' ';
// }
// cout << endl;
ans[i] += vi->sum;
return vi;
}
int jmp[20][maxn];
int jump(int i, int d) {
for (int k = 19; k >= 0; k--) {
if (d & (1 << k)) i = jmp[k][i];
}
return i;
}
void addRange(int i, int j, int x) {
// cout << "adding cycle " << i << ' ' << j << ": " << x << endl;
// [i, j]
auto [ai, ci] = cycleIndex[i];
auto [aj, cj] = cycleIndex[j];
assert(ai == aj);
if (ci <= cj) {
cycles[ai][ci] += x;
if (cj+1 < sz(cycles[ai])) cycles[ai][cj+1] -= x;
}
else {
cycles[ai][ci] += x;
cycles[ai][0] += x;
if (cj+1 < sz(cycles[ai])) cycles[ai][cj+1] -= x;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> K;
for (int i = 1; i <= N; i++) {
int d; cin >> d;
fwd[i] = d;
jmp[0][i] = d;
bck[d].push_back(i);
}
for (int k = 1; k < 20; k++) {
for (int i = 1; i <= N; i++) {
jmp[k][i] = jmp[k-1][jmp[k-1][i]];
}
}
for (int i = 1; i <= N; i++) {
DFS(i);
}
for (int i = 1; i <= N; i++) {
if (incycle[i]) {
DS* vi = dfs(i);
// cout << i << ": " << cycleIndex[i].first << ' ' << cycleIndex[i].second << endl;
int extra = (K + 1) - vi->size();
// cout << i << ": " << vi->size() << ' ' << extra << ": " << cycleSize[i] << endl;
if (extra >= cycleSize[i]) {
// add vi->sum to all except i
addRange(fwd[i], jump(fwd[i], cycleSize[i] - 2), vi->sum);
}
else {
int j = jump(i, extra);
// add vi->sum to all [fwd[i], j]
if (extra > 0) addRange(fwd[i], j, vi->sum);
j = fwd[j];
while (vi->size() > 0 && j != i) {
vi->pop();
ans[j] += vi->sum;
j = fwd[j];
}
}
}
}
for (auto& v: cycles) {
for (int i = 1; i < sz(v); i++) {
v[i] += v[i-1];
}
}
for (int i = 1; i <= N; i++) {
if (incycle[i]) {
auto [ai, ci] = cycleIndex[i];
ans[i] += cycles[ai][ci];
}
}
for (int i = 1; i <= N; i++) {
cout << ans[i] << '\n';
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 50904kb
Test #2:
score: 0
Accepted
time: 0ms
memory: 52720kb
Test #3:
score: 0
Accepted
time: 55ms
memory: 179248kb
Test #4:
score: 0
Accepted
time: 30ms
memory: 128456kb
Test #5:
score: 0
Accepted
time: 35ms
memory: 188752kb
Test #6:
score: 0
Accepted
time: 30ms
memory: 159384kb
Test #7:
score: 0
Accepted
time: 16ms
memory: 114984kb
Test #8:
score: 0
Accepted
time: 27ms
memory: 155644kb
Test #9:
score: 0
Accepted
time: 16ms
memory: 153160kb
Test #10:
score: 0
Accepted
time: 27ms
memory: 171812kb
Test #11:
score: 0
Accepted
time: 16ms
memory: 172028kb
Test #12:
score: 0
Accepted
time: 3ms
memory: 50736kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 50776kb
Test #14:
score: 0
Accepted
time: 0ms
memory: 50748kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 48924kb
Test #16:
score: 0
Accepted
time: 0ms
memory: 50900kb
Test #17:
score: 0
Accepted
time: 0ms
memory: 50680kb
Test #18:
score: 0
Accepted
time: 39ms
memory: 145980kb
Test #19:
score: 0
Accepted
time: 36ms
memory: 143992kb
Test #20:
score: 0
Accepted
time: 50ms
memory: 143808kb
Test #21:
score: 0
Accepted
time: 0ms
memory: 50844kb
Test #22:
score: 0
Accepted
time: 24ms
memory: 179580kb
Test #23:
score: 0
Accepted
time: 0ms
memory: 50720kb
Test #24:
score: 0
Accepted
time: 0ms
memory: 50700kb
Test #25:
score: 0
Accepted
time: 3ms
memory: 50680kb
Test #26:
score: 0
Accepted
time: 3ms
memory: 50712kb
Test #27:
score: 0
Accepted
time: 0ms
memory: 50688kb
Test #28:
score: 0
Accepted
time: 0ms
memory: 50688kb
Test #29:
score: 0
Accepted
time: 0ms
memory: 50780kb
Test #30:
score: 0
Accepted
time: 0ms
memory: 50684kb
Test #31:
score: 0
Accepted
time: 0ms
memory: 48668kb
Test #32:
score: 0
Accepted
time: 3ms
memory: 50752kb
Test #33:
score: 0
Accepted
time: 3ms
memory: 54772kb
Test #34:
score: 0
Accepted
time: 0ms
memory: 50688kb
Test #35:
score: 0
Accepted
time: 0ms
memory: 50780kb
Test #36:
score: 0
Accepted
time: 0ms
memory: 50684kb
Test #37:
score: 0
Accepted
time: 0ms
memory: 50968kb
Test #38:
score: 0
Accepted
time: 0ms
memory: 50684kb
Test #39:
score: 0
Accepted
time: 0ms
memory: 50688kb
Test #40:
score: 0
Accepted
time: 0ms
memory: 50640kb
Test #41:
score: 0
Accepted
time: 0ms
memory: 50696kb
Test #42:
score: 0
Accepted
time: 3ms
memory: 50688kb
Test #43:
score: 0
Accepted
time: 3ms
memory: 50680kb
Test #44:
score: 0
Accepted
time: 0ms
memory: 54800kb
Test #45:
score: 0
Accepted
time: 0ms
memory: 50964kb
Test #46:
score: 0
Accepted
time: 0ms
memory: 51616kb
Test #47:
score: 0
Accepted
time: 0ms
memory: 59696kb
Test #48:
score: 0
Accepted
time: 43ms
memory: 140940kb
Test #49:
score: 0
Accepted
time: 15ms
memory: 167252kb
Test #50:
score: 0
Accepted
time: 23ms
memory: 167132kb
Test #51:
score: 0
Accepted
time: 30ms
memory: 177516kb
Test #52:
score: 0
Accepted
time: 20ms
memory: 175136kb
Test #53:
score: 0
Accepted
time: 306ms
memory: 517356kb
Test #54:
score: 0
Accepted
time: 332ms
memory: 473068kb
Test #55:
score: 0
Accepted
time: 104ms
memory: 659676kb
Test #56:
score: 0
Accepted
time: 361ms
memory: 698836kb
Test #57:
score: 0
Accepted
time: 474ms
memory: 485976kb
Test #58:
score: 0
Accepted
time: 514ms
memory: 547996kb
Test #59:
score: 0
Accepted
time: 0ms
memory: 50772kb
Test #60:
score: 0
Accepted
time: 11ms
memory: 65252kb
Test #61:
score: 0
Accepted
time: 16ms
memory: 81956kb
Test #62:
score: 0
Accepted
time: 34ms
memory: 111212kb
Test #63:
score: 0
Accepted
time: 66ms
memory: 165428kb
Test #64:
score: 0
Accepted
time: 122ms
memory: 285872kb
Test #65:
score: 0
Accepted
time: 298ms
memory: 516984kb
Test #66:
score: 0
Accepted
time: 3ms
memory: 69160kb
Test #67:
score: 0
Accepted
time: 7ms
memory: 83548kb
Test #68:
score: 0
Accepted
time: 11ms
memory: 118560kb
Test #69:
score: 0
Accepted
time: 23ms
memory: 180372kb
Test #70:
score: 0
Accepted
time: 74ms
memory: 313376kb
Test #71:
score: 0
Accepted
time: 149ms
memory: 577788kb
Test #72:
score: 0
Accepted
time: 0ms
memory: 71008kb
Test #73:
score: 0
Accepted
time: 3ms
memory: 88992kb
Test #74:
score: 0
Accepted
time: 14ms
memory: 133124kb
Test #75:
score: 0
Accepted
time: 28ms
memory: 212020kb
Test #76:
score: 0
Accepted
time: 43ms
memory: 374132kb
Test #77:
score: 0
Accepted
time: 120ms
memory: 699300kb
Test #78:
score: 0
Accepted
time: 67ms
memory: 248656kb
Test #79:
score: 0
Accepted
time: 79ms
memory: 597676kb
Test #80:
score: 0
Accepted
time: 90ms
memory: 625268kb
Test #81:
score: 0
Accepted
time: 254ms
memory: 546716kb
Test #82:
score: 0
Accepted
time: 264ms
memory: 544644kb