QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#180448 | #7108. Couleur | wky606 | TL | 2ms | 9712kb | C++20 | 4.0kb | 2023-09-15 20:50:51 | 2023-09-15 20:50:52 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define INF 0x3f3f3f3f3f3f3f3f
#define lowbit(x) (x & -x)
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
#define N 200005
#define lc(x) tr[x].l
#define rc(x) tr[x].r
struct node
{
int l, r, s; // s:节点值域中有多少个数
} tr[N * 20];
int root[N], idx;
int a[N], p[N], inv[N];
void build(int &x, int l, int r)
{
x = ++idx;
if (l == r)
return;
int m = l + r >> 1;
build(lc(x), l, m);
build(rc(x), m + 1, r);
}
void insert(int x, int &y, int l, int r, int k)
{
y = ++idx;
tr[y] = tr[x];
tr[y].s++;
if (l == r)
return;
int m = l + r >> 1;
if (k <= m)
insert(lc(x), lc(y), l, m, k);
else
insert(rc(x), rc(y), m + 1, r, k);
}
void clear()
{
idx = 0;
tr[0] = {0, 0, 0};
}
int quesum(int x, int y, int l, int r, int k)
{
if (l > r)
return 0;
if (l >= k)
return tr[y].s - tr[x].s;
if (r < k)
return 0;
int m = l + r >> 1;
int res = 0;
res += quesum(rc(x), rc(y), m + 1, r, k);
res += quesum(lc(x), lc(y), l, m, k);
return res;
}
void solve()
{
clear();
int n;
cin >> n;
int maxv = -1;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
maxv = max(a[i], maxv);
}
build(root[0] = 0, 1, maxv);
for (int i = 1; i <= n; i++)
{
insert(root[i - 1], root[i] = 0, 1, maxv, a[i]);
}
int top = 0;
for (int i = 1; i <= n; i++)
{
top += quesum(root[0], root[i - 1], 1, maxv, a[i] + 1);
}
multiset<int> ms;
ms.insert(top);
inv[1] = top;
set<int> s;
s.insert(0);
s.insert(n + 1);
for (int j = 1; j <= n; j++)
{
cin >> p[j];
if (top == 0)
{
cout << "0 ";
continue;
}
// top = *ms.rbegin();
top = *prev(ms.end());
cout << top << " ";
int site = p[j] ^ top;
auto t = upper_bound(s.begin(), s.end(), site);
int L = *prev(t) + 1;
int R = *t - 1;
int V = inv[L];
ms.extract(V);
s.insert(site);
if (L == R)
{
continue;
}
else if (L == site)
{
int midv = R - (site + 1) + 1 - quesum(root[site], root[R], 1, maxv, a[site]);
ms.insert(V - midv);
inv[L + 1] = V - midv;
continue;
}
else if (R == site)
{
int midv = quesum(root[L - 1], root[R - 1], 1, maxv, a[site] + 1);
ms.insert(V - midv);
inv[L] = V - midv;
continue;
}
int ll = L, lr = site - 1, rl = site + 1, rr = R;
int midv = quesum(root[ll - 1], root[lr], 1, maxv, a[site] + 1);
midv += rr - rl + 1 - quesum(root[rl - 1], root[rr], 1, maxv, a[site]);
int tinv = 0, resinv = 0;
int Lv = 0, Rv = 0;
if (lr - ll < rr - rl)
{
for (int i = ll; i <= lr; i++)
{
tinv += quesum(root[ll - 1], root[i], 1, maxv, a[i] + 1);
resinv += rr - rl + 1 - quesum(root[rl - 1], root[rr], 1, maxv, a[i]);
}
Lv = tinv;
Rv = V - midv - tinv - resinv;
}
else
{
for (int i = rl; i <= rr; i++)
{
tinv += quesum(root[rl - 1], root[i], 1, maxv, a[i] + 1);
resinv += quesum(root[ll - 1], root[lr], 1, maxv, a[i] + 1);
}
Lv = V - midv - tinv - resinv;
Rv = tinv;
}
inv[ll] = Lv;
inv[rl] = Rv;
ms.insert(Lv);
ms.insert(Rv);
}
cout << "\n";
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9712kb
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: -100
Time Limit Exceeded
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 ...