QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#608493 | #7627. Phony | DBsoleil# | WA | 4ms | 16136kb | C++20 | 7.8kb | 2024-10-03 22:13:49 | 2024-10-03 22:13:49 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
static constexpr int Maxn = 5e5 + 5;
static constexpr int64_t inf = 0x3f3f3f3f3f3f3f3f;
int n, qn, an, bn, b[Maxn];
int64_t a[Maxn], k, A[Maxn];
pair<int64_t, int> B1[Maxn], b1[Maxn];
uint64_t randu64(void) {
static const auto FIXED = chrono::steady_clock::now().time_since_epoch().count();
static mt19937_64 gen(FIXED); return gen();
} // randu64
namespace tp {
struct node {
int ls, rs, size, v;
uint64_t fix;
node() = default;
node(int v) : ls(0), rs(0), size(1), fix(randu64()), v(v) { }
} tr[Maxn * 4]; int tn;
template<typename ...Argv>
int newnode(Argv ...args) { return tr[++tn] = node(args...), tn; }
void print(int p) {
if (!p) return ;
print(tr[p].ls);
fprintf(stderr, "%d, ", tr[p].v);
print(tr[p].rs);
} // print
void pushup(int p) {
tr[p].size = tr[tr[p].ls].size + tr[tr[p].rs].size + 1;
} // pushup
int join(int l, int r) {
if (!l || !r) return l | r;
if (tr[l].fix < tr[r].fix) {
tr[l].rs = join(tr[l].rs, r);
return pushup(l), l;
} else {
tr[r].ls = join(l, tr[r].ls);
return pushup(r), r;
}
} // join
void split_v(int p, int v, int &l, int &r) { // v], (v
if (!p) l = r = 0;
else {
if (tr[p].v >= v) l = p, split_v(tr[p].rs, v, tr[l].rs, r), pushup(l);
else r = p, split_v(tr[p].ls, v, l, tr[r].ls), pushup(r);
}
} // split_v
void split_s(int p, int s, int &l, int &r) { // s], (s
if (!p) l = r = 0;
else {
if (tr[tr[p].ls].size + 1 <= s)
l = p, split_s(tr[p].rs, s - tr[tr[p].ls].size - 1, tr[l].rs, r), pushup(l);
else r = p, split_s(tr[p].ls, s, l, tr[r].ls), pushup(r);
}
} // split_s
int find(int p, int s) {
//fprintf(stderr, "find: p = "), print(p), fprintf(stderr, "\n");
if (tr[tr[p].ls].size + 1 == s) return tr[p].v;
if (tr[tr[p].ls].size >= s) return find(tr[p].ls, s);
else return find(tr[p].rs, s - tr[tr[p].ls].size - 1);
} // find
void insert(int &p, int64_t v) {
int A, B;
split_v(p, v, A, B);
p = join(A, join(newnode(v), B));
} // insert
} // namespace tp
struct tnode {
int ls, rs, size;
uint64_t fix;
int root, s;
int64_t v;
tnode() = default;
tnode(int root, int64_t v) : ls(0), rs(0), size(1), fix(randu64()),
root(root), s(tp::tr[root].size), v(v) { }
void renew(void) { s = tp::tr[root].size; }
} tr[Maxn << 1];
int root, tn;
template<typename ...Argv>
int newnode(Argv ...args) { return tr[++tn] = tnode(args...), tn; }
void pushup(int p) {
tr[p].size = tr[tr[p].ls].size + tr[tr[p].rs].size + 1;
tr[p].s = tp::tr[tr[p].root].size + tr[tr[p].ls].s + tr[tr[p].rs].s;
} // pushup
void print_all(int p) {
if (!p) return ;
print_all(tr[p].ls);
tp::print(tr[p].root); fprintf(stderr, "\n");
print_all(tr[p].rs);
} // print_all
int join(int l, int r) {
if (!l || !r) return l | r;
// fprintf(stderr, "BEGIN @#$$@# l: \n"); print_all(l); fprintf(stderr, "@!#$%!$%!$%$\n");
// fprintf(stderr, "BEGIN @#$$@# r: \n"); print_all(r); fprintf(stderr, "@!#$%!$%!$%$\n");
if (tr[l].fix < tr[r].fix) {
tr[l].rs = join(tr[l].rs, r);
pushup(l);
// fprintf(stderr, "#$@#$@#$#$$@#$ l: \n");
// print_all(l);
// fprintf(stderr, "END #$@#$@#$#$$@#$ l: \n");
return l;
} else {
tr[r].ls = join(l, tr[r].ls);
pushup(r);
// fprintf(stderr, "#$@#$@#$#$$@#$ r: \n");
// print_all(r);
// fprintf(stderr, "END #$@#$@#$#$$@#$ r: \n");
return r;
}
} // join
void split_a(int p, int s, int &l, int &r) { // s], (s
if (!p) l = r = 0;
else {
//fprintf(stderr, "s = %d\n", s);
//fprintf(stderr, "BEGIN ls: \n");
//print_all(tr[p].ls);
//fprintf(stderr, "END ls\n");
if (tr[tr[p].ls].s >= s) r = p, split_a(tr[p].ls, s, l, tr[r].ls), pushup(r);
else l = p, split_a(tr[p].rs, s - tr[tr[p].ls].s - tp::tr[tr[p].root].size, tr[l].rs, r), pushup(l);
}
} // split_a
void split_s(int p, int s, int &l, int &r) { // s], (s
if (!p) l = r = 0;
else {
if (tr[tr[p].ls].size + 1 <= s)
l = p, split_s(tr[p].rs, s - tr[tr[p].ls].size - 1, tr[l].rs, r), pushup(l);
else r = p, split_s(tr[p].ls, s, l, tr[r].ls), pushup(r);
}
} // split_s
void merge_all(int &p, int q) {
if (!q) return ;
tp::insert(p, tp::tr[q].v);
merge_all(p, tp::tr[q].ls);
merge_all(p, tp::tr[q].rs);
} // merge_all
int main(void) {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> qn >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) b1[i] = B1[i] = make_pair(a[i] % k, i), A[i] = a[i] /= k;
sort(B1 + 1, B1 + n + 1); bn = unique(B1 + 1, B1 + n + 1) - B1 - 1;
for (int i = 1; i <= n; ++i) b[i] = lower_bound(B1 + 1, B1 + bn + 1, b1[i]) - B1;
sort(A + 1, A + n + 1, greater<int64_t>()); an = unique(A + 1, A + n + 1) - A - 1;
for (int i = 1; i <= n; ++i) a[i] = lower_bound(A + 1, A + an + 1, a[i], greater<int64_t>()) - A;
static vector<int> ia[Maxn];
for (int i = 1; i <= n; ++i) ia[a[i]].push_back(b[i]);
for (int i = 1; i <= an; ++i) sort(ia[i].begin(), ia[i].end(), greater<int>());
for (int i = 1; i <= an; ++i) {
int64_t v = A[i]; int rt = 0;
for (int j: ia[i]) rt = tp::join(rt, tp::newnode(j));
root = join(root, newnode(rt, v));
}
while (qn--) {
//fprintf(stderr, "-----------------------------------------\n");
string op; int64_t t; cin >> op >> t;
int A, B, C, D, E, F;
if (op == "C") {
while (t > 0) {
/*fprintf(stderr, "==-=-=-=-==- root: ");
print_all(root);
fprintf(stderr, "END print_all root\n");*/
split_s(root, 1, A, B);
if (B == 0) {
int ss = tp::tr[tr[A].root].size;
int64_t T = t / ss, R = t % ss;
tr[A].v -= T;
tp::split_s(tr[A].root, R, D, tr[A].root); tr[A].renew();
E = newnode(D, tr[A].v - 1);
root = join(A, E);
t = 0;
} else {
split_s(B, 1, B, C);
if ((tr[A].v - tr[B].v) * tp::tr[tr[A].root].size < t) {
int64_t vv = tr[B].v;
t -= (tr[A].v - tr[B].v) * tp::tr[tr[A].root].size;
merge_all(tr[A].root, tr[B].root); tr[A].renew();
tr[A].v = vv;
root = join(A, C);
} else {
int ss = tp::tr[tr[A].root].size;
int64_t T = t / ss, R = t % ss;
tr[A].v -= T;
tp::split_s(tr[A].root, R, D, tr[A].root); tr[A].renew();
//fprintf(stderr, "tr[A].root: "), tp::print(tr[A].root), fprintf(stderr, "\n");
//fprintf(stderr, "#### D: "), tp::print(D), fprintf(stderr, "\n");
E = newnode(D, tr[A].v - 1);
if (tr[E].v == tr[B].v) {
merge_all(tr[E].root, tr[B].root); tr[E].renew();
//fprintf(stderr, "tr[E].root: "), tp::print(tr[E].root), fprintf(stderr, "\n");
E = join(E, C);
//fprintf(stderr, "tr[E].root: "), tp::print(tr[E].root), fprintf(stderr, "\n");
//fprintf(stderr, "A = %d, E = %d\n", A, E);
root = join(A, E);
//fprintf(stderr, "root: "); print_all(root); fprintf(stderr, "-!$%#$^#%^@#%^#!^#%^!#$\n");
} else {
root = join(A, join(E, join(B, C)));
}
t = 0;
}
}
}
} else {
split_a(root, t, B, C);
//fprintf(stderr, "tr[B].size = %d\n", tr[B].size);
split_s(B, tr[B].size - 1, A, B);
t = t - tr[A].s;
//fprintf(stderr, "A: t = %d\n", t);
int64_t v = tr[B].v;
int r = tp::find(tr[B].root, t);
//fprintf(stderr, "============= v = %lld, r = %d\n", v, r);
cout << v * k + B1[r].first << "\n";
root = join(A, join(B, C));
}
//print_all(root);
}
return 0;
} // main
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 16136kb
input:
3 5 5 7 3 9 A 3 C 1 A 2 C 2 A 3
output:
3 4 -1
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 4ms
memory: 15908kb
input:
5 8 8 294 928 293 392 719 A 4 C 200 A 5 C 10 A 2 C 120 A 1 A 3
output:
294 200 191 0 -2
result:
ok 5 lines
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 15884kb
input:
100 100 233 5101 8001 6561 6329 6305 7745 4321 811 49 1121 3953 8054 8415 9876 6701 4097 6817 6081 495 5521 2389 2042 4721 8119 7441 7840 8001 5756 5561 129 1 5981 4801 7201 8465 7251 6945 5201 5626 3361 5741 3650 7901 2513 8637 3841 5621 9377 101 3661 5105 4241 5137 7501 5561 3581 4901 561 8721 811...
output:
6881 9161 4721 8200 2945 7647 7531 5291 5001 2042 4721 4721 6881 4097 7201 7006 7035 7018 811 6752 2561 6683 6114 6135 3581 5291 1485 5957 5393 2042 5303 5171 5205 4721 5084 4029 4097 4591 4816 4586 4705 2042 4535 4454 4603 4435 3581 4345 115 2042 4284 2042 4274 1485 4326 -17552
result:
wrong answer 15th lines differ - expected: '7187', found: '7201'