QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#160346 | #7108. Couleur | ucup-team1447# | AC ✓ | 5042ms | 229836kb | C++14 | 5.3kb | 2023-09-02 20:11:07 | 2023-09-02 20:11:08 |
Judging History
answer
// This Code was made by Chinese_zjc_.
#include <bits/stdc++.h>
// #define debug
int L, T, N, n, m, a[200005], b[200005], sum[200005], in[200005], e[505][200005];
long long c[505], d[505][505], pre[200005], suf[200005];
std::pair<int, int> A[200005];
std::vector<int> rk[505];
void add(int pos, int val)
{
while (pos <= n)
{
sum[pos] += val;
pos += pos & -pos;
}
}
int query(int pos)
{
int res = 0;
while (pos)
{
res += sum[pos];
pos -= pos & -pos;
}
return res;
}
long long merge(const std::vector<int> &A, const std::vector<int> &B)
{
long long res = 0;
std::size_t Ai = 0, Bi = 0;
while (Bi != B.size())
{
while (Ai != A.size() && A[Ai] < B[Bi])
++Ai;
res += Ai;
++Bi;
}
return res;
}
long long merge1(const std::vector<int> &A, const std::vector<int> &B)
{
long long res = 0;
std::size_t Ai = 0, Bi = 0;
while (Bi != B.size())
{
while (Ai != A.size() && A[Ai] < B[Bi])
++Ai;
res += Ai;
++Bi;
}
return res;
}
long long merge2(const std::vector<int> &A, const std::vector<int> &B)
{
long long res = 0;
std::size_t Ai = 0, Bi = 0;
while (Bi != B.size())
{
while (Ai != A.size() && A[Ai] < B[Bi])
++Ai;
res += Ai;
++Bi;
}
return res;
}
long long query(int l, int r)
{
static long long ans;
static std::vector<int> A, B;
A.clear(), B.clear();
if (in[l] == in[r])
{
for (auto i : rk[in[l]])
if (b[i] < l)
A.push_back(i);
else if (b[i] > r)
B.push_back(i);
ans = pre[r] + suf[l] + merge1(A, B) - c[in[l]] + c[in[l] - 1];
}
else
{
int L = in[l] + 1, R = in[r] - 1;
ans = d[L][R] + c[R] - c[L - 1] + suf[l] + pre[r];
for (int i = l; in[l] == in[i]; ++i)
ans -= e[R][i] - e[L - 1][i];
for (int i = r; in[r] == in[i]; --i)
ans += e[R][i] - e[L - 1][i];
for (auto i : rk[in[l]])
if (b[i] >= l)
A.push_back(i);
for (auto i : rk[in[r]])
if (b[i] <= r)
B.push_back(i);
ans += A.size() * (R - L + 1) * ::L;
ans += merge2(A, B);
}
return ans;
}
signed main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> T;
while (T--)
{
std::cin >> N;
n = N;
L = std::min(375, N);
for (int i = 1; i <= n; ++i)
std::cin >> A[i].first, A[i].second = i;
std::sort(A + 1, A + 1 + n);
for (int i = 1; i <= n; ++i)
a[A[i].second] = i;
for (int i = 1; i <= n; ++i)
a[i] = n - a[i] + 1;
while (n % L)
++n, a[n] = n;
for (int i = 1; i <= n; ++i)
b[a[i]] = i;
std::fill(sum, sum + 1 + n, 0);
std::fill(in, in + 1 + n, 0);
std::fill(pre, pre + 1 + n, 0);
std::fill(suf, suf + 1 + n, 0);
for (int i = 1; i <= n / L; ++i)
{
c[i] = 0;
std::fill(d[i], d[i] + 1 + n / L, 0);
std::fill(e[i], e[i] + 1 + n, 0);
rk[i].clear();
}
for (int i = 1; i <= n / L; ++i)
{
int l = (i - 1) * L + 1, r = i * L;
rk[i].resize(L);
std::fill(in + l, in + r + 1, i);
std::copy(a + l, a + r + 1, rk[i].begin());
std::sort(rk[i].begin(), rk[i].end());
std::copy(e[i - 1], e[i - 1] + n + 1, e[i]);
for (int j = l; j <= r; ++j)
++e[i][a[j]];
int tmp = 0;
for (int j = l; j <= r; ++j)
{
tmp += query(a[j]);
add(a[j], +1);
pre[j] = tmp;
}
c[i] = tmp;
for (int j = l; j <= r; ++j)
{
suf[j] = tmp;
add(a[j], -1);
tmp -= query(n) - query(a[j]);
}
c[i] += c[i - 1];
}
for (int i = 1; i <= n / L; ++i)
{
for (int j = 1; j <= n; ++j)
e[i][j] += e[i][j - 1];
int tmp[n + 1];
for (int j = 1; j <= n; ++j)
tmp[j] = e[i][a[j]];
std::copy(tmp + 1, tmp + 1 + n, e[i] + 1);
}
for (int i = 1; i <= n / L; ++i)
for (int j = i; --j;)
d[j][i] = d[j + 1][i] + d[j][i - 1] - d[j + 1][i - 1] + merge(rk[j], rk[i]);
std::multiset<long long> ans;
std::set<int> del;
del.insert(0);
del.insert(N + 1);
ans.insert(query(1, N));
for (int i = 1; i <= N; ++i)
{
std::cout << *ans.rbegin() << " \n"[i == N];
static long long c;
std::cin >> c;
c ^= *ans.rbegin();
std::set<int>::iterator nxt = del.lower_bound(c), lst = std::prev(nxt);
ans.erase(ans.find(query(*lst + 1, *nxt - 1)));
if (*nxt - c > 1)
ans.insert(query(c + 1, *nxt - 1));
if (c - *lst > 1)
ans.insert(query(*lst + 1, c - 1));
del.insert(c);
}
}
return 0;
}
这程序好像有点Bug,我给组数据试试?
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 16204kb
input:
3 5 4 3 1 1 1 5 4 5 3 1 10 9 7 1 4 7 8 5 7 4 8 21 8 15 5 9 2 4 5 10 6 15 4 8 8 1 12 1 10 14 7 14 2 9 13 10 3 37 19 23 15 7 2 10 15 2 13 4 5 8 7 10
output:
7 0 0 0 0 20 11 7 2 0 0 0 0 0 0 42 31 21 14 14 4 1 1 1 0 0 0 0 0 0
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 5042ms
memory: 229836kb
input:
11116 10 10 5 10 3 6 4 8 5 9 8 31 27 24 11 12 3 0 2 3 1 10 8 2 7 2 8 10 1 10 9 10 6 5 2 13 2 1 0 1 3 1 10 7 10 7 6 1 3 10 6 7 9 21 18 10 1 6 5 4 8 9 10 10 2 10 4 8 8 5 7 2 6 7 20 10 9 1 15 0 4 2 9 7 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 10 6 3 5 2 7 10 9 1 4 8 10 1 10 1 3...
output:
21 18 16 12 10 6 4 1 1 0 12 12 10 10 4 4 4 2 1 0 20 16 9 5 3 3 3 0 0 0 22 14 8 8 5 5 2 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 12 7 4 4 2 2 1 0 0 20 18 8 3 1 1 0 0 0 0 45 21 21 10 3 3 3 0 0 0 17 11 8 2 1 1 1 0 0 0 13 4 1 0 0 0 0 0 0 0 29 27 22 15 9 7 4 3 1 0 26 16 9 2 1 1 1 1 1 0 0 0 0 0 0 ...
result:
ok 11116 lines
Extra Test:
score: 0
Extra Test Passed