QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#649776 | #8557. Goldberg Machine | caijianhong | RE | 34ms | 71212kb | C++23 | 4.4kb | 2024-10-18 10:14:05 | 2024-10-18 10:14:06 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define endl "\n"
#define debug(...) void(0)
#endif
using LL = long long;
namespace ds_bf {
int c[200010];
void add(int l, int r, int k) {
if (l > r) return ;
for (int i = l; i <= r; i++) c[i] += k;
}
LL sum(int n, int k) {
LL res = 0;
for (int i = 1; i <= n; i++) res += min(k, c[i]);
return res;
}
int select(int n, LL k, int idx) {
assert(idx > 0);
for (int i = 1; i <= n; i++) if (c[i] <= k && --idx == 0) return i;
return -1;
}
};
namespace ds {
constexpr int B = 1400, N = 1e5 + 10;
struct block {
int elem[B], tag, ptr[N], val[B];
LL psm[B];
block() {
memset(elem, 0, sizeof elem);
memset(val, 0, sizeof val);
memset(psm, 0, sizeof psm);
tag = 0;
fill(ptr + 1, ptr + N, B);
}
void add_value(int v) { ++val[--ptr[v + 1]]; }
void del_value(int v) { --val[ptr[v]++]; }
void buildsum() {
psm[0] = val[0];
for (int i = 1; i < B; i++) psm[i] = psm[i - 1] + val[i];
}
void _add(int l, int r) {
for (int i = l; i <= r; i++) add_value(elem[i]++);
buildsum();
}
void _del(int l, int r) {
for (int i = l; i <= r; i++) del_value(elem[i]--);
buildsum();
}
void add(int l, int r, int k) { return k == 1 ? _add(l, r) : _del(l, r); }
LL calc(int k) { // sum_x min(x, k)
int pos = ptr[k - tag];
return (pos ? psm[pos - 1] + 1ll * pos * tag: 0) + 1ll * (B - pos) * k;
}
int count(int k) { // sum_x [x <= k]
return ptr[k - tag + 1];
}
int select(int k, int idx) { // idx-th x, x <= k
for (int i = 0; i < B; i++) if (elem[i] <= k - tag && --idx == 0) return i;
throw logic_error("ds::block::select: wrong argument");
}
} blk[N * 2 / B + 10];
void add(int l, int r, int k) {
debug("add(%d, %d, %d)\n", l, r, k);
if (l / B == r / B) return blk[l / B].add(l % B, r % B, k);
if (l % B) blk[l / B].add(l % B, B - 1, k), l = (l / B + 1) * B;
while (l + B - 1 <= r) blk[l / B].tag += k, l += B;
assert(l / B == r / B), blk[l / B].add(l % B, r % B, k);
}
LL sum(int n, int k) {
LL res = 0;
for (int i = 0; i * B <= n; i++) res += blk[i].calc(k);
debug("sum(%d, %d) = %lld\n", n, k, res);
return res;
}
int select(int n, int k, int idx) {
++idx; // a[0] = 0
for (int i = 0; i * B <= n; i++) {
if ((i + 1) * B <= n && blk[i].count(k) < idx) idx -= blk[i].count(i);
else return blk[i].select(k, idx) + i * B;
}
throw logic_error("ds::select: wrong argument");
}
};
int n, dfn[100010], rdel[100010], ptr[100010], fa[100010], cnt, siz[100010], rnk[200010], q;
vector<int> g[100010];
void dfs(int u, int _fa) {
if (u != 1) {
int &pos = rdel[u] = (int)(find(g[u].begin(), g[u].end(), _fa) - g[u].begin()), c = (int)g[u].size();
pos = (pos + 1) % c, ptr[u] = (ptr[u] - pos + c) % c;
rotate(g[u].begin(), g[u].begin() + pos, g[u].end());
}
dfn[u] = ++cnt, siz[u] = 1, fa[u] = _fa, rnk[cnt] = u;
for (int v : g[u]) if (v != _fa) dfs(v, u), siz[u] += siz[v], rnk[++cnt] = u;
}
void update_t(int u, int k) {
int l = dfn[u] + 1, r;
if (u > 1 && ptr[u] + 1 == (int)g[u].size()) r = dfn[u] + siz[u] * 2 - 2;
else r = dfn[g[u][ptr[u]]] - 1;
r = min(r, cnt);
if (l <= r) ds::add(l, r, k);
}
int main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#endif
cin >> n;
for (int i = 1, c; i <= n; i++) {
cin >> c >> ptr[i];
g[i].resize(c);
for (int j = 0; j < c; j++) cin >> g[i][j];
}
dfs(1, 0), --cnt;
for (int i = 1; i <= n; i++) update_t(i, +1);
cin >> q;
while (q--) {
char op;
cin >> op;
if (op == 'C') {
int x, y;
cin >> x >> y;
update_t(x, -1);
int c = (int)g[x].size();
ptr[x] = (y - rdel[x] + c) % c;
update_t(x, +1);
} else {
LL k;
cin >> k;
++k;
int now = 0;
auto calc = [&](int now) { return cnt * now - ds::sum(cnt, now); };
for (int j = __lg(n); j >= 0; j--) if (calc(now + (1 << j)) < k) now += 1 << j;
debug("calc(%d) = %lld\n", now, calc(now));
if (now >= n) cout << rnk[(k - calc(now) - 1) % cnt + 1] << endl;
else cout << rnk[ds::select(cnt, now, k - calc(now))] << endl;
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 67796kb
input:
7 2 0 2 3 3 0 1 4 5 3 0 1 6 7 1 0 2 1 0 2 1 0 3 1 0 3 5 Q 10 C 2 1 Q 10 C 3 2 Q 10
output:
1 4 1
result:
ok 3 number(s): "1 4 1"
Test #2:
score: 0
Accepted
time: 6ms
memory: 71168kb
input:
2 1 0 2 1 0 1 10000 Q 330436 Q 980610 Q 275 Q 22846 Q 85515 Q 339826 Q 647638 Q 689585 Q 153103 Q 934080 Q 633096 Q 431962 Q 692869 Q 914798 Q 436671 Q 832351 Q 78371 Q 328899 Q 727824 Q 351781 Q 819165 Q 267140 Q 722576 Q 133777 Q 414375 Q 205621 Q 160403 Q 445832 Q 628729 Q 532763 Q 934703 Q 80779...
output:
1 1 2 1 2 1 1 2 2 1 1 1 2 1 2 2 2 2 1 2 2 1 1 2 2 2 2 1 2 2 2 1 2 1 2 1 2 1 1 2 2 2 1 1 2 2 2 2 1 1 2 2 2 1 2 2 1 2 1 2 2 2 2 1 2 1 2 1 2 2 2 2 1 1 2 1 1 1 2 1 1 1 2 1 2 1 1 2 2 1 2 2 1 1 1 1 2 1 2 2 2 1 2 1 1 1 2 1 2 2 2 1 1 2 2 2 1 1 2 1 1 1 2 1 1 1 1 1 2 2 2 2 1 2 1 1 2 1 2 1 1 2 2 1 1 2 2 1 1 1 ...
result:
ok 10000 numbers
Test #3:
score: 0
Accepted
time: 27ms
memory: 71212kb
input:
3 2 1 2 3 1 0 1 1 0 1 100000 C 2 0 C 2 0 Q 145832 C 3 0 Q 19924 Q 443533 C 1 0 C 1 0 C 3 0 C 2 0 C 3 0 Q 635031 Q 280859 Q 509745 C 1 1 Q 262631 Q 340848 C 2 0 C 3 0 C 2 0 C 2 0 Q 775790 Q 614349 C 2 0 Q 684238 Q 797643 C 1 1 C 1 1 C 3 0 C 2 0 C 1 0 Q 669611 Q 257133 Q 631101 Q 731654 C 2 0 Q 942620...
output:
1 1 3 3 3 2 2 1 1 3 1 2 3 2 2 1 1 3 2 2 2 3 1 3 3 3 3 1 2 1 2 2 1 1 2 2 1 1 1 3 2 2 1 3 3 1 2 1 2 3 1 3 2 1 1 1 3 1 1 1 2 1 3 2 1 1 2 1 2 1 3 3 3 1 3 2 1 2 2 1 2 2 1 3 3 3 1 1 1 3 1 3 3 1 1 1 2 1 1 1 1 1 1 1 1 1 3 3 1 2 1 3 1 1 3 3 1 2 1 3 3 1 2 3 2 1 3 2 2 3 2 1 1 3 1 1 1 1 3 3 1 3 2 1 2 3 1 1 1 1 ...
result:
ok 50039 numbers
Test #4:
score: 0
Accepted
time: 34ms
memory: 71116kb
input:
100 3 1 2 7 3 5 2 12 82 1 45 5 6 4 50 4 83 9 1 6 4 2 16 22 59 3 4 3 11 2 10 21 5 2 20 27 3 14 60 4 0 53 1 13 8 6 5 56 39 15 96 7 68 4 0 80 3 18 31 6 2 48 30 5 65 24 36 2 1 5 23 3 0 2 81 33 3 2 7 37 17 3 1 69 73 6 4 2 78 26 8 19 2 0 4 54 2 1 13 100 5 4 61 57 9 28 32 2 1 29 15 2 0 66 6 2 0 5 35 4 0 25...
output:
15 10 8 5 11 71 13 12 2 7 49 17 16 23 20 45 20 3 39 10 36 59 90 76 8 96 65 14 59 24 30 69 55 56 40 4 69 39 22 53 4 57 45 77 7 2 21 42 53 48 19 5 2 29 85 5 3 82 69 39 67 30 61 39 31 85 39 3 39 49 84 5 93 49 17 75 60 40 70 88 42 18 3 63 54 22 39 39 39 9 1 7 22 17 36 2 48 24 61 95 31 16 10 96 77 14 13 ...
result:
ok 50092 numbers
Test #5:
score: -100
Runtime Error
input:
10000 1 0 2 2 1 3 1 2 0 4 2 2 0 5 3 2 0 6 4 2 1 7 5 2 1 6 8 2 1 7 9 2 1 8 10 2 1 11 9 2 1 12 10 2 1 11 13 2 1 14 12 2 0 15 13 2 1 14 16 2 0 15 17 2 1 18 16 2 1 17 19 2 1 18 20 2 1 19 21 2 0 20 22 2 1 21 23 2 1 24 22 2 1 23 25 2 1 24 26 2 0 27 25 2 0 26 28 2 0 27 29 2 1 28 30 2 1 31 29 2 1 32 30 2 0 ...