QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#649772 | #8557. Goldberg Machine | caijianhong | RE | 9ms | 66276kb | C++23 | 4.4kb | 2024-10-18 10:11:37 | 2024-10-18 10:11:39 |
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];
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 66276kb
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: 9ms
memory: 65292kb
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: -100
Runtime Error
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...